|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import * as sinon from 'sinon'; |
| 20 | + |
| 21 | +import { testUser } from '../../../test/mock_auth'; |
| 22 | +import {PersistedBlob, PersistenceType} from './'; |
| 23 | +import {ReactNativePersistence} from './react'; |
| 24 | +import {inMemoryPersistence} from './in_memory'; |
| 25 | +import {ReactNativeAsyncStorage} from '@firebase/auth-types-exp'; |
| 26 | + |
| 27 | +/** |
| 28 | + * Wraps in-memory storage with the react native AsyncStorage API. |
| 29 | + */ |
| 30 | +class FakeAsyncStorage implements ReactNativeAsyncStorage { |
| 31 | + private readonly delegate = inMemoryPersistence; |
| 32 | + |
| 33 | + getItem(key: string): Promise<string | null> { |
| 34 | + return this.delegate.get(key); |
| 35 | + } |
| 36 | + removeItem(key: string): Promise<void> { |
| 37 | + return this.delegate.remove(key); |
| 38 | + } |
| 39 | + setItem(key: string, value: string): Promise<void> { |
| 40 | + return this.delegate.set(key, value); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +describe('core/persistence/react', () => { |
| 45 | + const persistence = new ReactNativePersistence(new FakeAsyncStorage()); |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + localStorage.clear(); |
| 49 | + sessionStorage.clear(); |
| 50 | + }); |
| 51 | + |
| 52 | + afterEach(() => sinon.restore()); |
| 53 | + |
| 54 | + describe('ReactNativePersistence', () => { |
| 55 | + |
| 56 | + it('should work with persistence type', async () => { |
| 57 | + const key = 'my-super-special-persistence-type'; |
| 58 | + const value = PersistenceType.LOCAL; |
| 59 | + expect(await persistence.get(key)).to.be.null; |
| 60 | + await persistence.set(key, value); |
| 61 | + expect(await persistence.get(key)).to.be.eq(value); |
| 62 | + expect(await persistence.get('other-key')).to.be.null; |
| 63 | + await persistence.remove(key); |
| 64 | + expect(await persistence.get(key)).to.be.null; |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return persistedblob from user', async () => { |
| 68 | + const key = 'my-super-special-user'; |
| 69 | + const value = testUser('some-uid'); |
| 70 | + |
| 71 | + expect(await persistence.get(key)).to.be.null; |
| 72 | + await persistence.set(key, value.toPlainObject()); |
| 73 | + const out = await persistence.get<PersistedBlob>(key); |
| 74 | + expect(out!['uid']).to.eql(value.uid); |
| 75 | + await persistence.remove(key); |
| 76 | + expect(await persistence.get(key)).to.be.null; |
| 77 | + }); |
| 78 | + |
| 79 | + describe('#isAvailable', () => { |
| 80 | + it('should emit false if localStorage setItem throws', async () => { |
| 81 | + sinon.stub(localStorage, 'setItem').throws(new Error('nope')); |
| 82 | + expect(await persistence.isAvailable()).to.be.false; |
| 83 | + }); |
| 84 | + |
| 85 | + it('should emit false if localStorage removeItem throws', async () => { |
| 86 | + sinon.stub(localStorage, 'removeItem').throws(new Error('nope')); |
| 87 | + expect(await persistence.isAvailable()).to.be.false; |
| 88 | + }); |
| 89 | + |
| 90 | + it('should emit true if everything works properly', async () => { |
| 91 | + expect(await persistence.isAvailable()).to.be.true; |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments