-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathsource-code-cache.test.ts
More file actions
168 lines (141 loc) · 5.69 KB
/
source-code-cache.test.ts
File metadata and controls
168 lines (141 loc) · 5.69 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
/* 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 { getSourceViewCode, getSourceCodeCache } from '../../selectors/code';
import { storeWithProfile } from '../fixtures/stores';
import { updateUrlState } from '../../actions/app';
import { stateFromLocation } from '../../app-logic/url-handling';
import {
beginLoadingSourceCodeFromUrl,
beginLoadingSourceCodeFromBrowserConnection,
finishLoadingSourceCode,
failLoadingSourceCode,
} from '../../actions/code';
import type { IndexIntoSourceTable } from 'firefox-profiler/types';
describe('source code cache with IndexIntoSourceTable', function () {
function setupStoreWithSourceIndex(sourceIndex: IndexIntoSourceTable | null) {
const store = storeWithProfile();
if (sourceIndex !== null) {
const urlState = stateFromLocation({
pathname: '/public/fakehash/',
search: `?sourceViewIndex=${sourceIndex}`,
hash: '',
});
store.dispatch(updateUrlState(urlState));
}
return store;
}
it('returns undefined when no source code is cached', function () {
const { getState } = setupStoreWithSourceIndex(0);
const sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toBeUndefined();
});
it('returns undefined when sourceIndex is null', function () {
const { getState } = setupStoreWithSourceIndex(null);
const sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toBeUndefined();
});
it('returns undefined when sourceIndex is not in cache', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(999);
// Add some other source to cache but not index 999
dispatch(finishLoadingSourceCode(0, 'some code'));
const sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toBeUndefined();
});
it('retrieves cached source code for AVAILABLE status', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(0);
dispatch(finishLoadingSourceCode(0, 'console.log("Source 0");'));
const sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toEqual({
type: 'AVAILABLE',
code: 'console.log("Source 0");',
});
});
it('retrieves cached source code for LOADING status with URL', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(2);
dispatch(
beginLoadingSourceCodeFromUrl(2, 'https://example.com/source2.js')
);
const sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toEqual({
type: 'LOADING',
source: { type: 'URL', url: 'https://example.com/source2.js' },
});
});
it('retrieves cached source code for ERROR status', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(3);
dispatch(
failLoadingSourceCode(3, [
{
type: 'NETWORK_ERROR',
url: 'https://example.com/source3.js',
networkErrorMessage: 'Failed to fetch',
},
])
);
const sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toEqual({
type: 'ERROR',
errors: [
{
type: 'NETWORK_ERROR',
url: 'https://example.com/source3.js',
networkErrorMessage: 'Failed to fetch',
},
],
});
});
it('retrieves cached source code for LOADING status with BROWSER_CONNECTION', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(4);
dispatch(beginLoadingSourceCodeFromBrowserConnection(4));
const sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toEqual({
type: 'LOADING',
source: { type: 'BROWSER_CONNECTION' },
});
});
describe('getSourceCodeCache selector', function () {
it('returns the cache Map with cached entries', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(null);
dispatch(finishLoadingSourceCode(0, 'console.log("Source 0");'));
const result = getSourceCodeCache(getState());
expect(result).toBeInstanceOf(Map);
expect(result.get(0)).toEqual({
type: 'AVAILABLE',
code: 'console.log("Source 0");',
});
});
it('returns empty Map when no cache is set', function () {
const { getState } = setupStoreWithSourceIndex(null);
const result = getSourceCodeCache(getState());
expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(0);
});
});
describe('state transitions', function () {
it('can transition from LOADING to AVAILABLE', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(0);
dispatch(beginLoadingSourceCodeFromUrl(0, 'https://example.com/test.js'));
let sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode?.type).toBe('LOADING');
dispatch(finishLoadingSourceCode(0, 'const result = 42;'));
sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toEqual({
type: 'AVAILABLE',
code: 'const result = 42;',
});
});
it('can transition from LOADING to ERROR', function () {
const { getState, dispatch } = setupStoreWithSourceIndex(1);
dispatch(beginLoadingSourceCodeFromUrl(1, 'https://example.com/test.js'));
let sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode?.type).toBe('LOADING');
dispatch(failLoadingSourceCode(1, [{ type: 'NO_KNOWN_CORS_URL' }]));
sourceViewCode = getSourceViewCode(getState());
expect(sourceViewCode).toEqual({
type: 'ERROR',
errors: [{ type: 'NO_KNOWN_CORS_URL' }],
});
});
});
});