15
15
* limitations under the License.
16
16
*/
17
17
18
- import * as firestore from '@firebase/firestore-types' ;
19
18
import { expect } from 'chai' ;
20
19
21
20
import { addEqualityMatcher } from '../../util/equality_matcher' ;
22
21
import { EventsAccumulator } from '../util/events_accumulator' ;
23
- import * as firebaseExport from '../util/firebase_export' ;
22
+ import {
23
+ doc ,
24
+ DocumentData ,
25
+ DocumentReference ,
26
+ DocumentSnapshot ,
27
+ getDocFromCache ,
28
+ onSnapshot ,
29
+ setDoc ,
30
+ updateDoc ,
31
+ arrayRemove ,
32
+ arrayUnion
33
+ } from '../util/firebase_export' ;
24
34
import { apiDescribe , withTestDb , withTestDoc } from '../util/helpers' ;
25
35
26
36
addEqualityMatcher ( ) ;
27
37
28
- const FieldValue = firebaseExport . FieldValue ;
29
-
30
38
/**
31
39
* Note: Transforms are tested pretty thoroughly in server_timestamp.test.ts
32
40
* (via set, update, transactions, nested in documents, multiple transforms
@@ -35,27 +43,25 @@ const FieldValue = firebaseExport.FieldValue;
35
43
*/
36
44
apiDescribe ( 'Array Transforms:' , ( persistence : boolean ) => {
37
45
// A document reference to read and write to.
38
- let docRef : firestore . DocumentReference ;
46
+ let docRef : DocumentReference ;
39
47
40
48
// Accumulator used to capture events during the test.
41
- let accumulator : EventsAccumulator < firestore . DocumentSnapshot > ;
49
+ let accumulator : EventsAccumulator < DocumentSnapshot > ;
42
50
43
51
// Listener registration for a listener maintained during the course of the
44
52
// test.
45
53
let unsubscribe : ( ) => void ;
46
54
47
55
/** Writes some initialData and consumes the events generated. */
48
- async function writeInitialData (
49
- initialData : firestore . DocumentData
50
- ) : Promise < void > {
51
- await docRef . set ( initialData ) ;
56
+ async function writeInitialData ( initialData : DocumentData ) : Promise < void > {
57
+ await setDoc ( docRef , initialData ) ;
52
58
await accumulator . awaitLocalEvent ( ) ;
53
59
const snapshot = await accumulator . awaitRemoteEvent ( ) ;
54
60
expect ( snapshot . data ( ) ) . to . deep . equal ( initialData ) ;
55
61
}
56
62
57
63
async function expectLocalAndRemoteEvent (
58
- expected : firestore . DocumentData
64
+ expected : DocumentData
59
65
) : Promise < void > {
60
66
const localSnap = await accumulator . awaitLocalEvent ( ) ;
61
67
expect ( localSnap . data ( ) ) . to . deep . equal ( expected ) ;
@@ -70,8 +76,9 @@ apiDescribe('Array Transforms:', (persistence: boolean) => {
70
76
async function withTestSetup < T > ( test : ( ) => Promise < T > ) : Promise < void > {
71
77
await withTestDoc ( persistence , async doc => {
72
78
docRef = doc ;
73
- accumulator = new EventsAccumulator < firestore . DocumentSnapshot > ( ) ;
74
- unsubscribe = docRef . onSnapshot (
79
+ accumulator = new EventsAccumulator < DocumentSnapshot > ( ) ;
80
+ unsubscribe = onSnapshot (
81
+ docRef ,
75
82
{ includeMetadataChanges : true } ,
76
83
accumulator . storeEvent
77
84
) ;
@@ -86,35 +93,32 @@ apiDescribe('Array Transforms:', (persistence: boolean) => {
86
93
87
94
it ( 'create document with arrayUnion()' , async ( ) => {
88
95
await withTestSetup ( async ( ) => {
89
- await docRef . set ( { array : FieldValue . arrayUnion ( 1 , 2 ) } ) ;
96
+ await setDoc ( docRef , { array : arrayUnion ( 1 , 2 ) } ) ;
90
97
await expectLocalAndRemoteEvent ( { array : [ 1 , 2 ] } ) ;
91
98
} ) ;
92
99
} ) ;
93
100
94
101
it ( 'append to array via update()' , async ( ) => {
95
102
await withTestSetup ( async ( ) => {
96
103
await writeInitialData ( { array : [ 1 , 3 ] } ) ;
97
- await docRef . update ( { array : FieldValue . arrayUnion ( 2 , 1 , 4 ) } ) ;
104
+ await updateDoc ( docRef , { array : arrayUnion ( 2 , 1 , 4 ) } ) ;
98
105
await expectLocalAndRemoteEvent ( { array : [ 1 , 3 , 2 , 4 ] } ) ;
99
106
} ) ;
100
107
} ) ;
101
108
102
109
it ( 'append to array via set(..., {merge: true})' , async ( ) => {
103
110
await withTestSetup ( async ( ) => {
104
111
await writeInitialData ( { array : [ 1 , 3 ] } ) ;
105
- await docRef . set (
106
- { array : FieldValue . arrayUnion ( 2 , 1 , 4 ) } ,
107
- { merge : true }
108
- ) ;
112
+ await setDoc ( docRef , { array : arrayUnion ( 2 , 1 , 4 ) } , { merge : true } ) ;
109
113
await expectLocalAndRemoteEvent ( { array : [ 1 , 3 , 2 , 4 ] } ) ;
110
114
} ) ;
111
115
} ) ;
112
116
113
117
it ( 'append object to array via update()' , async ( ) => {
114
118
await withTestSetup ( async ( ) => {
115
119
await writeInitialData ( { array : [ { a : 'hi' } ] } ) ;
116
- await docRef . update ( {
117
- array : FieldValue . arrayUnion ( { a : 'hi' } , { a : 'bye' } )
120
+ await updateDoc ( docRef , {
121
+ array : arrayUnion ( { a : 'hi' } , { a : 'bye' } )
118
122
} ) ;
119
123
await expectLocalAndRemoteEvent ( { array : [ { a : 'hi' } , { a : 'bye' } ] } ) ;
120
124
} ) ;
@@ -123,33 +127,30 @@ apiDescribe('Array Transforms:', (persistence: boolean) => {
123
127
it ( 'remove from array via update()' , async ( ) => {
124
128
await withTestSetup ( async ( ) => {
125
129
await writeInitialData ( { array : [ 1 , 3 , 1 , 3 ] } ) ;
126
- await docRef . update ( { array : FieldValue . arrayRemove ( 1 , 4 ) } ) ;
130
+ await updateDoc ( docRef , { array : arrayRemove ( 1 , 4 ) } ) ;
127
131
await expectLocalAndRemoteEvent ( { array : [ 3 , 3 ] } ) ;
128
132
} ) ;
129
133
} ) ;
130
134
131
135
it ( 'remove from array via set(..., {merge: true})' , async ( ) => {
132
136
await withTestSetup ( async ( ) => {
133
137
await writeInitialData ( { array : [ 1 , 3 , 1 , 3 ] } ) ;
134
- await docRef . set (
135
- { array : FieldValue . arrayRemove ( 1 , 4 ) } ,
136
- { merge : true }
137
- ) ;
138
+ await setDoc ( docRef , { array : arrayRemove ( 1 , 4 ) } , { merge : true } ) ;
138
139
await expectLocalAndRemoteEvent ( { array : [ 3 , 3 ] } ) ;
139
140
} ) ;
140
141
} ) ;
141
142
142
143
it ( 'remove object from array via update()' , async ( ) => {
143
144
await withTestSetup ( async ( ) => {
144
145
await writeInitialData ( { array : [ { a : 'hi' } , { a : 'bye' } ] } ) ;
145
- await docRef . update ( { array : FieldValue . arrayRemove ( { a : 'hi' } ) } ) ;
146
+ await updateDoc ( docRef , { array : arrayRemove ( { a : 'hi' } ) } ) ;
146
147
await expectLocalAndRemoteEvent ( { array : [ { a : 'bye' } ] } ) ;
147
148
} ) ;
148
149
} ) ;
149
150
150
151
it ( 'arrayUnion() supports DocumentReference' , async ( ) => {
151
152
await withTestSetup ( async ( ) => {
152
- await docRef . set ( { array : FieldValue . arrayUnion ( docRef ) } ) ;
153
+ await setDoc ( docRef , { array : arrayUnion ( docRef ) } ) ;
153
154
await expectLocalAndRemoteEvent ( { array : [ docRef ] } ) ;
154
155
} ) ;
155
156
} ) ;
@@ -165,8 +166,8 @@ apiDescribe('Array Transforms:', (persistence: boolean) => {
165
166
( persistence ? describe : describe . skip ) ( 'Server Application: ' , ( ) => {
166
167
it ( 'set() with no cached base doc' , async ( ) => {
167
168
await withTestDoc ( persistence , async docRef => {
168
- await docRef . set ( { array : FieldValue . arrayUnion ( 1 , 2 ) } ) ;
169
- const snapshot = await docRef . get ( { source : 'cache' } ) ;
169
+ await setDoc ( docRef , { array : arrayUnion ( 1 , 2 ) } ) ;
170
+ const snapshot = await getDocFromCache ( docRef ) ;
170
171
expect ( snapshot . data ( ) ) . to . deep . equal ( { array : [ 1 , 2 ] } ) ;
171
172
} ) ;
172
173
} ) ;
@@ -177,18 +178,18 @@ apiDescribe('Array Transforms:', (persistence: boolean) => {
177
178
// stored in our cache
178
179
await withTestDoc ( persistence , async docRef => {
179
180
path = docRef . path ;
180
- await docRef . set ( { array : [ 42 ] } ) ;
181
+ await setDoc ( docRef , { array : [ 42 ] } ) ;
181
182
} ) ;
182
183
183
184
await withTestDb ( persistence , async db => {
184
- const docRef = db . doc ( path ! ) ;
185
- await docRef . update ( { array : FieldValue . arrayUnion ( 1 , 2 ) } ) ;
185
+ const docRef = doc ( db , path ! ) ;
186
+ await updateDoc ( docRef , { array : arrayUnion ( 1 , 2 ) } ) ;
186
187
187
188
// Nothing should be cached since it was an update and we had no base
188
189
// doc.
189
190
let errCaught = false ;
190
191
try {
191
- await docRef . get ( { source : 'cache' } ) ;
192
+ await getDocFromCache ( docRef ) ;
192
193
} catch ( err ) {
193
194
expect ( err . code ) . to . equal ( 'unavailable' ) ;
194
195
errCaught = true ;
@@ -203,36 +204,33 @@ apiDescribe('Array Transforms:', (persistence: boolean) => {
203
204
// stored in our cache
204
205
await withTestDoc ( persistence , async docRef => {
205
206
path = docRef . path ;
206
- await docRef . set ( { array : [ 42 ] } ) ;
207
+ await setDoc ( docRef , { array : [ 42 ] } ) ;
207
208
} ) ;
208
209
209
210
await withTestDb ( persistence , async db => {
210
- const docRef = db . doc ( path ! ) ;
211
- await docRef . set (
212
- { array : FieldValue . arrayUnion ( 1 , 2 ) } ,
213
- { merge : true }
214
- ) ;
211
+ const docRef = doc ( db , path ! ) ;
212
+ await setDoc ( docRef , { array : arrayUnion ( 1 , 2 ) } , { merge : true } ) ;
215
213
216
214
// Document will be cached but we'll be missing 42.
217
- const snapshot = await docRef . get ( { source : 'cache' } ) ;
215
+ const snapshot = await getDocFromCache ( docRef ) ;
218
216
expect ( snapshot . data ( ) ) . to . deep . equal ( { array : [ 1 , 2 ] } ) ;
219
217
} ) ;
220
218
} ) ;
221
219
222
220
it ( 'update() with cached base doc using arrayUnion()' , async ( ) => {
223
221
await withTestDoc ( persistence , async docRef => {
224
- await docRef . set ( { array : [ 42 ] } ) ;
225
- await docRef . update ( { array : FieldValue . arrayUnion ( 1 , 2 ) } ) ;
226
- const snapshot = await docRef . get ( { source : 'cache' } ) ;
222
+ await setDoc ( docRef , { array : [ 42 ] } ) ;
223
+ await updateDoc ( docRef , { array : arrayUnion ( 1 , 2 ) } ) ;
224
+ const snapshot = await getDocFromCache ( docRef ) ;
227
225
expect ( snapshot . data ( ) ) . to . deep . equal ( { array : [ 42 , 1 , 2 ] } ) ;
228
226
} ) ;
229
227
} ) ;
230
228
231
229
it ( 'update() with cached base doc using arrayRemove()' , async ( ) => {
232
230
await withTestDoc ( persistence , async docRef => {
233
- await docRef . set ( { array : [ 42 , 1 , 2 ] } ) ;
234
- await docRef . update ( { array : FieldValue . arrayRemove ( 1 , 2 ) } ) ;
235
- const snapshot = await docRef . get ( { source : 'cache' } ) ;
231
+ await setDoc ( docRef , { array : [ 42 , 1 , 2 ] } ) ;
232
+ await updateDoc ( docRef , { array : arrayRemove ( 1 , 2 ) } ) ;
233
+ const snapshot = await getDocFromCache ( docRef ) ;
236
234
expect ( snapshot . data ( ) ) . to . deep . equal ( { array : [ 42 ] } ) ;
237
235
} ) ;
238
236
} ) ;
0 commit comments