@@ -14,31 +14,7 @@ import 'rxjs/add/operator/skip';
14
14
import { TestBed , inject } from '@angular/core/testing' ;
15
15
import { COMMON_CONFIG } from '../test-config' ;
16
16
17
- interface Stock {
18
- name : string ;
19
- price : number ;
20
- }
21
-
22
- const FAKE_STOCK_DATA = { name : 'FAKE' , price : 1 } ;
23
-
24
- const randomName = ( firestore ) : string => firestore . collection ( 'a' ) . doc ( ) . id ;
25
-
26
- const createRandomStocks = async ( firestore : firestore . Firestore , collectionRef : firestore . CollectionReference , numberOfItems ) => {
27
- // Create a batch to update everything at once
28
- const batch = firestore . batch ( ) ;
29
- // Store the random names to delete them later
30
- let count = 0 ;
31
- let names : string [ ] = [ ] ;
32
- Array . from ( Array ( numberOfItems ) ) . forEach ( ( a , i ) => {
33
- const name = randomName ( firestore ) ;
34
- batch . set ( collectionRef . doc ( name ) , FAKE_STOCK_DATA ) ;
35
- names = [ ...names , name ] ;
36
- } ) ;
37
- // Create the batch entries
38
- // Commit!
39
- await batch . commit ( ) ;
40
- return names ;
41
- }
17
+ import { Stock , randomName , FAKE_STOCK_DATA , createRandomStocks , delayAdd , delayDelete , delayUpdate , deleteThemAll } from '../utils.spec' ;
42
18
43
19
describe ( 'AngularFirestoreCollection' , ( ) => {
44
20
let app : firebase . app . App ;
@@ -63,29 +39,6 @@ describe('AngularFirestoreCollection', () => {
63
39
done ( ) ;
64
40
} ) ;
65
41
66
- function deleteThemAll ( names , ref ) {
67
- const promises = names . map ( name => ref . doc ( name ) . delete ( ) ) ;
68
- return Promise . all ( promises ) ;
69
- }
70
-
71
- function delayUpdate < T > ( collection : AngularFirestoreCollection < T > , path , data , delay = 250 ) {
72
- setTimeout ( ( ) => {
73
- collection . doc ( path ) . update ( data ) ;
74
- } , delay ) ;
75
- }
76
-
77
- function delayAdd < T > ( collection : AngularFirestoreCollection < T > , path , data , delay = 250 ) {
78
- setTimeout ( ( ) => {
79
- collection . doc ( path ) . set ( data ) ;
80
- } , delay ) ;
81
- }
82
-
83
- function delayDelete < T > ( collection : AngularFirestoreCollection < T > , path , delay = 250 ) {
84
- setTimeout ( ( ) => {
85
- collection . doc ( path ) . delete ( ) ;
86
- } , delay ) ;
87
- }
88
-
89
42
it ( 'should get unwrapped snapshot' , async ( done : any ) => {
90
43
const randomCollectionName = randomName ( afs . firestore ) ;
91
44
const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
@@ -114,15 +67,15 @@ describe('AngularFirestoreCollection', () => {
114
67
115
68
} ) ;
116
69
117
- it ( 'should get snapshot updates' , async ( done : any ) => {
70
+ it ( 'should get stateChanges() updates' , async ( done : any ) => {
118
71
const randomCollectionName = randomName ( afs . firestore ) ;
119
72
const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
120
73
const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
121
74
const ITEMS = 10 ;
122
75
123
76
const names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
124
77
125
- const sub = stocks . snapshotChanges ( ) . subscribe ( data => {
78
+ const sub = stocks . stateChanges ( ) . subscribe ( data => {
126
79
// unsub immediately as we will be deleting data at the bottom
127
80
// and that will trigger another subscribe callback and fail
128
81
// the test
@@ -140,84 +93,94 @@ describe('AngularFirestoreCollection', () => {
140
93
141
94
} ) ;
142
95
143
- it ( 'should listen to all changes by default' , async ( done ) => {
144
- const randomCollectionName = randomName ( afs . firestore ) ;
145
- const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
146
- const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
147
- const ITEMS = 10 ;
148
- let count = 0 ;
149
- const names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
150
- const sub = stocks . snapshotChanges ( ) . subscribe ( data => {
151
- count = count + 1 ;
152
- if ( count === 1 ) {
153
- stocks . doc ( names [ 0 ] ) . update ( { price : 2 } ) ;
154
- }
155
- if ( count === 2 ) {
156
- expect ( data . length ) . toEqual ( 1 ) ;
157
- expect ( data [ 0 ] . type ) . toEqual ( 'modified' ) ;
158
- deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
159
- }
160
- } ) ;
161
- } ) ;
162
-
163
- it ( 'should be able to filter change types - modified' , async ( done ) => {
164
- const randomCollectionName = randomName ( afs . firestore ) ;
165
- const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
166
- const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
167
- const ITEMS = 10 ;
168
- let count = 0 ;
169
- const names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
170
-
171
- const sub = stocks . snapshotChanges ( [ 'modified' ] ) . subscribe ( data => {
172
- expect ( data . length ) . toEqual ( 1 ) ;
173
- expect ( data [ 0 ] . payload . doc . data ( ) . price ) . toEqual ( 2 ) ;
174
- expect ( data [ 0 ] . type ) . toEqual ( 'modified' ) ;
175
- deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
176
- done ( ) ;
177
- } ) ;
96
+ fdescribe ( 'snapshotChanges()' , ( ) => {
178
97
179
- delayUpdate ( stocks , names [ 0 ] , { price : 2 } ) ;
180
- } ) ;
98
+ it ( 'should listen to all snapshotChanges() by default' , async ( done ) => {
181
99
182
- it ( 'should be able to filter change types - added' , async ( done ) => {
183
- const randomCollectionName = randomName ( afs . firestore ) ;
184
- const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
185
- const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
186
- const ITEMS = 10 ;
187
- let count = 0 ;
188
- let names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
189
-
190
- const sub = stocks . snapshotChanges ( [ 'added' ] ) . skip ( 1 ) . subscribe ( data => {
191
- expect ( data . length ) . toEqual ( 1 ) ;
192
- expect ( data [ 0 ] . payload . doc . data ( ) . price ) . toEqual ( 2 ) ;
193
- expect ( data [ 0 ] . type ) . toEqual ( 'added' ) ;
194
- deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
195
- done ( ) ;
196
100
} ) ;
197
101
198
- const nextId = ref . doc ( 'a' ) . id ;
199
- names = names . concat ( [ nextId ] ) ;
200
- delayAdd ( stocks , nextId , { price : 2 } ) ;
201
102
} ) ;
202
103
203
- fit ( 'should be able to filter change types - removed' , async ( done ) => {
204
- const randomCollectionName = randomName ( afs . firestore ) ;
205
- const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
206
- const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
207
- const ITEMS = 10 ;
208
- let count = 0 ;
209
- let names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
104
+ describe ( 'stateChanges()' , ( ) => {
105
+ it ( 'should listen to all stateChanges() by default' , async ( done ) => {
106
+ const randomCollectionName = randomName ( afs . firestore ) ;
107
+ const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
108
+ const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
109
+ const ITEMS = 10 ;
110
+ let count = 0 ;
111
+ const names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
112
+ const sub = stocks . stateChanges ( ) . subscribe ( data => {
113
+ count = count + 1 ;
114
+ if ( count === 1 ) {
115
+ stocks . doc ( names [ 0 ] ) . update ( { price : 2 } ) ;
116
+ }
117
+ if ( count === 2 ) {
118
+ expect ( data . length ) . toEqual ( 1 ) ;
119
+ expect ( data [ 0 ] . type ) . toEqual ( 'modified' ) ;
120
+ deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
121
+ }
122
+ } ) ;
123
+ } ) ;
210
124
211
- const sub = stocks . snapshotChanges ( [ 'removed' ] ) . subscribe ( data => {
212
- debugger ;
213
- expect ( data . length ) . toEqual ( 1 ) ;
214
- expect ( data [ 0 ] . type ) . toEqual ( 'removed' ) ;
215
- deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
216
- done ( ) ;
125
+ it ( 'should be able to filter stateChanges() types - modified' , async ( done ) => {
126
+ const randomCollectionName = randomName ( afs . firestore ) ;
127
+ const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
128
+ const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
129
+ const ITEMS = 10 ;
130
+ let count = 0 ;
131
+ const names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
132
+
133
+ const sub = stocks . stateChanges ( [ 'modified' ] ) . subscribe ( data => {
134
+ sub . unsubscribe ( ) ;
135
+ expect ( data . length ) . toEqual ( 1 ) ;
136
+ expect ( data [ 0 ] . payload . doc . data ( ) . price ) . toEqual ( 2 ) ;
137
+ expect ( data [ 0 ] . type ) . toEqual ( 'modified' ) ;
138
+ deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
139
+ done ( ) ;
140
+ } ) ;
141
+
142
+ delayUpdate ( stocks , names [ 0 ] , { price : 2 } ) ;
217
143
} ) ;
218
-
219
- debugger ;
220
- delayDelete ( stocks , names [ 0 ] , 400 ) ;
221
- } ) ;
144
+
145
+ it ( 'should be able to filter stateChanges() types - added' , async ( done ) => {
146
+ const randomCollectionName = randomName ( afs . firestore ) ;
147
+ const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
148
+ const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
149
+ const ITEMS = 10 ;
150
+ let count = 0 ;
151
+ let names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
152
+
153
+ const sub = stocks . stateChanges ( [ 'added' ] ) . skip ( 1 ) . subscribe ( data => {
154
+ sub . unsubscribe ( ) ;
155
+ expect ( data . length ) . toEqual ( 1 ) ;
156
+ expect ( data [ 0 ] . payload . doc . data ( ) . price ) . toEqual ( 2 ) ;
157
+ expect ( data [ 0 ] . type ) . toEqual ( 'added' ) ;
158
+ deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
159
+ done ( ) ;
160
+ } ) ;
161
+
162
+ const nextId = ref . doc ( 'a' ) . id ;
163
+ names = names . concat ( [ nextId ] ) ;
164
+ delayAdd ( stocks , nextId , { price : 2 } ) ;
165
+ } ) ;
166
+
167
+ it ( 'should be able to filter stateChanges() types - removed' , async ( done ) => {
168
+ const randomCollectionName = randomName ( afs . firestore ) ;
169
+ const ref = afs . firestore . collection ( `${ randomCollectionName } ` ) ;
170
+ const stocks = new AngularFirestoreCollection < Stock > ( ref , ref ) ;
171
+ const ITEMS = 10 ;
172
+ let names = await createRandomStocks ( afs . firestore , ref , ITEMS ) ;
173
+
174
+ const sub = stocks . stateChanges ( [ 'removed' ] ) . subscribe ( data => {
175
+ sub . unsubscribe ( ) ;
176
+ expect ( data . length ) . toEqual ( 1 ) ;
177
+ expect ( data [ 0 ] . type ) . toEqual ( 'removed' ) ;
178
+ deleteThemAll ( names , ref ) . then ( done ) . catch ( done . fail ) ;
179
+ done ( ) ;
180
+ } ) ;
181
+
182
+ delayDelete ( stocks , names [ 0 ] , 400 ) ;
183
+ } ) ;
184
+ } ) ;
222
185
223
186
} ) ;
0 commit comments