17
17
18
18
import { initializeApp , deleteApp } from '@firebase/app' ;
19
19
import { Deferred } from '@firebase/util' ;
20
- import { expect } from 'chai' ;
20
+ import { expect , use } from 'chai' ;
21
+ import chaiAsPromised from 'chai-as-promised' ;
21
22
22
23
import {
23
24
get ,
@@ -44,6 +45,8 @@ import {
44
45
waitFor
45
46
} from '../helpers/util' ;
46
47
48
+ use ( chaiAsPromised ) ;
49
+
47
50
export function createTestApp ( ) {
48
51
return initializeApp ( { databaseURL : DATABASE_URL } ) ;
49
52
}
@@ -257,26 +260,38 @@ describe('Database@exp Tests', () => {
257
260
expect ( events [ 0 ] ) . to . equal ( 'a' ) ;
258
261
} ) ;
259
262
260
- it ( 'Can goOffline/goOnline' , async ( ) => {
261
- const db = getDatabase ( defaultApp ) ;
262
- goOffline ( db ) ;
263
- try {
264
- await get ( ref ( db , 'foo/bar' ) ) ;
265
- expect . fail ( 'Should have failed since we are offline' ) ;
266
- } catch ( e ) {
267
- expect ( e . message ) . to . equal ( 'Error: Client is offline.' ) ;
268
- }
269
- goOnline ( db ) ;
270
- await get ( ref ( db , 'foo/bar' ) ) ;
271
- } ) ;
272
-
273
263
it ( 'Can delete app' , async ( ) => {
274
264
const db = getDatabase ( defaultApp ) ;
275
265
await deleteApp ( defaultApp ) ;
276
266
expect ( ( ) => ref ( db ) ) . to . throw ( 'Cannot call ref on a deleted database.' ) ;
277
267
defaultApp = undefined ;
278
268
} ) ;
279
269
270
+ it ( 'blocks get requests until the database is online' , async ( ) => {
271
+ const db = getDatabase ( defaultApp ) ;
272
+ const r = ref ( db , 'foo3' ) ;
273
+ const initial = {
274
+ test : 1
275
+ } ;
276
+ await set ( r , initial ) ;
277
+ goOffline ( db ) ;
278
+ const pendingGet = get ( r ) ;
279
+ let resolvedData : any = null ;
280
+ pendingGet . then (
281
+ data => {
282
+ resolvedData = data ;
283
+ } ,
284
+ ( ) => {
285
+ resolvedData = new Error ( 'rejected' ) ;
286
+ }
287
+ ) ;
288
+ await waitFor ( 2000 ) ;
289
+ expect ( resolvedData ) . to . equal ( null ) ;
290
+ goOnline ( db ) ;
291
+ await waitFor ( 2000 ) ;
292
+ expect ( resolvedData . val ( ) ) . to . deep . equal ( initial ) ;
293
+ } ) ;
294
+
280
295
it ( 'Can listen to transaction changes' , async ( ) => {
281
296
// Repro for https://github.com/firebase/firebase-js-sdk/issues/5195
282
297
let latestValue = 0 ;
0 commit comments