|
| 1 | +import asyncStorage from '@react-native-async-storage/async-storage'; |
| 2 | +import { |
| 3 | + isMemoryStorage, |
| 4 | + memoryStorage, |
| 5 | + getItem, |
| 6 | + setItem, |
| 7 | + removeItem, |
| 8 | + setReactNativeAsyncStorageInternal, |
| 9 | + prefix, |
| 10 | +} from '@react-native-firebase/app/lib/internal/asyncStorage'; |
| 11 | + |
| 12 | +describe('firebase.setReactNativeAsyncStorage()', function () { |
| 13 | + beforeEach(async function () { |
| 14 | + setReactNativeAsyncStorageInternal(); |
| 15 | + await asyncStorage.clear(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('throws if asyncStorage is not an object', function () { |
| 19 | + try { |
| 20 | + firebase.setReactNativeAsyncStorage(123); |
| 21 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 22 | + } catch (error) { |
| 23 | + error.message.should.containEql("'asyncStorage' must be an object"); |
| 24 | + return Promise.resolve(); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + it('throws if asyncStorage.setItem is not a function', function () { |
| 29 | + try { |
| 30 | + firebase.setReactNativeAsyncStorage({ setItem: 123 }); |
| 31 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 32 | + } catch (error) { |
| 33 | + error.message.should.containEql("'asyncStorage.setItem' must be a function"); |
| 34 | + return Promise.resolve(); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it('throws if asyncStorage.getItem is not a function', function () { |
| 39 | + try { |
| 40 | + firebase.setReactNativeAsyncStorage({ setItem: sinon.spy(), getItem: 123 }); |
| 41 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 42 | + } catch (error) { |
| 43 | + error.message.should.containEql("'asyncStorage.getItem' must be a function"); |
| 44 | + return Promise.resolve(); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('throws if asyncStorage.removeItem is not a function', function () { |
| 49 | + try { |
| 50 | + firebase.setReactNativeAsyncStorage({ |
| 51 | + setItem: sinon.spy(), |
| 52 | + getItem: sinon.spy(), |
| 53 | + removeItem: 123, |
| 54 | + }); |
| 55 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 56 | + } catch (error) { |
| 57 | + error.message.should.containEql("'asyncStorage.removeItem' must be a function"); |
| 58 | + return Promise.resolve(); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('sets the async storage instance', async function () { |
| 63 | + isMemoryStorage().should.equal(true); |
| 64 | + |
| 65 | + const setItemSpy = sinon.spy(); |
| 66 | + const getItemSpy = sinon.spy(); |
| 67 | + const removeItemSpy = sinon.spy(); |
| 68 | + |
| 69 | + firebase.setReactNativeAsyncStorage({ |
| 70 | + setItem: setItemSpy, |
| 71 | + getItem: getItemSpy, |
| 72 | + removeItem: removeItemSpy, |
| 73 | + }); |
| 74 | + |
| 75 | + isMemoryStorage().should.equal(false); |
| 76 | + |
| 77 | + await setItem('foo', 'bar'); |
| 78 | + await getItem('foo'); |
| 79 | + await removeItem('foo'); |
| 80 | + |
| 81 | + setItemSpy.should.be.calledOnce(); |
| 82 | + getItemSpy.should.be.calledOnce(); |
| 83 | + removeItemSpy.should.be.calledOnce(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('works with @react-native-async-storage/async-storage', async function () { |
| 87 | + isMemoryStorage().should.equal(true); |
| 88 | + const key = Date.now().toString(); |
| 89 | + const value = 'bar'; |
| 90 | + firebase.setReactNativeAsyncStorage(asyncStorage); |
| 91 | + isMemoryStorage().should.equal(false); |
| 92 | + |
| 93 | + // Through our internals, |
| 94 | + await setItem(key, value); |
| 95 | + // Get the value from async-storage, which is prefixed, |
| 96 | + const valueFromAsyncStorage = await asyncStorage.getItem(`${prefix}${key}`); |
| 97 | + // Get the value from our internals, which is not prefixed, |
| 98 | + valueFromAsyncStorage.should.equal(value); |
| 99 | + const valueFromInternal = await getItem(key); |
| 100 | + valueFromInternal.should.equal(value); |
| 101 | + // Values should be identical |
| 102 | + valueFromInternal.should.equal(valueFromAsyncStorage); |
| 103 | + await removeItem(key); |
| 104 | + const valueAfterRemoveInternal = await getItem(key); |
| 105 | + const valueAfterRemoveAsyncStorage = await asyncStorage.getItem(`${prefix}${key}`); |
| 106 | + should.equal(valueAfterRemoveInternal, null); |
| 107 | + should.equal(valueAfterRemoveInternal, valueAfterRemoveAsyncStorage); |
| 108 | + }); |
| 109 | + |
| 110 | + it('works with memory storage', async function () { |
| 111 | + isMemoryStorage().should.equal(true); |
| 112 | + const key = Date.now().toString(); |
| 113 | + const value = 'bar'; |
| 114 | + await setItem(key, value); |
| 115 | + memoryStorage.has(`${prefix}${key}`).should.equal(true); |
| 116 | + memoryStorage.get(`${prefix}${key}`).should.equal(value); |
| 117 | + (await getItem(key)).should.equal(value); |
| 118 | + await removeItem(key); |
| 119 | + memoryStorage.has(`${prefix}${key}`).should.equal(false); |
| 120 | + should.equal(await getItem(key), null); |
| 121 | + }); |
| 122 | +}); |
0 commit comments