my fork of the bluesky client
1import {beforeEach, expect, jest, test} from '@jest/globals'
2
3import {Storage} from '#/storage'
4
5jest.mock('react-native-mmkv', () => ({
6 MMKV: class MMKVMock {
7 _store = new Map()
8
9 set(key: string, value: unknown) {
10 this._store.set(key, value)
11 }
12
13 getString(key: string) {
14 return this._store.get(key)
15 }
16
17 delete(key: string) {
18 return this._store.delete(key)
19 }
20 },
21}))
22
23type Schema = {
24 boo: boolean
25 str: string | null
26 num: number
27 obj: Record<string, unknown>
28}
29
30const scope = `account`
31const store = new Storage<['account'], Schema>({id: 'test'})
32
33beforeEach(() => {
34 store.removeMany([scope], ['boo', 'str', 'num', 'obj'])
35})
36
37test(`stores and retrieves data`, () => {
38 store.set([scope, 'boo'], true)
39 store.set([scope, 'str'], 'string')
40 store.set([scope, 'num'], 1)
41 expect(store.get([scope, 'boo'])).toEqual(true)
42 expect(store.get([scope, 'str'])).toEqual('string')
43 expect(store.get([scope, 'num'])).toEqual(1)
44})
45
46test(`removes data`, () => {
47 store.set([scope, 'boo'], true)
48 expect(store.get([scope, 'boo'])).toEqual(true)
49 store.remove([scope, 'boo'])
50 expect(store.get([scope, 'boo'])).toEqual(undefined)
51})
52
53test(`removes multiple keys at once`, () => {
54 store.set([scope, 'boo'], true)
55 store.set([scope, 'str'], 'string')
56 store.set([scope, 'num'], 1)
57 store.removeMany([scope], ['boo', 'str', 'num'])
58 expect(store.get([scope, 'boo'])).toEqual(undefined)
59 expect(store.get([scope, 'str'])).toEqual(undefined)
60 expect(store.get([scope, 'num'])).toEqual(undefined)
61})
62
63test(`concatenates keys`, () => {
64 store.remove([scope, 'str'])
65 store.set([scope, 'str'], 'concat')
66 // @ts-ignore accessing these properties for testing purposes only
67 expect(store.store.getString(`${scope}${store.sep}str`)).toBeTruthy()
68})
69
70test(`can store falsy values`, () => {
71 store.set([scope, 'str'], null)
72 store.set([scope, 'num'], 0)
73 expect(store.get([scope, 'str'])).toEqual(null)
74 expect(store.get([scope, 'num'])).toEqual(0)
75})
76
77test(`can store objects`, () => {
78 const obj = {foo: true}
79 store.set([scope, 'obj'], obj)
80 expect(store.get([scope, 'obj'])).toEqual(obj)
81})