|
| 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 | + |
| 20 | +import { testUser } from '../../../test/mock_auth'; |
| 21 | +import { PersistedBlob, PersistenceType } from './'; |
| 22 | +import { ReactNativePersistence } from './react_native'; |
| 23 | +import { ReactNativeAsyncStorage } from '@firebase/auth-types-exp'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Wraps in-memory storage with the react native AsyncStorage API. |
| 27 | + */ |
| 28 | +class FakeAsyncStorage implements ReactNativeAsyncStorage { |
| 29 | + storage: { |
| 30 | + [key: string]: string; |
| 31 | + } = {}; |
| 32 | + |
| 33 | + async getItem(key: string): Promise<string | null> { |
| 34 | + const value = this.storage[key]; |
| 35 | + return value ?? null; |
| 36 | + } |
| 37 | + async removeItem(key: string): Promise<void> { |
| 38 | + delete this.storage[key]; |
| 39 | + } |
| 40 | + async setItem(key: string, value: string): Promise<void> { |
| 41 | + this.storage[key] = value; |
| 42 | + } |
| 43 | + clear(): void { |
| 44 | + this.storage = {}; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +describe('core/persistence/react', () => { |
| 49 | + const fakeAsyncStorage = new FakeAsyncStorage(); |
| 50 | + const persistence = new ReactNativePersistence(fakeAsyncStorage); |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + fakeAsyncStorage.clear(); |
| 54 | + }); |
| 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 | +}); |
0 commit comments