-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathcontainer.test.tsx
More file actions
237 lines (194 loc) · 7.28 KB
/
container.test.tsx
File metadata and controls
237 lines (194 loc) · 7.28 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import * as SigninPasswordlessCodeModule from '.';
import * as ReactUtils from 'fxa-react/lib/utils';
import { SigninPasswordlessCodeProps } from './interfaces';
import { Integration } from '../../../models';
import { renderWithLocalizationProvider } from 'fxa-react/lib/test-utils/localizationProvider';
import { LocationProvider } from '@reach/router';
import SigninPasswordlessCodeContainer from './container';
import { screen, waitFor } from '@testing-library/react';
import { MOCK_EMAIL, MOCK_CLIENT_ID } from '../../mocks';
import { createMockWebIntegration } from '../../../lib/integrations/mocks';
import { createMockPasswordlessLocationState } from './mocks';
let integration: Integration;
function mockWebIntegration() {
integration = createMockWebIntegration({ clientIdFallback: MOCK_CLIENT_ID }) as Integration;
}
function applyDefaultMocks() {
jest.resetAllMocks();
jest.restoreAllMocks();
mockReactUtilsModule();
mockWebIntegration();
mockSigninPasswordlessCodeModule();
}
let mockAuthClient: any;
jest.mock('../../../models', () => {
return {
...jest.requireActual('../../../models'),
useAuthClient: () => mockAuthClient,
};
});
// Set this when testing location state
let mockLocationState = {};
const mockLocation = () => {
return {
pathname: '/signin_passwordless_code',
state: mockLocationState,
};
};
const mockNavigate = jest.fn();
jest.mock('@reach/router', () => {
return {
__esModule: true,
...jest.requireActual('@reach/router'),
useNavigate: () => mockNavigate,
useLocation: () => mockLocation(),
};
});
let currentSigninPasswordlessCodeProps: SigninPasswordlessCodeProps | undefined;
function mockSigninPasswordlessCodeModule() {
currentSigninPasswordlessCodeProps = undefined;
jest
.spyOn(SigninPasswordlessCodeModule, 'default')
.mockImplementation((props: SigninPasswordlessCodeProps) => {
currentSigninPasswordlessCodeProps = props;
return <div>signin passwordless code mock</div>;
});
}
function mockReactUtilsModule() {
jest.spyOn(ReactUtils, 'hardNavigate').mockImplementation(() => { });
}
function resetMockAuthClient() {
mockAuthClient = {
passwordlessSendCode: jest.fn().mockResolvedValue(true),
};
}
async function render() {
renderWithLocalizationProvider(
<LocationProvider>
<SigninPasswordlessCodeContainer
{...{
integration,
serviceName: 'sync',
}}
/>
</LocationProvider>
);
}
describe('SigninPasswordlessCode container', () => {
beforeEach(() => {
applyDefaultMocks();
resetMockAuthClient();
});
describe('initial states', () => {
describe('email', () => {
it('can be set from router state', async () => {
mockLocationState = createMockPasswordlessLocationState();
await render();
await waitFor(() =>
expect(screen.getByText('signin passwordless code mock')).toBeInTheDocument()
);
expect(currentSigninPasswordlessCodeProps?.email).toBe(MOCK_EMAIL);
expect(currentSigninPasswordlessCodeProps?.integration).toBe(integration);
expect(SigninPasswordlessCodeModule.default).toHaveBeenCalled();
});
it('is handled if not provided in location state', async () => {
mockLocationState = {};
await render();
await waitFor(() => {
expect(mockNavigate).toHaveBeenCalledWith('/');
});
expect(SigninPasswordlessCodeModule.default).not.toHaveBeenCalled();
});
it('shows loading state while sending initial code', async () => {
mockLocationState = createMockPasswordlessLocationState();
// Make passwordlessSendCode slow to simulate loading
mockAuthClient.passwordlessSendCode = jest.fn().mockImplementation(
() => new Promise((resolve) => setTimeout(resolve, 100))
);
await render();
// Should show loading initially
expect(screen.queryByText('signin passwordless code mock')).not.toBeInTheDocument();
});
});
describe('code sending', () => {
beforeEach(() => {
mockLocationState = createMockPasswordlessLocationState();
});
it('sends code on mount with email and clientId', async () => {
await render();
await waitFor(() => {
expect(mockAuthClient.passwordlessSendCode).toHaveBeenCalledWith(
MOCK_EMAIL,
{ clientId: MOCK_CLIENT_ID }
);
});
});
it('renders error state if code sending fails', async () => {
mockAuthClient.passwordlessSendCode = jest
.fn()
.mockRejectedValue(new Error('Failed to send'));
await render();
await waitFor(() => {
expect(screen.getByText(/Error:/)).toBeInTheDocument();
});
expect(SigninPasswordlessCodeModule.default).not.toHaveBeenCalled();
});
it('does not send code multiple times', async () => {
await render();
await waitFor(() => {
expect(mockAuthClient.passwordlessSendCode).toHaveBeenCalledTimes(1);
});
// Even if component re-renders, should not send again
expect(mockAuthClient.passwordlessSendCode).toHaveBeenCalledTimes(1);
});
it('does not re-send code when codeSent is in location state (page refresh)', async () => {
// Simulate page refresh: location state persists via History API
// with codeSent: true from the previous render's history.replaceState
mockLocationState = {
...createMockPasswordlessLocationState(),
codeSent: true,
};
await render();
await waitFor(() => {
expect(screen.getByText('signin passwordless code mock')).toBeInTheDocument();
});
// Should NOT send code because location state has codeSent: true
expect(mockAuthClient.passwordlessSendCode).not.toHaveBeenCalled();
});
});
describe('isSignup flag', () => {
it('passes isSignup=true from location state', async () => {
mockLocationState = createMockPasswordlessLocationState(true);
await render();
await waitFor(() => {
expect(currentSigninPasswordlessCodeProps?.isSignup).toBe(true);
});
});
it('passes isSignup=false from location state', async () => {
mockLocationState = createMockPasswordlessLocationState(false);
await render();
await waitFor(() => {
expect(currentSigninPasswordlessCodeProps?.isSignup).toBe(false);
});
});
});
});
describe('OAuth errors', () => {
it('displays OAuthDataError when present', async () => {
// Mock useFinishOAuthFlowHandler to return an error
jest.mock('../../../lib/oauth/hooks', () => ({
useFinishOAuthFlowHandler: jest.fn().mockReturnValue({
finishOAuthFlowHandler: jest.fn(),
oAuthDataError: new Error('OAuth error'),
}),
}));
mockLocationState = createMockPasswordlessLocationState();
await render();
// The OAuthDataError component should be rendered instead
// This would need the actual component rendering logic to verify
});
});
});