-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
actionsCollection.js
301 lines (281 loc) · 9.16 KB
/
actionsCollection.js
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import test from 'ava'
import { isPlainObject } from 'is-what'
import wait from './helpers/wait'
import {store} from './helpers/index.cjs.js'
const box = store.state.pokemonBox
const boxRef = store.getters['pokemonBox/dbRef']
// actions
test('store set up', async t => {
t.true(isPlainObject(box.pokemon))
})
test('[COLLECTION] set with no id', async t => {
let id, docR, doc
await wait(3)
// insert set
id = await store.dispatch('pokemonBox/insert', {name: 'Unown'})
t.truthy(box.pokemon[id])
t.is(box.pokemon[id].name, 'Unown')
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.is(doc.name, 'Unown')
// set set
id = await store.dispatch('pokemonBox/set', {name: 'Unown1'})
t.truthy(box.pokemon[id])
t.is(box.pokemon[id].name, 'Unown1')
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.is(doc.name, 'Unown1')
// insert set
id = await store.dispatch('pokemonBox/insert', {name: {is: 'nested'}})
t.truthy(box.pokemon[id])
t.deepEqual(box.pokemon[id].name, {is: 'nested'})
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.name, {is: 'nested'})
// set set
id = await store.dispatch('pokemonBox/set', {name: {is: 'nested1'}})
t.truthy(box.pokemon[id])
t.deepEqual(box.pokemon[id].name, {is: 'nested1'})
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.name, {is: 'nested1'})
})
test('[COLLECTION] insert and patch right after each other', async t => {
await wait(3)
// insert
const id = boxRef.doc().id
store.dispatch('pokemonBox/insert', {id, name: 'Mew', type: {normal: true}})
t.truthy(box.pokemon[id])
t.is(box.pokemon[id].name, 'Mew')
t.deepEqual(box.pokemon[id].type, {normal: true})
// patch
store.dispatch('pokemonBox/patch', {id, type: {psychic: true}})
t.is(box.pokemon[id].name, 'Mew')
t.deepEqual(box.pokemon[id].type, {normal: true, psychic: true})
// await server
await wait(3)
const docR = await boxRef.doc(id).get()
const doc = docR.data()
t.is(doc.name, 'Mew')
t.deepEqual(doc.type, {normal: true, psychic: true})
})
test('[COLLECTION] edit twice right after each other', async t => {
await wait(3)
// insert
const id = boxRef.doc().id
store.dispatch('pokemonBox/insert', {id, name: 'Mew', type: {normal: true}})
t.truthy(box.pokemon[id])
t.is(box.pokemon[id].name, 'Mew')
await wait(3)
// patch once
store.dispatch('pokemonBox/patch', {id, name: 'Mew!'})
t.is(box.pokemon[id].name, 'Mew!')
await wait(0.5)
t.is(box.pokemon[id].name, 'Mew!')
store.dispatch('pokemonBox/patch', {id, name: 'Mew!!'})
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
await wait(0.1)
t.is(box.pokemon[id].name, 'Mew!!')
})
test('[COLLECTION] set & delete: top lvl', async t => {
const id = boxRef.doc().id
const id2 = boxRef.doc().id
const date = new Date()
// ini set
const pokemonValues = {
id,
name: 'Squirtle',
type: ['water'],
meta: {date}
}
store.dispatch('pokemonBox/insert', pokemonValues)
t.truthy(box.pokemon[id])
t.is(box.pokemon[id].name, 'Squirtle')
t.is(box.pokemon[id].meta.date, date)
await wait(3)
let docR, doc
docR = await boxRef.doc(id).get()
doc = docR.data()
console.log('doc → ', doc)
t.is(doc.name, 'Squirtle')
t.falsy(doc.meta) // not a fillable
// update
const date2 = new Date('1990-06-22')
const pokemonValuesNew = {
id,
name: 'COOL Squirtle!',
meta: {date: date2}
}
store.dispatch('pokemonBox/set', pokemonValuesNew)
t.truthy(box.pokemon[id])
t.is(box.pokemon[id].name, 'COOL Squirtle!')
t.deepEqual(box.pokemon[id].type, ['water'])
t.is(box.pokemon[id].meta.date, date2)
await wait(3)
docR = await boxRef.doc(id).get()
t.is(docR.exists, true)
doc = docR.data()
t.is(doc.name, 'COOL Squirtle!')
t.deepEqual(doc.type, ['water'])
// update with {id: val}
store.dispatch('pokemonBox/set', {[id]: {name: 'very berry COOL Squirtle!'}})
t.is(box.pokemon[id].name, 'very berry COOL Squirtle!')
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.is(doc.name, 'very berry COOL Squirtle!')
// add a new prop (deep)
store.dispatch('pokemonBox/set', {id, nested: {new: {deep: {prop: true}}}})
t.deepEqual(box.pokemon[id].nested, {new: {deep: {prop: true}}})
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.nested, {new: {deep: {prop: true}}})
// update arrays
store.dispatch('pokemonBox/set', {type: ['water', 'normal'], id})
t.deepEqual(box.pokemon[id].type, ['water', 'normal'])
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.type, ['water', 'normal'])
// SECOND SET + set chooses insert appropriately
store.dispatch('pokemonBox/set', {name: 'Charmander', id: id2})
t.truthy(box.pokemon[id2])
t.is(box.pokemon[id2].name, 'Charmander')
await wait(3)
docR = await boxRef.doc(id2).get()
t.is(docR.exists, true)
doc = docR.data()
t.is(doc.name, 'Charmander')
// delete
store.dispatch('pokemonBox/delete', id)
t.falsy(box.pokemon[id])
await wait(3)
docR = await boxRef.doc(id).get()
t.is(docR.exists, false)
// DELETE
t.truthy(box.pokemon[id2])
store.commit('pokemonBox/DELETE_DOC', id2)
t.falsy(box.pokemon[id2])
})
test('[COLLECTION] set & delete: deep', async t => {
let docR, doc
const id = boxRef.doc().id
await store.dispatch('pokemonBox/insert', {id, nested: {a: {met: {de: 'aba'}}}})
t.truthy(box.pokemon[id])
t.deepEqual(box.pokemon[id].nested, {a: {met: {de: 'aba'}}})
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.nested, {a: {met: {de: 'aba'}}})
// update {id, val}
await store.dispatch('pokemonBox/set', {id, nested: {a: {met: {de: 'ebe'}}}})
t.deepEqual(box.pokemon[id].nested, {a: {met: {de: 'ebe'}}})
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.nested.a.met, {de: 'ebe'})
t.deepEqual(doc.nested, {a: {met: {de: 'ebe'}}})
// update {id: val}
await store.dispatch('pokemonBox/patch', {[id]: {nested: {a: {met: {de: 'ibi'}}}}})
t.deepEqual(box.pokemon[id].nested, {a: {met: {de: 'ibi'}}})
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.nested.a.met, {de: 'ibi'})
t.deepEqual(doc.nested, {a: {met: {de: 'ibi'}}})
// delete via prop Delete
await store.dispatch('pokemonBox/delete', `${id}.nested.a.met.de`)
t.deepEqual(box.pokemon[id].nested, {a: {met: {}}})
await wait(3)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.nested, {a: {met: {}}})
})
test('[COLLECTION] set & delete: batches', async t => {
// ini set
const id1 = boxRef.doc().id
const a = {id: id1, name: 'Bulbasaur', type: {grass: true}}
const id2 = boxRef.doc().id
const b = {id: id2, name: 'Charmander', type: {fire: true}}
const id3 = boxRef.doc().id
const c = {id: id3, name: 'Squirtle', type: {water: true}}
const pokemonValues = [a, b, c]
await store.dispatch('pokemonBox/insertBatch', pokemonValues)
.catch(console.error)
t.deepEqual(box.pokemon[id1], a)
t.deepEqual(box.pokemon[id2], b)
t.deepEqual(box.pokemon[id3], c)
await wait(3)
const docR1 = await boxRef.doc(id1).get()
const doc1 = docR1.data()
const docR2 = await boxRef.doc(id2).get()
const doc2 = docR2.data()
const docR3 = await boxRef.doc(id3).get()
const doc3 = docR3.data()
t.is(doc1.name, a.name)
t.deepEqual(doc1.type, a.type)
t.is(doc2.name, b.name)
t.deepEqual(doc2.type, b.type)
t.is(doc3.name, c.name)
t.deepEqual(doc3.type, c.type)
})
test('[COLLECTION] duplicate', async t => {
let res
const id = boxRef.doc().id
await store.dispatch('pokemonBox/insert', {id, name: 'Jamie Lannister'})
t.is(box.pokemon[id].name, 'Jamie Lannister')
// dupe 1
res = await store.dispatch('pokemonBox/duplicate', id)
t.deepEqual(Object.keys(res), [id])
const dId = res[id]
t.is(box.pokemon[dId].name, 'Jamie Lannister')
// dupe many
res = await store.dispatch('pokemonBox/duplicateBatch', [id, dId])
t.deepEqual(Object.keys(res), [id, dId])
t.is(box.pokemon[res[id]].name, 'Jamie Lannister')
t.is(box.pokemon[res[dId]].name, 'Jamie Lannister')
// check Firestore
await wait(3)
let docR, doc
docR = await boxRef.doc(id).get()
doc = docR.data()
t.is(doc.name, 'Jamie Lannister')
docR = await boxRef.doc(dId).get()
doc = docR.data()
t.is(doc.name, 'Jamie Lannister')
docR = await boxRef.doc(res[id]).get()
doc = docR.data()
t.is(doc.name, 'Jamie Lannister')
docR = await boxRef.doc(res[dId]).get()
doc = docR.data()
t.is(doc.name, 'Jamie Lannister')
})