-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
actions_arrays.js
100 lines (96 loc) · 2.81 KB
/
actions_arrays.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
import test from 'ava'
import { isPlainObject, isArray, isDate } from 'is-what'
import wait from './helpers/wait'
import { store } from './helpers/index.cjs.js'
import { arrayUnion, arrayRemove } from '../src/index'
import * as Firebase from 'firebase/app'
import 'firebase/firestore'
const box = store.state.pokemonBox
const char = store.state.mainCharacter
const boxRef = store.getters['pokemonBox/dbRef']
const charRef = store.getters['mainCharacter/dbRef']
// actions
test('store set up', async t => {
t.true(isPlainObject(box.pokemon))
t.true(isArray(char.items))
})
test('[COLLECTION] set arrayUnion & arrayRemove', async t => {
let docR, doc
const id = boxRef.doc().id
// ini set
const pokemonValues = {
id,
arr1: [1, 2, 3],
arr2: [1, 2, 3],
nested: {
arr1: [1, 2, 3],
arr2: [1, 2, 3],
}
}
store.dispatch('pokemonBox/insert', pokemonValues)
t.truthy(box.pokemon[id])
await wait(2)
const pokemonValuesNew = {
id,
arr1: arrayUnion(0),
arr2: arrayRemove(2),
nested: {
arr1: arrayUnion(0),
arr2: arrayRemove(2),
}
}
store.dispatch('pokemonBox/set', pokemonValuesNew)
t.truthy(box.pokemon[id])
t.deepEqual(box.pokemon[id].arr1, [1, 2, 3, 0])
t.deepEqual(box.pokemon[id].arr2, [1, 3])
t.deepEqual(box.pokemon[id].nested.arr1, [1, 2, 3, 0])
t.deepEqual(box.pokemon[id].nested.arr2, [1, 3])
await wait(2)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.arr1, [1, 2, 3, 0])
t.deepEqual(doc.arr2, [1, 3])
t.deepEqual(doc.nested.arr1, [1, 2, 3, 0])
t.deepEqual(doc.nested.arr2, [1, 3])
// multiple values
const pokemonValuesNewMultiple = {
id,
arr1: arrayUnion(0, 'a', 'b'),
arr2: arrayRemove(1, 3),
nested: {
arr1: arrayUnion(0, 'a', 'b'),
arr2: arrayRemove(1, 3),
}
}
store.dispatch('pokemonBox/set', pokemonValuesNewMultiple)
t.truthy(box.pokemon[id])
t.deepEqual(box.pokemon[id].arr1, [1, 2, 3, 0, 'a', 'b'])
t.deepEqual(box.pokemon[id].arr2, [])
t.deepEqual(box.pokemon[id].nested.arr1, [1, 2, 3, 0, 'a', 'b'])
t.deepEqual(box.pokemon[id].nested.arr2, [])
await wait(2)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.arr1, [1, 2, 3, 0, 'a', 'b'])
t.deepEqual(doc.arr2, [])
t.deepEqual(doc.nested.arr1, [1, 2, 3, 0, 'a', 'b'])
t.deepEqual(doc.nested.arr2, [])
// multiple values right after each other
store.dispatch('pokemonBox/set', {
id,
arr1: arrayRemove(1),
arr2: arrayUnion(1),
})
store.dispatch('pokemonBox/set', {
id,
arr1: arrayRemove('a'),
arr2: arrayUnion('a'),
})
t.deepEqual(box.pokemon[id].arr1, [2, 3, 0, 'b'])
t.deepEqual(box.pokemon[id].arr2, [1, 'a'])
await wait(2)
docR = await boxRef.doc(id).get()
doc = docR.data()
t.deepEqual(doc.arr1, [2, 3, 0, 'b'])
t.deepEqual(doc.arr2, [1, 'a'])
})