1717
1818import { initializeApp , deleteApp } from '@firebase/app' ;
1919import { Deferred } from '@firebase/util' ;
20- import { expect } from 'chai' ;
20+ import { expect , use } from 'chai' ;
21+ import chaiAsPromised from 'chai-as-promised' ;
2122
2223import {
2324 get ,
@@ -44,6 +45,8 @@ import {
4445 waitFor
4546} from '../helpers/util' ;
4647
48+ use ( chaiAsPromised ) ;
49+
4750export function createTestApp ( ) {
4851 return initializeApp ( { databaseURL : DATABASE_URL } ) ;
4952}
@@ -257,26 +260,38 @@ describe('Database@exp Tests', () => {
257260 expect ( events [ 0 ] ) . to . equal ( 'a' ) ;
258261 } ) ;
259262
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-
273263 it ( 'Can delete app' , async ( ) => {
274264 const db = getDatabase ( defaultApp ) ;
275265 await deleteApp ( defaultApp ) ;
276266 expect ( ( ) => ref ( db ) ) . to . throw ( 'Cannot call ref on a deleted database.' ) ;
277267 defaultApp = undefined ;
278268 } ) ;
279269
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+
280295 it ( 'Can listen to transaction changes' , async ( ) => {
281296 // Repro for https://github.com/firebase/firebase-js-sdk/issues/5195
282297 let latestValue = 0 ;
0 commit comments