Skip to content

Commit

Permalink
Restore LoginActions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Nov 2, 2023
1 parent 6af7923 commit 38e056a
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/components/Login/Redux/tests/LoginActions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { User } from "api/models";
import { asyncLogIn, asyncSignUp } from "components/Login/Redux/LoginActions";
import { LoginStatus } from "components/Login/Redux/LoginReduxTypes";
import { setupStore } from "store";
import { newUser } from "types/user";

jest.mock("backend", () => ({
addUser: (user: User) => mockAddUser(user),
authenticateUser: (...args: any[]) => mockAuthenticateUser(...args),
}));

// Mock the track and identify methods of segment analytics.
global.analytics = { identify: jest.fn(), track: jest.fn() } as any;

const mockAddUser = jest.fn();
const mockAuthenticateUser = jest.fn();

const mockEmail = "[email protected]";
const mockName = "testName";
const mockPassword = "testPass";
const mockUsername = "testUsername";
const mockUser = {
...newUser(mockName, mockUsername, mockPassword),
email: mockEmail,
};

beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
});

describe("LoginAction", () => {
describe("asyncLogIn", () => {
it("correctly affects state on failure", async () => {
const store = setupStore();
mockAuthenticateUser.mockRejectedValueOnce({});
await store.dispatch(asyncLogIn(mockUsername, mockPassword));
const loginState = store.getState().loginState;
expect(loginState.error).not.toEqual("");
expect(loginState.loginStatus).toEqual(LoginStatus.Failure);
expect(loginState.signupStatus).toEqual(LoginStatus.Default);
expect(loginState.username).toEqual(mockUsername);
});

it("correctly affects state on success", async () => {
const store = setupStore();
mockAuthenticateUser.mockResolvedValueOnce(mockUser);
await store.dispatch(asyncLogIn(mockUsername, mockPassword));
const loginState = store.getState().loginState;
expect(loginState.error).toEqual("");
expect(loginState.loginStatus).toEqual(LoginStatus.Success);
expect(loginState.signupStatus).toEqual(LoginStatus.Default);
expect(loginState.username).toEqual(mockUsername);
});
});

describe("asyncSignUp", () => {
it("correctly affects state on failure", async () => {
const store = setupStore();
mockAddUser.mockRejectedValueOnce({});
await store.dispatch(
asyncSignUp(mockName, mockUsername, mockEmail, mockPassword)
);
const loginState = store.getState().loginState;
expect(loginState.error).not.toEqual("");
expect(loginState.loginStatus).toEqual(LoginStatus.Default);
expect(loginState.signupStatus).toEqual(LoginStatus.Failure);
expect(loginState.username).toEqual(mockUsername);

// A failed signup does not trigger a login.
jest.runAllTimers();
expect(mockAuthenticateUser).not.toBeCalled();
});

it("correctly affects state on success", async () => {
const store = setupStore();
mockAddUser.mockResolvedValueOnce({});
await store.dispatch(
asyncSignUp(mockName, mockUsername, mockEmail, mockPassword)
);
const loginState = store.getState().loginState;
expect(loginState.error).toEqual("");
expect(loginState.loginStatus).toEqual(LoginStatus.Default);
expect(loginState.signupStatus).toEqual(LoginStatus.Success);
expect(loginState.username).toEqual(mockUsername);

// A successful signup triggers a login using `setTimeout`.
mockAuthenticateUser.mockRejectedValueOnce({});
jest.runAllTimers();
expect(mockAuthenticateUser).toBeCalledTimes(1);
});
});
});

0 comments on commit 38e056a

Please sign in to comment.