-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.test.ts
More file actions
77 lines (71 loc) · 2.16 KB
/
index.test.ts
File metadata and controls
77 lines (71 loc) · 2.16 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
/* eslint-disable @typescript-eslint/consistent-type-assertions */
import {chrome} from 'jest-chrome';
import {
describe, it, assert, expect,
} from 'vitest';
import {executeFunction, getTabsByUrl} from './index.js';
const tab1 = {
id: 1,
url: 'https://example.com/index.html',
} as chrome.tabs.Tab;
const tab2 = {
id: 2,
url: 'http://no-way.example.com/other/index.html',
} as chrome.tabs.Tab;
const queryMap = new Map([
['https://example.com/*', [tab1]],
['http://no-way.example.com/*', [tab2]],
['*://*/*', [tab1, tab2]],
]);
// @ts-expect-error junk types
chrome.tabs.query.mockImplementation((query, callback: (...arguments_: any) => void) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- Junk types
callback(queryMap.get(query.url[0]) ?? []);
});
describe('getTabsByUrl', () => {
it('should handle the matches array', async () => {
assert.deepEqual(
await getTabsByUrl([]),
[],
'No patterns means no tabs',
);
assert.deepEqual(
await getTabsByUrl(['https://example.com/*']),
[1],
'It should pass the query to chrome.tabs',
);
assert.deepEqual(
await getTabsByUrl(['*://*/*']),
[1, 2],
'It should pass the query to chrome.tabs',
);
});
it('should handle the `excludeMatches` array', async () => {
const excludeMatches = ['http://*/*'];
assert.deepEqual(
await getTabsByUrl([], excludeMatches),
[],
'No patterns means no tabs',
);
assert.deepEqual(
await getTabsByUrl(['https://example.com/*'], excludeMatches),
[1],
'It should pass the query to chrome.tabs',
);
assert.deepEqual(
await getTabsByUrl(['*://*/*'], excludeMatches),
[1],
'It should exclude tabs with URLs matching `excludeMatches`',
);
assert.deepEqual(
await getTabsByUrl(['http://no-way.example.com/*'], excludeMatches),
[],
'It should exclude tabs with URLs matching `excludeMatches`, even if it’s the only match',
);
});
});
describe('executeFunction', () => {
it('should throw with native functions', async () => {
await expect(executeFunction(1, Date)).rejects.toMatchInlineSnapshot('[TypeError: Native functions need to be wrapped first, like `executeFunction(1, () => alert(1))`]');
});
});