forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockStorage.js
34 lines (29 loc) · 1.01 KB
/
MockStorage.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
/* global jest */
export default class MockStorage {
constructor(cache = {}) {
this.storageCache = cache;
}
setItem = jest.fn((key, value) => {
return new Promise((resolve, reject) => {
return typeof key !== 'string' || typeof value !== 'string'
? reject(new Error('key and value must be string'))
: resolve((this.storageCache[key] = value));
});
});
getItem = jest.fn(key => {
return new Promise(resolve => {
return this.storageCache.hasOwnProperty(key) ? resolve(this.storageCache[key]) : resolve(null);
});
});
removeItem = jest.fn(key => {
return new Promise((resolve, reject) => {
return this.storageCache.hasOwnProperty(key) ? resolve(delete this.storageCache[key]) : reject(new Error('No such key!'));
});
});
clear = jest.fn(key => {
return new Promise((resolve, reject) => resolve((this.storageCache = {})));
});
getAllKeys = jest.fn(key => {
return new Promise((resolve, reject) => resolve(Object.keys(this.storageCache)));
});
}