-
Notifications
You must be signed in to change notification settings - Fork 18
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
Showing
12 changed files
with
521 additions
and
75 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 @@ | ||
require('isomorphic-fetch'); |
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
2 changes: 1 addition & 1 deletion
2
src/__tests__/api/loginHandler.test.ts → src/api/__tests__/loginHandler.test.ts
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
3 changes: 2 additions & 1 deletion
3
src/__tests__/api/passwordCheckHandler.ts → ...pi/__tests__/passwordCheckHandler.test.ts
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
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
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,113 @@ | ||
import React from 'react'; | ||
import { | ||
act, | ||
render, | ||
cleanup, | ||
screen, | ||
waitFor, | ||
fireEvent, | ||
} from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { rest } from 'msw'; | ||
import { setupServer } from 'msw/node'; | ||
|
||
import { LoginComponent } from '../LoginComponent'; | ||
|
||
const server = setupServer(); | ||
|
||
describe('[hoc] LoginComponent', () => { | ||
beforeAll(() => server.listen()); | ||
afterEach(() => { | ||
server.resetHandlers(); | ||
cleanup(); | ||
jest.restoreAllMocks(); | ||
}); | ||
afterAll(() => server.close()); | ||
|
||
it('should reload on successful login', async () => { | ||
server.use( | ||
rest.post('http://localhost/api/login', async (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.json({})); | ||
}), | ||
); | ||
|
||
const reloadMock = jest.fn(); | ||
delete window.location; | ||
window.location = { ...window.location, reload: reloadMock }; | ||
|
||
render(<LoginComponent apiUrl="http://localhost/api/login" />); | ||
|
||
act(() => { | ||
userEvent.click(screen.getByRole('button')); | ||
}); | ||
|
||
await waitFor(() => expect(reloadMock).toBeCalled()); | ||
}); | ||
|
||
it('should show error state on failed login', async () => { | ||
server.use( | ||
rest.post('http://localhost/api/login', async (req, res, ctx) => { | ||
return res( | ||
ctx.status(400), | ||
ctx.json({ message: 'Incorrect password' }), | ||
); | ||
}), | ||
); | ||
|
||
render(<LoginComponent apiUrl="http://localhost/api/login" />); | ||
|
||
act(() => { | ||
userEvent.click(screen.getByRole('button')); | ||
}); | ||
|
||
await waitFor(() => | ||
expect(screen.getByLabelText('Password')).toHaveClass('invalid'), | ||
); | ||
|
||
expect(screen.getByTestId('error')).toHaveTextContent('Incorrect password'); | ||
}); | ||
|
||
it('should show error state on error', async () => { | ||
server.use( | ||
rest.post('/api/login', async (req, res, ctx) => { | ||
return res(ctx.status(500)); | ||
}), | ||
); | ||
|
||
render(<LoginComponent />); | ||
|
||
act(() => { | ||
userEvent.click(screen.getByRole('button')); | ||
}); | ||
|
||
await waitFor(() => | ||
expect(screen.getByLabelText('Password')).toHaveClass('invalid'), | ||
); | ||
|
||
expect(screen.getByTestId('error')).toHaveTextContent( | ||
'An error has occured.', | ||
); | ||
}); | ||
|
||
it('should not check if already checking', async () => { | ||
server.use( | ||
rest.post('http://localhost/api/login', async (req, res, ctx) => { | ||
return res(ctx.status(500), ctx.json({})); | ||
}), | ||
); | ||
|
||
const fetchMock = jest.fn(() => new Promise(() => {})); | ||
jest.spyOn(global, 'fetch').mockImplementation(fetchMock as any); | ||
|
||
render(<LoginComponent apiUrl="http://localhost/api/login" />); | ||
|
||
await act(async () => { | ||
await fireEvent.submit(screen.getByTestId('form')); | ||
await fireEvent.submit(screen.getByTestId('form')); | ||
await fireEvent.submit(screen.getByTestId('form')); | ||
await fireEvent.submit(screen.getByTestId('form')); | ||
}); | ||
|
||
expect(fetchMock).toBeCalledTimes(1); | ||
}); | ||
}); |
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,153 @@ | ||
import React from 'react'; | ||
import { act, render, cleanup, screen, waitFor } from '@testing-library/react'; | ||
import { rest } from 'msw'; | ||
import { setupServer } from 'msw/node'; | ||
import * as hooks from 'next/amp'; | ||
|
||
import { withPasswordProtect } from '../withPasswordProtect'; | ||
|
||
const App = ({ Component }) => <Component />; | ||
|
||
const server = setupServer(); | ||
|
||
describe('[hoc] withPasswordProtect', () => { | ||
beforeAll(() => server.listen()); | ||
afterEach(() => { | ||
server.resetHandlers(); | ||
cleanup(); | ||
jest.restoreAllMocks(); | ||
}); | ||
afterAll(() => server.close()); | ||
|
||
it('should render normally if logged in', async () => { | ||
server.use( | ||
rest.get('http://localhost/api/check', async (req, res, ctx) => { | ||
return res(ctx.status(200)); | ||
}), | ||
); | ||
|
||
const Wrapped = withPasswordProtect(App, { | ||
checkApiUrl: 'http://localhost/api/check', | ||
}); | ||
|
||
await act(async () => { | ||
render( | ||
<Wrapped | ||
Component={() => <div>hidden</div>} | ||
pageProps={{}} | ||
router={{} as any} | ||
/>, | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('hidden')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should render login if logged out', async () => { | ||
server.use( | ||
rest.get('http://localhost/api/check', async (req, res, ctx) => { | ||
return res(ctx.status(401)); | ||
}), | ||
); | ||
|
||
const Wrapped = withPasswordProtect(App, { | ||
checkApiUrl: 'http://localhost/api/check', | ||
}); | ||
|
||
await act(async () => { | ||
render( | ||
<Wrapped | ||
Component={() => <div>hidden</div>} | ||
pageProps={{}} | ||
router={{} as any} | ||
/>, | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Password')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should allow setting check api url', async () => { | ||
server.use( | ||
rest.get('http://localhost/api/check', async (req, res, ctx) => { | ||
return res(ctx.status(200)); | ||
}), | ||
); | ||
|
||
const Wrapped = withPasswordProtect(App); | ||
|
||
await act(async () => { | ||
render( | ||
<Wrapped | ||
Component={() => <div>hidden</div>} | ||
pageProps={{}} | ||
router={{} as any} | ||
/>, | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Password')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should render nothing if logged out and amp', async () => { | ||
server.use( | ||
rest.get('http://localhost/api/check', async (req, res, ctx) => { | ||
return res(ctx.status(401)); | ||
}), | ||
); | ||
|
||
jest.spyOn(hooks, 'useAmp').mockImplementation(() => true); | ||
|
||
const Wrapped = withPasswordProtect(App, { | ||
checkApiUrl: 'http://localhost/api/check', | ||
}); | ||
|
||
let container; | ||
|
||
await act(async () => { | ||
container = render( | ||
<Wrapped | ||
Component={() => <div>hidden</div>} | ||
pageProps={{}} | ||
router={{} as any} | ||
/>, | ||
).container; | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('Password')).not.toBeInTheDocument(); | ||
expect(screen.queryByText('hidden')).not.toBeInTheDocument(); | ||
expect(container.firstChild).toBeNull(); | ||
}); | ||
}); | ||
|
||
it('should render login if error', async () => { | ||
jest.spyOn(global, 'fetch').mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
|
||
const Wrapped = withPasswordProtect(App, { | ||
checkApiUrl: 'http://localhost/api/check', | ||
}); | ||
|
||
await act(async () => { | ||
render( | ||
<Wrapped | ||
Component={() => <div>hidden</div>} | ||
pageProps={{}} | ||
router={{} as any} | ||
/>, | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Password')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.