@@ -20,6 +20,7 @@ import { Deferred } from '@firebase/util';
2020import { expect , use } from 'chai' ;
2121import chaiAsPromised from 'chai-as-promised' ;
2222
23+ import { connectDatabaseEmulator } from '../../src/api/Database' ;
2324import {
2425 child ,
2526 get ,
@@ -48,6 +49,7 @@ import {
4849 DATABASE_URL ,
4950 getFreshRepo ,
5051 getRWRefs ,
52+ isEmulatorActive ,
5153 waitFor ,
5254 waitUntil ,
5355 writeAndValidate
@@ -138,6 +140,43 @@ describe('Database@exp Tests', () => {
138140 unsubscribe ( ) ;
139141 } ) ;
140142
143+ it ( 'can connected to emulator' , async ( ) => {
144+ if ( isEmulatorActive ( ) ) {
145+ const db = getDatabase ( defaultApp ) ;
146+ connectDatabaseEmulator ( db , 'localhost' , 9000 ) ;
147+ await get ( refFromURL ( db , `${ DATABASE_ADDRESS } /foo/bar` ) ) ;
148+ }
149+ } ) ;
150+
151+ it ( 'can chnage emulator config before network operations' , async ( ) => {
152+ if ( isEmulatorActive ( ) ) {
153+ const db = getDatabase ( defaultApp ) ;
154+ connectDatabaseEmulator ( db , 'localhost' , 9001 ) ;
155+ connectDatabaseEmulator ( db , 'localhost' , 9000 ) ;
156+ await get ( refFromURL ( db , `${ DATABASE_ADDRESS } /foo/bar` ) ) ;
157+ }
158+ } ) ;
159+
160+ it ( 'can connected to emulator after network operations with same parameters' , async ( ) => {
161+ if ( isEmulatorActive ( ) ) {
162+ const db = getDatabase ( defaultApp ) ;
163+ connectDatabaseEmulator ( db , 'localhost' , 9000 ) ;
164+ await get ( refFromURL ( db , `${ DATABASE_ADDRESS } /foo/bar` ) ) ;
165+ connectDatabaseEmulator ( db , 'localhost' , 9000 ) ;
166+ }
167+ } ) ;
168+
169+ it ( 'cannot connect to emulator after network operations with different parameters' , async ( ) => {
170+ if ( isEmulatorActive ( ) ) {
171+ const db = getDatabase ( defaultApp ) ;
172+ connectDatabaseEmulator ( db , 'localhost' , 9000 ) ;
173+ await get ( refFromURL ( db , `${ DATABASE_ADDRESS } /foo/bar` ) ) ;
174+ expect ( ( ) => {
175+ connectDatabaseEmulator ( db , 'localhost' , 9001 ) ;
176+ } ) . to . throw ( ) ;
177+ }
178+ } ) ;
179+
141180 it ( 'can properly handle unknown deep merges' , async ( ) => {
142181 // Note: This test requires `testIndex` to be added as an index.
143182 // Please run `yarn test:setup` to ensure that this gets added.
0 commit comments