15
15
* limitations under the License.
16
16
*/
17
17
18
- import { expect } from 'chai' ;
18
+ import { expect } from 'chai' ;
19
19
20
- import {
21
- collection ,
22
- collectionGroup ,
23
- doc ,
24
- disableNetwork ,
25
- query ,
26
- terminate ,
27
- where ,
28
- writeBatch ,
29
- getCountFromServer
30
- } from '../util/firebase_export' ;
31
- import {
32
- apiDescribe ,
33
- postConverter ,
34
- withEmptyTestCollection ,
35
- withTestCollection ,
36
- withTestDb
37
- } from '../util/helpers' ;
38
-
39
- apiDescribe ( 'Count query' , ( persistence : boolean ) => {
40
- it ( 'can run count query getCountFromServer' , ( ) => {
41
- const testDocs = {
42
- a : { author : 'authorA' , title : 'titleA' } ,
43
- b : { author : 'authorB' , title : 'titleB' }
44
- } ;
45
- return withTestCollection ( persistence , testDocs , async coll => {
46
- const snapshot = await getCountFromServer ( coll ) ;
47
- expect ( snapshot . data ( ) . count ) . to . equal ( 2 ) ;
48
- } ) ;
49
- } ) ;
50
-
51
- it ( 'count query supports withConverter' , ( ) => {
52
- const testDocs = {
53
- a : { author : 'authorA' , title : 'titleA' } ,
54
- b : { author : 'authorB' , title : 'titleB' }
55
- } ;
56
- return withTestCollection ( persistence , testDocs , async coll => {
57
- const query_ = query (
58
- coll ,
59
- where ( 'author' , '==' , 'authorA' )
60
- ) . withConverter ( postConverter ) ;
61
- const snapshot = await getCountFromServer ( query_ ) ;
62
- expect ( snapshot . data ( ) . count ) . to . equal ( 1 ) ;
63
- } ) ;
64
- } ) ;
65
-
66
- it ( 'count query supports collection groups' , ( ) => {
67
- return withTestDb ( persistence , async db => {
68
- const collectionGroupId = doc ( collection ( db , 'aggregateQueryTest' ) ) . id ;
69
- const docPaths = [
70
- `${ collectionGroupId } /cg-doc1` ,
71
- `abc/123/${ collectionGroupId } /cg-doc2` ,
72
- `zzz${ collectionGroupId } /cg-doc3` ,
73
- `abc/123/zzz${ collectionGroupId } /cg-doc4` ,
74
- `abc/123/zzz/${ collectionGroupId } `
75
- ] ;
76
- const batch = writeBatch ( db ) ;
77
- for ( const docPath of docPaths ) {
78
- batch . set ( doc ( db , docPath ) , { x : 1 } ) ;
79
- }
80
- await batch . commit ( ) ;
81
- const snapshot = await getCountFromServer (
82
- collectionGroup ( db , collectionGroupId )
83
- ) ;
84
- expect ( snapshot . data ( ) . count ) . to . equal ( 2 ) ;
85
- } ) ;
86
- } ) ;
87
-
88
- it ( 'getCountFromServer fails if firestore is terminated' , ( ) => {
89
- return withEmptyTestCollection ( persistence , async ( coll , firestore ) => {
90
- await terminate ( firestore ) ;
91
- expect ( ( ) => getCountFromServer ( coll ) ) . to . throw (
92
- 'The client has already been terminated.'
93
- ) ;
94
- } ) ;
95
- } ) ;
96
-
97
- it ( "terminate doesn't crash when there is count query in flight" , ( ) => {
98
- return withEmptyTestCollection ( persistence , async ( coll , firestore ) => {
99
- void getCountFromServer ( coll ) ;
100
- await terminate ( firestore ) ;
101
- } ) ;
102
- } ) ;
103
-
104
- it ( 'getCountFromServer fails if user is offline' , ( ) => {
105
- return withEmptyTestCollection ( persistence , async ( coll , firestore ) => {
106
- await disableNetwork ( firestore ) ;
107
- await expect ( getCountFromServer ( coll ) ) . to . be . eventually . rejectedWith (
108
- 'Failed to get count result because the client is offline'
109
- ) ;
110
- } ) ;
111
- } ) ;
112
- } ) ;
20
+ import {
21
+ collection ,
22
+ collectionGroup ,
23
+ doc ,
24
+ disableNetwork ,
25
+ query ,
26
+ terminate ,
27
+ where ,
28
+ writeBatch ,
29
+ getCountFromServer
30
+ } from '../util/firebase_export' ;
31
+ import {
32
+ apiDescribe ,
33
+ postConverter ,
34
+ withEmptyTestCollection ,
35
+ withTestCollection ,
36
+ withTestDb
37
+ } from '../util/helpers' ;
38
+
39
+ apiDescribe ( 'Count query' , ( persistence : boolean ) => {
40
+ it ( 'can run count query getCountFromServer' , ( ) => {
41
+ const testDocs = {
42
+ a : { author : 'authorA' , title : 'titleA' } ,
43
+ b : { author : 'authorB' , title : 'titleB' }
44
+ } ;
45
+ return withTestCollection ( persistence , testDocs , async coll => {
46
+ const snapshot = await getCountFromServer ( coll ) ;
47
+ expect ( snapshot . data ( ) . count ) . to . equal ( 2 ) ;
48
+ } ) ;
49
+ } ) ;
50
+
51
+ it ( 'count query supports withConverter' , ( ) => {
52
+ const testDocs = {
53
+ a : { author : 'authorA' , title : 'titleA' } ,
54
+ b : { author : 'authorB' , title : 'titleB' }
55
+ } ;
56
+ return withTestCollection ( persistence , testDocs , async coll => {
57
+ const query_ = query (
58
+ coll ,
59
+ where ( 'author' , '==' , 'authorA' )
60
+ ) . withConverter ( postConverter ) ;
61
+ const snapshot = await getCountFromServer ( query_ ) ;
62
+ expect ( snapshot . data ( ) . count ) . to . equal ( 1 ) ;
63
+ } ) ;
64
+ } ) ;
65
+
66
+ it ( 'count query supports collection groups' , ( ) => {
67
+ return withTestDb ( persistence , async db => {
68
+ const collectionGroupId = doc ( collection ( db , 'aggregateQueryTest' ) ) . id ;
69
+ const docPaths = [
70
+ `${ collectionGroupId } /cg-doc1` ,
71
+ `abc/123/${ collectionGroupId } /cg-doc2` ,
72
+ `zzz${ collectionGroupId } /cg-doc3` ,
73
+ `abc/123/zzz${ collectionGroupId } /cg-doc4` ,
74
+ `abc/123/zzz/${ collectionGroupId } `
75
+ ] ;
76
+ const batch = writeBatch ( db ) ;
77
+ for ( const docPath of docPaths ) {
78
+ batch . set ( doc ( db , docPath ) , { x : 1 } ) ;
79
+ }
80
+ await batch . commit ( ) ;
81
+ const snapshot = await getCountFromServer (
82
+ collectionGroup ( db , collectionGroupId )
83
+ ) ;
84
+ expect ( snapshot . data ( ) . count ) . to . equal ( 2 ) ;
85
+ } ) ;
86
+ } ) ;
87
+
88
+ it ( 'getCountFromServer fails if firestore is terminated' , ( ) => {
89
+ return withEmptyTestCollection ( persistence , async ( coll , firestore ) => {
90
+ await terminate ( firestore ) ;
91
+ expect ( ( ) => getCountFromServer ( coll ) ) . to . throw (
92
+ 'The client has already been terminated.'
93
+ ) ;
94
+ } ) ;
95
+ } ) ;
96
+
97
+ it ( "terminate doesn't crash when there is count query in flight" , ( ) => {
98
+ return withEmptyTestCollection ( persistence , async ( coll , firestore ) => {
99
+ void getCountFromServer ( coll ) ;
100
+ await terminate ( firestore ) ;
101
+ } ) ;
102
+ } ) ;
103
+
104
+ it ( 'getCountFromServer fails if user is offline' , ( ) => {
105
+ return withEmptyTestCollection ( persistence , async ( coll , firestore ) => {
106
+ await disableNetwork ( firestore ) ;
107
+ await expect ( getCountFromServer ( coll ) ) . to . be . eventually . rejectedWith (
108
+ 'Failed to get count result because the client is offline'
109
+ ) ;
110
+ } ) ;
111
+ } ) ;
112
+ } ) ;
0 commit comments