-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsaga.test.ts
164 lines (128 loc) · 5.42 KB
/
saga.test.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { redirectOnUserLogin, redirectToEntryPath, saga } from './saga';
import { getHistory, getNavigator } from '../../lib/browser';
import { rootReducer } from '../reducer';
import { getCurrentUser } from '../authentication/saga';
import { call, spawn } from 'redux-saga/effects';
import { expectSaga } from '../../test/saga';
class StubHistory {
pathname: string;
location: object;
constructor(pathname?: string) {
this.pathname = pathname ?? '';
this.location = { pathname };
}
replace = jest.fn();
}
describe('page-load saga', () => {
let history: StubHistory;
function subject(...args: Parameters<typeof expectSaga>) {
return expectSaga(...args).provide([
[call(getHistory), history],
[call(getCurrentUser), true],
[call(getNavigator), stubNavigator()],
[spawn(redirectOnUserLogin), null],
]);
}
it('redirects to main page if user is present and tries to access login page', async () => {
const initialState = { pageload: { isComplete: false } };
history = new StubHistory('/login');
const { storeState: loginStoreState } = await subject(saga)
.withReducer(rootReducer, initialState as any)
.run();
expect(history.replace).toHaveBeenCalledWith({ pathname: '/' });
expect(loginStoreState.pageload.isComplete).toBe(true);
});
it('redirects to main page if user is present and tries to access signup page', async () => {
const initialState = { pageload: { isComplete: false } };
history = new StubHistory('/get-access');
const { storeState: getAccessStoreState } = await subject(saga)
.withReducer(rootReducer, initialState as any)
.run();
expect(history.replace).toHaveBeenCalledWith({ pathname: '/' });
expect(getAccessStoreState.pageload.isComplete).toBe(true);
});
it('sets isComplete to true, if user is not present & stays on login page', async () => {
const initialState = { pageload: { isComplete: false } };
let history = new StubHistory('/login');
const {
storeState: { pageload },
} = await subject(saga)
.withReducer(rootReducer, initialState as any)
.provide([[call(getCurrentUser), false]])
.run();
expect(pageload.isComplete).toBe(true);
expect(history.replace).not.toHaveBeenCalled();
});
it('redirects to login page if user is not present', async () => {
const initialState = { pageload: { isComplete: false } };
history = new StubHistory('/');
const { storeState } = await subject(saga)
.provide([[call(getCurrentUser), false]])
.withReducer(rootReducer, initialState as any)
.run();
expect(storeState.pageload.isComplete).toBe(true);
expect(history.replace).toHaveBeenCalledWith({ pathname: '/login' });
});
it('saves the entry path when redirecting to the login page', async () => {
history = new StubHistory('/some/path');
const { storeState } = await subject(saga)
.provide([[call(getCurrentUser), false]])
.withReducer(rootReducer)
.run();
expect(storeState.pageload.entryPath).toEqual('/some/path');
});
it('redirects authenticated user from /reset-password to main page', async () => {
const initialState = { pageload: { isComplete: false } };
history = new StubHistory('/reset-password');
const { storeState: resetPasswordStoreState } = await subject(saga)
.withReducer(rootReducer, initialState as any)
.run();
expect(history.replace).toHaveBeenCalledWith({ pathname: '/' });
expect(resetPasswordStoreState.pageload.isComplete).toBe(true);
});
it('allows unauthenticated user to stay on /reset-password page', async () => {
const initialState = { pageload: { isComplete: false } };
history = new StubHistory('/reset-password');
const { storeState: resetPasswordStoreState } = await subject(saga)
.withReducer(rootReducer, initialState as any)
.provide([[call(getCurrentUser), false]])
.run();
expect(resetPasswordStoreState.pageload.isComplete).toBe(true);
expect(history.replace).not.toHaveBeenCalled();
});
it('redirects to /restricted if on mobile', async () => {
const initialState = { pageload: { isComplete: false } };
history = new StubHistory('/');
const { storeState } = await subject(saga)
.provide([
[call(getNavigator), stubNavigator('Mobi')],
])
.withReducer(rootReducer, initialState as any)
.run();
expect(history.replace).toHaveBeenCalledWith({ pathname: '/restricted' });
expect(storeState.pageload.isComplete).toBe(true);
});
});
describe(redirectToEntryPath, () => {
it('redirects to the saved entry path', async () => {
const initialState = { pageload: { entryPath: '/saved/path' } };
const history = new StubHistory('/login');
await expectSaga(redirectToEntryPath)
.provide([[call(getHistory), history]])
.withReducer(rootReducer, initialState as any)
.run();
expect(history.replace).toHaveBeenCalledWith({ pathname: '/saved/path' });
});
it('resets the entry path state', async () => {
const initialState = { pageload: { entryPath: '/saved/path' } };
const history = new StubHistory('/login');
const { storeState } = await expectSaga(redirectToEntryPath)
.provide([[call(getHistory), history]])
.withReducer(rootReducer, initialState as any)
.run();
expect(storeState.pageload.entryPath).toEqual('');
});
});
function stubNavigator(userAgent: string = 'chrome') {
return { userAgent };
}