-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathUrlManager.test.tsx
More file actions
409 lines (335 loc) · 14.2 KB
/
UrlManager.test.tsx
File metadata and controls
409 lines (335 loc) · 14.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* 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 { Provider } from 'react-redux';
import { render, act } from 'firefox-profiler/test/fixtures/testing-library';
import { getView, getUrlSetupPhase } from '../../selectors/app';
import { UrlManager } from '../../components/app/UrlManager';
import { blankStore } from '../fixtures/stores';
import * as receiveProfileModule from 'firefox-profiler/actions/receive-profile';
import {
getDataSource,
getHash,
getCurrentSearchString,
} from '../../selectors/url-state';
import { waitUntilState } from '../fixtures/utils';
import { createGeckoProfile } from '../fixtures/profiles/gecko-profile';
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import { CURRENT_URL_VERSION } from '../../app-logic/url-handling';
import { autoMockFullNavigation } from '../fixtures/mocks/window-navigation';
import { profilePublished } from 'firefox-profiler/actions/publish';
import {
changeCallTreeSearchString,
setDataSource,
} from 'firefox-profiler/actions/profile-view';
import { getProfileOrNull } from '../../selectors/profile';
jest.mock('../../profile-logic/symbol-store');
import { simulateOldWebChannelAndFrameScript } from '../fixtures/mocks/web-channel';
import type { UrlSetupPhase } from 'firefox-profiler/types';
describe('UrlManager', function () {
autoMockFullNavigation();
function getSerializableProfile() {
return getProfileFromTextSamples('A').profile;
}
function setup(urlPath?: string) {
jest
.spyOn(navigator, 'userAgent', 'get')
.mockReturnValue(
'Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0'
);
if (typeof urlPath === 'string') {
window.location.replace(`http://localhost${urlPath}`);
}
const store = blankStore();
const { dispatch, getState } = store;
const createUrlManager = () =>
render(
<Provider store={store}>
<UrlManager>Contents</UrlManager>
</Provider>
);
const waitUntilUrlSetupPhase = (phase: UrlSetupPhase) =>
act(() =>
waitUntilState(store, (state) => getUrlSetupPhase(state) === phase)
);
return {
store,
dispatch,
getState,
createUrlManager,
waitUntilUrlSetupPhase,
};
}
beforeEach(function () {
const profileJSON = createGeckoProfile();
const mockGetProfile = jest.fn().mockResolvedValue(profileJSON);
const geckoProfiler = {
getProfile: mockGetProfile,
getSymbolTable: jest
.fn()
.mockRejectedValue(new Error('No symbol tables available')),
};
simulateOldWebChannelAndFrameScript(geckoProfiler);
});
afterEach(function () {
// @ts-expect-error geckoProfilerPromise not optional
delete window.geckoProfilerPromise;
});
it('sets up the URL', async function () {
const { getState, createUrlManager, waitUntilUrlSetupPhase } = setup();
expect(getUrlSetupPhase(getState())).toBe('initial-load');
createUrlManager();
expect(getUrlSetupPhase(getState())).toBe('loading-profile');
await waitUntilUrlSetupPhase('done');
expect(getUrlSetupPhase(getState())).toBe('done');
expect(getDataSource(getState())).toMatch('none');
});
it('has no data source by default', async function () {
const { getState, createUrlManager, waitUntilUrlSetupPhase } = setup();
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('none');
});
it('sets the data source to from-browser', async function () {
const { getState, createUrlManager, waitUntilUrlSetupPhase } =
setup('/from-browser/');
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('from-browser');
});
it('sets the data source to from-browser when coming from the legacy URL /from-addon', async function () {
const { getState, createUrlManager, waitUntilUrlSetupPhase } =
setup('/from-addon');
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('from-browser');
});
it('sets the data source to from-browser when coming from the legacy URL /from-addon even when there are double slashes', async function () {
jest.spyOn(console, 'error').mockImplementation(() => {});
const { getState, createUrlManager, waitUntilUrlSetupPhase } =
setup('//from-addon');
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('from-browser');
expect(console.error).not.toHaveBeenCalled();
});
it('redirects from-file back to no data source', async function () {
const { getState, createUrlManager, waitUntilUrlSetupPhase } =
setup('/from-file/');
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('none');
});
it(`sets the data source to public and doesn't change the URL when there's a fetch error`, async function () {
window.fetchMock.get('*', { throws: new Error('Simulated network error') });
const urlPath = '/public/FAKE_HASH/marker-chart';
const { getState, createUrlManager, waitUntilUrlSetupPhase } =
setup(urlPath);
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('public');
const view: any = getView(getState());
expect(view.phase).toBe('FATAL_ERROR');
expect(view.error).toBeTruthy();
expect(view.error.message).toBe('Simulated network error');
expect(window.location.pathname).toBe(urlPath);
});
it(`sets the data source to public and doesn't change the URL when there's a URL upgrading error`, async function () {
window.fetchMock.get('*', getSerializableProfile());
const urlPath = '/public/FAKE_HASH/calltree';
const searchString = '?v=' + (CURRENT_URL_VERSION + 1);
const { getState, createUrlManager, waitUntilUrlSetupPhase } = setup(
urlPath + searchString
);
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('public');
const view: any = getView(getState());
expect(view.phase).toBe('FATAL_ERROR');
expect(view.error).toBeTruthy();
expect(view.error.message).toMatch('Unable to parse a url');
expect(view.error.name).toBe('UrlUpgradeError');
expect(window.location.pathname).toBe(urlPath);
expect(window.location.search).toBe(searchString);
});
it('routes errors thrown in finalizeProfileView to the FATAL_ERROR state', async function () {
window.fetchMock.get('*', getSerializableProfile());
const error = new Error('Simulated error in finalizeProfileView');
jest
.spyOn(receiveProfileModule, 'finalizeProfileView')
.mockReturnValue(async () => {
throw error;
});
const urlPath = '/public/FAKE_HASH/calltree/';
const { store, getState, createUrlManager } = setup(
urlPath + '?v=' + CURRENT_URL_VERSION
);
createUrlManager();
// urlSetupDone() fires synchronously before the .catch() microtask runs,
// so we wait for the FATAL_ERROR view phase directly instead of waiting
// for the url setup phase.
await act(() =>
waitUntilState(store, (state) => getView(state).phase === 'FATAL_ERROR')
);
const view: any = getView(getState());
expect(view.phase).toBe('FATAL_ERROR');
expect(view.error).toBe(error);
});
it(`fetches profile and sets the phase to done when everything works`, async function () {
window.fetchMock.get('*', getSerializableProfile());
const urlPath = '/public/FAKE_HASH/';
const expectedResultingPath = urlPath + 'calltree/';
const searchString = 'v=' + CURRENT_URL_VERSION;
const { getState, createUrlManager, waitUntilUrlSetupPhase } = setup(
urlPath + '?' + searchString
);
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toMatch('public');
expect(getView(getState()).phase).toBe('DATA_LOADED');
expect(window.location.pathname).toBe(expectedResultingPath);
expect(window.location.search).toContain(searchString);
});
it('allows navigating back and forward when changing view options', async () => {
window.fetchMock.get('*', getSerializableProfile());
const urlPath = '/public/FAKE_HASH/calltree/';
const searchString = 'v=' + CURRENT_URL_VERSION;
const { getState, createUrlManager, waitUntilUrlSetupPhase, dispatch } =
setup(urlPath + '?' + searchString);
expect(getDataSource(getState())).toMatch('none');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(window.history.length).toBe(1);
// The user changes is looking for some specific call node.
act(() => {
dispatch(changeCallTreeSearchString('B'));
});
expect(getCurrentSearchString(getState())).toBe('B');
expect(window.history.length).toBe(2);
// The user can't find anything, he goes back in history.
act(() => window.history.back());
expect(getCurrentSearchString(getState())).toBe('');
expect(window.history.length).toBe(2);
// Look again at this search.
act(() => window.history.forward());
expect(getCurrentSearchString(getState())).toBe('B');
});
it('allows navigating back and forward when moving between content pages', async () => {
// The test will start at the home.
const urlPath = '/ ';
const { getState, createUrlManager, waitUntilUrlSetupPhase, dispatch } =
setup(urlPath);
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(getDataSource(getState())).toBe('none');
expect(window.history.length).toBe(1);
// Now the user clicks on the "all recordings" link. This will change the
// datasource, we're simulating that.
act(() => {
dispatch(setDataSource('uploaded-recordings'));
});
expect(getDataSource(getState())).toBe('uploaded-recordings');
expect(window.history.length).toBe(2);
// The user goes back to the home by pressing the browser's back button.
act(() => window.history.back());
expect(getDataSource(getState())).toBe('none');
// Now the user goes to the compare form clicking a link on the homepage,
// we're simulating that by changing the data source.
act(() => {
dispatch(setDataSource('compare'));
});
expect(getDataSource(getState())).toBe('compare');
// Click on the header
act(() => {
dispatch(setDataSource('none'));
});
expect(window.history.length).toBe(3);
// The user goes back to the compare form using the browser's back button.
act(() => window.history.back());
expect(getDataSource(getState())).toBe('compare');
// The user goes back to the home using the browser's back button.
act(() => window.history.back());
expect(getDataSource(getState())).toBe('none');
});
it('prevents navigating back after publishing', async () => {
// This loads a profile from the browser.
const { getState, dispatch, createUrlManager, waitUntilUrlSetupPhase } =
setup('/from-browser/');
createUrlManager();
await waitUntilUrlSetupPhase('done');
expect(window.history.length).toBe(1);
// Now the user publishes.
act(() => {
dispatch(profilePublished('SOME_HASH', '', null));
});
expect(getDataSource(getState())).toMatch('public');
expect(getHash(getState())).toMatch('SOME_HASH');
expect(window.history.length).toBe(2);
// Then wants to go back in history. This shouldn't work!
let previousLocation = window.location.href;
act(() => window.history.back());
expect(getDataSource(getState())).toMatch('public');
expect(getHash(getState())).toMatch('SOME_HASH');
expect(previousLocation).toEqual(window.location.href);
// We went back, the entry number 2 has been replaced, but there are still 3
// entries in the history.
expect(window.history.length).toBe(2);
// Now let's publish again
act(() => {
dispatch(profilePublished('SOME_OTHER_HASH', '', null));
});
expect(getDataSource(getState())).toMatch('public');
expect(getHash(getState())).toMatch('SOME_OTHER_HASH');
// It's still 3 because the 3rd entry has been removed and replaced by this
// new state (remember we were at entry number 2).
expect(window.history.length).toBe(2);
// The user wants to go back, but this won't work!
previousLocation = window.location.href;
act(() => window.history.back());
expect(getDataSource(getState())).toMatch('public');
expect(getHash(getState())).toMatch('SOME_OTHER_HASH');
expect(previousLocation).toEqual(window.location.href);
});
it('can handle a from-post-message data source', async () => {
const { getState, waitUntilUrlSetupPhase, createUrlManager } = setup(
'/from-post-message/'
);
jest.spyOn(console, 'log').mockImplementation(() => {});
expect(getProfileOrNull(getState())).toBeFalsy();
const profile = getSerializableProfile();
const targetOrigin = '*';
await createUrlManager();
await waitUntilUrlSetupPhase('done');
await new Promise((resolve) => {
function listener({ data }: MessageEvent) {
if (
data &&
typeof data === 'object' &&
data.name === 'ready:response'
) {
resolve(undefined);
window.removeEventListener('message', listener);
}
}
window.addEventListener('message', listener);
window.postMessage({ name: 'ready:request' }, '*');
});
window.postMessage(
{
name: 'inject-profile',
profile,
},
targetOrigin
);
// Wait a tick so that postMessage has time to be processed.
await act(() => new Promise((resolve) => setTimeout(resolve, 0)));
expect(getProfileOrNull(getState())).toBeTruthy();
});
});