Skip to content

Commit 9e2a125

Browse files
committed
Add tests for souce code cache
1 parent 9478c98 commit 9e2a125

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
// @flow
6+
7+
import { getSourceViewCode } from '../../selectors/code';
8+
import { storeWithProfile } from '../fixtures/stores';
9+
import { updateBottomBoxContentsAndMaybeOpen } from '../../actions/profile-view';
10+
11+
import type { State, SourceCodeStatus } from 'firefox-profiler/types';
12+
13+
describe('source code cache with sourceId', function () {
14+
describe('source view code selector', function () {
15+
it('should handle source code with sourceId correctly', function () {
16+
const store = storeWithProfile();
17+
18+
// Test that the selector works with sourceId
19+
store.dispatch(
20+
updateBottomBoxContentsAndMaybeOpen('calltree', {
21+
libIndex: 0,
22+
sourceId: 42,
23+
sourceFile: 'test-file.js',
24+
nativeSymbols: [],
25+
})
26+
);
27+
28+
const sourceViewCode = getSourceViewCode(store.getState());
29+
30+
// The selector should return undefined since there's no cached source code
31+
expect(sourceViewCode).toBe(undefined);
32+
});
33+
34+
it('should handle source code without sourceId', function () {
35+
const store = storeWithProfile();
36+
37+
// Test that the selector works without sourceId
38+
store.dispatch(
39+
updateBottomBoxContentsAndMaybeOpen('calltree', {
40+
libIndex: 0,
41+
sourceId: null,
42+
sourceFile: 'test-file.js',
43+
nativeSymbols: [],
44+
})
45+
);
46+
47+
const sourceViewCode = getSourceViewCode(store.getState());
48+
49+
// The selector should return undefined since there's no cached source code
50+
expect(sourceViewCode).toBe(undefined);
51+
});
52+
53+
it('should create different cache lookups for different sourceIds', function () {
54+
const store = storeWithProfile();
55+
56+
// Mock the source code cache to have entries
57+
const mockCache = new Map();
58+
mockCache.set('test-file.js', {
59+
type: 'AVAILABLE',
60+
code: 'source without sourceId',
61+
});
62+
mockCache.set('test-file.js-42', {
63+
type: 'AVAILABLE',
64+
code: 'source with sourceId 42',
65+
});
66+
mockCache.set('test-file.js-123', {
67+
type: 'AVAILABLE',
68+
code: 'source with sourceId 123',
69+
});
70+
71+
// First, let's open the bottom box with sourceId
72+
store.dispatch(
73+
updateBottomBoxContentsAndMaybeOpen('calltree', {
74+
libIndex: 0,
75+
sourceId: 42,
76+
sourceFile: 'test-file.js',
77+
nativeSymbols: [],
78+
})
79+
);
80+
81+
// Manually set the cache in the state for testing
82+
const state = {
83+
...store.getState(),
84+
code: {
85+
...store.getState().code,
86+
sourceCodeCache: mockCache,
87+
},
88+
};
89+
90+
const sourceViewCode = getSourceViewCode(state);
91+
92+
expect(sourceViewCode).toEqual({
93+
type: 'AVAILABLE',
94+
code: 'source with sourceId 42',
95+
});
96+
});
97+
98+
it('should fallback to cache without sourceId when sourceId is null', function () {
99+
const store = storeWithProfile();
100+
101+
// Mock the source code cache to have entries
102+
const mockCache: Map<string, SourceCodeStatus> = new Map();
103+
mockCache.set('test-file.js', {
104+
type: 'AVAILABLE',
105+
code: 'source without sourceId',
106+
});
107+
mockCache.set('test-file.js-42', {
108+
type: 'AVAILABLE',
109+
code: 'source with sourceId 42',
110+
});
111+
112+
// First, let's open the bottom box with null sourceId
113+
store.dispatch(
114+
updateBottomBoxContentsAndMaybeOpen('calltree', {
115+
libIndex: 0,
116+
sourceId: null,
117+
sourceFile: 'test-file.js',
118+
nativeSymbols: [],
119+
})
120+
);
121+
122+
// Manually set the cache in the state for testing
123+
const state: State = {
124+
...store.getState(),
125+
code: {
126+
...store.getState().code,
127+
sourceCodeCache: mockCache,
128+
},
129+
};
130+
131+
const sourceViewCode = getSourceViewCode(state);
132+
133+
expect(sourceViewCode).toEqual({
134+
type: 'AVAILABLE',
135+
code: 'source without sourceId',
136+
});
137+
});
138+
});
139+
});

0 commit comments

Comments
 (0)