jest-webextension-mock 3.9.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/push.yml +1 -1
- package/CHANGELOG.md +12 -0
- package/__tests__/browserAction.test.js +8 -8
- package/__tests__/commands.test.js +2 -2
- package/__tests__/downloads.test.js +46 -25
- package/__tests__/notifications.test.js +10 -10
- package/__tests__/runtime.test.js +30 -19
- package/__tests__/storage.test.js +14 -33
- package/__tests__/tabs.test.js +26 -24
- package/dist/setup.js +118 -176
- package/package.json +12 -13
- package/src/storage.js +36 -146
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# jest-webextension-mock
|
|
2
2
|
|
|
3
|
+
## 4.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- b8594f9: Drop support for node 14 and 16, add support for node 18 and 22
|
|
8
|
+
|
|
9
|
+
## 3.9.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Fix storage returning undefined for unknown keys
|
|
14
|
+
|
|
3
15
|
## 3.9.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
|
@@ -5,8 +5,8 @@ describe('browser.browserAction', () => {
|
|
|
5
5
|
browser.browserAction.setTitle(title);
|
|
6
6
|
expect(browser.browserAction.setTitle).toHaveBeenCalledWith(title);
|
|
7
7
|
});
|
|
8
|
-
test('getTitle', (
|
|
9
|
-
const callback = jest.fn(
|
|
8
|
+
test('getTitle', () => {
|
|
9
|
+
const callback = jest.fn();
|
|
10
10
|
expect(jest.isMockFunction(browser.browserAction.getTitle)).toBe(true);
|
|
11
11
|
browser.browserAction.getTitle({}, callback);
|
|
12
12
|
expect(browser.browserAction.getTitle).toHaveBeenCalledTimes(1);
|
|
@@ -29,8 +29,8 @@ describe('browser.browserAction', () => {
|
|
|
29
29
|
browser.browserAction.setPopup(details);
|
|
30
30
|
expect(browser.browserAction.setPopup).toHaveBeenCalledWith(details);
|
|
31
31
|
});
|
|
32
|
-
test('getPopup', (
|
|
33
|
-
const callback = jest.fn(
|
|
32
|
+
test('getPopup', () => {
|
|
33
|
+
const callback = jest.fn();
|
|
34
34
|
expect(jest.isMockFunction(browser.browserAction.getPopup)).toBe(true);
|
|
35
35
|
browser.browserAction.getPopup({}, callback);
|
|
36
36
|
expect(browser.browserAction.getPopup).toHaveBeenCalledTimes(1);
|
|
@@ -46,8 +46,8 @@ describe('browser.browserAction', () => {
|
|
|
46
46
|
browser.browserAction.setBadgeText(text);
|
|
47
47
|
expect(browser.browserAction.setBadgeText).toHaveBeenCalledWith(text);
|
|
48
48
|
});
|
|
49
|
-
test('getBadgeText', (
|
|
50
|
-
const callback = jest.fn(
|
|
49
|
+
test('getBadgeText', () => {
|
|
50
|
+
const callback = jest.fn();
|
|
51
51
|
expect(jest.isMockFunction(browser.browserAction.getBadgeText)).toBe(true);
|
|
52
52
|
browser.browserAction.getBadgeText({}, callback);
|
|
53
53
|
expect(browser.browserAction.getBadgeText).toHaveBeenCalledTimes(1);
|
|
@@ -69,8 +69,8 @@ describe('browser.browserAction', () => {
|
|
|
69
69
|
details
|
|
70
70
|
);
|
|
71
71
|
});
|
|
72
|
-
test('getBadgeBackgroundColor', (
|
|
73
|
-
const callback = jest.fn(
|
|
72
|
+
test('getBadgeBackgroundColor', () => {
|
|
73
|
+
const callback = jest.fn();
|
|
74
74
|
expect(
|
|
75
75
|
jest.isMockFunction(browser.browserAction.getBadgeBackgroundColor)
|
|
76
76
|
).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
describe('browser.commands', () => {
|
|
2
|
-
test('getAll', (
|
|
3
|
-
const callback = jest.fn(
|
|
2
|
+
test('getAll', () => {
|
|
3
|
+
const callback = jest.fn();
|
|
4
4
|
expect(jest.isMockFunction(browser.commands.getAll)).toBe(true);
|
|
5
5
|
browser.commands.getAll(callback);
|
|
6
6
|
expect(browser.commands.getAll).toHaveBeenCalledTimes(1);
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
describe('browser.downloads', () => {
|
|
2
|
-
test('acceptDanger', (
|
|
2
|
+
test('acceptDanger', () => {
|
|
3
3
|
const downloadId = 1;
|
|
4
|
-
const callback = jest.fn(
|
|
4
|
+
const callback = jest.fn();
|
|
5
5
|
|
|
6
6
|
expect(jest.isMockFunction(chrome.downloads.acceptDanger)).toBe(true);
|
|
7
7
|
chrome.downloads.acceptDanger(downloadId, callback);
|
|
8
8
|
expect(chrome.downloads.acceptDanger).toHaveBeenCalledTimes(1);
|
|
9
|
-
expect(chrome.downloads.acceptDanger).toHaveBeenLastCalledWith(
|
|
9
|
+
expect(chrome.downloads.acceptDanger).toHaveBeenLastCalledWith(
|
|
10
|
+
downloadId,
|
|
11
|
+
callback
|
|
12
|
+
);
|
|
10
13
|
expect(callback).toHaveBeenCalled();
|
|
11
14
|
});
|
|
12
15
|
test('acceptDanger Promise', () => {
|
|
@@ -14,14 +17,17 @@ describe('browser.downloads', () => {
|
|
|
14
17
|
|
|
15
18
|
expect(chrome.downloads.acceptDanger(downloadId)).resolves.toBeUndefined();
|
|
16
19
|
});
|
|
17
|
-
test('cancel', (
|
|
20
|
+
test('cancel', () => {
|
|
18
21
|
const downloadId = 1;
|
|
19
|
-
const callback = jest.fn(
|
|
22
|
+
const callback = jest.fn();
|
|
20
23
|
|
|
21
24
|
expect(jest.isMockFunction(chrome.downloads.cancel)).toBe(true);
|
|
22
25
|
chrome.downloads.cancel(downloadId, callback);
|
|
23
26
|
expect(chrome.downloads.cancel).toHaveBeenCalledTimes(1);
|
|
24
|
-
expect(chrome.downloads.cancel).toHaveBeenLastCalledWith(
|
|
27
|
+
expect(chrome.downloads.cancel).toHaveBeenLastCalledWith(
|
|
28
|
+
downloadId,
|
|
29
|
+
callback
|
|
30
|
+
);
|
|
25
31
|
expect(callback).toHaveBeenCalled();
|
|
26
32
|
});
|
|
27
33
|
test('cancel Promise', () => {
|
|
@@ -29,14 +35,17 @@ describe('browser.downloads', () => {
|
|
|
29
35
|
|
|
30
36
|
expect(chrome.downloads.cancel(downloadId)).resolves.toBeUndefined();
|
|
31
37
|
});
|
|
32
|
-
test('download', (
|
|
38
|
+
test('download', () => {
|
|
33
39
|
const options = {};
|
|
34
|
-
const callback = jest.fn(
|
|
40
|
+
const callback = jest.fn();
|
|
35
41
|
|
|
36
42
|
expect(jest.isMockFunction(chrome.downloads.download)).toBe(true);
|
|
37
43
|
chrome.downloads.download(options, callback);
|
|
38
44
|
expect(chrome.downloads.download).toHaveBeenCalledTimes(1);
|
|
39
|
-
expect(chrome.downloads.download).toHaveBeenLastCalledWith(
|
|
45
|
+
expect(chrome.downloads.download).toHaveBeenLastCalledWith(
|
|
46
|
+
options,
|
|
47
|
+
callback
|
|
48
|
+
);
|
|
40
49
|
expect(callback).toHaveBeenCalled();
|
|
41
50
|
});
|
|
42
51
|
test('download Promise', () => {
|
|
@@ -44,9 +53,9 @@ describe('browser.downloads', () => {
|
|
|
44
53
|
|
|
45
54
|
expect(chrome.downloads.download(options)).resolves.toBeUndefined();
|
|
46
55
|
});
|
|
47
|
-
test('erase', (
|
|
56
|
+
test('erase', () => {
|
|
48
57
|
const query = {};
|
|
49
|
-
const callback = jest.fn(
|
|
58
|
+
const callback = jest.fn();
|
|
50
59
|
|
|
51
60
|
expect(jest.isMockFunction(chrome.downloads.erase)).toBe(true);
|
|
52
61
|
chrome.downloads.erase(query, callback);
|
|
@@ -59,14 +68,17 @@ describe('browser.downloads', () => {
|
|
|
59
68
|
|
|
60
69
|
expect(chrome.downloads.erase(query)).resolves.toBeUndefined();
|
|
61
70
|
});
|
|
62
|
-
test('getFileIcon', (
|
|
71
|
+
test('getFileIcon', () => {
|
|
63
72
|
const downloadId = 1;
|
|
64
|
-
const callback = jest.fn(
|
|
73
|
+
const callback = jest.fn();
|
|
65
74
|
|
|
66
75
|
expect(jest.isMockFunction(chrome.downloads.getFileIcon)).toBe(true);
|
|
67
76
|
chrome.downloads.getFileIcon(downloadId, callback);
|
|
68
77
|
expect(chrome.downloads.getFileIcon).toHaveBeenCalledTimes(1);
|
|
69
|
-
expect(chrome.downloads.getFileIcon).toHaveBeenLastCalledWith(
|
|
78
|
+
expect(chrome.downloads.getFileIcon).toHaveBeenLastCalledWith(
|
|
79
|
+
downloadId,
|
|
80
|
+
callback
|
|
81
|
+
);
|
|
70
82
|
expect(callback).toHaveBeenCalled();
|
|
71
83
|
});
|
|
72
84
|
test('getFileIcon Promise', () => {
|
|
@@ -82,14 +94,17 @@ describe('browser.downloads', () => {
|
|
|
82
94
|
expect(chrome.downloads.open).toHaveBeenCalledTimes(1);
|
|
83
95
|
expect(chrome.downloads.open).toHaveBeenLastCalledWith(downloadId);
|
|
84
96
|
});
|
|
85
|
-
test('pause', (
|
|
97
|
+
test('pause', () => {
|
|
86
98
|
const downloadId = 1;
|
|
87
|
-
const callback = jest.fn(
|
|
99
|
+
const callback = jest.fn();
|
|
88
100
|
|
|
89
101
|
expect(jest.isMockFunction(chrome.downloads.pause)).toBe(true);
|
|
90
102
|
chrome.downloads.pause(downloadId, callback);
|
|
91
103
|
expect(chrome.downloads.pause).toHaveBeenCalledTimes(1);
|
|
92
|
-
expect(chrome.downloads.pause).toHaveBeenLastCalledWith(
|
|
104
|
+
expect(chrome.downloads.pause).toHaveBeenLastCalledWith(
|
|
105
|
+
downloadId,
|
|
106
|
+
callback
|
|
107
|
+
);
|
|
93
108
|
expect(callback).toHaveBeenCalled();
|
|
94
109
|
});
|
|
95
110
|
test('pause Promise', () => {
|
|
@@ -97,14 +112,17 @@ describe('browser.downloads', () => {
|
|
|
97
112
|
|
|
98
113
|
expect(chrome.downloads.pause(downloadId)).resolves.toBeUndefined();
|
|
99
114
|
});
|
|
100
|
-
test('removeFile', (
|
|
115
|
+
test('removeFile', () => {
|
|
101
116
|
const downloadId = 1;
|
|
102
|
-
const callback = jest.fn(
|
|
117
|
+
const callback = jest.fn();
|
|
103
118
|
|
|
104
119
|
expect(jest.isMockFunction(chrome.downloads.removeFile)).toBe(true);
|
|
105
120
|
chrome.downloads.removeFile(downloadId, callback);
|
|
106
121
|
expect(chrome.downloads.removeFile).toHaveBeenCalledTimes(1);
|
|
107
|
-
expect(chrome.downloads.removeFile).toHaveBeenLastCalledWith(
|
|
122
|
+
expect(chrome.downloads.removeFile).toHaveBeenLastCalledWith(
|
|
123
|
+
downloadId,
|
|
124
|
+
callback
|
|
125
|
+
);
|
|
108
126
|
expect(callback).toHaveBeenCalled();
|
|
109
127
|
});
|
|
110
128
|
test('removeFile Promise', () => {
|
|
@@ -112,14 +130,17 @@ describe('browser.downloads', () => {
|
|
|
112
130
|
|
|
113
131
|
expect(chrome.downloads.removeFile(downloadId)).resolves.toBeUndefined();
|
|
114
132
|
});
|
|
115
|
-
test('resume', (
|
|
133
|
+
test('resume', () => {
|
|
116
134
|
const downloadId = 1;
|
|
117
|
-
const callback = jest.fn(
|
|
135
|
+
const callback = jest.fn();
|
|
118
136
|
|
|
119
137
|
expect(jest.isMockFunction(chrome.downloads.resume)).toBe(true);
|
|
120
138
|
chrome.downloads.resume(downloadId, callback);
|
|
121
139
|
expect(chrome.downloads.resume).toHaveBeenCalledTimes(1);
|
|
122
|
-
expect(chrome.downloads.resume).toHaveBeenLastCalledWith(
|
|
140
|
+
expect(chrome.downloads.resume).toHaveBeenLastCalledWith(
|
|
141
|
+
downloadId,
|
|
142
|
+
callback
|
|
143
|
+
);
|
|
123
144
|
expect(callback).toHaveBeenCalled();
|
|
124
145
|
});
|
|
125
146
|
test('resume Promise', () => {
|
|
@@ -127,9 +148,9 @@ describe('browser.downloads', () => {
|
|
|
127
148
|
|
|
128
149
|
expect(chrome.downloads.resume(downloadId)).resolves.toBeUndefined();
|
|
129
150
|
});
|
|
130
|
-
test('search', (
|
|
151
|
+
test('search', () => {
|
|
131
152
|
const query = {};
|
|
132
|
-
const callback = jest.fn(
|
|
153
|
+
const callback = jest.fn();
|
|
133
154
|
|
|
134
155
|
expect(jest.isMockFunction(chrome.downloads.search)).toBe(true);
|
|
135
156
|
chrome.downloads.search(query, callback);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
describe('chrome.notifications', () => {
|
|
2
|
-
test('create', (
|
|
2
|
+
test('create', () => {
|
|
3
3
|
const options = { type: 'basic' };
|
|
4
|
-
const callback = jest.fn(
|
|
4
|
+
const callback = jest.fn();
|
|
5
5
|
|
|
6
6
|
expect(jest.isMockFunction(chrome.notifications.create)).toBe(true);
|
|
7
7
|
|
|
@@ -31,9 +31,9 @@ describe('chrome.notifications', () => {
|
|
|
31
31
|
);
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
test('update', (
|
|
34
|
+
test('update', () => {
|
|
35
35
|
const options = { type: 'basic' };
|
|
36
|
-
const callback = jest.fn(
|
|
36
|
+
const callback = jest.fn();
|
|
37
37
|
|
|
38
38
|
expect(jest.isMockFunction(chrome.notifications.update)).toBe(true);
|
|
39
39
|
chrome.notifications.update('id', options, callback);
|
|
@@ -52,8 +52,8 @@ describe('chrome.notifications', () => {
|
|
|
52
52
|
return expect(chrome.notifications.update(options)).resolves.toBe(true);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
test('clear', (
|
|
56
|
-
const callback = jest.fn(
|
|
55
|
+
test('clear', () => {
|
|
56
|
+
const callback = jest.fn();
|
|
57
57
|
|
|
58
58
|
expect(jest.isMockFunction(chrome.notifications.clear)).toBe(true);
|
|
59
59
|
chrome.notifications.clear('id', callback);
|
|
@@ -66,8 +66,8 @@ describe('chrome.notifications', () => {
|
|
|
66
66
|
return expect(chrome.notifications.clear('id')).resolves.toBe(true);
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
test('getAll', (
|
|
70
|
-
const callback = jest.fn(
|
|
69
|
+
test('getAll', () => {
|
|
70
|
+
const callback = jest.fn();
|
|
71
71
|
|
|
72
72
|
expect(jest.isMockFunction(chrome.notifications.getAll)).toBe(true);
|
|
73
73
|
chrome.notifications.getAll(callback);
|
|
@@ -80,8 +80,8 @@ describe('chrome.notifications', () => {
|
|
|
80
80
|
return expect(chrome.notifications.getAll()).resolves.toEqual([]);
|
|
81
81
|
});
|
|
82
82
|
|
|
83
|
-
test('getPermissionLevel', (
|
|
84
|
-
const callback = jest.fn(
|
|
83
|
+
test('getPermissionLevel', () => {
|
|
84
|
+
const callback = jest.fn();
|
|
85
85
|
|
|
86
86
|
expect(jest.isMockFunction(chrome.notifications.getPermissionLevel)).toBe(
|
|
87
87
|
true
|
|
@@ -20,23 +20,26 @@ describe('browser.runtime', () => {
|
|
|
20
20
|
expect(jest.isMockFunction(connection.disconnect)).toBe(true);
|
|
21
21
|
expect(browser.runtime.connect).toHaveBeenCalledTimes(1);
|
|
22
22
|
});
|
|
23
|
-
test('connect.onMessage listener', (
|
|
23
|
+
test('connect.onMessage listener', () => {
|
|
24
24
|
const name = 'CONNECT_NAME';
|
|
25
25
|
const listener = jest.fn();
|
|
26
|
+
const callback = jest.fn();
|
|
26
27
|
browser.runtime.connect(name).onMessage.addListener(listener);
|
|
27
|
-
browser.runtime.sendMessage({ test: 'message' },
|
|
28
|
+
browser.runtime.sendMessage({ test: 'message' }, callback);
|
|
28
29
|
expect(listener).toHaveBeenCalledWith({ test: 'message' });
|
|
30
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
29
31
|
});
|
|
30
32
|
test('getURL', () => {
|
|
31
33
|
const path = 'TEST_PATH';
|
|
32
|
-
const extensionOriginURL =
|
|
34
|
+
const extensionOriginURL =
|
|
35
|
+
globalThis[Symbol.for('jest-webextension-mock')].extensionPath;
|
|
33
36
|
expect(jest.isMockFunction(browser.runtime.getURL)).toBe(true);
|
|
34
37
|
const respPath = browser.runtime.getURL(path);
|
|
35
38
|
expect(respPath).toEqual(extensionOriginURL + path);
|
|
36
39
|
expect(browser.runtime.getURL).toHaveBeenCalledTimes(1);
|
|
37
40
|
});
|
|
38
|
-
test('sendMessage', (
|
|
39
|
-
const callback = jest.fn(
|
|
41
|
+
test('sendMessage', () => {
|
|
42
|
+
const callback = jest.fn();
|
|
40
43
|
expect(jest.isMockFunction(browser.runtime.sendMessage)).toBe(true);
|
|
41
44
|
browser.runtime.sendMessage({ test: 'message' }, callback);
|
|
42
45
|
expect(browser.runtime.sendMessage).toHaveBeenCalledTimes(1);
|
|
@@ -44,11 +47,13 @@ describe('browser.runtime', () => {
|
|
|
44
47
|
browser.runtime.sendMessage({ test: 'message' });
|
|
45
48
|
expect(browser.runtime.sendMessage).toHaveBeenCalledTimes(2);
|
|
46
49
|
});
|
|
47
|
-
test('sendMessage listener', (
|
|
50
|
+
test('sendMessage listener', () => {
|
|
48
51
|
const listener = jest.fn();
|
|
52
|
+
const callback = jest.fn();
|
|
49
53
|
browser.runtime.onMessage.addListener(listener);
|
|
50
|
-
browser.runtime.sendMessage({ test: 'message' },
|
|
54
|
+
browser.runtime.sendMessage({ test: 'message' }, callback);
|
|
51
55
|
expect(listener).toHaveBeenCalledWith({ test: 'message' });
|
|
56
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
52
57
|
});
|
|
53
58
|
test('sendMessage promise', () => {
|
|
54
59
|
return expect(browser.runtime.sendMessage({})).resolves.toBeUndefined();
|
|
@@ -85,32 +90,38 @@ describe('browser.runtime', () => {
|
|
|
85
90
|
});
|
|
86
91
|
test(`onMessageExternal.addListener`, () => {
|
|
87
92
|
const callback = jest.fn();
|
|
88
|
-
expect(
|
|
89
|
-
|
|
90
|
-
);
|
|
93
|
+
expect(
|
|
94
|
+
jest.isMockFunction(browser.runtime.onMessageExternal.addListener)
|
|
95
|
+
).toBe(true);
|
|
91
96
|
browser.runtime.onMessageExternal.addListener(callback);
|
|
92
|
-
expect(browser.runtime.onMessageExternal.addListener).toHaveBeenCalledTimes(
|
|
97
|
+
expect(browser.runtime.onMessageExternal.addListener).toHaveBeenCalledTimes(
|
|
98
|
+
1
|
|
99
|
+
);
|
|
93
100
|
expect(callback).toHaveBeenCalledTimes(0);
|
|
94
101
|
});
|
|
95
102
|
test(`onMessageExternal.removeListener`, () => {
|
|
96
103
|
const callback = jest.fn();
|
|
97
|
-
expect(
|
|
98
|
-
|
|
99
|
-
);
|
|
104
|
+
expect(
|
|
105
|
+
jest.isMockFunction(browser.runtime.onMessageExternal.removeListener)
|
|
106
|
+
).toBe(true);
|
|
100
107
|
browser.runtime.onMessageExternal.removeListener(callback);
|
|
101
108
|
expect(browser.runtime.onMessageExternal.hasListener(callback)).toBe(false);
|
|
102
|
-
expect(
|
|
109
|
+
expect(
|
|
110
|
+
browser.runtime.onMessageExternal.removeListener
|
|
111
|
+
).toHaveBeenCalledTimes(1);
|
|
103
112
|
expect(callback).toHaveBeenCalledTimes(0);
|
|
104
113
|
});
|
|
105
114
|
test(`onMessageExternal.hasListener`, () => {
|
|
106
115
|
const callback = jest.fn();
|
|
107
|
-
expect(
|
|
108
|
-
|
|
109
|
-
);
|
|
116
|
+
expect(
|
|
117
|
+
jest.isMockFunction(browser.runtime.onMessageExternal.hasListener)
|
|
118
|
+
).toBe(true);
|
|
110
119
|
browser.runtime.onMessageExternal.addListener(callback);
|
|
111
120
|
const returnVal = browser.runtime.onMessageExternal.hasListener(callback);
|
|
112
121
|
expect(returnVal).toBe(true);
|
|
113
|
-
expect(browser.runtime.onMessageExternal.hasListener).toHaveBeenCalledTimes(
|
|
122
|
+
expect(browser.runtime.onMessageExternal.hasListener).toHaveBeenCalledTimes(
|
|
123
|
+
1
|
|
124
|
+
);
|
|
114
125
|
expect(callback).toHaveBeenCalledTimes(0);
|
|
115
126
|
});
|
|
116
127
|
['addListener', 'removeListener', 'hasListener'].forEach((method) => {
|
|
@@ -19,32 +19,21 @@ describe('browser.storage', () => {
|
|
|
19
19
|
test('a string key', (done) => {
|
|
20
20
|
const key = 'test';
|
|
21
21
|
storage.get(key, (result) => {
|
|
22
|
-
expect(result).
|
|
23
|
-
expect(typeof result === 'object').toBeTruthy();
|
|
24
|
-
expect(result).toHaveProperty(key, undefined);
|
|
22
|
+
expect(result).toStrictEqual({});
|
|
25
23
|
done();
|
|
26
24
|
});
|
|
27
25
|
});
|
|
28
26
|
test('an array key', (done) => {
|
|
29
27
|
const keys = ['test1', 'test2'];
|
|
30
28
|
storage.get(keys, (result) => {
|
|
31
|
-
expect(result).
|
|
32
|
-
expect(typeof result === 'object').toBeTruthy();
|
|
33
|
-
keys.forEach((k) => {
|
|
34
|
-
expect(result).toHaveProperty(k, undefined);
|
|
35
|
-
});
|
|
29
|
+
expect(result).toStrictEqual({});
|
|
36
30
|
done();
|
|
37
31
|
});
|
|
38
32
|
});
|
|
39
33
|
test('an object key', (done) => {
|
|
40
34
|
const key = { test: [] };
|
|
41
35
|
storage.get(key, (result) => {
|
|
42
|
-
expect(result).
|
|
43
|
-
expect(typeof result === 'object').toBeTruthy();
|
|
44
|
-
Object.keys(key).forEach((k) => {
|
|
45
|
-
expect(result).toHaveProperty(k);
|
|
46
|
-
expect(result[k]).toEqual(key[k]);
|
|
47
|
-
});
|
|
36
|
+
expect(result).toStrictEqual({ test: [] });
|
|
48
37
|
done();
|
|
49
38
|
});
|
|
50
39
|
});
|
|
@@ -71,8 +60,8 @@ describe('browser.storage', () => {
|
|
|
71
60
|
const key = 'key';
|
|
72
61
|
return expect(storage.get(key)).resolves.toEqual({ key: undefined });
|
|
73
62
|
});
|
|
74
|
-
test('getBytesInUse', (
|
|
75
|
-
const callback = jest.fn(
|
|
63
|
+
test('getBytesInUse', () => {
|
|
64
|
+
const callback = jest.fn();
|
|
76
65
|
expect(jest.isMockFunction(storage.getBytesInUse)).toBe(true);
|
|
77
66
|
storage.getBytesInUse('key', callback);
|
|
78
67
|
expect(storage.getBytesInUse).toHaveBeenCalledTimes(1);
|
|
@@ -81,8 +70,8 @@ describe('browser.storage', () => {
|
|
|
81
70
|
test('getBytesInUse promise', () => {
|
|
82
71
|
return expect(storage.getBytesInUse('key')).resolves.toBe(0);
|
|
83
72
|
});
|
|
84
|
-
test('set', (
|
|
85
|
-
const callback = jest.fn(
|
|
73
|
+
test('set', () => {
|
|
74
|
+
const callback = jest.fn();
|
|
86
75
|
expect(jest.isMockFunction(storage.set)).toBe(true);
|
|
87
76
|
storage.set({ key: 'foo' }, callback);
|
|
88
77
|
expect(storage.set).toHaveBeenCalledTimes(1);
|
|
@@ -91,8 +80,8 @@ describe('browser.storage', () => {
|
|
|
91
80
|
test('set promise', () => {
|
|
92
81
|
return expect(storage.set(1)).resolves.toBeUndefined();
|
|
93
82
|
});
|
|
94
|
-
test('remove', (
|
|
95
|
-
const callback = jest.fn(
|
|
83
|
+
test('remove', () => {
|
|
84
|
+
const callback = jest.fn();
|
|
96
85
|
expect(jest.isMockFunction(storage.remove)).toBe(true);
|
|
97
86
|
storage.remove('key', callback);
|
|
98
87
|
expect(storage.remove).toHaveBeenCalledTimes(1);
|
|
@@ -101,8 +90,8 @@ describe('browser.storage', () => {
|
|
|
101
90
|
test('remove promise', () => {
|
|
102
91
|
return expect(storage.remove(['foo', 'bar'])).resolves.toBeUndefined();
|
|
103
92
|
});
|
|
104
|
-
test('clear', (
|
|
105
|
-
const callback = jest.fn(
|
|
93
|
+
test('clear', () => {
|
|
94
|
+
const callback = jest.fn();
|
|
106
95
|
expect(jest.isMockFunction(browser.storage.sync.clear)).toBe(true);
|
|
107
96
|
storage.clear(callback);
|
|
108
97
|
expect(storage.clear).toHaveBeenCalledTimes(1);
|
|
@@ -120,24 +109,16 @@ describe('browser.storage', () => {
|
|
|
120
109
|
storage.set({ key: 'value', foo: 'bar', foo2: 'bar2' }, () => {
|
|
121
110
|
// get 'key'
|
|
122
111
|
storage.get(['key'], (result) => {
|
|
123
|
-
expect(result).
|
|
124
|
-
expect(typeof result === 'object').toBeTruthy();
|
|
125
|
-
expect(result).toHaveProperty('key', 'value');
|
|
126
|
-
expect(result).not.toHaveProperty('foo');
|
|
127
|
-
expect(result).not.toHaveProperty('foo2');
|
|
112
|
+
expect(result).toStrictEqual({ key: 'value' });
|
|
128
113
|
// remove 'key'
|
|
129
114
|
storage.remove('key', () => {
|
|
130
115
|
// get all values
|
|
131
116
|
storage.get(null, (result) => {
|
|
132
|
-
expect(result).
|
|
133
|
-
expect(result).toHaveProperty('foo', 'bar');
|
|
134
|
-
expect(result).toHaveProperty('foo2', 'bar2');
|
|
117
|
+
expect(result).toStrictEqual({ foo: 'bar', foo2: 'bar2' });
|
|
135
118
|
// clear values
|
|
136
119
|
storage.clear(() => {
|
|
137
120
|
storage.get(['key', 'foo', 'foo2'], (result) => {
|
|
138
|
-
expect(result).
|
|
139
|
-
expect(result).toHaveProperty('foo', undefined);
|
|
140
|
-
expect(result).toHaveProperty('foo2', undefined);
|
|
121
|
+
expect(result).toStrictEqual({});
|
|
141
122
|
done();
|
|
142
123
|
});
|
|
143
124
|
});
|
package/__tests__/tabs.test.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
describe('browser.tabs', () => {
|
|
2
|
-
test('get', (
|
|
3
|
-
const callback = jest.fn(
|
|
2
|
+
test('get', () => {
|
|
3
|
+
const callback = jest.fn();
|
|
4
4
|
expect(jest.isMockFunction(chrome.tabs.get)).toBe(true);
|
|
5
5
|
chrome.tabs.get(1, callback);
|
|
6
6
|
expect(chrome.tabs.get).toHaveBeenCalledTimes(1);
|
|
7
7
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
8
8
|
});
|
|
9
|
-
test('getCurrent', (
|
|
10
|
-
const callback = jest.fn(
|
|
9
|
+
test('getCurrent', () => {
|
|
10
|
+
const callback = jest.fn();
|
|
11
11
|
expect(jest.isMockFunction(chrome.tabs.getCurrent)).toBe(true);
|
|
12
12
|
chrome.tabs.getCurrent(callback);
|
|
13
13
|
expect(chrome.tabs.getCurrent).toHaveBeenCalledTimes(1);
|
|
@@ -24,8 +24,8 @@ describe('browser.tabs', () => {
|
|
|
24
24
|
expect(jest.isMockFunction(connection.onMessage.addListener)).toBe(true);
|
|
25
25
|
expect(chrome.tabs.connect).toHaveBeenCalledTimes(1);
|
|
26
26
|
});
|
|
27
|
-
test('create', (
|
|
28
|
-
const callback = jest.fn(
|
|
27
|
+
test('create', () => {
|
|
28
|
+
const callback = jest.fn();
|
|
29
29
|
const props = { pinned: true };
|
|
30
30
|
expect(jest.isMockFunction(chrome.tabs.create)).toBe(true);
|
|
31
31
|
chrome.tabs.create(props, callback);
|
|
@@ -37,16 +37,16 @@ describe('browser.tabs', () => {
|
|
|
37
37
|
const props = { pinned: true };
|
|
38
38
|
return expect(chrome.tabs.create(props)).resolves.toBe(props);
|
|
39
39
|
});
|
|
40
|
-
test('duplicate', (
|
|
41
|
-
const callback = jest.fn(
|
|
40
|
+
test('duplicate', () => {
|
|
41
|
+
const callback = jest.fn();
|
|
42
42
|
expect(jest.isMockFunction(chrome.tabs.duplicate)).toBe(true);
|
|
43
43
|
chrome.tabs.duplicate(1, callback);
|
|
44
44
|
expect(chrome.tabs.duplicate).toHaveBeenCalledTimes(1);
|
|
45
45
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
46
46
|
expect(callback).toHaveBeenCalledWith({ id: 1 });
|
|
47
47
|
});
|
|
48
|
-
test('remove', (
|
|
49
|
-
const callback = jest.fn(
|
|
48
|
+
test('remove', () => {
|
|
49
|
+
const callback = jest.fn();
|
|
50
50
|
expect(jest.isMockFunction(chrome.tabs.remove)).toBe(true);
|
|
51
51
|
chrome.tabs.remove([1], callback);
|
|
52
52
|
expect(chrome.tabs.remove).toHaveBeenCalledTimes(1);
|
|
@@ -55,23 +55,23 @@ describe('browser.tabs', () => {
|
|
|
55
55
|
test('remove promise', () => {
|
|
56
56
|
expect(chrome.tabs.remove([1])).resolves.toBeUndefined();
|
|
57
57
|
});
|
|
58
|
-
test('query', (
|
|
59
|
-
const callback = jest.fn(
|
|
58
|
+
test('query', () => {
|
|
59
|
+
const callback = jest.fn();
|
|
60
60
|
expect(jest.isMockFunction(chrome.tabs.query)).toBe(true);
|
|
61
61
|
chrome.tabs.query({ pinned: true }, callback);
|
|
62
62
|
expect(chrome.tabs.query).toHaveBeenCalledTimes(1);
|
|
63
63
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
64
64
|
expect(callback).toHaveBeenCalledWith([{}]);
|
|
65
65
|
});
|
|
66
|
-
test('highlight', (
|
|
67
|
-
const callback = jest.fn(
|
|
66
|
+
test('highlight', () => {
|
|
67
|
+
const callback = jest.fn();
|
|
68
68
|
expect(jest.isMockFunction(chrome.tabs.highlight)).toBe(true);
|
|
69
69
|
chrome.tabs.highlight({}, callback);
|
|
70
70
|
expect(chrome.tabs.highlight).toHaveBeenCalledTimes(1);
|
|
71
71
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
72
72
|
});
|
|
73
|
-
test('update', (
|
|
74
|
-
const callback = jest.fn(
|
|
73
|
+
test('update', () => {
|
|
74
|
+
const callback = jest.fn();
|
|
75
75
|
const props = { pinned: true };
|
|
76
76
|
expect(jest.isMockFunction(chrome.tabs.update)).toBe(true);
|
|
77
77
|
chrome.tabs.update(1, props, callback);
|
|
@@ -81,8 +81,8 @@ describe('browser.tabs', () => {
|
|
|
81
81
|
chrome.tabs.update(props);
|
|
82
82
|
expect(chrome.tabs.update).toHaveBeenCalledTimes(2);
|
|
83
83
|
});
|
|
84
|
-
test('move', (
|
|
85
|
-
const callback = jest.fn(
|
|
84
|
+
test('move', () => {
|
|
85
|
+
const callback = jest.fn();
|
|
86
86
|
const props = { pinned: true };
|
|
87
87
|
expect(jest.isMockFunction(chrome.tabs.move)).toBe(true);
|
|
88
88
|
chrome.tabs.move([1, 2, 3], props, callback);
|
|
@@ -119,8 +119,8 @@ describe('browser.tabs', () => {
|
|
|
119
119
|
expect(callback).toHaveBeenCalledTimes(0);
|
|
120
120
|
});
|
|
121
121
|
});
|
|
122
|
-
test('reload', (
|
|
123
|
-
const callback = jest.fn(
|
|
122
|
+
test('reload', () => {
|
|
123
|
+
const callback = jest.fn();
|
|
124
124
|
expect(jest.isMockFunction(chrome.tabs.reload)).toBe(true);
|
|
125
125
|
|
|
126
126
|
chrome.tabs.reload(1, {}, callback);
|
|
@@ -128,8 +128,8 @@ describe('browser.tabs', () => {
|
|
|
128
128
|
expect(chrome.tabs.reload).toHaveBeenCalledTimes(1);
|
|
129
129
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
130
130
|
});
|
|
131
|
-
test('sendMessage', (
|
|
132
|
-
const callback = jest.fn(
|
|
131
|
+
test('sendMessage', () => {
|
|
132
|
+
const callback = jest.fn();
|
|
133
133
|
expect(jest.isMockFunction(chrome.tabs.sendMessage)).toBe(true);
|
|
134
134
|
chrome.tabs.sendMessage(1, { test: 'message' }, callback);
|
|
135
135
|
expect(chrome.tabs.sendMessage).toHaveBeenCalledTimes(1);
|
|
@@ -137,11 +137,13 @@ describe('browser.tabs', () => {
|
|
|
137
137
|
chrome.tabs.sendMessage(1, { test: 'message' });
|
|
138
138
|
expect(chrome.tabs.sendMessage).toHaveBeenCalledTimes(2);
|
|
139
139
|
});
|
|
140
|
-
test('sendMessage listener', (
|
|
140
|
+
test('sendMessage listener', () => {
|
|
141
141
|
const listener = jest.fn();
|
|
142
|
+
const callback = jest.fn();
|
|
142
143
|
browser.runtime.onMessage.addListener(listener);
|
|
143
|
-
chrome.tabs.sendMessage(1, { test: 'message' },
|
|
144
|
+
chrome.tabs.sendMessage(1, { test: 'message' }, callback);
|
|
144
145
|
expect(listener).toHaveBeenCalledWith(1, { test: 'message' });
|
|
146
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
145
147
|
});
|
|
146
148
|
test('sendMessage promise', () => {
|
|
147
149
|
return expect(chrome.tabs.sendMessage({})).resolves.toBeUndefined();
|
package/dist/setup.js
CHANGED
|
@@ -1,47 +1,104 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
function
|
|
4
|
-
|
|
3
|
+
function _arrayLikeToArray(r, a) {
|
|
4
|
+
(null == a || a > r.length) && (a = r.length);
|
|
5
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
6
|
+
return n;
|
|
7
|
+
}
|
|
8
|
+
function _arrayWithHoles(r) {
|
|
9
|
+
if (Array.isArray(r)) return r;
|
|
10
|
+
}
|
|
11
|
+
function _defineProperty(e, r, t) {
|
|
12
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
13
|
+
value: t,
|
|
14
|
+
enumerable: !0,
|
|
15
|
+
configurable: !0,
|
|
16
|
+
writable: !0
|
|
17
|
+
}) : e[r] = t, e;
|
|
18
|
+
}
|
|
19
|
+
function _iterableToArrayLimit(r, l) {
|
|
20
|
+
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
21
|
+
if (null != t) {
|
|
22
|
+
var e,
|
|
23
|
+
n,
|
|
24
|
+
i,
|
|
25
|
+
u,
|
|
26
|
+
a = [],
|
|
27
|
+
f = !0,
|
|
28
|
+
o = !1;
|
|
29
|
+
try {
|
|
30
|
+
if (i = (t = t.call(r)).next, 0 === l) {
|
|
31
|
+
if (Object(t) !== t) return;
|
|
32
|
+
f = !1;
|
|
33
|
+
} else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
|
|
34
|
+
} catch (r) {
|
|
35
|
+
o = !0, n = r;
|
|
36
|
+
} finally {
|
|
37
|
+
try {
|
|
38
|
+
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
|
|
39
|
+
} finally {
|
|
40
|
+
if (o) throw n;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return a;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function _nonIterableRest() {
|
|
47
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
48
|
+
}
|
|
49
|
+
function ownKeys(e, r) {
|
|
50
|
+
var t = Object.keys(e);
|
|
5
51
|
if (Object.getOwnPropertySymbols) {
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
return Object.getOwnPropertyDescriptor(
|
|
9
|
-
})),
|
|
52
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
53
|
+
r && (o = o.filter(function (r) {
|
|
54
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
55
|
+
})), t.push.apply(t, o);
|
|
10
56
|
}
|
|
11
|
-
return
|
|
57
|
+
return t;
|
|
12
58
|
}
|
|
13
|
-
function _objectSpread2(
|
|
14
|
-
for (var
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
-
_defineProperty(
|
|
18
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(
|
|
19
|
-
Object.defineProperty(
|
|
59
|
+
function _objectSpread2(e) {
|
|
60
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
61
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
62
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
|
|
63
|
+
_defineProperty(e, r, t[r]);
|
|
64
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
65
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
20
66
|
});
|
|
21
67
|
}
|
|
22
|
-
return
|
|
68
|
+
return e;
|
|
69
|
+
}
|
|
70
|
+
function _slicedToArray(r, e) {
|
|
71
|
+
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
|
|
23
72
|
}
|
|
24
|
-
function
|
|
73
|
+
function _toPrimitive(t, r) {
|
|
74
|
+
if ("object" != typeof t || !t) return t;
|
|
75
|
+
var e = t[Symbol.toPrimitive];
|
|
76
|
+
if (void 0 !== e) {
|
|
77
|
+
var i = e.call(t, r || "default");
|
|
78
|
+
if ("object" != typeof i) return i;
|
|
79
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
80
|
+
}
|
|
81
|
+
return ("string" === r ? String : Number)(t);
|
|
82
|
+
}
|
|
83
|
+
function _toPropertyKey(t) {
|
|
84
|
+
var i = _toPrimitive(t, "string");
|
|
85
|
+
return "symbol" == typeof i ? i : i + "";
|
|
86
|
+
}
|
|
87
|
+
function _typeof(o) {
|
|
25
88
|
"@babel/helpers - typeof";
|
|
26
89
|
|
|
27
|
-
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (
|
|
28
|
-
return typeof
|
|
29
|
-
} : function (
|
|
30
|
-
return
|
|
31
|
-
}, _typeof(
|
|
90
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
|
|
91
|
+
return typeof o;
|
|
92
|
+
} : function (o) {
|
|
93
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
94
|
+
}, _typeof(o);
|
|
32
95
|
}
|
|
33
|
-
function
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: true
|
|
40
|
-
});
|
|
41
|
-
} else {
|
|
42
|
-
obj[key] = value;
|
|
96
|
+
function _unsupportedIterableToArray(r, a) {
|
|
97
|
+
if (r) {
|
|
98
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
99
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
100
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
43
101
|
}
|
|
44
|
-
return obj;
|
|
45
102
|
}
|
|
46
103
|
|
|
47
104
|
// https://developer.chrome.com/extensions/omnibox
|
|
@@ -259,115 +316,34 @@ var createEventListeners = function createEventListeners() {
|
|
|
259
316
|
};
|
|
260
317
|
};
|
|
261
318
|
|
|
262
|
-
var syncStore = {};
|
|
263
|
-
var localStore = {};
|
|
264
|
-
var managedStore = {};
|
|
265
319
|
function resolveKey(key, store) {
|
|
266
320
|
if (typeof key === 'string') {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
321
|
+
if (key in store) {
|
|
322
|
+
return {
|
|
323
|
+
key: store[key]
|
|
324
|
+
};
|
|
325
|
+
} else {
|
|
326
|
+
return {};
|
|
327
|
+
}
|
|
270
328
|
} else if (Array.isArray(key)) {
|
|
271
|
-
return key.reduce(function (acc,
|
|
272
|
-
acc
|
|
273
|
-
return acc;
|
|
329
|
+
return key.reduce(function (acc, currKey) {
|
|
330
|
+
return _objectSpread2(_objectSpread2({}, acc), resolveKey(currKey, store));
|
|
274
331
|
}, {});
|
|
275
332
|
} else if (_typeof(key) === 'object') {
|
|
276
|
-
return Object.
|
|
277
|
-
|
|
278
|
-
|
|
333
|
+
return Object.entries(key).reduce(function (acc, _ref) {
|
|
334
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
335
|
+
currKey = _ref2[0],
|
|
336
|
+
fallbackValue = _ref2[1];
|
|
337
|
+
return _objectSpread2(_objectSpread2({}, acc), {}, _defineProperty({}, currKey, fallbackValue), resolveKey(currKey, store));
|
|
279
338
|
}, {});
|
|
280
339
|
}
|
|
281
340
|
throw new Error('Wrong key given');
|
|
282
341
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
var result = id === null || id === undefined ? syncStore : resolveKey(id, syncStore);
|
|
287
|
-
if (cb !== undefined) {
|
|
288
|
-
return cb(result);
|
|
289
|
-
}
|
|
290
|
-
return Promise.resolve(result);
|
|
291
|
-
}),
|
|
292
|
-
getBytesInUse: jest.fn(function (id, cb) {
|
|
293
|
-
if (cb !== undefined) {
|
|
294
|
-
return cb(0);
|
|
295
|
-
}
|
|
296
|
-
return Promise.resolve(0);
|
|
297
|
-
}),
|
|
298
|
-
set: jest.fn(function (payload, cb) {
|
|
299
|
-
Object.keys(payload).forEach(function (key) {
|
|
300
|
-
return syncStore[key] = payload[key];
|
|
301
|
-
});
|
|
302
|
-
if (cb !== undefined) {
|
|
303
|
-
return cb();
|
|
304
|
-
}
|
|
305
|
-
return Promise.resolve();
|
|
306
|
-
}),
|
|
307
|
-
remove: jest.fn(function (id, cb) {
|
|
308
|
-
var keys = typeof id === 'string' ? [id] : id;
|
|
309
|
-
keys.forEach(function (key) {
|
|
310
|
-
return delete syncStore[key];
|
|
311
|
-
});
|
|
312
|
-
if (cb !== undefined) {
|
|
313
|
-
return cb();
|
|
314
|
-
}
|
|
315
|
-
return Promise.resolve();
|
|
316
|
-
}),
|
|
317
|
-
clear: jest.fn(function (cb) {
|
|
318
|
-
syncStore = {};
|
|
319
|
-
if (cb !== undefined) {
|
|
320
|
-
return cb();
|
|
321
|
-
}
|
|
322
|
-
return Promise.resolve();
|
|
323
|
-
}),
|
|
324
|
-
onChanged: createEventListeners()
|
|
325
|
-
},
|
|
326
|
-
local: {
|
|
327
|
-
get: jest.fn(function (id, cb) {
|
|
328
|
-
var result = id === null || id === undefined ? localStore : resolveKey(id, localStore);
|
|
329
|
-
if (cb !== undefined) {
|
|
330
|
-
return cb(result);
|
|
331
|
-
}
|
|
332
|
-
return Promise.resolve(result);
|
|
333
|
-
}),
|
|
334
|
-
getBytesInUse: jest.fn(function (id, cb) {
|
|
335
|
-
if (cb !== undefined) {
|
|
336
|
-
return cb(0);
|
|
337
|
-
}
|
|
338
|
-
return Promise.resolve(0);
|
|
339
|
-
}),
|
|
340
|
-
set: jest.fn(function (payload, cb) {
|
|
341
|
-
Object.keys(payload).forEach(function (key) {
|
|
342
|
-
return localStore[key] = payload[key];
|
|
343
|
-
});
|
|
344
|
-
if (cb !== undefined) {
|
|
345
|
-
return cb();
|
|
346
|
-
}
|
|
347
|
-
return Promise.resolve();
|
|
348
|
-
}),
|
|
349
|
-
remove: jest.fn(function (id, cb) {
|
|
350
|
-
var keys = typeof id === 'string' ? [id] : id;
|
|
351
|
-
keys.forEach(function (key) {
|
|
352
|
-
return delete localStore[key];
|
|
353
|
-
});
|
|
354
|
-
if (cb !== undefined) {
|
|
355
|
-
return cb();
|
|
356
|
-
}
|
|
357
|
-
return Promise.resolve();
|
|
358
|
-
}),
|
|
359
|
-
clear: jest.fn(function (cb) {
|
|
360
|
-
localStore = {};
|
|
361
|
-
if (cb !== undefined) {
|
|
362
|
-
return cb();
|
|
363
|
-
}
|
|
364
|
-
return Promise.resolve();
|
|
365
|
-
}),
|
|
366
|
-
onChanged: createEventListeners()
|
|
367
|
-
},
|
|
368
|
-
session: {
|
|
342
|
+
function mockStore() {
|
|
343
|
+
var store = {};
|
|
344
|
+
return {
|
|
369
345
|
get: jest.fn(function (id, cb) {
|
|
370
|
-
var result = id === null || id === undefined ?
|
|
346
|
+
var result = id === null || id === undefined ? store : resolveKey(id, store);
|
|
371
347
|
if (cb !== undefined) {
|
|
372
348
|
return cb(result);
|
|
373
349
|
}
|
|
@@ -381,7 +357,7 @@ var storage = {
|
|
|
381
357
|
}),
|
|
382
358
|
set: jest.fn(function (payload, cb) {
|
|
383
359
|
Object.keys(payload).forEach(function (key) {
|
|
384
|
-
return
|
|
360
|
+
return store[key] = payload[key];
|
|
385
361
|
});
|
|
386
362
|
if (cb !== undefined) {
|
|
387
363
|
return cb();
|
|
@@ -391,7 +367,7 @@ var storage = {
|
|
|
391
367
|
remove: jest.fn(function (id, cb) {
|
|
392
368
|
var keys = typeof id === 'string' ? [id] : id;
|
|
393
369
|
keys.forEach(function (key) {
|
|
394
|
-
return delete
|
|
370
|
+
return delete store[key];
|
|
395
371
|
});
|
|
396
372
|
if (cb !== undefined) {
|
|
397
373
|
return cb();
|
|
@@ -399,56 +375,22 @@ var storage = {
|
|
|
399
375
|
return Promise.resolve();
|
|
400
376
|
}),
|
|
401
377
|
clear: jest.fn(function (cb) {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
return cb();
|
|
405
|
-
}
|
|
406
|
-
return Promise.resolve();
|
|
407
|
-
}),
|
|
408
|
-
onChanged: createEventListeners()
|
|
409
|
-
},
|
|
410
|
-
managed: {
|
|
411
|
-
get: jest.fn(function (id, cb) {
|
|
412
|
-
var result = id === null || id === undefined ? managedStore : resolveKey(id, managedStore);
|
|
413
|
-
if (cb !== undefined) {
|
|
414
|
-
return cb(result);
|
|
415
|
-
}
|
|
416
|
-
return Promise.resolve(result);
|
|
417
|
-
}),
|
|
418
|
-
getBytesInUse: jest.fn(function (id, cb) {
|
|
419
|
-
if (cb !== undefined) {
|
|
420
|
-
return cb(0);
|
|
421
|
-
}
|
|
422
|
-
return Promise.resolve(0);
|
|
423
|
-
}),
|
|
424
|
-
set: jest.fn(function (payload, cb) {
|
|
425
|
-
Object.keys(payload).forEach(function (key) {
|
|
426
|
-
return managedStore[key] = payload[key];
|
|
427
|
-
});
|
|
428
|
-
if (cb !== undefined) {
|
|
429
|
-
return cb();
|
|
430
|
-
}
|
|
431
|
-
return Promise.resolve();
|
|
432
|
-
}),
|
|
433
|
-
remove: jest.fn(function (id, cb) {
|
|
434
|
-
var keys = typeof id === 'string' ? [id] : id;
|
|
435
|
-
keys.forEach(function (key) {
|
|
436
|
-
return delete managedStore[key];
|
|
378
|
+
Object.keys(store).forEach(function (key) {
|
|
379
|
+
return delete store[key];
|
|
437
380
|
});
|
|
438
381
|
if (cb !== undefined) {
|
|
439
382
|
return cb();
|
|
440
383
|
}
|
|
441
384
|
return Promise.resolve();
|
|
442
385
|
}),
|
|
443
|
-
clear: jest.fn(function (cb) {
|
|
444
|
-
managedStore = {};
|
|
445
|
-
if (cb !== undefined) {
|
|
446
|
-
return cb();
|
|
447
|
-
}
|
|
448
|
-
return Promise.resolve();
|
|
449
|
-
}),
|
|
450
386
|
onChanged: createEventListeners()
|
|
451
|
-
}
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
var storage = {
|
|
390
|
+
sync: mockStore(),
|
|
391
|
+
local: mockStore(),
|
|
392
|
+
session: mockStore(),
|
|
393
|
+
managed: mockStore(),
|
|
452
394
|
onChanged: createEventListeners()
|
|
453
395
|
};
|
|
454
396
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jest-webextension-mock",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Mock the components of a WebExtension",
|
|
5
5
|
"main": "dist/setup.js",
|
|
6
6
|
"module": "src/setup.js",
|
|
@@ -18,20 +18,19 @@
|
|
|
18
18
|
"eslint-check": "eslint --print-config .eslintrc.js | eslint-config-prettier-check"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@babel/core": "^7.
|
|
22
|
-
"@babel/preset-env": "^7.
|
|
23
|
-
"@changesets/cli": "^2.27.
|
|
24
|
-
"babel-
|
|
25
|
-
"
|
|
26
|
-
"eslint": "^
|
|
27
|
-
"eslint-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"rollup": "^2.30.0",
|
|
21
|
+
"@babel/core": "^7.25.8",
|
|
22
|
+
"@babel/preset-env": "^7.25.8",
|
|
23
|
+
"@changesets/cli": "^2.27.9",
|
|
24
|
+
"babel-jest": "^29.7.0",
|
|
25
|
+
"eslint": "^9.13.0",
|
|
26
|
+
"eslint-config-prettier": "^9.1.0",
|
|
27
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
28
|
+
"jest": "^29.7.0",
|
|
29
|
+
"prettier": "^3.3.3",
|
|
30
|
+
"rollup": "^2.79.2",
|
|
32
31
|
"rollup-plugin-babel": "^4.4.0",
|
|
33
32
|
"rollup-plugin-node-resolve": "^5.2.0",
|
|
34
|
-
"rollup-watch": "^4.
|
|
33
|
+
"rollup-watch": "^4.3.1"
|
|
35
34
|
},
|
|
36
35
|
"jest": {
|
|
37
36
|
"bail": true,
|
package/src/storage.js
CHANGED
|
@@ -1,157 +1,40 @@
|
|
|
1
1
|
import { createEventListeners } from './createEventListeners';
|
|
2
2
|
|
|
3
|
-
let syncStore = {};
|
|
4
|
-
let localStore = {};
|
|
5
|
-
let managedStore = {};
|
|
6
|
-
let sessionStore = {};
|
|
7
|
-
|
|
8
3
|
function resolveKey(key, store) {
|
|
9
4
|
if (typeof key === 'string') {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
if (key in store) {
|
|
6
|
+
return { key: store[key] };
|
|
7
|
+
} else {
|
|
8
|
+
return {};
|
|
9
|
+
}
|
|
13
10
|
} else if (Array.isArray(key)) {
|
|
14
|
-
return key.reduce(
|
|
15
|
-
acc
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
return key.reduce(
|
|
12
|
+
(acc, currKey) => ({
|
|
13
|
+
...acc,
|
|
14
|
+
...resolveKey(currKey, store),
|
|
15
|
+
}),
|
|
16
|
+
{}
|
|
17
|
+
);
|
|
18
18
|
} else if (typeof key === 'object') {
|
|
19
|
-
return Object.
|
|
20
|
-
acc[
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
return Object.entries(key).reduce(
|
|
20
|
+
(acc, [currKey, fallbackValue]) => ({
|
|
21
|
+
...acc,
|
|
22
|
+
[currKey]: fallbackValue,
|
|
23
|
+
...resolveKey(currKey, store),
|
|
24
|
+
}),
|
|
25
|
+
{}
|
|
26
|
+
);
|
|
23
27
|
}
|
|
24
28
|
throw new Error('Wrong key given');
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
id === null || id === undefined ? syncStore : resolveKey(id, syncStore);
|
|
32
|
-
if (cb !== undefined) {
|
|
33
|
-
return cb(result);
|
|
34
|
-
}
|
|
35
|
-
return Promise.resolve(result);
|
|
36
|
-
}),
|
|
37
|
-
getBytesInUse: jest.fn((id, cb) => {
|
|
38
|
-
if (cb !== undefined) {
|
|
39
|
-
return cb(0);
|
|
40
|
-
}
|
|
41
|
-
return Promise.resolve(0);
|
|
42
|
-
}),
|
|
43
|
-
set: jest.fn((payload, cb) => {
|
|
44
|
-
Object.keys(payload).forEach((key) => (syncStore[key] = payload[key]));
|
|
45
|
-
if (cb !== undefined) {
|
|
46
|
-
return cb();
|
|
47
|
-
}
|
|
48
|
-
return Promise.resolve();
|
|
49
|
-
}),
|
|
50
|
-
remove: jest.fn((id, cb) => {
|
|
51
|
-
const keys = typeof id === 'string' ? [id] : id;
|
|
52
|
-
keys.forEach((key) => delete syncStore[key]);
|
|
53
|
-
if (cb !== undefined) {
|
|
54
|
-
return cb();
|
|
55
|
-
}
|
|
56
|
-
return Promise.resolve();
|
|
57
|
-
}),
|
|
58
|
-
clear: jest.fn((cb) => {
|
|
59
|
-
syncStore = {};
|
|
60
|
-
if (cb !== undefined) {
|
|
61
|
-
return cb();
|
|
62
|
-
}
|
|
63
|
-
return Promise.resolve();
|
|
64
|
-
}),
|
|
65
|
-
onChanged: createEventListeners(),
|
|
66
|
-
},
|
|
67
|
-
local: {
|
|
68
|
-
get: jest.fn((id, cb) => {
|
|
69
|
-
const result =
|
|
70
|
-
id === null || id === undefined
|
|
71
|
-
? localStore
|
|
72
|
-
: resolveKey(id, localStore);
|
|
73
|
-
if (cb !== undefined) {
|
|
74
|
-
return cb(result);
|
|
75
|
-
}
|
|
76
|
-
return Promise.resolve(result);
|
|
77
|
-
}),
|
|
78
|
-
getBytesInUse: jest.fn((id, cb) => {
|
|
79
|
-
if (cb !== undefined) {
|
|
80
|
-
return cb(0);
|
|
81
|
-
}
|
|
82
|
-
return Promise.resolve(0);
|
|
83
|
-
}),
|
|
84
|
-
set: jest.fn((payload, cb) => {
|
|
85
|
-
Object.keys(payload).forEach((key) => (localStore[key] = payload[key]));
|
|
86
|
-
if (cb !== undefined) {
|
|
87
|
-
return cb();
|
|
88
|
-
}
|
|
89
|
-
return Promise.resolve();
|
|
90
|
-
}),
|
|
91
|
-
remove: jest.fn((id, cb) => {
|
|
92
|
-
const keys = typeof id === 'string' ? [id] : id;
|
|
93
|
-
keys.forEach((key) => delete localStore[key]);
|
|
94
|
-
if (cb !== undefined) {
|
|
95
|
-
return cb();
|
|
96
|
-
}
|
|
97
|
-
return Promise.resolve();
|
|
98
|
-
}),
|
|
99
|
-
clear: jest.fn((cb) => {
|
|
100
|
-
localStore = {};
|
|
101
|
-
if (cb !== undefined) {
|
|
102
|
-
return cb();
|
|
103
|
-
}
|
|
104
|
-
return Promise.resolve();
|
|
105
|
-
}),
|
|
106
|
-
onChanged: createEventListeners(),
|
|
107
|
-
},
|
|
108
|
-
session: {
|
|
109
|
-
get: jest.fn((id, cb) => {
|
|
110
|
-
const result =
|
|
111
|
-
id === null || id === undefined
|
|
112
|
-
? localStore
|
|
113
|
-
: resolveKey(id, localStore);
|
|
114
|
-
if (cb !== undefined) {
|
|
115
|
-
return cb(result);
|
|
116
|
-
}
|
|
117
|
-
return Promise.resolve(result);
|
|
118
|
-
}),
|
|
119
|
-
getBytesInUse: jest.fn((id, cb) => {
|
|
120
|
-
if (cb !== undefined) {
|
|
121
|
-
return cb(0);
|
|
122
|
-
}
|
|
123
|
-
return Promise.resolve(0);
|
|
124
|
-
}),
|
|
125
|
-
set: jest.fn((payload, cb) => {
|
|
126
|
-
Object.keys(payload).forEach((key) => (localStore[key] = payload[key]));
|
|
127
|
-
if (cb !== undefined) {
|
|
128
|
-
return cb();
|
|
129
|
-
}
|
|
130
|
-
return Promise.resolve();
|
|
131
|
-
}),
|
|
132
|
-
remove: jest.fn((id, cb) => {
|
|
133
|
-
const keys = typeof id === 'string' ? [id] : id;
|
|
134
|
-
keys.forEach((key) => delete localStore[key]);
|
|
135
|
-
if (cb !== undefined) {
|
|
136
|
-
return cb();
|
|
137
|
-
}
|
|
138
|
-
return Promise.resolve();
|
|
139
|
-
}),
|
|
140
|
-
clear: jest.fn((cb) => {
|
|
141
|
-
localStore = {};
|
|
142
|
-
if (cb !== undefined) {
|
|
143
|
-
return cb();
|
|
144
|
-
}
|
|
145
|
-
return Promise.resolve();
|
|
146
|
-
}),
|
|
147
|
-
onChanged: createEventListeners(),
|
|
148
|
-
},
|
|
149
|
-
managed: {
|
|
31
|
+
function mockStore() {
|
|
32
|
+
const store = {};
|
|
33
|
+
|
|
34
|
+
return {
|
|
150
35
|
get: jest.fn((id, cb) => {
|
|
151
36
|
const result =
|
|
152
|
-
id === null || id === undefined
|
|
153
|
-
? managedStore
|
|
154
|
-
: resolveKey(id, managedStore);
|
|
37
|
+
id === null || id === undefined ? store : resolveKey(id, store);
|
|
155
38
|
if (cb !== undefined) {
|
|
156
39
|
return cb(result);
|
|
157
40
|
}
|
|
@@ -164,7 +47,7 @@ export const storage = {
|
|
|
164
47
|
return Promise.resolve(0);
|
|
165
48
|
}),
|
|
166
49
|
set: jest.fn((payload, cb) => {
|
|
167
|
-
Object.keys(payload).forEach((key) => (
|
|
50
|
+
Object.keys(payload).forEach((key) => (store[key] = payload[key]));
|
|
168
51
|
if (cb !== undefined) {
|
|
169
52
|
return cb();
|
|
170
53
|
}
|
|
@@ -172,20 +55,27 @@ export const storage = {
|
|
|
172
55
|
}),
|
|
173
56
|
remove: jest.fn((id, cb) => {
|
|
174
57
|
const keys = typeof id === 'string' ? [id] : id;
|
|
175
|
-
keys.forEach((key) => delete
|
|
58
|
+
keys.forEach((key) => delete store[key]);
|
|
176
59
|
if (cb !== undefined) {
|
|
177
60
|
return cb();
|
|
178
61
|
}
|
|
179
62
|
return Promise.resolve();
|
|
180
63
|
}),
|
|
181
64
|
clear: jest.fn((cb) => {
|
|
182
|
-
|
|
65
|
+
Object.keys(store).forEach((key) => delete store[key]);
|
|
183
66
|
if (cb !== undefined) {
|
|
184
67
|
return cb();
|
|
185
68
|
}
|
|
186
69
|
return Promise.resolve();
|
|
187
70
|
}),
|
|
188
71
|
onChanged: createEventListeners(),
|
|
189
|
-
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const storage = {
|
|
76
|
+
sync: mockStore(),
|
|
77
|
+
local: mockStore(),
|
|
78
|
+
session: mockStore(),
|
|
79
|
+
managed: mockStore(),
|
|
190
80
|
onChanged: createEventListeners(),
|
|
191
81
|
};
|