Created
May 3, 2019 08:24
-
-
Save oshimayoan/5e842e79e0de44be0b9ce262f98777ba to your computer and use it in GitHub Desktop.
Mocking AsyncStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('No such key!'); //eslint-disable-line | |
}); | |
}); | |
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))); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your imported dependencies goes here | |
import MockStorage from '../path/to/__mocks__/mockStorage'; | |
let storageCache = {}; | |
const AsyncStorage = new MockStorage(storageCache); | |
jest.setMock('AsyncStorage', AsyncStorage); | |
describe('Something to test', () => { | |
// Your test goes here | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cannot :(
I didn't put any
AFTER_FIRST_UNLOCK
mock thing