forked from artsy/eigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.tests.ts
254 lines (239 loc) · 6.52 KB
/
persistence.tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import AsyncStorage from "@react-native-async-storage/async-storage"
import { migrate } from "./migration"
import {
assignDeep,
LEGACY_SEARCH_STORAGE_KEY,
persist,
sanitize,
STORAGE_KEY,
unpersist,
} from "./persistence"
jest.mock("./migration", () => ({ migrate: jest.fn((a) => a.state) }))
describe("sanitize", () => {
const fixture = {
sessionState: { blah: true },
get computedProperty() {
return 3
},
persistedProperty: true,
bar: {
sessionState: { blah: true },
get nestedComputedProperty() {
return 3
},
nestedPersistedProperty: "hello",
},
baz: [
{
sessionState: { blah: true },
get nestedInArrayComputedProperty() {
return 3
},
nestedInArrayPersistedProperty: 3,
},
],
}
it("removes computed properties and session states", () => {
expect(sanitize(fixture)).toEqual({
persistedProperty: true,
bar: {
nestedPersistedProperty: "hello",
},
baz: [{ nestedInArrayPersistedProperty: 3 }],
})
})
it("can't serialize non-json-compatible properties", () => {
expect(sanitize({ regex: /abcdef/g })).toEqual({ regex: null })
expect(console.error).toHaveBeenCalled()
expect((console.error as jest.Mock).mock.calls[0][0]).toMatchInlineSnapshot(
`"Cannot serialize value at path regex: /abcdef/g"`
)
})
})
describe("assignDeep", () => {
it("merges one object into another, modifying the underlying object", () => {
const obj = {
foo: true,
}
assignDeep(obj, { bar: true })
expect(obj).toEqual({ foo: true, bar: true })
})
it("overwrites existing properties", () => {
const obj = {
foo: true,
bar: false,
}
assignDeep(obj, { bar: true })
expect(obj).toEqual({ foo: true, bar: true })
})
it("deeply merges aligned objects", () => {
const obj = {
level0: {
a: false,
b: true,
level1: {
a: false,
b: true,
},
},
}
assignDeep(obj, {
level0: {
a: true,
c: true,
level1: {
a: true,
c: true,
},
},
})
expect(obj).toEqual({
level0: {
a: true,
b: true,
c: true,
level1: {
a: true,
b: true,
c: true,
},
},
})
})
it("overwrites misaligned objects", () => {
const obj = {
foo: { bar: true },
bar: true,
baz: false,
}
assignDeep(obj, { foo: true, bar: { foo: true } })
expect(obj).toEqual({ foo: true, bar: { foo: true }, baz: false })
})
it("does not merge inside arrays", () => {
const obj = {
foo: [{ bar: true }],
}
assignDeep(obj, { foo: [{}] })
expect(obj).toEqual({ foo: [{}] })
})
})
describe(persist, () => {
beforeEach(() => {
AsyncStorage.clear()
})
it("omits the sessionStorage key", async () => {
await persist({
bottomTabs: {
selectedTab: "home",
sessionState: { unreadCounts: { unreadConversation: 5 } },
},
} as any)
expect(JSON.parse((await AsyncStorage.getItem(STORAGE_KEY)) ?? "")).toEqual({
bottomTabs: { selectedTab: "home" },
})
})
it("overwrites the previous value", async () => {
await persist({
bottomTabs: {
selectedTab: "home",
sessionState: { unreadCounts: { unreadConversation: 5 } },
},
} as any)
expect(JSON.parse((await AsyncStorage.getItem(STORAGE_KEY)) ?? "")).toEqual({
bottomTabs: { selectedTab: "home" },
})
await persist({
bottomTabs: {
selectedTab: "explore",
sessionState: { unreadCounts: { unreadConversation: 5 } },
},
} as any)
expect(JSON.parse((await AsyncStorage.getItem(STORAGE_KEY)) ?? "")).toEqual({
bottomTabs: { selectedTab: "explore" },
})
})
})
describe(unpersist, () => {
beforeEach(() => {
AsyncStorage.clear()
})
it("returns an empty object if there was nothing saved before", async () => {
expect(await unpersist()).toEqual({})
})
it("runs migrations if something was saved before", async () => {
;(migrate as jest.Mock).mockImplementationOnce((data) => ({ ...data.state, wasMigrated: true }))
await AsyncStorage.setItem(STORAGE_KEY, '{"wasStored": true}')
expect(await unpersist()).toEqual({
wasStored: true,
wasMigrated: true,
})
})
it("returns an empty object if the stored json is corrupt", async () => {
await AsyncStorage.setItem(STORAGE_KEY, "{: 0}")
expect(await unpersist()).toEqual({})
})
it("loads legacy search state", async () => {
await AsyncStorage.setItem(
STORAGE_KEY,
JSON.stringify({
search: {
recentSearches: [],
},
})
)
await AsyncStorage.setItem(
LEGACY_SEARCH_STORAGE_KEY,
JSON.stringify([
{
type: "AUTOSUGGEST_RESULT_TAPPED",
props: {
displayLabel: "Banksy",
displayType: "Artist",
href: "https://artsy.com/artist/banksy",
imageUrl: "https://org-name.my-cloud-provider.com/bucket-hash/content-hash.jpg",
},
},
])
)
expect(await unpersist()).toMatchObject({
search: { recentSearches: [{ props: { displayLabel: "Banksy" } }] },
})
})
it("ignores corrupt legacy search state", async () => {
await AsyncStorage.setItem(
STORAGE_KEY,
JSON.stringify({
search: {
recentSearches: [{ props: { displayLabel: "i am a recent search" } }],
},
})
)
await AsyncStorage.setItem(LEGACY_SEARCH_STORAGE_KEY, "{: 0}")
expect((await unpersist()).search?.recentSearches?.[0]?.props?.displayLabel).toBe(
"i am a recent search"
)
await AsyncStorage.setItem(LEGACY_SEARCH_STORAGE_KEY, "{}")
expect((await unpersist()).search?.recentSearches?.[0]?.props?.displayLabel).toBe(
"i am a recent search"
)
})
it("loads legacy search state even if no existing app state was present", async () => {
await AsyncStorage.setItem(
LEGACY_SEARCH_STORAGE_KEY,
JSON.stringify([
{
type: "AUTOSUGGEST_RESULT_TAPPED",
props: {
displayLabel: "Banksy",
displayType: "Artist",
href: "https://artsy.com/artist/banksy",
imageUrl: "https://org-name.my-cloud-provider.com/bucket-hash/content-hash.jpg",
},
},
])
)
expect(await unpersist()).toMatchObject({
search: { recentSearches: [{ props: { displayLabel: "Banksy" } }] },
})
})
})