-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
64 lines (59 loc) · 1.6 KB
/
Copy pathjest.setup.js
File metadata and controls
64 lines (59 loc) · 1.6 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
Object.assign(global, require('jest-chrome'));
let localStorage = {};
let syncStorage = {};
const getter = (storage) => (keys, callback) => {
if (keys === null) {
callback(storage);
return;
}
if (!keys) {
callback(undefined);
return;
}
if (Array.isArray(keys)) {
const res = {};
keys.forEach((key) => {
res[key] = storage[key];
});
callback(res);
} else {
callback({ [keys]: storage[keys] });
}
};
const setter = (storage) => (object, callback) => {
if (!object) {
callback(true);
return;
}
Object.entries(object).forEach(([key, value]) => {
storage[key] = value;
});
callback(true);
};
const remover = (storage) => (keys, callback) => {
if (!keys) {
callback(true);
return;
}
if (Array.isArray(keys)) {
keys.forEach((key) => {
delete storage[key];
});
callback(true);
} else {
delete storage[keys];
callback(true);
}
};
chrome.storage.local.get.mockImplementation(getter(localStorage));
chrome.storage.local.set.mockImplementation(setter(localStorage));
chrome.storage.local.remove.mockImplementation(remover(localStorage));
chrome.storage.local.clear.mockImplementation(() => {
localStorage = {};
});
chrome.storage.sync.get.mockImplementation(getter(syncStorage));
chrome.storage.sync.set.mockImplementation(setter(syncStorage));
chrome.storage.local.remove.mockImplementation(remover(syncStorage));
chrome.storage.local.clear.mockImplementation(() => {
syncStorage = {};
});