-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: successful login with weak or strong password
- Loading branch information
1 parent
707ad07
commit 6699927
Showing
2 changed files
with
97 additions
and
7 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,85 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import UIProvider, { DefaultTheme } from 'hew/Theme'; | ||
import { useInitApi } from 'hew/Toast'; | ||
import { useState } from 'react'; | ||
|
||
import { login } from 'services/api'; | ||
|
||
import DeterminedAuth, { | ||
PASSWORD_ID, | ||
SUBMIT_ID, | ||
USERNAME_ID, | ||
WEAK_PASSWORD_SUBJECT, | ||
} from './DeterminedAuth'; | ||
import { ThemeProvider } from './ThemeProvider'; | ||
|
||
const USERNAME = 'username'; | ||
const WEAK_PASSWORD = 'password'; | ||
const STRONG_PASSWORD = 'dO2Ccf5ceozXJFuMhjEx32UgtR4X4yZTm1Vv'; | ||
|
||
vi.mock('services/api', () => ({ | ||
login: vi.fn().mockResolvedValue({ token: '', user: {} }), | ||
})); | ||
|
||
const Container = (): JSX.Element => { | ||
const [canceler] = useState(new AbortController()); | ||
useInitApi(); | ||
return <DeterminedAuth canceler={canceler} />; | ||
}; | ||
|
||
const setup = () => { | ||
render( | ||
<UIProvider theme={DefaultTheme.Light}> | ||
<ThemeProvider> | ||
<Container /> | ||
</ThemeProvider> | ||
</UIProvider>, | ||
); | ||
}; | ||
|
||
const user = userEvent.setup(); | ||
describe('DeterminedAuth', () => { | ||
it('logs in with strong password', async () => { | ||
setup(); | ||
|
||
await user.type(screen.getByTestId(USERNAME_ID), USERNAME); | ||
await user.type(screen.getByTestId(PASSWORD_ID), STRONG_PASSWORD); | ||
user.click(screen.getByTestId(SUBMIT_ID)); | ||
|
||
await waitFor(() => { | ||
// onFinish begins: | ||
expect(screen.getByRole('button')).toBeDisabled(); | ||
}); | ||
await waitFor(() => { | ||
// onFinish ends: | ||
expect(screen.getByRole('button')).not.toBeDisabled(); | ||
|
||
expect(screen.queryByText(WEAK_PASSWORD_SUBJECT)).not.toBeInTheDocument(); | ||
expect(vi.mocked(login)).toHaveBeenLastCalledWith( | ||
{ password: STRONG_PASSWORD, username: USERNAME }, | ||
{ | ||
signal: expect.any(AbortSignal), | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
it('logs in with weak password', async () => { | ||
setup(); | ||
await user.clear(screen.getByTestId(USERNAME_ID)); | ||
await user.type(screen.getByTestId(USERNAME_ID), USERNAME); | ||
await user.type(screen.getByTestId(PASSWORD_ID), WEAK_PASSWORD); | ||
user.click(screen.getByTestId(SUBMIT_ID)); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(WEAK_PASSWORD_SUBJECT)).toBeInTheDocument(); | ||
expect(vi.mocked(login)).toHaveBeenLastCalledWith( | ||
{ password: WEAK_PASSWORD, username: USERNAME }, | ||
{ | ||
signal: expect.any(AbortSignal), | ||
}, | ||
); | ||
}); | ||
}); | ||
}); |
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