-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathAuthenticated.spec.tsx
62 lines (55 loc) · 2.26 KB
/
Authenticated.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as React from 'react';
import expect from 'expect';
import { cleanup, wait } from '@testing-library/react';
import Authenticated from './Authenticated';
import AuthContext from './AuthContext';
import renderWithRedux from '../util/renderWithRedux';
import { showNotification } from '../actions/notificationActions';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';
describe('<Authenticated>', () => {
afterEach(cleanup);
const Foo = () => <div>Foo</div>;
it('should render its child by default', async () => {
const { dispatch, queryByText } = renderWithRedux(
<Authenticated>
<Foo />
</Authenticated>
);
expect(queryByText('Foo')).not.toBeNull();
await wait();
expect(dispatch).toHaveBeenCalledTimes(0);
});
it('should logout, redirect to login and show a notification after a tick if the auth fails', async () => {
const authProvider = {
login: jest.fn().mockResolvedValue(''),
logout: jest.fn().mockResolvedValue(''),
checkAuth: jest.fn().mockRejectedValue(undefined),
checkError: jest.fn().mockResolvedValue(''),
getPermissions: jest.fn().mockResolvedValue(''),
};
const history = createMemoryHistory();
const { dispatch } = renderWithRedux(
<Router history={history}>
<AuthContext.Provider value={authProvider}>
<Authenticated>
<Foo />
</Authenticated>
</AuthContext.Provider>
</Router>
);
await wait();
expect(authProvider.checkAuth.mock.calls[0][0]).toEqual({});
expect(authProvider.logout.mock.calls[0][0]).toEqual({});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.mock.calls[0][0]).toEqual(
showNotification('ra.auth.auth_check_error', 'warning', {
messageArgs: {},
undoable: false,
})
);
expect(dispatch.mock.calls[1][0]).toEqual({ type: 'RA/CLEAR_STATE' });
expect(history.location.pathname).toEqual('/login');
expect(history.location.state).toEqual({ nextPathname: '/' });
});
});