@@ -33,49 +33,59 @@ apiDescribe('Firestore', (persistence: boolean) => {
33
33
async function expectRoundtrip (
34
34
db : firestore . FirebaseFirestore ,
35
35
data : { } ,
36
- validateSnapshots = true
37
- ) : Promise < void > {
36
+ validateSnapshots = true ,
37
+ expectedData ?: { }
38
+ ) : Promise < firestore . DocumentSnapshot > {
39
+ expectedData = expectedData ?? data ;
40
+
38
41
const collection = db . collection ( db . collection ( 'a' ) . doc ( ) . id ) ;
39
42
const doc = collection . doc ( ) ;
40
- await doc . set ( data ) ;
41
43
44
+ await doc . set ( data ) ;
42
45
let docSnapshot = await doc . get ( ) ;
43
- expect ( docSnapshot . data ( ) ) . to . deep . equal ( data ) ;
46
+ expect ( docSnapshot . data ( ) ) . to . deep . equal ( expectedData ) ;
47
+
48
+ await doc . update ( data ) ;
49
+ docSnapshot = await doc . get ( ) ;
50
+ expect ( docSnapshot . data ( ) ) . to . deep . equal ( expectedData ) ;
44
51
45
52
// Validate that the transaction API returns the same types
46
53
await db . runTransaction ( async transaction => {
47
54
docSnapshot = await transaction . get ( doc ) ;
48
- expect ( docSnapshot . data ( ) ) . to . deep . equal ( data ) ;
55
+ expect ( docSnapshot . data ( ) ) . to . deep . equal ( expectedData ) ;
49
56
} ) ;
50
57
51
58
if ( validateSnapshots ) {
52
59
let querySnapshot = await collection . get ( ) ;
53
60
docSnapshot = querySnapshot . docs [ 0 ] ;
54
- expect ( docSnapshot . data ( ) ) . to . deep . equal ( data ) ;
61
+ expect ( docSnapshot . data ( ) ) . to . deep . equal ( expectedData ) ;
55
62
56
- const eventsAccumulator = new EventsAccumulator < firestore . QuerySnapshot > ( ) ;
63
+ const eventsAccumulator =
64
+ new EventsAccumulator < firestore . QuerySnapshot > ( ) ;
57
65
const unlisten = collection . onSnapshot ( eventsAccumulator . storeEvent ) ;
58
66
querySnapshot = await eventsAccumulator . awaitEvent ( ) ;
59
67
docSnapshot = querySnapshot . docs [ 0 ] ;
60
- expect ( docSnapshot . data ( ) ) . to . deep . equal ( data ) ;
68
+ expect ( docSnapshot . data ( ) ) . to . deep . equal ( expectedData ) ;
61
69
62
70
unlisten ( ) ;
63
71
}
72
+
73
+ return docSnapshot ;
64
74
}
65
75
66
76
it ( 'can read and write null fields' , ( ) => {
67
- return withTestDb ( persistence , db => {
68
- return expectRoundtrip ( db , { a : 1 , b : null } ) ;
77
+ return withTestDb ( persistence , async db => {
78
+ await expectRoundtrip ( db , { a : 1 , b : null } ) ;
69
79
} ) ;
70
80
} ) ;
71
81
72
82
it ( 'can read and write number fields' , ( ) => {
73
- return withTestDb ( persistence , db => {
83
+ return withTestDb ( persistence , async db => {
74
84
// TODO(b/174486484): If we build ViewSnapshots from IndexedDb, this test
75
85
// fails since we first store the backend proto in IndexedDb, which turns
76
86
// -0.0 into 0.0.
77
87
const validateSnapshots = ! persistence ;
78
- return expectRoundtrip (
88
+ await expectRoundtrip (
79
89
db ,
80
90
{ a : 1 , b : NaN , c : Infinity , d : - 0.0 } ,
81
91
validateSnapshots
@@ -84,91 +94,78 @@ apiDescribe('Firestore', (persistence: boolean) => {
84
94
} ) ;
85
95
86
96
it ( 'can read and write array fields' , ( ) => {
87
- return withTestDb ( persistence , db => {
88
- return expectRoundtrip ( db , { array : [ 1 , 'foo' , { deep : true } , null ] } ) ;
97
+ return withTestDb ( persistence , async db => {
98
+ await expectRoundtrip ( db , { array : [ 1 , 'foo' , { deep : true } , null ] } ) ;
89
99
} ) ;
90
100
} ) ;
91
101
92
102
it ( 'can read and write geo point fields' , ( ) => {
93
- return withTestDoc ( persistence , doc => {
94
- return doc
95
- . set ( {
96
- geopoint1 : new GeoPoint ( 1.23 , 4.56 ) ,
97
- geopoint2 : new GeoPoint ( 0 , 0 )
98
- } )
99
- . then ( ( ) => {
100
- return doc . get ( ) ;
101
- } )
102
- . then ( docSnapshot => {
103
- const latLong = docSnapshot . data ( ) ! [ 'geopoint1' ] ;
104
- expect ( latLong instanceof GeoPoint ) . to . equal ( true ) ;
105
- expect ( latLong . latitude ) . to . equal ( 1.23 ) ;
106
- expect ( latLong . longitude ) . to . equal ( 4.56 ) ;
107
-
108
- const zeroLatLong = docSnapshot . data ( ) ! [ 'geopoint2' ] ;
109
- expect ( zeroLatLong instanceof GeoPoint ) . to . equal ( true ) ;
110
- expect ( zeroLatLong . latitude ) . to . equal ( 0 ) ;
111
- expect ( zeroLatLong . longitude ) . to . equal ( 0 ) ;
112
- } ) ;
103
+ return withTestDb ( persistence , async db => {
104
+ const docSnapshot = await expectRoundtrip ( db , {
105
+ geopoint1 : new GeoPoint ( 1.23 , 4.56 ) ,
106
+ geopoint2 : new GeoPoint ( 0 , 0 )
107
+ } ) ;
108
+
109
+ const latLong = docSnapshot . data ( ) ! [ 'geopoint1' ] ;
110
+ expect ( latLong instanceof GeoPoint ) . to . equal ( true ) ;
111
+ expect ( latLong . latitude ) . to . equal ( 1.23 ) ;
112
+ expect ( latLong . longitude ) . to . equal ( 4.56 ) ;
113
+
114
+ const zeroLatLong = docSnapshot . data ( ) ! [ 'geopoint2' ] ;
115
+ expect ( zeroLatLong instanceof GeoPoint ) . to . equal ( true ) ;
116
+ expect ( zeroLatLong . latitude ) . to . equal ( 0 ) ;
117
+ expect ( zeroLatLong . longitude ) . to . equal ( 0 ) ;
113
118
} ) ;
114
119
} ) ;
115
120
116
121
it ( 'can read and write bytes fields' , ( ) => {
117
- return withTestDoc ( persistence , doc => {
118
- return doc
119
- . set ( {
120
- bytes : Blob . fromUint8Array ( new Uint8Array ( [ 0 , 1 , 255 ] ) )
121
- } )
122
- . then ( ( ) => {
123
- return doc . get ( ) ;
124
- } )
125
- . then ( docSnapshot => {
126
- const blob = docSnapshot . data ( ) ! [ 'bytes' ] ;
127
- // TODO(firestorexp): As part of the Compat migration, the SDK
128
- // should re-wrap the firestore-exp types into the Compat API.
129
- // Comment this change back in once this is complete (note that this
130
- // check passes in the legacy API).
131
- // expect(blob instanceof Blob).to.equal(true);
132
- expect ( blob . toUint8Array ( ) ) . to . deep . equal (
133
- new Uint8Array ( [ 0 , 1 , 255 ] )
134
- ) ;
135
- } ) ;
122
+ return withTestDb ( persistence , async db => {
123
+ const docSnapshot = await expectRoundtrip ( db , {
124
+ bytes : Blob . fromUint8Array ( new Uint8Array ( [ 0 , 1 , 255 ] ) )
125
+ } ) ;
126
+
127
+ const blob = docSnapshot . data ( ) ! [ 'bytes' ] ;
128
+ // TODO(firestorexp): As part of the Compat migration, the SDK
129
+ // should re-wrap the firestore-exp types into the Compat API.
130
+ // Comment this change back in once this is complete (note that this
131
+ // check passes in the legacy API).
132
+ // expect(blob instanceof Blob).to.equal(true);
133
+ expect ( blob . toUint8Array ( ) ) . to . deep . equal ( new Uint8Array ( [ 0 , 1 , 255 ] ) ) ;
136
134
} ) ;
137
135
} ) ;
138
136
139
137
it ( 'can read and write date fields' , ( ) => {
140
- return withTestDb ( persistence , db => {
141
- const dateValue = new Date ( '2017-04-10T09:10:11.123Z' ) ;
142
- // Dates are returned as Timestamps, so expectRoundtrip can't be used
143
- // here.
144
- const doc = db . collection ( 'rooms' ) . doc ( ) ;
145
- return doc
146
- . set ( { date : dateValue } )
147
- . then ( ( ) => doc . get ( ) )
148
- . then ( snapshot => {
149
- expect ( snapshot . data ( ) ) . to . deep . equal ( {
150
- date : Timestamp . fromDate ( dateValue )
151
- } ) ;
152
- } ) ;
138
+ return withTestDb ( persistence , async db => {
139
+ const date = new Date ( '2017-04-10T09:10:11.123Z' ) ;
140
+ // Dates are returned as Timestamps
141
+ const data = { date } ;
142
+ const expectedData = { date : Timestamp . fromDate ( date ) } ;
143
+
144
+ await expectRoundtrip (
145
+ db ,
146
+ data ,
147
+ /* validateSnapshot= */ true ,
148
+ expectedData
149
+ ) ;
153
150
} ) ;
154
151
} ) ;
155
152
156
153
it ( 'can read and write timestamp fields' , ( ) => {
157
- return withTestDb ( persistence , db => {
154
+ return withTestDb ( persistence , async db => {
158
155
const timestampValue = Timestamp . now ( ) ;
159
- return expectRoundtrip ( db , { timestamp : timestampValue } ) ;
156
+ await expectRoundtrip ( db , { timestamp : timestampValue } ) ;
160
157
} ) ;
161
158
} ) ;
162
159
163
160
it ( 'can read and write document references' , ( ) => {
164
- return withTestDoc ( persistence , doc => {
165
- return expectRoundtrip ( doc . firestore , { a : 42 , ref : doc } ) ;
161
+ return withTestDoc ( persistence , async doc => {
162
+ await expectRoundtrip ( doc . firestore , { a : 42 , ref : doc } ) ;
166
163
} ) ;
167
164
} ) ;
168
165
169
166
it ( 'can read and write document references in an array' , ( ) => {
170
- return withTestDoc ( persistence , doc => {
171
- return expectRoundtrip ( doc . firestore , { a : 42 , refs : [ doc ] } ) ;
167
+ return withTestDoc ( persistence , async doc => {
168
+ await expectRoundtrip ( doc . firestore , { a : 42 , refs : [ doc ] } ) ;
172
169
} ) ;
173
170
} ) ;
174
171
} ) ;
0 commit comments