-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6af7923
commit 38e056a
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |