diff --git a/packages/database/test/exp/integration.test.ts b/packages/database/test/exp/integration.test.ts index adf5094f222..642543214cc 100644 --- a/packages/database/test/exp/integration.test.ts +++ b/packages/database/test/exp/integration.test.ts @@ -20,6 +20,7 @@ import { Deferred } from '@firebase/util'; import { expect, use } from 'chai'; import chaiAsPromised from 'chai-as-promised'; +import { connectDatabaseEmulator } from '../../src/api/Database'; import { child, get, @@ -48,6 +49,7 @@ import { DATABASE_URL, getFreshRepo, getRWRefs, + isEmulatorActive, waitFor, waitUntil, writeAndValidate @@ -138,6 +140,43 @@ describe('Database@exp Tests', () => { unsubscribe(); }); + it('can connected to emulator', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + connectDatabaseEmulator(db, 'localhost', 9000); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + } + }); + + it('can chnage emulator config before network operations', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + connectDatabaseEmulator(db, 'localhost', 9001); + connectDatabaseEmulator(db, 'localhost', 9000); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + } + }); + + it('can connected to emulator after network operations with same parameters', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + connectDatabaseEmulator(db, 'localhost', 9000); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + connectDatabaseEmulator(db, 'localhost', 9000); + } + }); + + it('cannot connect to emulator after network operations with different parameters', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + connectDatabaseEmulator(db, 'localhost', 9000); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + expect(() => { + connectDatabaseEmulator(db, 'localhost', 9001); + }).to.throw(); + } + }); + it('can properly handle unknown deep merges', async () => { // Note: This test requires `testIndex` to be added as an index. // Please run `yarn test:setup` to ensure that this gets added. diff --git a/packages/database/test/helpers/util.ts b/packages/database/test/helpers/util.ts index 73eb04a8c5e..f2712b8a4f9 100644 --- a/packages/database/test/helpers/util.ts +++ b/packages/database/test/helpers/util.ts @@ -143,3 +143,7 @@ export async function waitUntil(cb: () => boolean, maxRetries = 5) { } }); } + +export function isEmulatorActive(): boolean { + return USE_EMULATOR; +}