forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipped-profiles.test.js
308 lines (261 loc) · 9.65 KB
/
zipped-profiles.test.js
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
/* 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/. */
// @flow
import {
formatZipFileTable,
storeWithZipFile,
} from '../fixtures/profiles/zip-file';
import * as ProfileViewSelectors from '../../selectors/profile';
import * as ZippedProfilesSelectors from '../../selectors/zipped-profiles';
import * as UrlStateSelectors from '../../selectors/url-state';
import createStore from '../../app-logic/create-store';
import { ensureExists } from '../../utils/flow';
import JSZip from 'jszip';
import * as ZippedProfilesActions from '../../actions/zipped-profiles';
import * as ReceiveProfileActions from '../../actions/receive-profile';
import * as ProfileViewActions from '../../actions/profile-view';
describe('reducer zipFileState', function () {
it('can store the zip file in the reducer', async function () {
const { zippedProfiles } = await storeWithZipFile();
expect(zippedProfiles).toBe(zippedProfiles);
});
it('can load a profile from the zip file', async function () {
const { dispatch, getState } = await storeWithZipFile([
'foo/bar/profile1.json',
'foo/profile2.json',
'baz/profile3.json',
]);
expect(ProfileViewSelectors.getProfileOrNull(getState())).toEqual(null);
await dispatch(
ZippedProfilesActions.viewProfileFromPathInZipFile(
'foo/bar/profile1.json'
)
);
expect(ZippedProfilesSelectors.getZipFileState(getState()).phase).toBe(
'VIEW_PROFILE_IN_ZIP_FILE'
);
const profile1 = ProfileViewSelectors.getProfile(getState());
expect(profile1).toBeTruthy();
});
describe('profileName handling', function () {
async function setup() {
const { dispatch, getState } = await storeWithZipFile([
'foo/bar/profile1.json',
'foo/profile2.json',
'baz/profile3.json',
'profile4.json',
]);
await dispatch(
ZippedProfilesActions.viewProfileFromPathInZipFile(
'foo/bar/profile1.json'
)
);
return { getState, dispatch };
}
it('computes a profile name from the loaded file', async function () {
const { getState } = await setup();
const expectedName = 'bar/profile1.json';
expect(UrlStateSelectors.getProfileNameWithDefault(getState())).toBe(
expectedName
);
});
it('computes a profile when no folder present', async function () {
const { dispatch, getState } = await storeWithZipFile([
'foo/bar/profile1.json',
'foo/profile2.json',
'baz/profile3.json',
'profile4.json',
]);
await dispatch(
ZippedProfilesActions.viewProfileFromPathInZipFile('profile4.json')
);
const expectedName = 'profile4.json';
expect(UrlStateSelectors.getProfileNameWithDefault(getState())).toBe(
expectedName
);
});
it('prefers ProfileName if it is given in the URL', async function () {
const { getState, dispatch } = await setup();
const profileNameFromURL = 'good profile';
dispatch(ProfileViewActions.changeProfileName(profileNameFromURL));
expect(UrlStateSelectors.getProfileNameWithDefault(getState())).toBe(
profileNameFromURL
);
});
});
it('will fail when trying to load an invalid profile', async function () {
const store = createStore();
const { getState, dispatch } = store;
const zip = new JSZip();
zip.file('not-a-profile.json', 'not a profile');
dispatch(ReceiveProfileActions.receiveZipFile(zip));
jest.spyOn(console, 'error').mockImplementation(() => {});
await dispatch(
ZippedProfilesActions.viewProfileFromPathInZipFile('not-a-profile.json')
);
expect(ZippedProfilesSelectors.getZipFileState(getState()).phase).toEqual(
'FAILED_TO_PROCESS_PROFILE_FROM_ZIP_FILE'
);
// console error was called.
expect(console.error.mock.calls.length >= 1).toEqual(true);
expect(console.error.mock.calls).toMatchSnapshot();
});
it('will fail when not finding a profile', async function () {
const store = createStore();
const { getState, dispatch } = store;
dispatch(ReceiveProfileActions.receiveZipFile(new JSZip()));
dispatch(
ZippedProfilesActions.viewProfileFromPathInZipFile('nothing-here.json')
);
expect(ZippedProfilesSelectors.getZipFileState(getState()).phase).toEqual(
'FILE_NOT_FOUND_IN_ZIP_FILE'
);
});
it('can compute a ZipFileTable', async function () {
const { getState } = await storeWithZipFile([
'foo/bar/profile1.json',
'foo/profile2.json',
'foo/profile3.json',
'foo/profile4.json',
'baz/profile5.json',
]);
const zipFileTable = ZippedProfilesSelectors.getZipFileTable(getState());
expect(formatZipFileTable(zipFileTable)).toEqual([
'foo (dir)',
' bar (dir)',
' profile1.json (file)',
' profile2.json (file)',
' profile3.json (file)',
' profile4.json (file)',
'baz (dir)',
' profile5.json (file)',
]);
});
it('computes the zip file max depth', async function () {
const { getState } = await storeWithZipFile([
'foo/bar/profile1.json',
'foo/profile2.json',
'foo/profile3.json',
'foo/profile4.json',
'baz/profile5.json',
]);
expect(ZippedProfilesSelectors.getZipFileMaxDepth(getState())).toEqual(2);
});
});
describe('selected and expanded zip files', function () {
it('can expand selections in the zip file', function () {
const { dispatch, getState } = createStore();
expect(
ZippedProfilesSelectors.getExpandedZipFileIndexes(getState())
).toEqual([]);
// The indexes don't check that they are valid when you add them.
dispatch(ZippedProfilesActions.changeExpandedZipFile([123, 456, 789]));
expect(
ZippedProfilesSelectors.getExpandedZipFileIndexes(getState())
).toEqual([123, 456, 789]);
});
it('can procure an interesting selection', async function () {
const { getState, dispatch } = await storeWithZipFile([
'a/profile1.json',
'a/profile2.json',
'b/profile3.json',
'b/profile4.json',
'c/profile5.json',
'c/profile6.json',
'd/profile7.json',
'd/profile8.json',
]);
const zipFileTree = ZippedProfilesSelectors.getZipFileTree(getState());
const zipFileTable = ZippedProfilesSelectors.getZipFileTable(getState());
dispatch(
ZippedProfilesActions.changeExpandedZipFile([
...zipFileTree.getAllDescendants(null),
])
);
const expanded =
ZippedProfilesSelectors.getExpandedZipFileIndexes(getState());
const expandedNames = expanded.map(
(index) => zipFileTable.path[ensureExists(index)]
);
expect(expandedNames).toEqual([
'a',
'a/profile1.json',
'a/profile2.json',
'b',
'b/profile3.json',
'b/profile4.json',
'c',
'c/profile5.json',
'c/profile6.json',
'd',
'd/profile7.json',
'd/profile8.json',
]);
});
it('can select a zip file', function () {
const { dispatch, getState } = createStore();
expect(ZippedProfilesSelectors.getSelectedZipFileIndex(getState())).toEqual(
null
);
// The indexes don't check that they are valid when you add them.
dispatch(ZippedProfilesActions.changeSelectedZipFile(123));
expect(ZippedProfilesSelectors.getSelectedZipFileIndex(getState())).toEqual(
123
);
});
});
describe('profile state invalidation when switching between profiles', function () {
const getStoreViewingProfile = async () => {
const { dispatch, getState } = await storeWithZipFile([
'profile1.json',
'profile2.json',
]);
const viewProfile = (path) =>
dispatch(ZippedProfilesActions.viewProfileFromPathInZipFile(path));
return { dispatch, getState, viewProfile };
};
it('invalidates profile-specific url state', async function () {
const { dispatch, getState, viewProfile } = await getStoreViewingProfile();
viewProfile('profile1.json');
// It starts out empty.
expect(UrlStateSelectors.getAllCommittedRanges(getState())).toEqual([]);
// Add a url-encoded bit of state.
dispatch(ProfileViewActions.commitRange(0, 10));
expect(UrlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 0, end: 10 },
]);
// It switches to another profile and invalidates.
dispatch(ZippedProfilesActions.returnToZipFileList());
viewProfile('profile2.json');
expect(UrlStateSelectors.getAllCommittedRanges(getState())).toEqual([]);
});
it('invalidates profile view state', async function () {
const { dispatch, getState, viewProfile } = await getStoreViewingProfile();
viewProfile('profile1.json');
// Create new copies of the selection on each assertion and change, so
// that we are not relying on strict equality.
const getNoSelection = () => ({ hasSelection: false, isModifying: false });
const getSomeSelection = () => ({
hasSelection: true,
isModifying: true,
selectionStart: 0,
selectionEnd: 10,
});
// It starts with no selection.
expect(ProfileViewSelectors.getPreviewSelection(getState())).toEqual(
getNoSelection()
);
// Add a selection.
dispatch(ProfileViewActions.updatePreviewSelection(getSomeSelection()));
expect(ProfileViewSelectors.getPreviewSelection(getState())).toEqual(
getSomeSelection()
);
dispatch(ZippedProfilesActions.returnToZipFileList());
viewProfile('profile2.json');
// It no longer has a selection when viewing another profile.
expect(ProfileViewSelectors.getPreviewSelection(getState())).toEqual(
getNoSelection()
);
});
});