BlueWallet/MockStorage.js

35 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-03-30 20:31:10 +02:00
/* 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 => {
2018-07-07 15:04:32 +02:00
return this.storageCache.hasOwnProperty(key) ? resolve(this.storageCache[key]) : resolve(null);
2018-03-30 20:31:10 +02:00
});
});
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!'));
2018-03-30 20:31:10 +02:00
});
});
clear = jest.fn(key => {
return new Promise((resolve, reject) => resolve((this.storageCache = {})));
});
getAllKeys = jest.fn(key => {
2018-07-07 15:04:32 +02:00
return new Promise((resolve, reject) => resolve(Object.keys(this.storageCache)));
2018-03-30 20:31:10 +02:00
});
}