-
Notifications
You must be signed in to change notification settings - Fork 36
/
tabs.test.js
133 lines (131 loc) · 5.42 KB
/
tabs.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
describe('browser.tabs', () => {
test('get', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.get)).toBe(true);
chrome.tabs.get(1, callback);
expect(chrome.tabs.get).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
});
test('getCurrent', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.getCurrent)).toBe(true);
chrome.tabs.getCurrent(callback);
expect(chrome.tabs.getCurrent).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
});
test('connect', () => {
const name = 'CONNECT_NAME';
expect(jest.isMockFunction(chrome.tabs.connect)).toBe(true);
const connection = chrome.tabs.connect(1, { name });
expect(connection.name).toEqual(name);
expect(jest.isMockFunction(connection.disconnect)).toBe(true);
expect(jest.isMockFunction(connection.postMessage)).toBe(true);
expect(jest.isMockFunction(connection.onDisconnect.addListener)).toBe(true);
expect(jest.isMockFunction(connection.onMessage.addListener)).toBe(true);
expect(chrome.tabs.connect).toHaveBeenCalledTimes(1);
});
test('create', (done) => {
const callback = jest.fn(() => done());
const props = { pinned: true };
expect(jest.isMockFunction(chrome.tabs.create)).toBe(true);
chrome.tabs.create(props, callback);
expect(chrome.tabs.create).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(props);
});
test('create promise', () => {
const props = { pinned: true };
return expect(chrome.tabs.create(props)).resolves.toBe(props);
});
test('duplicate', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.duplicate)).toBe(true);
chrome.tabs.duplicate(1, callback);
expect(chrome.tabs.duplicate).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith({ id: 1 });
});
test('remove', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.remove)).toBe(true);
chrome.tabs.remove([1], callback);
expect(chrome.tabs.remove).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
});
test('remove promise', () => {
expect(chrome.tabs.remove([1])).resolves.toBeUndefined();
});
test('query', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.query)).toBe(true);
chrome.tabs.query({ pinned: true }, callback);
expect(chrome.tabs.query).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith([{}]);
});
test('highlight', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.highlight)).toBe(true);
chrome.tabs.highlight({}, callback);
expect(chrome.tabs.highlight).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
});
test('update', (done) => {
const callback = jest.fn(() => done());
const props = { pinned: true };
expect(jest.isMockFunction(chrome.tabs.update)).toBe(true);
chrome.tabs.update(1, props, callback);
expect(chrome.tabs.update).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith({ id: 1, pinned: true });
chrome.tabs.update(props);
expect(chrome.tabs.update).toHaveBeenCalledTimes(2);
});
test('move', (done) => {
const callback = jest.fn(() => done());
const props = { pinned: true };
expect(jest.isMockFunction(chrome.tabs.move)).toBe(true);
chrome.tabs.move([1, 2, 3], props, callback);
expect(chrome.tabs.move).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith([
{ id: 1, pinned: true },
{ id: 2, pinned: true },
{ id: 3, pinned: true },
]);
});
['addListener', 'removeListener', 'hasListener'].forEach((method) => {
test(`onUpdated.${method}`, () => {
const callback = jest.fn();
expect(jest.isMockFunction(browser.tabs.onUpdated[method])).toBe(true);
browser.tabs.onUpdated[method](callback);
expect(browser.tabs.onUpdated[method]).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(0);
});
});
test('reload', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.reload)).toBe(true);
chrome.tabs.reload(1, {}, callback);
expect(chrome.tabs.reload).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
});
test('sendMessage', (done) => {
const callback = jest.fn(() => done());
expect(jest.isMockFunction(chrome.tabs.sendMessage)).toBe(true);
chrome.tabs.sendMessage(1, { test: 'message' }, callback);
expect(chrome.tabs.sendMessage).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
chrome.tabs.sendMessage(1, { test: 'message' });
expect(chrome.tabs.sendMessage).toHaveBeenCalledTimes(2);
});
test('sendMessage listener', (done) => {
const listener = jest.fn();
browser.runtime.onMessage.addListener(listener);
chrome.tabs.sendMessage(1, { test: 'message' }, done);
expect(listener).toHaveBeenCalledWith(1, { test: 'message' });
});
test('sendMessage promise', () => {
return expect(chrome.tabs.sendMessage({})).resolves.toBeUndefined();
});
});