-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathicons.test.ts
More file actions
135 lines (118 loc) · 4.56 KB
/
icons.test.ts
File metadata and controls
135 lines (118 loc) · 4.56 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
/* 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 { createImageMock } from '../fixtures/mocks/image';
import { blankStore } from '../fixtures/stores';
import * as iconsAccessors from '../../selectors/icons';
import * as iconsActions from '../../actions/icons';
import type { CallNodeDisplayData } from 'firefox-profiler/types';
import { waitFor } from 'firefox-profiler/test/fixtures/testing-library';
describe('actions/icons', function () {
const validIcons = [
'https://valid.icon1.example.org/favicon.ico',
'https://valid.icon2.example.org/favicon.ico',
];
const expectedClasses = ['favicon-1', 'favicon-2'];
const invalidIcon = 'https://invalid.icon.example.org/favicon.ico';
let imageInstances: HTMLImageElement[] = [];
beforeEach(() => {
const mock = createImageMock();
imageInstances = mock.instances;
(window as any).Image = mock.Image;
});
afterEach(() => {
delete (window as any).Image;
imageInstances = [];
iconsActions._resetIconCounter();
});
function _createCallNodeWithIcon(icon: string): CallNodeDisplayData {
return {
total: '0',
totalWithUnit: '0 ms',
totalPercent: '0',
self: '0',
selfWithUnit: '0 ms',
name: 'icon',
lib: 'icon',
isFrameLabel: false,
categoryName: 'Other',
categoryColor: 'grey',
icon,
iconSrc: 'https://edition.cnn.com/favicon.ico',
ariaLabel: 'fake aria label',
};
}
describe('With the initial state', function () {
function getInitialState() {
return blankStore().getState();
}
it('getIconsWithClassNames returns an empty map', function () {
const initialState =
iconsAccessors.getIconsWithClassNames(getInitialState());
expect(initialState).toBeInstanceOf(Map);
expect(initialState.size).toEqual(0);
});
it('getIconClassName returns an empty string for any icon', function () {
const subject = iconsAccessors.getIconClassName(
getInitialState(),
_createCallNodeWithIcon(validIcons[0]).icon
);
expect(subject).toBe('');
});
});
describe('Requesting an existing icon', function () {
it('will populate the local cache', async function () {
const { dispatch, getState } = blankStore();
const promises = [
dispatch(iconsActions.iconStartLoading(validIcons[0])),
// Second request for the same icon shouldn't dspatch anything
dispatch(iconsActions.iconStartLoading(validIcons[0])),
// 3rd request for another icon should dispatch the loaded action
dispatch(iconsActions.iconStartLoading(validIcons[1])),
];
// Wait until we have 2 image instances after calling iconStartLoading.
await waitFor(() => expect(imageInstances.length).toBe(2));
// Only 2 requests because only 2 different icons
expect(imageInstances.length).toBe(2);
imageInstances.forEach((instance, i) => {
expect(instance.src).toEqual(validIcons[i]);
expect(instance.referrerPolicy).toEqual('no-referrer');
});
imageInstances.forEach((instance) => (instance as any).onload());
await Promise.all(promises);
const state = getState();
const subjects = iconsAccessors.getIconsWithClassNames(state);
expect([...subjects]).toEqual(
validIcons.map((icon, i) => [icon, expectedClasses[i]])
);
validIcons.forEach((icon, i) => {
const iconClass = iconsAccessors.getIconClassName(
state,
_createCallNodeWithIcon(icon).icon
);
expect(iconClass).toEqual(expectedClasses[i]);
});
});
});
describe('Requesting a non-existing image', function () {
it('will not populate the local cache', async function () {
const { dispatch, getState } = blankStore();
const actionPromise = dispatch(
iconsActions.iconStartLoading(invalidIcon)
);
// Wait until we have 2 image instances after calling iconStartLoading.
await waitFor(() => expect(imageInstances.length).toBe(1));
expect(imageInstances.length).toBe(1);
(imageInstances[0] as any).onerror();
await actionPromise;
const state = getState();
const subjects = iconsAccessors.getIconsWithClassNames(state);
expect([...subjects]).toEqual([]);
const iconClass = iconsAccessors.getIconClassName(
state,
_createCallNodeWithIcon(invalidIcon).icon
);
expect(iconClass).toBe('');
});
});
});