forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-channel.test.js
144 lines (115 loc) · 4.24 KB
/
web-channel.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
/* 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 {
enableMenuButton,
queryIsMenuButtonEnabled,
WebChannelError,
} from '../../app-logic/web-channel';
import { mockWebChannel } from '../fixtures/mocks/web-channel';
describe('event handlers for Firefox WebChannel events', function () {
it('can test if the menu button is enabled', async () => {
const {
messagesSentToBrowser,
listeners,
triggerResponse,
getLastRequestId,
} = mockWebChannel();
// Initially there are no listeners
expect(listeners).toHaveLength(0);
expect(messagesSentToBrowser).toHaveLength(0);
// Query the menu button is enabled.
const response = queryIsMenuButtonEnabled();
// Now there should be a listener.
expect(messagesSentToBrowser).toHaveLength(1);
expect(listeners).toHaveLength(1);
expect(messagesSentToBrowser[0].message.type).toEqual('STATUS_QUERY');
// Trigger the response from the browser.
triggerResponse({
type: 'SUCCESS_RESPONSE',
requestId: getLastRequestId(),
response: {
menuButtonIsEnabled: true,
},
});
// Check that the response makes sense and the listeners are cleared.
const isMenuButtonEnabled = await response;
expect(listeners).toHaveLength(0);
expect(isMenuButtonEnabled).toBe(true);
});
it('handles legacy STATUS_QUERY responses correctly', async () => {
// This test can be removed once the oldest supported Firefox ESR version is 93 or newer.
const { triggerResponse, getLastRequestId } = mockWebChannel();
// Query the menu button is enabled.
const responseForTrue = queryIsMenuButtonEnabled();
// Trigger the response from the browser.
triggerResponse(
({
type: 'STATUS_RESPONSE',
requestId: getLastRequestId(),
menuButtonIsEnabled: true,
}: any)
);
const isMenuButtonEnabledForTrue = await responseForTrue;
expect(isMenuButtonEnabledForTrue).toBe(true);
// Query the menu button is enabled.
const responseForFalse = queryIsMenuButtonEnabled();
// Trigger the response from the browser.
triggerResponse(
({
type: 'STATUS_RESPONSE',
requestId: getLastRequestId(),
menuButtonIsEnabled: false,
}: any)
);
const isMenuButtonEnabledForFalse = await responseForFalse;
expect(isMenuButtonEnabledForFalse).toBe(false);
});
it('handles legacy ENABLE_MENU_BUTTON_DONE responses correctly', async () => {
// This test can be removed once the oldest supported Firefox ESR version is 93 or newer.
const { triggerResponse, getLastRequestId } = mockWebChannel();
// Ask the browser to enable the menu button.
const response = enableMenuButton();
// Trigger the response from the browser.
triggerResponse(
({
type: 'ENABLE_MENU_BUTTON_DONE',
requestId: getLastRequestId(),
}: any)
);
await expect(response).resolves.toBe(undefined);
});
it('will error if the message is not understood by Firefox', async function () {
const { triggerResponse } = mockWebChannel();
const consoleError = jest
.spyOn(console, 'error')
.mockImplementation(() => {});
const response = queryIsMenuButtonEnabled();
triggerResponse({
errno: 2,
error: 'No Such Channel',
});
await expect(response).rejects.toEqual(
new WebChannelError({
errno: 2,
error: 'No Such Channel',
})
);
expect(consoleError).toHaveBeenCalled();
});
it('will error if the messages are received that are malformed', async function () {
const { triggerResponse } = mockWebChannel();
const consoleError = jest
.spyOn(console, 'error')
.mockImplementation(() => {});
const response = queryIsMenuButtonEnabled();
// The triggerResponse doesn't allow unknown message types, so coerce it
// into a Function to test the error path.
(triggerResponse: any)('Invalid message');
await expect(response).rejects.toEqual(
new Error('A malformed WebChannel event was received.')
);
expect(consoleError).toHaveBeenCalled();
});
});