Skip to content

Commit 566a813

Browse files
committed
Make the source code cache tests use the proper actions
1 parent 852a522 commit 566a813

File tree

1 file changed

+68
-114
lines changed

1 file changed

+68
-114
lines changed

src/test/unit/source-code-cache.test.ts

Lines changed: 68 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import { getSourceViewCode, getSourceCodeCache } from '../../selectors/code';
66
import { storeWithProfile } from '../fixtures/stores';
77
import { updateUrlState } from '../../actions/app';
88
import { stateFromLocation } from '../../app-logic/url-handling';
9+
import {
10+
beginLoadingSourceCodeFromUrl,
11+
beginLoadingSourceCodeFromBrowserConnection,
12+
finishLoadingSourceCode,
13+
failLoadingSourceCode,
14+
} from '../../actions/code';
915

10-
import type {
11-
State,
12-
Store,
13-
SourceCodeStatus,
14-
IndexIntoSourceTable,
15-
} from 'firefox-profiler/types';
16+
import type { IndexIntoSourceTable } from 'firefox-profiler/types';
1617

1718
describe('source code cache with IndexIntoSourceTable', function () {
1819
function setupStoreWithSourceIndex(sourceIndex: IndexIntoSourceTable | null) {
@@ -28,97 +29,64 @@ describe('source code cache with IndexIntoSourceTable', function () {
2829
return store;
2930
}
3031

31-
function createStateWithMockCache(
32-
store: Store,
33-
mockCache: Map<IndexIntoSourceTable, SourceCodeStatus>
34-
): State {
35-
return {
36-
...store.getState(),
37-
code: {
38-
...store.getState().code,
39-
sourceCodeCache: mockCache,
40-
},
41-
};
42-
}
43-
44-
function createMockCache(): Map<IndexIntoSourceTable, SourceCodeStatus> {
45-
const cache = new Map<IndexIntoSourceTable, SourceCodeStatus>();
46-
cache.set(0, {
47-
type: 'AVAILABLE',
48-
code: 'console.log("Source 0");',
49-
});
50-
cache.set(1, {
51-
type: 'AVAILABLE',
52-
code: 'function source1() { return "hello"; }',
53-
});
54-
cache.set(2, {
55-
type: 'LOADING',
56-
source: { type: 'URL', url: 'https://example.com/source2.js' },
57-
});
58-
cache.set(3, {
59-
type: 'ERROR',
60-
errors: [
61-
{
62-
type: 'NETWORK_ERROR',
63-
url: 'https://example.com/source3.js',
64-
networkErrorMessage: 'Failed to fetch',
65-
},
66-
],
67-
});
68-
return cache;
69-
}
70-
7132
it('returns undefined when no source code is cached', function () {
72-
const store = setupStoreWithSourceIndex(0);
73-
const sourceViewCode = getSourceViewCode(store.getState());
33+
const { getState } = setupStoreWithSourceIndex(0);
34+
const sourceViewCode = getSourceViewCode(getState());
7435
expect(sourceViewCode).toBeUndefined();
7536
});
7637

7738
it('returns undefined when sourceIndex is null', function () {
78-
const store = setupStoreWithSourceIndex(null);
79-
const sourceViewCode = getSourceViewCode(store.getState());
39+
const { getState } = setupStoreWithSourceIndex(null);
40+
const sourceViewCode = getSourceViewCode(getState());
8041
expect(sourceViewCode).toBeUndefined();
8142
});
8243

8344
it('returns undefined when sourceIndex is not in cache', function () {
84-
const store = setupStoreWithSourceIndex(999); // sourceIndex not in cache
85-
const cache = createMockCache();
86-
const state = createStateWithMockCache(store, cache);
45+
const { getState, dispatch } = setupStoreWithSourceIndex(999);
46+
// Add some other source to cache but not index 999
47+
dispatch(finishLoadingSourceCode(0, 'some code'));
8748

88-
const sourceViewCode = getSourceViewCode(state);
49+
const sourceViewCode = getSourceViewCode(getState());
8950
expect(sourceViewCode).toBeUndefined();
9051
});
9152

9253
it('retrieves cached source code for AVAILABLE status', function () {
93-
const store = setupStoreWithSourceIndex(0);
94-
const cache = createMockCache();
95-
const state = createStateWithMockCache(store, cache);
54+
const { getState, dispatch } = setupStoreWithSourceIndex(0);
55+
dispatch(finishLoadingSourceCode(0, 'console.log("Source 0");'));
9656

97-
const sourceViewCode = getSourceViewCode(state);
57+
const sourceViewCode = getSourceViewCode(getState());
9858
expect(sourceViewCode).toEqual({
9959
type: 'AVAILABLE',
10060
code: 'console.log("Source 0");',
10161
});
10262
});
10363

10464
it('retrieves cached source code for LOADING status with URL', function () {
105-
const store = setupStoreWithSourceIndex(2);
106-
const cache = createMockCache();
107-
const state = createStateWithMockCache(store, cache);
65+
const { getState, dispatch } = setupStoreWithSourceIndex(2);
66+
dispatch(
67+
beginLoadingSourceCodeFromUrl(2, 'https://example.com/source2.js')
68+
);
10869

109-
const sourceViewCode = getSourceViewCode(state);
70+
const sourceViewCode = getSourceViewCode(getState());
11071
expect(sourceViewCode).toEqual({
11172
type: 'LOADING',
11273
source: { type: 'URL', url: 'https://example.com/source2.js' },
11374
});
11475
});
11576

11677
it('retrieves cached source code for ERROR status', function () {
117-
const store = setupStoreWithSourceIndex(3);
118-
const cache = createMockCache();
119-
const state = createStateWithMockCache(store, cache);
78+
const { getState, dispatch } = setupStoreWithSourceIndex(3);
79+
dispatch(
80+
failLoadingSourceCode(3, [
81+
{
82+
type: 'NETWORK_ERROR',
83+
url: 'https://example.com/source3.js',
84+
networkErrorMessage: 'Failed to fetch',
85+
},
86+
])
87+
);
12088

121-
const sourceViewCode = getSourceViewCode(state);
89+
const sourceViewCode = getSourceViewCode(getState());
12290
expect(sourceViewCode).toEqual({
12391
type: 'ERROR',
12492
errors: [
@@ -132,82 +100,68 @@ describe('source code cache with IndexIntoSourceTable', function () {
132100
});
133101

134102
it('retrieves cached source code for LOADING status with BROWSER_CONNECTION', function () {
135-
const store = setupStoreWithSourceIndex(4);
136-
const cache = new Map<IndexIntoSourceTable, SourceCodeStatus>();
137-
cache.set(4, {
138-
type: 'LOADING',
139-
source: { type: 'BROWSER_CONNECTION' },
140-
});
141-
const state = createStateWithMockCache(store, cache);
103+
const { getState, dispatch } = setupStoreWithSourceIndex(4);
104+
dispatch(beginLoadingSourceCodeFromBrowserConnection(4));
142105

143-
const sourceViewCode = getSourceViewCode(state);
106+
const sourceViewCode = getSourceViewCode(getState());
144107
expect(sourceViewCode).toEqual({
145108
type: 'LOADING',
146109
source: { type: 'BROWSER_CONNECTION' },
147110
});
148111
});
149112

150113
describe('getSourceCodeCache selector', function () {
151-
it('returns the cache Map directly', function () {
152-
const store = setupStoreWithSourceIndex(null);
153-
const cache = createMockCache();
154-
const state = createStateWithMockCache(store, cache);
114+
it('returns the cache Map with cached entries', function () {
115+
const { getState, dispatch } = setupStoreWithSourceIndex(null);
116+
dispatch(finishLoadingSourceCode(0, 'console.log("Source 0");'));
155117

156-
const result = getSourceCodeCache(state);
157-
expect(result).toBe(cache);
118+
const result = getSourceCodeCache(getState());
119+
expect(result).toBeInstanceOf(Map);
158120
expect(result.get(0)).toEqual({
159121
type: 'AVAILABLE',
160122
code: 'console.log("Source 0");',
161123
});
162124
});
163125

164126
it('returns empty Map when no cache is set', function () {
165-
const store = setupStoreWithSourceIndex(null);
166-
const state = store.getState();
127+
const { getState } = setupStoreWithSourceIndex(null);
167128

168-
const result = getSourceCodeCache(state);
129+
const result = getSourceCodeCache(getState());
169130
expect(result).toBeInstanceOf(Map);
170131
expect(result.size).toBe(0);
171132
});
172133
});
173134

174-
describe('edge cases', function () {
175-
it('handles different SourceCodeStatus types correctly', function () {
176-
const cache = createMockCache();
135+
describe('state transitions', function () {
136+
it('can transition from LOADING to AVAILABLE', function () {
137+
const { getState, dispatch } = setupStoreWithSourceIndex(0);
138+
dispatch(beginLoadingSourceCodeFromUrl(0, 'https://example.com/test.js'));
177139

178-
// Test each type of status
179-
expect(cache.get(0)?.type).toBe('AVAILABLE');
180-
expect(cache.get(2)?.type).toBe('LOADING');
181-
expect(cache.get(3)?.type).toBe('ERROR');
182-
});
140+
let sourceViewCode = getSourceViewCode(getState());
141+
expect(sourceViewCode?.type).toBe('LOADING');
183142

184-
it('handles multiple error types', function () {
185-
const store = setupStoreWithSourceIndex(5);
186-
const cache = new Map<IndexIntoSourceTable, SourceCodeStatus>();
187-
cache.set(5, {
188-
type: 'ERROR',
189-
errors: [
190-
{
191-
type: 'NETWORK_ERROR',
192-
url: 'https://example.com/source.js',
193-
networkErrorMessage: 'Network failed',
194-
},
195-
{ type: 'NO_KNOWN_CORS_URL' },
196-
],
143+
dispatch(finishLoadingSourceCode(0, 'const result = 42;'));
144+
145+
sourceViewCode = getSourceViewCode(getState());
146+
expect(sourceViewCode).toEqual({
147+
type: 'AVAILABLE',
148+
code: 'const result = 42;',
197149
});
198-
const state = createStateWithMockCache(store, cache);
150+
});
151+
152+
it('can transition from LOADING to ERROR', function () {
153+
const { getState, dispatch } = setupStoreWithSourceIndex(1);
154+
dispatch(beginLoadingSourceCodeFromUrl(1, 'https://example.com/test.js'));
155+
156+
let sourceViewCode = getSourceViewCode(getState());
157+
expect(sourceViewCode?.type).toBe('LOADING');
158+
159+
dispatch(failLoadingSourceCode(1, [{ type: 'NO_KNOWN_CORS_URL' }]));
199160

200-
const sourceViewCode = getSourceViewCode(state);
161+
sourceViewCode = getSourceViewCode(getState());
201162
expect(sourceViewCode).toEqual({
202163
type: 'ERROR',
203-
errors: [
204-
{
205-
type: 'NETWORK_ERROR',
206-
url: 'https://example.com/source.js',
207-
networkErrorMessage: 'Network failed',
208-
},
209-
{ type: 'NO_KNOWN_CORS_URL' },
210-
],
164+
errors: [{ type: 'NO_KNOWN_CORS_URL' }],
211165
});
212166
});
213167
});

0 commit comments

Comments
 (0)