2020-07-21 13:55:50 +00:00
|
|
|
import setupApi from "@test/api";
|
|
|
|
import { act, renderHook } from "@testing-library/react-hooks";
|
|
|
|
import ApolloClient from "apollo-client";
|
|
|
|
|
|
|
|
import { useAuthProvider } from "./AuthProvider";
|
2020-07-29 13:10:39 +00:00
|
|
|
import { getTokens, setAuthToken } from "./utils";
|
2020-07-21 13:55:50 +00:00
|
|
|
|
|
|
|
const apolloClient = setupApi();
|
|
|
|
|
|
|
|
function renderAuthProvider(apolloClient: ApolloClient<any>) {
|
|
|
|
const intl = {
|
|
|
|
formatMessage: ({ defaultMessage }) => defaultMessage
|
|
|
|
};
|
|
|
|
const notify = jest.fn();
|
|
|
|
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useAuthProvider(intl as any, notify, apolloClient)
|
|
|
|
);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:19:04 +00:00
|
|
|
const adminCredentials = {
|
2020-07-21 13:55:50 +00:00
|
|
|
email: "admin@example.com",
|
|
|
|
password: "admin",
|
2020-07-29 13:10:39 +00:00
|
|
|
token: null
|
2020-07-21 13:55:50 +00:00
|
|
|
};
|
|
|
|
|
2021-01-18 11:19:04 +00:00
|
|
|
const nonStaffUserCredentials = {
|
|
|
|
email: "client@example.com",
|
|
|
|
password: "password"
|
|
|
|
};
|
|
|
|
|
2020-07-21 13:55:50 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
localStorage.clear();
|
2020-07-23 13:37:39 +00:00
|
|
|
sessionStorage.clear();
|
2020-07-21 13:55:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("User", () => {
|
|
|
|
it("will be logged in if has valid credentials", async done => {
|
|
|
|
const hook = renderAuthProvider(apolloClient);
|
|
|
|
|
|
|
|
await act(() =>
|
2021-01-18 11:19:04 +00:00
|
|
|
hook.current.login(adminCredentials.email, adminCredentials.password)
|
2020-07-21 13:55:50 +00:00
|
|
|
);
|
2021-01-18 11:19:04 +00:00
|
|
|
expect(hook.current.userContext.email).toBe(adminCredentials.email);
|
|
|
|
adminCredentials.token = getTokens().auth;
|
2020-07-21 13:55:50 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("will not be logged in if doesn't have valid credentials", async done => {
|
|
|
|
const hook = renderAuthProvider(apolloClient);
|
|
|
|
|
|
|
|
await act(() =>
|
2021-01-18 11:19:04 +00:00
|
|
|
hook.current.login(adminCredentials.email, "NotAValidPassword123!")
|
2020-07-21 13:55:50 +00:00
|
|
|
);
|
|
|
|
expect(hook.current.userContext).toBe(null);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2021-01-18 11:19:04 +00:00
|
|
|
it("will not be logged in if is non-staff", async done => {
|
|
|
|
const hook = renderAuthProvider(apolloClient);
|
|
|
|
|
|
|
|
await act(() =>
|
|
|
|
hook.current.login(
|
|
|
|
nonStaffUserCredentials.email,
|
|
|
|
nonStaffUserCredentials.password
|
|
|
|
)
|
|
|
|
);
|
|
|
|
expect(hook.current.userContext).toBe(undefined);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-07-21 13:55:50 +00:00
|
|
|
it("will be logged if has valid token", async done => {
|
2021-01-18 11:19:04 +00:00
|
|
|
setAuthToken(adminCredentials.token, false);
|
2020-07-21 13:55:50 +00:00
|
|
|
const hook = renderAuthProvider(apolloClient);
|
|
|
|
|
|
|
|
await act(() => hook.current.autologinPromise.current);
|
2021-01-18 11:19:04 +00:00
|
|
|
expect(hook.current.userContext.email).toBe(adminCredentials.email);
|
2020-07-21 13:55:50 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("will not be logged if has invalid token", async done => {
|
2020-07-22 13:29:21 +00:00
|
|
|
setAuthToken("NotAToken", false);
|
2020-07-21 13:55:50 +00:00
|
|
|
const hook = renderAuthProvider(apolloClient);
|
|
|
|
|
|
|
|
await act(() => hook.current.autologinPromise.current);
|
|
|
|
expect(hook.current.userContext).toBe(undefined);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|