-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathRoot.test.tsx
More file actions
269 lines (231 loc) · 8.79 KB
/
Root.test.tsx
File metadata and controls
269 lines (231 loc) · 8.79 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* 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/. */
// We want to test these components in isolation and tightly control the actions
// dispatched and avoid any side-effects. That's why we mock this module and
// return dummy thunk actions that return a Promise.
jest.mock('../../actions/receive-profile', () => ({
// These mocks will get their implementation in the `setup` function.
// Otherwise the implementation is wiped before the test starts.
// See https://github.com/facebook/jest/issues/7573 for more info.
retrieveProfileFromBrowser: jest.fn(),
retrieveProfileFromStore: jest.fn(),
retrieveProfilesToCompare: jest.fn(),
}));
// We mock <ProfileViewer> because it's complex and we should really test it
// elsewhere. Using a custom name makes it possible to test that _this_
// component is used.
jest.mock('../../components/app/ProfileViewer', () => ({
ProfileViewer: 'profile-viewer',
}));
// We mock <Home> as well because it brings too much noise in snapshots and it's
// overly tested in another test file.
jest.mock('../../components/app/Home', () => ({
Home: 'home',
}));
jest.mock('../../components/app/CompareHome', () => ({
CompareHome: 'compare-home',
}));
// ListOfPublishedProfiles depends on IDB and renders asynchronously, so we'll
// just test we want to render it, but otherwise test it more fully in a
// separate test file.
jest.mock('../../components/app/ListOfPublishedProfiles', () => ({
ListOfPublishedProfiles: 'list-of-published-profiles',
}));
import { Provider } from 'react-redux';
import { render, act } from 'firefox-profiler/test/fixtures/testing-library';
import { AppViewRouter } from '../../components/app/AppViewRouter';
import { ProfileLoader } from '../../components/app/ProfileLoader';
import { updateUrlState, changeProfilesToCompare } from '../../actions/app';
import { fatalError } from '../../actions/errors';
// Because this module is mocked but we want the real actions in the test, we
// use `jest.requireActual` here.
const {
temporaryError,
viewProfile,
waitingForProfileFromBrowser,
waitingForProfileFromStore,
} = jest.requireActual('../../actions/receive-profile');
// These functions are mocks
import {
retrieveProfileFromStore,
retrieveProfileFromBrowser,
retrieveProfilesToCompare,
} from '../../actions/receive-profile';
import { stateFromLocation } from '../../app-logic/url-handling';
import { TemporaryError } from '../../utils/errors';
import { blankStore } from '../fixtures/stores';
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import type { Action } from 'firefox-profiler/types';
describe('app/AppViewRouter', function () {
it('renders an initial home', function () {
const { container } = setup();
expect(container.firstChild).toMatchSnapshot();
});
it('renders the addon loading page, and the profile view after capturing', function () {
const { container, dispatch, navigateToFromBrowserProfileLoadingPage } =
setup();
navigateToFromBrowserProfileLoadingPage();
dispatch(waitingForProfileFromBrowser());
expect(container.firstChild).toMatchSnapshot();
// We do not check that retrieveProfileFromBrowser gets called, because the call
// happens asynchronously.
const { profile } = getProfileFromTextSamples(`A`);
dispatch(viewProfile(profile));
expect(container.firstChild).toMatchSnapshot();
});
it('does not try to retrieve a profile when moving from from-browser to public', function () {
const {
container,
dispatch,
navigateToFromBrowserProfileLoadingPage,
navigateToStoreLoadingPage,
} = setup();
navigateToFromBrowserProfileLoadingPage();
dispatch(waitingForProfileFromBrowser());
const { profile } = getProfileFromTextSamples(`A`);
dispatch(viewProfile(profile));
navigateToStoreLoadingPage();
expect(container.firstChild).toMatchSnapshot();
expect(retrieveProfileFromStore).not.toHaveBeenCalled();
});
it('renders the profile view', function () {
const { container, dispatch, navigateToStoreLoadingPage } = setup();
navigateToStoreLoadingPage();
dispatch(waitingForProfileFromStore());
expect(container.firstChild).toMatchSnapshot();
expect(retrieveProfileFromStore).toHaveBeenCalledWith('ThisIsAFakeHash');
const { profile } = getProfileFromTextSamples(`A`);
dispatch(viewProfile(profile));
expect(container.firstChild).toMatchSnapshot();
});
it('renders temporary errors', function () {
const { container, dispatch, navigateToStoreLoadingPage } = setup();
navigateToStoreLoadingPage();
dispatch(
temporaryError(
new TemporaryError('This is a temporary error, wait a bit more.', {
count: 3,
total: 10,
})
)
);
expect(container.firstChild).toMatchSnapshot();
});
it('renders an home when the user pressed back after an error', function () {
const {
container,
dispatch,
navigateToFromFileProfileLoadingPage,
navigateBackToHome,
} = setup();
navigateToFromFileProfileLoadingPage();
dispatch(fatalError(new Error('Error while loading profile')));
expect(container.firstChild).toMatchSnapshot();
expect(console.error).toHaveBeenCalled();
navigateBackToHome();
expect(container.firstChild).toMatchSnapshot();
});
it('does not render back home link when not opened with from-file', function () {
const { container, dispatch, navigateToStoreLoadingPage } = setup();
navigateToStoreLoadingPage();
dispatch(fatalError(new Error('Error while loading profile')));
expect(container.firstChild).toMatchSnapshot();
expect(console.error).toHaveBeenCalled();
});
it('renders a compare home when navigating to /compare/, then loads when changing profiles', () => {
const { container, dispatch, navigateToCompareHome } = setup();
navigateToCompareHome();
expect(container.querySelector('compare-home')).toBeTruthy();
const url1 = 'http://fake-url.com/hash/1';
const url2 = 'http://fake-url.com/hash/2';
dispatch(changeProfilesToCompare([url1, url2]));
expect(container.querySelector('.loading')).toBeTruthy();
expect(retrieveProfilesToCompare).toHaveBeenCalledWith([url1, url2]);
});
it('renders a uploaded-recordings page when navigating to /uploaded-recordings', () => {
const { container, navigateToMyProfiles } = setup();
navigateToMyProfiles();
expect(container.querySelector('list-of-published-profiles')).toBeTruthy();
});
});
function setup() {
// Let's silence the error output to the console
jest.spyOn(console, 'error').mockImplementation(() => {});
// Flow doesn't know these actions are jest mocks.
(retrieveProfileFromBrowser as any).mockImplementation(() => async () => {});
(retrieveProfileFromStore as any).mockImplementation(() => async () => {});
(retrieveProfilesToCompare as any).mockImplementation(() => async () => {});
const store = blankStore();
const renderResult = render(
<Provider store={store}>
<>
<ProfileLoader />
<AppViewRouter />
</>
</Provider>
);
function actAndDispatch(what: Action) {
act(() => {
store.dispatch(what);
});
}
function navigateToStoreLoadingPage() {
const newUrlState = stateFromLocation({
pathname: '/public/ThisIsAFakeHash/calltree',
search: '',
hash: '',
});
actAndDispatch(updateUrlState(newUrlState));
}
function navigateToFromBrowserProfileLoadingPage() {
const newUrlState = stateFromLocation({
pathname: '/from-browser/',
search: '',
hash: '',
});
actAndDispatch(updateUrlState(newUrlState));
}
function navigateToFromFileProfileLoadingPage() {
const newUrlState = stateFromLocation({
pathname: '/from-file/calltree',
search: '',
hash: '',
});
actAndDispatch(updateUrlState(newUrlState));
}
function navigateBackToHome() {
const newUrlState = stateFromLocation({
pathname: '/',
hash: '',
search: '',
});
actAndDispatch(updateUrlState(newUrlState));
}
function navigateToCompareHome() {
const newUrlState = stateFromLocation({
pathname: '/compare/',
hash: '',
search: '',
});
actAndDispatch(updateUrlState(newUrlState));
}
function navigateToMyProfiles() {
const newUrlState = stateFromLocation({
pathname: '/uploaded-recordings/',
hash: '',
search: '',
});
actAndDispatch(updateUrlState(newUrlState));
}
return {
...renderResult,
dispatch: actAndDispatch,
navigateToStoreLoadingPage,
navigateToFromBrowserProfileLoadingPage,
navigateToFromFileProfileLoadingPage,
navigateBackToHome,
navigateToCompareHome,
navigateToMyProfiles,
};
}