This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrome-mock.test.js
135 lines (124 loc) · 4.53 KB
/
chrome-mock.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
const expect = require('chai').expect;
const sinon = require('sinon');
const ChromeMock = require('chrome-mock');
const wait = (amount) => new Promise(resolve => setTimeout(resolve, amount));
describe('ChromeMock', () => {
beforeEach(() => {
ChromeMock.reset();
});
describe('messaging', () => {
describe('browser_action -> content_script', () => {
it('should be able to query tabs', done => {
const tab1 = ChromeMock.createTab({});
const tab2 = ChromeMock.createTab({});
const browserActionScript = `
window.callback = () => {}; // To be stubbed
window.runTest = () => {
window.chrome.tabs.query({
currentWindow: true,
active: true
}, tabs => {
window.callback(tabs);
});
}
`;
const browserAction = ChromeMock.createBrowserAction({ script: browserActionScript });
browserAction.window.callback = sinon.stub();
browserAction.window.runTest();
wait(100).then(() => {
expect(browserAction.window.callback.calledWith([{
id: 0
}, {
id: 1
}])).to.be.true;
done();
});
});
it('should be able to send message to a particular tab', done => {
const tab1 = ChromeMock.createTab({
content_scripts: [`
window.callback = () => {}; // To be stubbed
window.chrome.runtime.onMessage((message, sender, sendResponse) => {
window.callback(message, sender);
sendResponse({ ok: true });
});
`]
});
const tab2 = ChromeMock.createTab({
content_scripts: [`
window.callback = () => {}; // To be stubbed
window.chrome.runtime.onMessage((message, sender, sendResponse) => {
window.callback(message, sender);
sendResponse({ ok: false });
});
`]
});
tab1.window.callback = sinon.stub();
tab2.window.callback = sinon.stub();
const browserActionScript = `
window.callback = () => {}; // To be stubbed
window.runTest = () => {
window.chrome.tabs.query({
currentWindow: true,
active: true
}, tabs => {
const targetTabId = tabs[0].id;
window.chrome.tabs.sendMessage(targetTabId, {
type: 'test'
}, response => {
window.callback(response);
});
});
}
`;
const browserAction = ChromeMock.createBrowserAction({ script: browserActionScript });
browserAction.window.callback = sinon.stub();
browserAction.window.runTest();
wait(200).then(() => {
expect(tab1.window.callback.calledWith({
type: 'test'
})).to.be.true;
expect(tab2.window.callback.called).to.be.false;
expect(browserAction.window.callback.calledWith({
ok: true
})).to.be.true;
done();
});
});
});
describe('content_script -> content_script', () => {
it('should be able to send message between two content_scripts', done => {
const contentScript1 = `
window.callback = () => {}; // To be stubbed
window.chrome.runtime.onMessage((message, sender, sendResponse) => {
window.callback(message, sender);
sendResponse({ response: true });
});
`;
const tab1 = ChromeMock.createTab({ content_scripts: [contentScript1] });
const contentScript2 = `
window.callback = () => {}; // To be stubbed
window.runTest = () => {
window.chrome.runtime.sendMessage(undefined, { message: true }, response => {
window.callback(response);
});
}
`;
const tab2 = ChromeMock.createTab({ content_scripts: [contentScript2] });
// Set stubs
tab1.window.callback = sinon.stub();
tab2.window.callback = sinon.stub();
// Run test
tab2.window.runTest();
wait(1000).then(() => {
const expectedMessage = { message: true };
const expectedResponse = { response: true };
const expectedSender = { tab: { id: tab2.tabId } };
expect(tab1.window.callback.calledWith(expectedMessage, expectedSender)).to.be.true;
expect(tab2.window.callback.calledWith(expectedResponse)).to.be.true;
done();
});
});
});
});
});