|
| 1 | +import { StrictAsyncStorage, DriverInterface } from '../src'; |
| 2 | +import { |
| 3 | + getSaved, |
| 4 | + setSaved, |
| 5 | + defaults, |
| 6 | + AsyncDriver, |
| 7 | + StorageName, |
| 8 | + restoreSaved, |
| 9 | + defLen, |
| 10 | +} from './helper'; |
| 11 | + |
| 12 | +describe('strict-async-storage', () => { |
| 13 | + jest |
| 14 | + .spyOn(Object.getPrototypeOf(localStorage) as Storage, 'getItem') |
| 15 | + .mockImplementation((name: string) => { |
| 16 | + return getSaved(name) ? getSaved(name) : null; |
| 17 | + }); |
| 18 | + jest |
| 19 | + .spyOn(Object.getPrototypeOf(localStorage) as Storage, 'setItem') |
| 20 | + .mockImplementation((name: string, value: string) => { |
| 21 | + setSaved(name, value); |
| 22 | + }); |
| 23 | + oneSetTest('default sync', new StrictAsyncStorage(defaults)); |
| 24 | + |
| 25 | + oneSetTest( |
| 26 | + 'cunstom async', |
| 27 | + new StrictAsyncStorage(defaults, { |
| 28 | + map: new Map(), |
| 29 | + driver: () => new AsyncDriver(), |
| 30 | + }), |
| 31 | + true |
| 32 | + ); |
| 33 | +}); |
| 34 | + |
| 35 | +function oneSetTest<TDriver extends DriverInterface = Storage>( |
| 36 | + testName: string | number | Function | jest.FunctionLike, |
| 37 | + strictAsyncStorage: StrictAsyncStorage<typeof defaults, StorageName, TDriver>, |
| 38 | + handleDispose = false |
| 39 | +) { |
| 40 | + describe(testName, () => { |
| 41 | + restoreSaved(); |
| 42 | + beforeAll(async () => { |
| 43 | + await strictAsyncStorage.init(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('test length', async () => { |
| 47 | + expect(await strictAsyncStorage.length).toBe(defLen); |
| 48 | + }); |
| 49 | + |
| 50 | + it('test getItem', () => { |
| 51 | + expect(strictAsyncStorage.getItem(StorageName.user)).toBe( |
| 52 | + getSaved(StorageName.user) |
| 53 | + ); |
| 54 | + expect(strictAsyncStorage.getItem(StorageName.no)).toBe( |
| 55 | + getSaved(StorageName.no) |
| 56 | + ); |
| 57 | + expect(strictAsyncStorage.getItem(StorageName.enable)).toBe( |
| 58 | + defaults[StorageName.enable] |
| 59 | + ); |
| 60 | + expect(strictAsyncStorage.getItem(StorageName.data)).toEqual( |
| 61 | + getSaved(StorageName.data) |
| 62 | + ); |
| 63 | + |
| 64 | + expect(() => strictAsyncStorage.getItem('other' as any)).toThrowError( |
| 65 | + RangeError |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('test setItem', async () => { |
| 70 | + const user = 'test001'; |
| 71 | + await strictAsyncStorage.setItem(StorageName.user, user); |
| 72 | + expect(strictAsyncStorage.getItem(StorageName.user)).toBe(user); |
| 73 | + |
| 74 | + await strictAsyncStorage.setItem(StorageName.user, undefined as any); |
| 75 | + expect(strictAsyncStorage.getItem(StorageName.user)).toBeUndefined(); |
| 76 | + await strictAsyncStorage.setItem(StorageName.user, null as any); |
| 77 | + expect(strictAsyncStorage.getItem(StorageName.user)).toBe( |
| 78 | + defaults[StorageName.user] |
| 79 | + ); |
| 80 | + |
| 81 | + await expect( |
| 82 | + strictAsyncStorage.setItem('other' as any, null as any) |
| 83 | + ).rejects.toThrowError(RangeError); |
| 84 | + }); |
| 85 | + |
| 86 | + it('test resetItem', async () => { |
| 87 | + expect(await strictAsyncStorage.resetItem(StorageName.user)).toBe( |
| 88 | + defaults[StorageName.user] |
| 89 | + ); |
| 90 | + expect(strictAsyncStorage.getItem(StorageName.user)).toBe( |
| 91 | + defaults[StorageName.user] |
| 92 | + ); |
| 93 | + |
| 94 | + await expect( |
| 95 | + strictAsyncStorage.resetItem('other' as any) |
| 96 | + ).rejects.toThrowError(RangeError); |
| 97 | + }); |
| 98 | + |
| 99 | + it('test resetAll', async () => { |
| 100 | + await strictAsyncStorage.resetAll(); |
| 101 | + |
| 102 | + expect(strictAsyncStorage.getItem(StorageName.user)).toBe( |
| 103 | + defaults[StorageName.user] |
| 104 | + ); |
| 105 | + expect(strictAsyncStorage.getItem(StorageName.no)).toBe( |
| 106 | + defaults[StorageName.no] |
| 107 | + ); |
| 108 | + expect(strictAsyncStorage.getItem(StorageName.enable)).toBe( |
| 109 | + defaults[StorageName.enable] |
| 110 | + ); |
| 111 | + expect(strictAsyncStorage.getItem(StorageName.data)).toEqual( |
| 112 | + defaults[StorageName.data] |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it('test dispose', () => { |
| 117 | + if (handleDispose) { |
| 118 | + const handler = jest.fn(); |
| 119 | + strictAsyncStorage.dispose(handler); |
| 120 | + expect(handler).toBeCalled(); |
| 121 | + } else { |
| 122 | + strictAsyncStorage.dispose(); |
| 123 | + expect(strictAsyncStorage.length).toBe(0); |
| 124 | + } |
| 125 | + }); |
| 126 | + }); |
| 127 | +} |
0 commit comments