diff --git a/packages/database/e2e/DatabaseStatics.e2e.js b/packages/database/e2e/DatabaseStatics.e2e.js index d8b9cf073d..fefe52aaa4 100644 --- a/packages/database/e2e/DatabaseStatics.e2e.js +++ b/packages/database/e2e/DatabaseStatics.e2e.js @@ -20,48 +20,105 @@ const { PATH, wipe } = require('./helpers'); const TEST_PATH = `${PATH}/statics`; describe('database.X', function () { - after(function () { - return wipe(TEST_PATH); - }); + describe('v8 compatibility', function () { + after(function () { + return wipe(TEST_PATH); + }); + + describe('ServerValue.TIMESTAMP', function () { + it('returns a valid object', function () { + const { TIMESTAMP } = firebase.database.ServerValue; + should.equal(Object.keys(TIMESTAMP).length, 1); + TIMESTAMP.should.have.property('.sv'); + TIMESTAMP['.sv'].should.eql('timestamp'); + }); + }); + + describe('ServerValue.increment', function () { + it('returns a valid object', function () { + const incrementObject = firebase.database.ServerValue.increment(1); + should.equal(Object.keys(incrementObject).length, 1); + incrementObject.should.have.property('.sv'); + incrementObject['.sv'].should.have.property('increment'); + }); + + it('increments on the server', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/increment`); + + await ref.set({ increment: 0 }); + + const res1 = await ref.once('value'); + res1.val().increment.should.equal(0); + + await ref.set({ increment: firebase.database.ServerValue.increment(1) }); + + const res2 = await ref.once('value'); + res2.val().increment.should.equal(1); + }); - describe('ServerValue.TIMESTAMP', function () { - it('returns a valid object', function () { - const { TIMESTAMP } = firebase.database.ServerValue; - should.equal(Object.keys(TIMESTAMP).length, 1); - TIMESTAMP.should.have.property('.sv'); - TIMESTAMP['.sv'].should.eql('timestamp'); + it('increments on the server when no value is present', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/increment-empty`); + + await ref.set({ increment: firebase.database.ServerValue.increment(2) }); + + const res = await ref.once('value'); + res.val().increment.should.equal(2); + }); }); }); - describe('ServerValue.increment', function () { - it('returns a valid object', function () { - const incrementObject = firebase.database.ServerValue.increment(1); - should.equal(Object.keys(incrementObject).length, 1); - incrementObject.should.have.property('.sv'); - incrementObject['.sv'].should.have.property('increment'); + describe('modular', function () { + after(function () { + return wipe(TEST_PATH); }); - it('increments on the server', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/increment`); + describe('serverTimestamp', function () { + it('returns a valid object', function () { + const { serverTimestamp } = databaseModular; + const timestamp = serverTimestamp(); - await ref.set({ increment: 0 }); + should.equal(Object.keys(timestamp).length, 1); + timestamp.should.have.property('.sv'); + timestamp['.sv'].should.eql('timestamp'); + }); + }); - const res1 = await ref.once('value'); - res1.val().increment.should.equal(0); + describe('increment', function () { + it('returns a valid object', function () { + const { increment } = databaseModular; - await ref.set({ increment: firebase.database.ServerValue.increment(1) }); + const incrementObject = increment(1); + should.equal(Object.keys(incrementObject).length, 1); + incrementObject.should.have.property('.sv'); + incrementObject['.sv'].should.have.property('increment'); + }); - const res2 = await ref.once('value'); - res2.val().increment.should.equal(1); - }); + it('increments on the server', async function () { + const { getDatabase, ref, set, get, increment } = databaseModular; + + const dbRef = ref(getDatabase(), `${TEST_PATH}/increment`); + + await set(dbRef, { increment: 0 }); + + const res1 = await get(dbRef); + res1.val().increment.should.equal(0); + + await set(dbRef, { increment: increment(1) }); + + const res2 = await get(dbRef); + res2.val().increment.should.equal(1); + }); + + it('increments on the server when no value is present', async function () { + const { getDatabase, ref, set, get, increment } = databaseModular; - it('increments on the server when no value is present', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/increment-empty`); + const dbRef = ref(getDatabase(), `${TEST_PATH}/increment-empty`); - await ref.set({ increment: firebase.database.ServerValue.increment(2) }); + await set(dbRef, { increment: increment(2) }); - const res = await ref.once('value'); - res.val().increment.should.equal(2); + const res = await get(dbRef); + res.val().increment.should.equal(2); + }); }); }); }); diff --git a/packages/database/e2e/DatabaseStatics.modular.e2e.js b/packages/database/e2e/DatabaseStatics.modular.e2e.js deleted file mode 100644 index fefe52aaa4..0000000000 --- a/packages/database/e2e/DatabaseStatics.modular.e2e.js +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, wipe } = require('./helpers'); - -const TEST_PATH = `${PATH}/statics`; - -describe('database.X', function () { - describe('v8 compatibility', function () { - after(function () { - return wipe(TEST_PATH); - }); - - describe('ServerValue.TIMESTAMP', function () { - it('returns a valid object', function () { - const { TIMESTAMP } = firebase.database.ServerValue; - should.equal(Object.keys(TIMESTAMP).length, 1); - TIMESTAMP.should.have.property('.sv'); - TIMESTAMP['.sv'].should.eql('timestamp'); - }); - }); - - describe('ServerValue.increment', function () { - it('returns a valid object', function () { - const incrementObject = firebase.database.ServerValue.increment(1); - should.equal(Object.keys(incrementObject).length, 1); - incrementObject.should.have.property('.sv'); - incrementObject['.sv'].should.have.property('increment'); - }); - - it('increments on the server', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/increment`); - - await ref.set({ increment: 0 }); - - const res1 = await ref.once('value'); - res1.val().increment.should.equal(0); - - await ref.set({ increment: firebase.database.ServerValue.increment(1) }); - - const res2 = await ref.once('value'); - res2.val().increment.should.equal(1); - }); - - it('increments on the server when no value is present', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/increment-empty`); - - await ref.set({ increment: firebase.database.ServerValue.increment(2) }); - - const res = await ref.once('value'); - res.val().increment.should.equal(2); - }); - }); - }); - - describe('modular', function () { - after(function () { - return wipe(TEST_PATH); - }); - - describe('serverTimestamp', function () { - it('returns a valid object', function () { - const { serverTimestamp } = databaseModular; - const timestamp = serverTimestamp(); - - should.equal(Object.keys(timestamp).length, 1); - timestamp.should.have.property('.sv'); - timestamp['.sv'].should.eql('timestamp'); - }); - }); - - describe('increment', function () { - it('returns a valid object', function () { - const { increment } = databaseModular; - - const incrementObject = increment(1); - should.equal(Object.keys(incrementObject).length, 1); - incrementObject.should.have.property('.sv'); - incrementObject['.sv'].should.have.property('increment'); - }); - - it('increments on the server', async function () { - const { getDatabase, ref, set, get, increment } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/increment`); - - await set(dbRef, { increment: 0 }); - - const res1 = await get(dbRef); - res1.val().increment.should.equal(0); - - await set(dbRef, { increment: increment(1) }); - - const res2 = await get(dbRef); - res2.val().increment.should.equal(1); - }); - - it('increments on the server when no value is present', async function () { - const { getDatabase, ref, set, get, increment } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/increment-empty`); - - await set(dbRef, { increment: increment(2) }); - - const res = await get(dbRef); - res.val().increment.should.equal(2); - }); - }); - }); -}); diff --git a/packages/database/e2e/database.e2e.js b/packages/database/e2e/database.e2e.js index 0cb783e61e..81e69ef0ae 100644 --- a/packages/database/e2e/database.e2e.js +++ b/packages/database/e2e/database.e2e.js @@ -16,196 +16,424 @@ */ describe('database()', function () { - describe('namespace', function () { - it('accessible from firebase.app()', function () { - const app = firebase.app(); - should.exist(app.database); - app.database().app.should.eql(app); + describe('v8 compatibility', function () { + describe('namespace', function () { + it('accessible from firebase.app()', function () { + const app = firebase.app(); + should.exist(app.database); + app.database().app.should.eql(app); + }); + + it('supports multiple apps', async function () { + firebase.database().app.name.should.eql('[DEFAULT]'); + + firebase + .database(firebase.app('secondaryFromNative')) + .app.name.should.eql('secondaryFromNative'); + + firebase.app('secondaryFromNative').database().app.name.should.eql('secondaryFromNative'); + }); }); - it('supports multiple apps', async function () { - firebase.database().app.name.should.eql('[DEFAULT]'); + describe('ref()', function () { + it('throws if path is not a string', async function () { + try { + firebase.database().ref({ foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + return Promise.resolve(); + } + }); - firebase - .database(firebase.app('secondaryFromNative')) - .app.name.should.eql('secondaryFromNative'); + it('throws if path is not a valid string', async function () { + try { + firebase.database().ref('$$$$$'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", + ); + return Promise.resolve(); + } + }); + }); - firebase.app('secondaryFromNative').database().app.name.should.eql('secondaryFromNative'); + describe('refFromURL()', function () { + it('throws if url is not a url', async function () { + try { + firebase.database().refFromURL('foobar'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'url' must be a valid database URL"); + return Promise.resolve(); + } + }); + + it('throws if url from a different domain', async function () { + try { + firebase.database().refFromURL('https://foobar.firebaseio.com'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'url' must be the same domain as the current instance"); + return Promise.resolve(); + } + }); + + it('returns a reference', async function () { + const ref1 = firebase.database().refFromURL(firebase.database()._customUrlOrRegion); + const ref2 = firebase + .database() + .refFromURL(`${firebase.database()._customUrlOrRegion}/foo/bar`); + const ref3 = firebase + .database() + .refFromURL(`${firebase.database()._customUrlOrRegion}/foo/bar?baz=foo`); + should.equal(ref1.path, '/'); + should.equal(ref2.path, 'foo/bar'); + should.equal(ref3.path, 'foo/bar'); + }); }); - }); - it('supports custom database URL', async function () { - firebase.database().app.name.should.eql('[DEFAULT]'); + describe('goOnline()', function () { + it('calls goOnline successfully', async function () { + await firebase.database().goOnline(); + }); + }); - firebase - .database(firebase.app('secondaryFromNative')) - .app.name.should.eql('secondaryFromNative'); + describe('goOffline()', function () { + it('calls goOffline successfully', async function () { + // await Utils.sleep(5000); + await firebase.database().goOffline(); - firebase.app('secondaryFromNative').database().app.name.should.eql('secondaryFromNative'); - }); + await firebase.database().goOnline(); + }); + }); + + describe('setPersistenceEnabled()', function () { + it('throws if enabled is not a boolean', async function () { + try { + firebase.database().setPersistenceEnabled('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'enabled' must be a boolean value"); + return Promise.resolve(); + } + }); - describe('ref()', function () { - it('throws if path is not a string', async function () { - try { - firebase.database().ref({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); - - it('throws if path is not a valid string', async function () { - try { - firebase.database().ref('$$$$$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", - ); - return Promise.resolve(); - } + it('calls setPersistenceEnabled successfully', async function () { + firebase.database().setPersistenceEnabled(true); + firebase.database().setPersistenceEnabled(false); + }); }); - }); - describe('refFromURL()', function () { - it('throws if url is not a url', async function () { - try { - firebase.database().refFromURL('foobar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'url' must be a valid database URL"); - return Promise.resolve(); - } - }); - - it('throws if url from a different domain', async function () { - try { - firebase.database().refFromURL('https://foobar.firebaseio.com'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'url' must be the same domain as the current instance"); - return Promise.resolve(); - } - }); - - it('returns a reference', async function () { - const ref1 = firebase.database().refFromURL(firebase.database()._customUrlOrRegion); - const ref2 = firebase - .database() - .refFromURL(`${firebase.database()._customUrlOrRegion}/foo/bar`); - const ref3 = firebase - .database() - .refFromURL(`${firebase.database()._customUrlOrRegion}/foo/bar?baz=foo`); - should.equal(ref1.path, '/'); - should.equal(ref2.path, 'foo/bar'); - should.equal(ref3.path, 'foo/bar'); + describe('setLoggingEnabled()', function () { + it('throws if enabled is not a boolean', async function () { + try { + firebase.database().setLoggingEnabled('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'enabled' must be a boolean value"); + return Promise.resolve(); + } + }); + + it('calls setLoggingEnabled successfully', async function () { + firebase.database().setLoggingEnabled(true); + firebase.database().setLoggingEnabled(false); + }); }); - }); - describe('goOnline()', function () { - it('calls goOnline successfully', async function () { - await firebase.database().goOnline(); + describe('setPersistenceCacheSizeBytes()', function () { + it('throws if bytes is not a number', async function () { + try { + firebase.database().setPersistenceCacheSizeBytes('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bytes' must be a number value"); + return Promise.resolve(); + } + }); + + it('throws if bytes is less than 1MB', async function () { + try { + firebase.database().setPersistenceCacheSizeBytes(1234); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bytes' must be greater than 1048576 bytes (1MB)"); + return Promise.resolve(); + } + }); + + it('throws if bytes is greater than 10MB', async function () { + try { + firebase.database().setPersistenceCacheSizeBytes(100000000000000); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bytes' must be less than 104857600 bytes (100MB)"); + return Promise.resolve(); + } + }); + + it('calls setPersistenceCacheSizeBytes successfully', async function () { + firebase.database().setPersistenceCacheSizeBytes(1048576); // 1mb + }); }); - }); - describe('goOffline()', function () { - it('calls goOffline successfully', async function () { - // await Utils.sleep(5000); - await firebase.database().goOffline(); + describe('getServerTime()', function () { + it('returns a valid date', async function () { + const date = firebase.database().getServerTime(); + date.getDate.should.be.Function(); + }); + }); - await firebase.database().goOnline(); + describe('handles invalid references()', function () { + it('returns a valid date', async function () { + try { + firebase.database().ref('$'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", + ); + return Promise.resolve(); + } + }); }); }); - describe('setPersistenceEnabled()', function () { - it('throws if enabled is not a boolean', async function () { - try { - firebase.database().setPersistenceEnabled('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'enabled' must be a boolean value"); - return Promise.resolve(); - } + describe('modular', function () { + describe('namespace', function () { + it('accessible from getDatabase', function () { + const { getDatabase } = databaseModular; + + const app = firebase.app(); + const database = getDatabase(app); + should.exist(app.database); + database.app.should.eql(app); + }); + + it('supports multiple apps', async function () { + const { getDatabase } = databaseModular; + const database = getDatabase(); + const secondaryDatabase = getDatabase(firebase.app('secondaryFromNative')); + + database.app.name.should.eql('[DEFAULT]'); + + secondaryDatabase.app.name.should.eql('secondaryFromNative'); + + firebase.app('secondaryFromNative').database().app.name.should.eql('secondaryFromNative'); + }); }); - it('calls setPersistenceEnabled successfully', async function () { - firebase.database().setPersistenceEnabled(true); - firebase.database().setPersistenceEnabled(false); + describe('ref()', function () { + it('throws if path is not a string', async function () { + const { getDatabase, ref } = databaseModular; + const db = getDatabase(); + + try { + ref(db, { foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + return Promise.resolve(); + } + }); + + it('throws if path is not a valid string', async function () { + const { getDatabase, ref } = databaseModular; + const db = getDatabase(); + + try { + ref(db, '$$$$$'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", + ); + return Promise.resolve(); + } + }); }); - }); - describe('setLoggingEnabled()', function () { - it('throws if enabled is not a boolean', async function () { - try { - firebase.database().setLoggingEnabled('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'enabled' must be a boolean value"); - return Promise.resolve(); - } + describe('refFromURL()', function () { + it('throws if url is not a url', async function () { + const { getDatabase, refFromURL } = databaseModular; + const db = getDatabase(); + + try { + refFromURL(db, 'foobar'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'url' must be a valid database URL"); + return Promise.resolve(); + } + }); + + it('throws if url from a different domain', async function () { + const { getDatabase, refFromURL } = databaseModular; + const db = getDatabase(); + + try { + refFromURL(db, 'https://foobar.firebaseio.com'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'url' must be the same domain as the current instance"); + return Promise.resolve(); + } + }); + + it('returns a reference', async function () { + const { getDatabase, refFromURL } = databaseModular; + const db = getDatabase(); + + const ref1 = refFromURL(db, db._customUrlOrRegion); + const ref2 = refFromURL(db, `${db._customUrlOrRegion}/foo/bar`); + const ref3 = refFromURL(db, `${db._customUrlOrRegion}/foo/bar?baz=foo`); + + should.equal(ref1.path, '/'); + should.equal(ref2.path, 'foo/bar'); + should.equal(ref3.path, 'foo/bar'); + }); }); - it('calls setLoggingEnabled successfully', async function () { - firebase.database().setLoggingEnabled(true); - firebase.database().setLoggingEnabled(false); + describe('goOnline()', function () { + it('calls goOnline successfully', async function () { + const { getDatabase, goOnline } = databaseModular; + await goOnline(getDatabase()); + }); }); - }); - describe('setPersistenceCacheSizeBytes()', function () { - it('throws if bytes is not a number', async function () { - try { - firebase.database().setPersistenceCacheSizeBytes('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be a number value"); - return Promise.resolve(); - } + describe('goOffline()', function () { + it('calls goOffline successfully', async function () { + const { getDatabase, goOffline, goOnline } = databaseModular; + const db = getDatabase(); + + // await Utils.sleep(5000); + await goOffline(db); + + await goOnline(db); + }); }); - it('throws if bytes is less than 1MB', async function () { - try { - firebase.database().setPersistenceCacheSizeBytes(1234); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be greater than 1048576 bytes (1MB)"); - return Promise.resolve(); - } + describe('setPersistenceEnabled()', function () { + it('throws if enabled is not a boolean', async function () { + const { getDatabase, setPersistenceEnabled } = databaseModular; + const db = getDatabase(); + + try { + setPersistenceEnabled(db, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'enabled' must be a boolean value"); + return Promise.resolve(); + } + }); + + it('calls setPersistenceEnabled successfully', async function () { + const { getDatabase, setPersistenceEnabled } = databaseModular; + const db = getDatabase(); + + setPersistenceEnabled(db, true); + setPersistenceEnabled(db, false); + }); }); - it('throws if bytes is greater than 10MB', async function () { - try { - firebase.database().setPersistenceCacheSizeBytes(100000000000000); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be less than 104857600 bytes (100MB)"); - return Promise.resolve(); - } + describe('setLoggingEnabled()', function () { + it('throws if enabled is not a boolean', async function () { + const { getDatabase, setLoggingEnabled } = databaseModular; + const db = getDatabase(); + + try { + setLoggingEnabled(db, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'enabled' must be a boolean value"); + return Promise.resolve(); + } + }); + + it('calls setLoggingEnabled successfully', async function () { + const { getDatabase, setLoggingEnabled } = databaseModular; + const db = getDatabase(); + + setLoggingEnabled(db, true); + setLoggingEnabled(db, false); + }); }); - it('calls setPersistenceCacheSizeBytes successfully', async function () { - firebase.database().setPersistenceCacheSizeBytes(1048576); // 1mb + describe('setPersistenceCacheSizeBytes()', function () { + it('throws if bytes is not a number', async function () { + const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; + const db = getDatabase(); + + try { + setPersistenceCacheSizeBytes(db, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bytes' must be a number value"); + return Promise.resolve(); + } + }); + + it('throws if bytes is less than 1MB', async function () { + const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; + const db = getDatabase(); + + try { + setPersistenceCacheSizeBytes(db, 1234); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bytes' must be greater than 1048576 bytes (1MB)"); + return Promise.resolve(); + } + }); + + it('throws if bytes is greater than 10MB', async function () { + const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; + const db = getDatabase(); + + try { + setPersistenceCacheSizeBytes(db, 100000000000000); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bytes' must be less than 104857600 bytes (100MB)"); + return Promise.resolve(); + } + }); + + it('calls setPersistenceCacheSizeBytes successfully', async function () { + const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; + const db = getDatabase(); + + setPersistenceCacheSizeBytes(db, 1048576); // 1mb + }); }); - }); - describe('getServerTime()', function () { - it('returns a valid date', async function () { - const date = firebase.database().getServerTime(); - date.getDate.should.be.Function(); + describe('getServerTime()', function () { + it('returns a valid date', async function () { + const { getDatabase, getServerTime } = databaseModular; + const db = getDatabase(); + + const date = getServerTime(db); + date.getDate.should.be.Function(); + }); }); - }); - describe('handles invalid references()', function () { - it('returns a valid date', async function () { - try { - firebase.database().ref('$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", - ); - return Promise.resolve(); - } + describe('handles invalid references()', function () { + it('returns a valid date', async function () { + const { getDatabase, ref } = databaseModular; + const db = getDatabase(); + + try { + ref(db, '$'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", + ); + return Promise.resolve(); + } + }); }); }); }); diff --git a/packages/database/e2e/database.modular.e2e.js b/packages/database/e2e/database.modular.e2e.js deleted file mode 100644 index 81e69ef0ae..0000000000 --- a/packages/database/e2e/database.modular.e2e.js +++ /dev/null @@ -1,439 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe('database()', function () { - describe('v8 compatibility', function () { - describe('namespace', function () { - it('accessible from firebase.app()', function () { - const app = firebase.app(); - should.exist(app.database); - app.database().app.should.eql(app); - }); - - it('supports multiple apps', async function () { - firebase.database().app.name.should.eql('[DEFAULT]'); - - firebase - .database(firebase.app('secondaryFromNative')) - .app.name.should.eql('secondaryFromNative'); - - firebase.app('secondaryFromNative').database().app.name.should.eql('secondaryFromNative'); - }); - }); - - describe('ref()', function () { - it('throws if path is not a string', async function () { - try { - firebase.database().ref({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); - - it('throws if path is not a valid string', async function () { - try { - firebase.database().ref('$$$$$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", - ); - return Promise.resolve(); - } - }); - }); - - describe('refFromURL()', function () { - it('throws if url is not a url', async function () { - try { - firebase.database().refFromURL('foobar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'url' must be a valid database URL"); - return Promise.resolve(); - } - }); - - it('throws if url from a different domain', async function () { - try { - firebase.database().refFromURL('https://foobar.firebaseio.com'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'url' must be the same domain as the current instance"); - return Promise.resolve(); - } - }); - - it('returns a reference', async function () { - const ref1 = firebase.database().refFromURL(firebase.database()._customUrlOrRegion); - const ref2 = firebase - .database() - .refFromURL(`${firebase.database()._customUrlOrRegion}/foo/bar`); - const ref3 = firebase - .database() - .refFromURL(`${firebase.database()._customUrlOrRegion}/foo/bar?baz=foo`); - should.equal(ref1.path, '/'); - should.equal(ref2.path, 'foo/bar'); - should.equal(ref3.path, 'foo/bar'); - }); - }); - - describe('goOnline()', function () { - it('calls goOnline successfully', async function () { - await firebase.database().goOnline(); - }); - }); - - describe('goOffline()', function () { - it('calls goOffline successfully', async function () { - // await Utils.sleep(5000); - await firebase.database().goOffline(); - - await firebase.database().goOnline(); - }); - }); - - describe('setPersistenceEnabled()', function () { - it('throws if enabled is not a boolean', async function () { - try { - firebase.database().setPersistenceEnabled('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'enabled' must be a boolean value"); - return Promise.resolve(); - } - }); - - it('calls setPersistenceEnabled successfully', async function () { - firebase.database().setPersistenceEnabled(true); - firebase.database().setPersistenceEnabled(false); - }); - }); - - describe('setLoggingEnabled()', function () { - it('throws if enabled is not a boolean', async function () { - try { - firebase.database().setLoggingEnabled('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'enabled' must be a boolean value"); - return Promise.resolve(); - } - }); - - it('calls setLoggingEnabled successfully', async function () { - firebase.database().setLoggingEnabled(true); - firebase.database().setLoggingEnabled(false); - }); - }); - - describe('setPersistenceCacheSizeBytes()', function () { - it('throws if bytes is not a number', async function () { - try { - firebase.database().setPersistenceCacheSizeBytes('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be a number value"); - return Promise.resolve(); - } - }); - - it('throws if bytes is less than 1MB', async function () { - try { - firebase.database().setPersistenceCacheSizeBytes(1234); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be greater than 1048576 bytes (1MB)"); - return Promise.resolve(); - } - }); - - it('throws if bytes is greater than 10MB', async function () { - try { - firebase.database().setPersistenceCacheSizeBytes(100000000000000); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be less than 104857600 bytes (100MB)"); - return Promise.resolve(); - } - }); - - it('calls setPersistenceCacheSizeBytes successfully', async function () { - firebase.database().setPersistenceCacheSizeBytes(1048576); // 1mb - }); - }); - - describe('getServerTime()', function () { - it('returns a valid date', async function () { - const date = firebase.database().getServerTime(); - date.getDate.should.be.Function(); - }); - }); - - describe('handles invalid references()', function () { - it('returns a valid date', async function () { - try { - firebase.database().ref('$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", - ); - return Promise.resolve(); - } - }); - }); - }); - - describe('modular', function () { - describe('namespace', function () { - it('accessible from getDatabase', function () { - const { getDatabase } = databaseModular; - - const app = firebase.app(); - const database = getDatabase(app); - should.exist(app.database); - database.app.should.eql(app); - }); - - it('supports multiple apps', async function () { - const { getDatabase } = databaseModular; - const database = getDatabase(); - const secondaryDatabase = getDatabase(firebase.app('secondaryFromNative')); - - database.app.name.should.eql('[DEFAULT]'); - - secondaryDatabase.app.name.should.eql('secondaryFromNative'); - - firebase.app('secondaryFromNative').database().app.name.should.eql('secondaryFromNative'); - }); - }); - - describe('ref()', function () { - it('throws if path is not a string', async function () { - const { getDatabase, ref } = databaseModular; - const db = getDatabase(); - - try { - ref(db, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); - - it('throws if path is not a valid string', async function () { - const { getDatabase, ref } = databaseModular; - const db = getDatabase(); - - try { - ref(db, '$$$$$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", - ); - return Promise.resolve(); - } - }); - }); - - describe('refFromURL()', function () { - it('throws if url is not a url', async function () { - const { getDatabase, refFromURL } = databaseModular; - const db = getDatabase(); - - try { - refFromURL(db, 'foobar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'url' must be a valid database URL"); - return Promise.resolve(); - } - }); - - it('throws if url from a different domain', async function () { - const { getDatabase, refFromURL } = databaseModular; - const db = getDatabase(); - - try { - refFromURL(db, 'https://foobar.firebaseio.com'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'url' must be the same domain as the current instance"); - return Promise.resolve(); - } - }); - - it('returns a reference', async function () { - const { getDatabase, refFromURL } = databaseModular; - const db = getDatabase(); - - const ref1 = refFromURL(db, db._customUrlOrRegion); - const ref2 = refFromURL(db, `${db._customUrlOrRegion}/foo/bar`); - const ref3 = refFromURL(db, `${db._customUrlOrRegion}/foo/bar?baz=foo`); - - should.equal(ref1.path, '/'); - should.equal(ref2.path, 'foo/bar'); - should.equal(ref3.path, 'foo/bar'); - }); - }); - - describe('goOnline()', function () { - it('calls goOnline successfully', async function () { - const { getDatabase, goOnline } = databaseModular; - await goOnline(getDatabase()); - }); - }); - - describe('goOffline()', function () { - it('calls goOffline successfully', async function () { - const { getDatabase, goOffline, goOnline } = databaseModular; - const db = getDatabase(); - - // await Utils.sleep(5000); - await goOffline(db); - - await goOnline(db); - }); - }); - - describe('setPersistenceEnabled()', function () { - it('throws if enabled is not a boolean', async function () { - const { getDatabase, setPersistenceEnabled } = databaseModular; - const db = getDatabase(); - - try { - setPersistenceEnabled(db, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'enabled' must be a boolean value"); - return Promise.resolve(); - } - }); - - it('calls setPersistenceEnabled successfully', async function () { - const { getDatabase, setPersistenceEnabled } = databaseModular; - const db = getDatabase(); - - setPersistenceEnabled(db, true); - setPersistenceEnabled(db, false); - }); - }); - - describe('setLoggingEnabled()', function () { - it('throws if enabled is not a boolean', async function () { - const { getDatabase, setLoggingEnabled } = databaseModular; - const db = getDatabase(); - - try { - setLoggingEnabled(db, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'enabled' must be a boolean value"); - return Promise.resolve(); - } - }); - - it('calls setLoggingEnabled successfully', async function () { - const { getDatabase, setLoggingEnabled } = databaseModular; - const db = getDatabase(); - - setLoggingEnabled(db, true); - setLoggingEnabled(db, false); - }); - }); - - describe('setPersistenceCacheSizeBytes()', function () { - it('throws if bytes is not a number', async function () { - const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; - const db = getDatabase(); - - try { - setPersistenceCacheSizeBytes(db, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be a number value"); - return Promise.resolve(); - } - }); - - it('throws if bytes is less than 1MB', async function () { - const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; - const db = getDatabase(); - - try { - setPersistenceCacheSizeBytes(db, 1234); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be greater than 1048576 bytes (1MB)"); - return Promise.resolve(); - } - }); - - it('throws if bytes is greater than 10MB', async function () { - const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; - const db = getDatabase(); - - try { - setPersistenceCacheSizeBytes(db, 100000000000000); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bytes' must be less than 104857600 bytes (100MB)"); - return Promise.resolve(); - } - }); - - it('calls setPersistenceCacheSizeBytes successfully', async function () { - const { getDatabase, setPersistenceCacheSizeBytes } = databaseModular; - const db = getDatabase(); - - setPersistenceCacheSizeBytes(db, 1048576); // 1mb - }); - }); - - describe('getServerTime()', function () { - it('returns a valid date', async function () { - const { getDatabase, getServerTime } = databaseModular; - const db = getDatabase(); - - const date = getServerTime(db); - date.getDate.should.be.Function(); - }); - }); - - describe('handles invalid references()', function () { - it('returns a valid date', async function () { - const { getDatabase, ref } = databaseModular; - const db = getDatabase(); - - try { - ref(db, '$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - "Paths must be non-empty strings and can't contain #, $, [, ], ' or ?", - ); - return Promise.resolve(); - } - }); - }); - }); -}); diff --git a/packages/database/e2e/internal/connected.e2e.js b/packages/database/e2e/internal/connected.e2e.js index dc5209a291..86faa1183a 100644 --- a/packages/database/e2e/internal/connected.e2e.js +++ b/packages/database/e2e/internal/connected.e2e.js @@ -22,38 +22,87 @@ describe("database().ref('.info/connected')", function () { before(async function () { await firebase.database().goOnline(); }); - after(async function () { - await firebase.database().goOnline(); - }); - xit('returns true when used with once', async function () { - const snapshot = await firebase.database().ref('.info/connected').once('value'); - snapshot.val().should.equal(true); - }); + describe('v8 compatibility', function () { + after(async function () { + await firebase.database().goOnline(); + }); + + xit('returns true when used with once', async function () { + const snapshot = await firebase.database().ref('.info/connected').once('value'); + snapshot.val().should.equal(true); + }); - xit('returns true when used with once with a previous call', async function () { - await firebase.database().ref(`${TEST_PATH}/foo`).once('value'); - const snapshot = await firebase.database().ref('.info/connected').once('value'); - snapshot.val().should.equal(true); + xit('returns true when used with once with a previous call', async function () { + await firebase.database().ref(`${TEST_PATH}/foo`).once('value'); + const snapshot = await firebase.database().ref('.info/connected').once('value'); + snapshot.val().should.equal(true); + }); + + // FIXME on android this can work against the emulator + // on iOS it doesn't work at all ? + xit('subscribes to online state', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref('.info/connected'); + const handler = $ => { + callback($.val()); + }; + + ref.on('value', handler); + await firebase.database().goOffline(); + await Utils.sleep(1000); // FIXME why is this sleep needed here? callback is called immediately + await firebase.database().goOnline(); + ref.off('value', handler); + + await Utils.spyToBeCalledTimesAsync(callback, 2); + callback.getCall(0).args[0].should.equal(false); + callback.getCall(1).args[0].should.equal(true); + }); }); - // FIXME on android this can work against the emulator - // on iOS it doesn't work at all ? - xit('subscribes to online state', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref('.info/connected'); - const handler = $ => { - callback($.val()); - }; - - ref.on('value', handler); - await firebase.database().goOffline(); - await Utils.sleep(1000); // FIXME why is this sleep needed here? callback is called immediately - await firebase.database().goOnline(); - ref.off('value', handler); + describe('modular', function () { + after(async function () { + const { getDatabase, goOnline } = databaseModular; + + await goOnline(getDatabase()); + }); + + xit('returns true when used with once', async function () { + const { getDatabase, ref, get } = databaseModular; + + const snapshot = await get(ref(getDatabase(), '.info/connected'), dbRef); + snapshot.val().should.equal(true); + }); + + xit('returns true when used with once with a previous call', async function () { + const { getDatabase, ref, get } = databaseModular; + + await get(ref(getDatabase(), `${TEST_PATH}/foo`)); + const snapshot = await firebase.database().ref('.info/connected').once('value'); + snapshot.val().should.equal(true); + }); + + // FIXME on android this can work against the emulator + // on iOS it doesn't work at all ? + xit('subscribes to online state', async function () { + const { getDatabase, ref, onValue, goOffline, goOnline, off } = databaseModular; + const db = getDatabase(); + + const callback = sinon.spy(); + const dbRef = ref(db, '.info/connected'); + const handler = $ => { + callback($.val()); + }; + + onValue(dbRef, handler); + await goOffline(db); + await Utils.sleep(1000); // FIXME why is this sleep needed here? callback is called immediately + await goOnline(db); + off('value', handler); - await Utils.spyToBeCalledTimesAsync(callback, 2); - callback.getCall(0).args[0].should.equal(false); - callback.getCall(1).args[0].should.equal(true); + await Utils.spyToBeCalledTimesAsync(callback, 2); + callback.getCall(0).args[0].should.equal(false); + callback.getCall(1).args[0].should.equal(true); + }); }); }); diff --git a/packages/database/e2e/internal/connected.modular.e2e.js b/packages/database/e2e/internal/connected.modular.e2e.js deleted file mode 100644 index 86faa1183a..0000000000 --- a/packages/database/e2e/internal/connected.modular.e2e.js +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH } = require('../helpers'); -const TEST_PATH = `${PATH}/connected`; - -describe("database().ref('.info/connected')", function () { - before(async function () { - await firebase.database().goOnline(); - }); - - describe('v8 compatibility', function () { - after(async function () { - await firebase.database().goOnline(); - }); - - xit('returns true when used with once', async function () { - const snapshot = await firebase.database().ref('.info/connected').once('value'); - snapshot.val().should.equal(true); - }); - - xit('returns true when used with once with a previous call', async function () { - await firebase.database().ref(`${TEST_PATH}/foo`).once('value'); - const snapshot = await firebase.database().ref('.info/connected').once('value'); - snapshot.val().should.equal(true); - }); - - // FIXME on android this can work against the emulator - // on iOS it doesn't work at all ? - xit('subscribes to online state', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref('.info/connected'); - const handler = $ => { - callback($.val()); - }; - - ref.on('value', handler); - await firebase.database().goOffline(); - await Utils.sleep(1000); // FIXME why is this sleep needed here? callback is called immediately - await firebase.database().goOnline(); - ref.off('value', handler); - - await Utils.spyToBeCalledTimesAsync(callback, 2); - callback.getCall(0).args[0].should.equal(false); - callback.getCall(1).args[0].should.equal(true); - }); - }); - - describe('modular', function () { - after(async function () { - const { getDatabase, goOnline } = databaseModular; - - await goOnline(getDatabase()); - }); - - xit('returns true when used with once', async function () { - const { getDatabase, ref, get } = databaseModular; - - const snapshot = await get(ref(getDatabase(), '.info/connected'), dbRef); - snapshot.val().should.equal(true); - }); - - xit('returns true when used with once with a previous call', async function () { - const { getDatabase, ref, get } = databaseModular; - - await get(ref(getDatabase(), `${TEST_PATH}/foo`)); - const snapshot = await firebase.database().ref('.info/connected').once('value'); - snapshot.val().should.equal(true); - }); - - // FIXME on android this can work against the emulator - // on iOS it doesn't work at all ? - xit('subscribes to online state', async function () { - const { getDatabase, ref, onValue, goOffline, goOnline, off } = databaseModular; - const db = getDatabase(); - - const callback = sinon.spy(); - const dbRef = ref(db, '.info/connected'); - const handler = $ => { - callback($.val()); - }; - - onValue(dbRef, handler); - await goOffline(db); - await Utils.sleep(1000); // FIXME why is this sleep needed here? callback is called immediately - await goOnline(db); - off('value', handler); - - await Utils.spyToBeCalledTimesAsync(callback, 2); - callback.getCall(0).args[0].should.equal(false); - callback.getCall(1).args[0].should.equal(true); - }); - }); -}); diff --git a/packages/database/e2e/internal/serverTimeOffset.e2e.js b/packages/database/e2e/internal/serverTimeOffset.e2e.js index d4b7f86b20..32954fe28f 100644 --- a/packages/database/e2e/internal/serverTimeOffset.e2e.js +++ b/packages/database/e2e/internal/serverTimeOffset.e2e.js @@ -16,9 +16,21 @@ */ describe("database().ref('.info/serverTimeOffset')", function () { - it('returns a valid number value', async function () { - const snapshot = await firebase.database().ref('.info/serverTimeOffset').once('value'); + describe('v8 compatibility', function () { + it('returns a valid number value', async function () { + const snapshot = await firebase.database().ref('.info/serverTimeOffset').once('value'); - should.equal(typeof snapshot.val(), 'number'); + should.equal(typeof snapshot.val(), 'number'); + }); + }); + + describe('modular', function () { + it('returns a valid number value', async function () { + const { getDatabase, ref, get } = databaseModular; + + const snapshot = await get(ref(getDatabase(), '.info/serverTimeOffset')); + + should.equal(typeof snapshot.val(), 'number'); + }); }); }); diff --git a/packages/database/e2e/internal/serverTimeOffset.modular.e2e.js b/packages/database/e2e/internal/serverTimeOffset.modular.e2e.js deleted file mode 100644 index 32954fe28f..0000000000 --- a/packages/database/e2e/internal/serverTimeOffset.modular.e2e.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe("database().ref('.info/serverTimeOffset')", function () { - describe('v8 compatibility', function () { - it('returns a valid number value', async function () { - const snapshot = await firebase.database().ref('.info/serverTimeOffset').once('value'); - - should.equal(typeof snapshot.val(), 'number'); - }); - }); - - describe('modular', function () { - it('returns a valid number value', async function () { - const { getDatabase, ref, get } = databaseModular; - - const snapshot = await get(ref(getDatabase(), '.info/serverTimeOffset')); - - should.equal(typeof snapshot.val(), 'number'); - }); - }); -}); diff --git a/packages/database/e2e/issues.e2e.js b/packages/database/e2e/issues.e2e.js index 4945a6cbea..85af01eebc 100644 --- a/packages/database/e2e/issues.e2e.js +++ b/packages/database/e2e/issues.e2e.js @@ -27,137 +27,289 @@ describe('database issues', function () { await wipe(TEST_PATH); }); - // FIXME requires a second database set up locally, full app initialization etc - xit('#2813 should return a null snapshot key if path is root', async function () { - firebase.database('https://react-native-firebase-testing-db2.firebaseio.com'); - const ref = firebase - .app() - .database('https://react-native-firebase-testing-db2.firebaseio.com') - .ref(); - const snapshot = await ref.once('value'); - should.equal(snapshot.key, null); - }); + describe('v8 compatibility', function () { + // FIXME requires a second database set up locally, full app initialization etc + xit('#2813 should return a null snapshot key if path is root', async function () { + firebase.database('https://react-native-firebase-testing-db2.firebaseio.com'); + const ref = firebase + .app() + .database('https://react-native-firebase-testing-db2.firebaseio.com') + .ref(); + const snapshot = await ref.once('value'); + should.equal(snapshot.key, null); + }); - it('#2833 should not mutate modifiers ordering', async function () { - const callback = sinon.spy(); - const testRef = firebase - .database() - .ref() - .child(TEST_PATH) - .orderByChild('disabled') - .equalTo(false); + it('#2833 should not mutate modifiers ordering', async function () { + const callback = sinon.spy(); + const testRef = firebase + .database() + .ref() + .child(TEST_PATH) + .orderByChild('disabled') + .equalTo(false); - testRef._modifiers.toString().should.be.a.String(); - testRef._modifiers.toArray()[0].name.should.equal('orderByChild'); + testRef._modifiers.toString().should.be.a.String(); + testRef._modifiers.toArray()[0].name.should.equal('orderByChild'); - testRef.on('value', snapshot => { - callback(snapshot.val()); - }); + testRef.on('value', snapshot => { + callback(snapshot.val()); + }); - await Utils.spyToBeCalledOnceAsync(callback, 3000); + await Utils.spyToBeCalledOnceAsync(callback, 3000); - testRef.off('value'); - }); - - it('#100 array should return null where key is missing', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/issue_100`); - - const data = { - 1: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - 2: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - 3: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - }; - - await ref.set(data); - const snapshot = await ref.once('value'); - - snapshot - .val() - .should.eql( - jet.contextify([ - null, - jet.contextify(data[1]), - jet.contextify(data[2]), - jet.contextify(data[3]), - ]), - ); - }); + testRef.off('value'); + }); - describe('#108 filters correctly by float values', function () { - it('returns filtered results', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/issue_108/filter`); + it('#100 array should return null where key is missing', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/issue_100`); const data = { - foobar: { - name: 'Foobar Pizzas', - latitude: 34.1013717, + 1: { + someKey: 'someValue', + someOtherKey: 'someOtherValue', }, - notTheFoobar: { - name: "Not the pizza you're looking for", - latitude: 34.456787, + 2: { + someKey: 'someValue', + someOtherKey: 'someOtherValue', }, - notAFloat: { - name: 'Not a float', - latitude: 37, + 3: { + someKey: 'someValue', + someOtherKey: 'someOtherValue', }, }; await ref.set(data); - const snapshot = await ref - .orderByChild('latitude') - .startAt(34.00867000999119) - .endAt(34.17462960866099) - .once('value'); + const snapshot = await ref.once('value'); + + snapshot + .val() + .should.eql( + jet.contextify([ + null, + jet.contextify(data[1]), + jet.contextify(data[2]), + jet.contextify(data[3]), + ]), + ); + }); + + describe('#108 filters correctly by float values', function () { + it('returns filtered results', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/issue_108/filter`); + + const data = { + foobar: { + name: 'Foobar Pizzas', + latitude: 34.1013717, + }, + notTheFoobar: { + name: "Not the pizza you're looking for", + latitude: 34.456787, + }, + notAFloat: { + name: 'Not a float', + latitude: 37, + }, + }; + + await ref.set(data); + const snapshot = await ref + .orderByChild('latitude') + .startAt(34.00867000999119) + .endAt(34.17462960866099) + .once('value'); - const val = snapshot.val(); - val.foobar.should.eql(jet.contextify(data.foobar)); + const val = snapshot.val(); + val.foobar.should.eql(jet.contextify(data.foobar)); - should.equal(Object.keys(val).length, 1); + should.equal(Object.keys(val).length, 1); + }); + + it('returns correct results when not using float values', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/issue_108/integer`); + + const data = { + foobar: { + name: 'Foobar Pizzas', + latitude: 34.1013717, + }, + notTheFoobar: { + name: "Not the pizza you're looking for", + latitude: 34.456787, + }, + notAFloat: { + name: 'Not a float', + latitude: 37, + }, + }; + + await ref.set(data); + const snapshot = await ref.orderByChild('latitude').equalTo(37).once('value'); + + const val = snapshot.val(); + + val.notAFloat.should.eql(jet.contextify(data.notAFloat)); + + should.equal(Object.keys(val).length, 1); + }); + }); + + it('#489 reutrns long numbers correctly', async function () { + const LONG = 1508777379000; + const ref = firebase.database().ref(`${TEST_PATH}/issue_489`); + await ref.set(LONG); + const snapshot = await ref.once('value'); + snapshot.val().should.eql(LONG); + }); + }); + + describe('modular', function () { + // FIXME requires a second database set up locally, full app initialization etc + xit('#2813 should return a null snapshot key if path is root', async function () { + const { getDatabase, ref, get } = databaseModular; + + const db = getDatabase( + /* takes default firebase.app() */ null, + 'https://react-native-firebase-testing-db2.firebaseio.com', + ); + const dbRef = ref(db); + const snapshot = await get(dbRef); + should.equal(snapshot.key, null); }); - it('returns correct results when not using float values', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/issue_108/integer`); + it('#2833 should not mutate modifiers ordering', async function () { + const { getDatabase, ref, child, query, equalTo, orderByChild, onValue } = databaseModular; + + const callback = sinon.spy(); + const testRef = query( + child(ref(getDatabase()), TEST_PATH), + orderByChild('disabled'), + equalTo(false), + ); + + testRef._modifiers.toString().should.be.a.String(); + testRef._modifiers.toArray()[0].name.should.equal('orderByChild'); + + const unsubscribe = onValue(testRef, snapshot => { + callback(snapshot.val()); + }); + + await Utils.spyToBeCalledOnceAsync(callback, 3000); + + unsubscribe(); + }); + + it('#100 array should return null where key is missing', async function () { + const { getDatabase, ref, set, get } = databaseModular; + + const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_100`); const data = { - foobar: { - name: 'Foobar Pizzas', - latitude: 34.1013717, + 1: { + someKey: 'someValue', + someOtherKey: 'someOtherValue', }, - notTheFoobar: { - name: "Not the pizza you're looking for", - latitude: 34.456787, + 2: { + someKey: 'someValue', + someOtherKey: 'someOtherValue', }, - notAFloat: { - name: 'Not a float', - latitude: 37, + 3: { + someKey: 'someValue', + someOtherKey: 'someOtherValue', }, }; - await ref.set(data); - const snapshot = await ref.orderByChild('latitude').equalTo(37).once('value'); + await set(dbRef, data); + const snapshot = await get(dbRef); + + snapshot + .val() + .should.eql( + jet.contextify([ + null, + jet.contextify(data[1]), + jet.contextify(data[2]), + jet.contextify(data[3]), + ]), + ); + }); + + describe('#108 filters correctly by float values', function () { + it('returns filtered results', async function () { + const { getDatabase, ref, set, get, query, orderByChild, startAt, endAt } = databaseModular; - const val = snapshot.val(); + const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_108/filter`); - val.notAFloat.should.eql(jet.contextify(data.notAFloat)); + const data = { + foobar: { + name: 'Foobar Pizzas', + latitude: 34.1013717, + }, + notTheFoobar: { + name: "Not the pizza you're looking for", + latitude: 34.456787, + }, + notAFloat: { + name: 'Not a float', + latitude: 37, + }, + }; - should.equal(Object.keys(val).length, 1); + await set(dbRef, data); + const snapshot = await get( + query( + dbRef, + orderByChild('latitude'), + startAt(34.00867000999119), + endAt(34.17462960866099), + ), + ); + + const val = snapshot.val(); + val.foobar.should.eql(jet.contextify(data.foobar)); + + should.equal(Object.keys(val).length, 1); + }); + + it('returns correct results when not using float values', async function () { + const { getDatabase, ref, set, get, query, orderByChild, equalTo } = databaseModular; + + const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_108/integer`); + + const data = { + foobar: { + name: 'Foobar Pizzas', + latitude: 34.1013717, + }, + notTheFoobar: { + name: "Not the pizza you're looking for", + latitude: 34.456787, + }, + notAFloat: { + name: 'Not a float', + latitude: 37, + }, + }; + + await set(dbRef, data); + const snapshot = await get(query(dbRef, orderByChild('latitude'), equalTo(37))); + + const val = snapshot.val(); + + val.notAFloat.should.eql(jet.contextify(data.notAFloat)); + + should.equal(Object.keys(val).length, 1); + }); }); - }); - it('#489 reutrns long numbers correctly', async function () { - const LONG = 1508777379000; - const ref = firebase.database().ref(`${TEST_PATH}/issue_489`); - await ref.set(LONG); - const snapshot = await ref.once('value'); - snapshot.val().should.eql(LONG); + it('#489 reutrns long numbers correctly', async function () { + const { getDatabase, ref, set, get } = databaseModular; + + const LONG = 1508777379000; + const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_489`); + await set(dbRef, LONG); + const snapshot = await get(dbRef); + snapshot.val().should.eql(LONG); + }); }); }); diff --git a/packages/database/e2e/issues.modular.e2e.js b/packages/database/e2e/issues.modular.e2e.js deleted file mode 100644 index 85af01eebc..0000000000 --- a/packages/database/e2e/issues.modular.e2e.js +++ /dev/null @@ -1,315 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('./helpers'); - -const TEST_PATH = `${PATH}/issues`; - -describe('database issues', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - // FIXME requires a second database set up locally, full app initialization etc - xit('#2813 should return a null snapshot key if path is root', async function () { - firebase.database('https://react-native-firebase-testing-db2.firebaseio.com'); - const ref = firebase - .app() - .database('https://react-native-firebase-testing-db2.firebaseio.com') - .ref(); - const snapshot = await ref.once('value'); - should.equal(snapshot.key, null); - }); - - it('#2833 should not mutate modifiers ordering', async function () { - const callback = sinon.spy(); - const testRef = firebase - .database() - .ref() - .child(TEST_PATH) - .orderByChild('disabled') - .equalTo(false); - - testRef._modifiers.toString().should.be.a.String(); - testRef._modifiers.toArray()[0].name.should.equal('orderByChild'); - - testRef.on('value', snapshot => { - callback(snapshot.val()); - }); - - await Utils.spyToBeCalledOnceAsync(callback, 3000); - - testRef.off('value'); - }); - - it('#100 array should return null where key is missing', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/issue_100`); - - const data = { - 1: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - 2: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - 3: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - }; - - await ref.set(data); - const snapshot = await ref.once('value'); - - snapshot - .val() - .should.eql( - jet.contextify([ - null, - jet.contextify(data[1]), - jet.contextify(data[2]), - jet.contextify(data[3]), - ]), - ); - }); - - describe('#108 filters correctly by float values', function () { - it('returns filtered results', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/issue_108/filter`); - - const data = { - foobar: { - name: 'Foobar Pizzas', - latitude: 34.1013717, - }, - notTheFoobar: { - name: "Not the pizza you're looking for", - latitude: 34.456787, - }, - notAFloat: { - name: 'Not a float', - latitude: 37, - }, - }; - - await ref.set(data); - const snapshot = await ref - .orderByChild('latitude') - .startAt(34.00867000999119) - .endAt(34.17462960866099) - .once('value'); - - const val = snapshot.val(); - val.foobar.should.eql(jet.contextify(data.foobar)); - - should.equal(Object.keys(val).length, 1); - }); - - it('returns correct results when not using float values', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/issue_108/integer`); - - const data = { - foobar: { - name: 'Foobar Pizzas', - latitude: 34.1013717, - }, - notTheFoobar: { - name: "Not the pizza you're looking for", - latitude: 34.456787, - }, - notAFloat: { - name: 'Not a float', - latitude: 37, - }, - }; - - await ref.set(data); - const snapshot = await ref.orderByChild('latitude').equalTo(37).once('value'); - - const val = snapshot.val(); - - val.notAFloat.should.eql(jet.contextify(data.notAFloat)); - - should.equal(Object.keys(val).length, 1); - }); - }); - - it('#489 reutrns long numbers correctly', async function () { - const LONG = 1508777379000; - const ref = firebase.database().ref(`${TEST_PATH}/issue_489`); - await ref.set(LONG); - const snapshot = await ref.once('value'); - snapshot.val().should.eql(LONG); - }); - }); - - describe('modular', function () { - // FIXME requires a second database set up locally, full app initialization etc - xit('#2813 should return a null snapshot key if path is root', async function () { - const { getDatabase, ref, get } = databaseModular; - - const db = getDatabase( - /* takes default firebase.app() */ null, - 'https://react-native-firebase-testing-db2.firebaseio.com', - ); - const dbRef = ref(db); - const snapshot = await get(dbRef); - should.equal(snapshot.key, null); - }); - - it('#2833 should not mutate modifiers ordering', async function () { - const { getDatabase, ref, child, query, equalTo, orderByChild, onValue } = databaseModular; - - const callback = sinon.spy(); - const testRef = query( - child(ref(getDatabase()), TEST_PATH), - orderByChild('disabled'), - equalTo(false), - ); - - testRef._modifiers.toString().should.be.a.String(); - testRef._modifiers.toArray()[0].name.should.equal('orderByChild'); - - const unsubscribe = onValue(testRef, snapshot => { - callback(snapshot.val()); - }); - - await Utils.spyToBeCalledOnceAsync(callback, 3000); - - unsubscribe(); - }); - - it('#100 array should return null where key is missing', async function () { - const { getDatabase, ref, set, get } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_100`); - - const data = { - 1: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - 2: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - 3: { - someKey: 'someValue', - someOtherKey: 'someOtherValue', - }, - }; - - await set(dbRef, data); - const snapshot = await get(dbRef); - - snapshot - .val() - .should.eql( - jet.contextify([ - null, - jet.contextify(data[1]), - jet.contextify(data[2]), - jet.contextify(data[3]), - ]), - ); - }); - - describe('#108 filters correctly by float values', function () { - it('returns filtered results', async function () { - const { getDatabase, ref, set, get, query, orderByChild, startAt, endAt } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_108/filter`); - - const data = { - foobar: { - name: 'Foobar Pizzas', - latitude: 34.1013717, - }, - notTheFoobar: { - name: "Not the pizza you're looking for", - latitude: 34.456787, - }, - notAFloat: { - name: 'Not a float', - latitude: 37, - }, - }; - - await set(dbRef, data); - const snapshot = await get( - query( - dbRef, - orderByChild('latitude'), - startAt(34.00867000999119), - endAt(34.17462960866099), - ), - ); - - const val = snapshot.val(); - val.foobar.should.eql(jet.contextify(data.foobar)); - - should.equal(Object.keys(val).length, 1); - }); - - it('returns correct results when not using float values', async function () { - const { getDatabase, ref, set, get, query, orderByChild, equalTo } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_108/integer`); - - const data = { - foobar: { - name: 'Foobar Pizzas', - latitude: 34.1013717, - }, - notTheFoobar: { - name: "Not the pizza you're looking for", - latitude: 34.456787, - }, - notAFloat: { - name: 'Not a float', - latitude: 37, - }, - }; - - await set(dbRef, data); - const snapshot = await get(query(dbRef, orderByChild('latitude'), equalTo(37))); - - const val = snapshot.val(); - - val.notAFloat.should.eql(jet.contextify(data.notAFloat)); - - should.equal(Object.keys(val).length, 1); - }); - }); - - it('#489 reutrns long numbers correctly', async function () { - const { getDatabase, ref, set, get } = databaseModular; - - const LONG = 1508777379000; - const dbRef = ref(getDatabase(), `${TEST_PATH}/issue_489`); - await set(dbRef, LONG); - const snapshot = await get(dbRef); - snapshot.val().should.eql(LONG); - }); - }); -}); diff --git a/packages/database/e2e/onDisconnect/cancel.e2e.js b/packages/database/e2e/onDisconnect/cancel.e2e.js index 8de9da3c01..bcde6b2d7d 100644 --- a/packages/database/e2e/onDisconnect/cancel.e2e.js +++ b/packages/database/e2e/onDisconnect/cancel.e2e.js @@ -24,49 +24,111 @@ describe('database().ref().onDisconnect().cancel()', function () { await wipe(TEST_PATH); }); - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); + describe('v8 compatibility', function () { + afterEach(async function () { + // Ensures the db is online before running each test + await firebase.database().goOnline(); + }); - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.cancel('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.cancel('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + it('cancels all previously queued events', async function () { + const ref = firebase.database().ref(TEST_PATH); + + await ref.set('foobar'); + const value = Date.now(); - it('cancels all previously queued events', async function () { - const ref = firebase.database().ref(TEST_PATH); + await ref.onDisconnect().set(value); + await ref.onDisconnect().cancel(); + await firebase.database().goOffline(); + await firebase.database().goOnline(); - await ref.set('foobar'); - const value = Date.now(); + const snapshot = await ref.once('value'); + snapshot.val().should.eql('foobar'); + }); - await ref.onDisconnect().set(value); - await ref.onDisconnect().cancel(); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + it('calls back to the onComplete function', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH); - const snapshot = await ref.once('value'); - snapshot.val().should.eql('foobar'); + // Set an initial value + await ref.set('foo'); + + await ref.onDisconnect().set('bar'); + await ref.onDisconnect().cancel(callback); + await firebase.database().goOffline(); + await firebase.database().goOnline(); + + callback.should.be.calledOnce(); + }); }); - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + afterEach(async function () { + const { getDatabase, goOnline } = databaseModular; + const db = getDatabase(); + + // Ensures the db is online before running each test + await goOnline(db); + }); + + it('throws if onComplete is not a function', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.cancel('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + it('cancels all previously queued events', async function () { + const { getDatabase, ref, onDisconnect, goOffline, goOnline, get } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + await dbRef.set('foobar'); + const value = Date.now(); + + await onDisconnect(dbRef).set(value); + await onDisconnect(dbRef).cancel(); + await goOffline(db); + await goOnline(db); + + const snapshot = await get(dbRef); + snapshot.val().should.eql('foobar'); + }); + + it('calls back to the onComplete function', async function () { + const { getDatabase, ref, onDisconnect, set, goOffline, goOnline } = databaseModular; + const db = getDatabase(); + + const callback = sinon.spy(); + const dbRef = ref(db, TEST_PATH); - // Set an initial value - await ref.set('foo'); + // Set an initial value + await set(dbRef, 'foo'); - await ref.onDisconnect().set('bar'); - await ref.onDisconnect().cancel(callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + await onDisconnect(dbRef).set('bar'); + await onDisconnect(dbRef).cancel(callback); + await goOffline(db); + await goOnline(db); - callback.should.be.calledOnce(); + callback.should.be.calledOnce(); + }); }); }); diff --git a/packages/database/e2e/onDisconnect/cancel.modular.e2e.js b/packages/database/e2e/onDisconnect/cancel.modular.e2e.js deleted file mode 100644 index bcde6b2d7d..0000000000 --- a/packages/database/e2e/onDisconnect/cancel.modular.e2e.js +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/onDisconnectCancel`; - -describe('database().ref().onDisconnect().cancel()', function () { - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); - - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.cancel('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('cancels all previously queued events', async function () { - const ref = firebase.database().ref(TEST_PATH); - - await ref.set('foobar'); - const value = Date.now(); - - await ref.onDisconnect().set(value); - await ref.onDisconnect().cancel(); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - const snapshot = await ref.once('value'); - snapshot.val().should.eql('foobar'); - }); - - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); - - // Set an initial value - await ref.set('foo'); - - await ref.onDisconnect().set('bar'); - await ref.onDisconnect().cancel(callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - callback.should.be.calledOnce(); - }); - }); - - describe('modular', function () { - afterEach(async function () { - const { getDatabase, goOnline } = databaseModular; - const db = getDatabase(); - - // Ensures the db is online before running each test - await goOnline(db); - }); - - it('throws if onComplete is not a function', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.cancel('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('cancels all previously queued events', async function () { - const { getDatabase, ref, onDisconnect, goOffline, goOnline, get } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - await dbRef.set('foobar'); - const value = Date.now(); - - await onDisconnect(dbRef).set(value); - await onDisconnect(dbRef).cancel(); - await goOffline(db); - await goOnline(db); - - const snapshot = await get(dbRef); - snapshot.val().should.eql('foobar'); - }); - - it('calls back to the onComplete function', async function () { - const { getDatabase, ref, onDisconnect, set, goOffline, goOnline } = databaseModular; - const db = getDatabase(); - - const callback = sinon.spy(); - const dbRef = ref(db, TEST_PATH); - - // Set an initial value - await set(dbRef, 'foo'); - - await onDisconnect(dbRef).set('bar'); - await onDisconnect(dbRef).cancel(callback); - await goOffline(db); - await goOnline(db); - - callback.should.be.calledOnce(); - }); - }); -}); diff --git a/packages/database/e2e/onDisconnect/remove.e2e.js b/packages/database/e2e/onDisconnect/remove.e2e.js index 6e0e9ab796..93963abb48 100644 --- a/packages/database/e2e/onDisconnect/remove.e2e.js +++ b/packages/database/e2e/onDisconnect/remove.e2e.js @@ -24,43 +24,102 @@ describe('database().ref().onDisconnect().remove()', function () { await wipe(TEST_PATH); }); - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); + describe('v8 compatibility', function () { + afterEach(async function () { + // Ensures the db is online before running each test + await firebase.database().goOnline(); + }); - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.remove('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.remove('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + xit('removes a node whilst offline', async function () { + const ref = firebase.database().ref(TEST_PATH).child('removeMe'); + await ref.set('foobar'); + await ref.onDisconnect().remove(); + await firebase.database().goOffline(); + await firebase.database().goOnline(); + const snapshot = await ref.once('value'); + snapshot.exists().should.eql(false); + }); + + it('calls back to the onComplete function', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH).child('removeMe'); - xit('removes a node whilst offline', async function () { - const ref = firebase.database().ref(TEST_PATH).child('removeMe'); - await ref.set('foobar'); - await ref.onDisconnect().remove(); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - const snapshot = await ref.once('value'); - snapshot.exists().should.eql(false); + // Set an initial value + await ref.set('foo'); + + await ref.onDisconnect().remove(callback); + await firebase.database().goOffline(); + await firebase.database().goOnline(); + + callback.should.be.calledOnce(); + }); }); - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('removeMe'); + describe('modular', function () { + afterEach(async function () { + const { getDatabase, goOnline } = databaseModular; + const db = getDatabase(); + + // Ensures the db is online before running each test + await goOnline(db); + }); + + it('throws if onComplete is not a function', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.remove('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + xit('removes a node whilst offline', async function () { + const { getDatabase, ref, child, onDisconnect, goOffline, goOnline, get } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + const childRef = child(dbRef, 'removeMe'); + + await childRef.set('foobar'); + await onDisconnect(childRef).remove(); + await goOffline(db); + await goOnline(db); + const snapshot = await get(childRef); + snapshot.exists().should.eql(false); + }); + + it('calls back to the onComplete function', async function () { + const callback = sinon.spy(); + + const { getDatabase, ref, child, onDisconnect, goOffline, goOnline } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + const childRef = child(dbRef, 'removeMe'); - // Set an initial value - await ref.set('foo'); + // Set an initial value + await childRef.set('foo'); - await ref.onDisconnect().remove(callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + await onDisconnect(childRef).remove(callback); + await goOffline(db); + await goOnline(db); - callback.should.be.calledOnce(); + callback.should.be.calledOnce(); + }); }); }); diff --git a/packages/database/e2e/onDisconnect/remove.modular.e2e.js b/packages/database/e2e/onDisconnect/remove.modular.e2e.js deleted file mode 100644 index 93963abb48..0000000000 --- a/packages/database/e2e/onDisconnect/remove.modular.e2e.js +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/onDisconnectRemove`; - -describe('database().ref().onDisconnect().remove()', function () { - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); - - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.remove('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('removes a node whilst offline', async function () { - const ref = firebase.database().ref(TEST_PATH).child('removeMe'); - await ref.set('foobar'); - await ref.onDisconnect().remove(); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - const snapshot = await ref.once('value'); - snapshot.exists().should.eql(false); - }); - - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('removeMe'); - - // Set an initial value - await ref.set('foo'); - - await ref.onDisconnect().remove(callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - callback.should.be.calledOnce(); - }); - }); - - describe('modular', function () { - afterEach(async function () { - const { getDatabase, goOnline } = databaseModular; - const db = getDatabase(); - - // Ensures the db is online before running each test - await goOnline(db); - }); - - it('throws if onComplete is not a function', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.remove('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('removes a node whilst offline', async function () { - const { getDatabase, ref, child, onDisconnect, goOffline, goOnline, get } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - const childRef = child(dbRef, 'removeMe'); - - await childRef.set('foobar'); - await onDisconnect(childRef).remove(); - await goOffline(db); - await goOnline(db); - const snapshot = await get(childRef); - snapshot.exists().should.eql(false); - }); - - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - - const { getDatabase, ref, child, onDisconnect, goOffline, goOnline } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - const childRef = child(dbRef, 'removeMe'); - - // Set an initial value - await childRef.set('foo'); - - await onDisconnect(childRef).remove(callback); - await goOffline(db); - await goOnline(db); - - callback.should.be.calledOnce(); - }); - }); -}); diff --git a/packages/database/e2e/onDisconnect/set.e2e.js b/packages/database/e2e/onDisconnect/set.e2e.js index d0a034db65..ce5cc1d488 100644 --- a/packages/database/e2e/onDisconnect/set.e2e.js +++ b/packages/database/e2e/onDisconnect/set.e2e.js @@ -24,57 +24,131 @@ describe('database().ref().onDisconnect().set()', function () { await wipe(TEST_PATH); }); - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); + describe('v8 compatibility', function () { + afterEach(async function () { + // Ensures the db is online before running each test + await firebase.database().goOnline(); + }); - it('throws if value is not a defined', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.set(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); + it('throws if value is not a defined', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.set(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be defined"); + return Promise.resolve(); + } + }); - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.set(null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.set(null, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + xit('sets value when disconnected', async function () { + const ref = firebase.database().ref(TEST_PATH); + + const value = Date.now(); - xit('sets value when disconnected', async function () { - const ref = firebase.database().ref(TEST_PATH); + await ref.onDisconnect().set(value); + await firebase.database().goOffline(); + await firebase.database().goOnline(); - const value = Date.now(); + const snapshot = await ref.once('value'); + snapshot.val().should.eql(value); + }); - await ref.onDisconnect().set(value); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + it('calls back to the onComplete function', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH); - const snapshot = await ref.once('value'); - snapshot.val().should.eql(value); + // Set an initial value + await ref.set('foo'); + + await ref.onDisconnect().set('bar', callback); + await firebase.database().goOffline(); + await firebase.database().goOnline(); + + callback.should.be.calledOnce(); + }); }); - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + afterEach(async function () { + const { getDatabase, goOnline } = databaseModular; + const db = getDatabase(); + + // Ensures the db is online before running each test + await goOnline(db); + }); + + it('throws if value is not a defined', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.set(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be defined"); + return Promise.resolve(); + } + }); + + it('throws if onComplete is not a function', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.set(null, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + xit('sets value when disconnected', async function () { + const { getDatabase, ref, onDisconnect, goOffline, goOnline, get } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const value = Date.now(); + + await onDisconnect(dbRef).set(value); + await goOffline(db); + await goOnline(db); + + const snapshot = await get(dbRef); + snapshot.val().should.eql(value); + }); + + it('calls back to the onComplete function', async function () { + const { getDatabase, ref, onDisconnect, set, goOnline, goOffline } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const callback = sinon.spy(); - // Set an initial value - await ref.set('foo'); + // Set an initial value + await set(dbRef, 'foo'); - await ref.onDisconnect().set('bar', callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + await onDisconnect(dbRef).set('bar', callback); + await goOffline(db); + await goOnline(db); - callback.should.be.calledOnce(); + callback.should.be.calledOnce(); + }); }); }); diff --git a/packages/database/e2e/onDisconnect/set.modular.e2e.js b/packages/database/e2e/onDisconnect/set.modular.e2e.js deleted file mode 100644 index ce5cc1d488..0000000000 --- a/packages/database/e2e/onDisconnect/set.modular.e2e.js +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/onDisconnectSet`; - -describe('database().ref().onDisconnect().set()', function () { - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); - - it('throws if value is not a defined', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.set(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.set(null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('sets value when disconnected', async function () { - const ref = firebase.database().ref(TEST_PATH); - - const value = Date.now(); - - await ref.onDisconnect().set(value); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - const snapshot = await ref.once('value'); - snapshot.val().should.eql(value); - }); - - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); - - // Set an initial value - await ref.set('foo'); - - await ref.onDisconnect().set('bar', callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - callback.should.be.calledOnce(); - }); - }); - - describe('modular', function () { - afterEach(async function () { - const { getDatabase, goOnline } = databaseModular; - const db = getDatabase(); - - // Ensures the db is online before running each test - await goOnline(db); - }); - - it('throws if value is not a defined', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.set(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.set(null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('sets value when disconnected', async function () { - const { getDatabase, ref, onDisconnect, goOffline, goOnline, get } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const value = Date.now(); - - await onDisconnect(dbRef).set(value); - await goOffline(db); - await goOnline(db); - - const snapshot = await get(dbRef); - snapshot.val().should.eql(value); - }); - - it('calls back to the onComplete function', async function () { - const { getDatabase, ref, onDisconnect, set, goOnline, goOffline } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const callback = sinon.spy(); - - // Set an initial value - await set(dbRef, 'foo'); - - await onDisconnect(dbRef).set('bar', callback); - await goOffline(db); - await goOnline(db); - - callback.should.be.calledOnce(); - }); - }); -}); diff --git a/packages/database/e2e/onDisconnect/setWithPriority.e2e.js b/packages/database/e2e/onDisconnect/setWithPriority.e2e.js index fbf4b94e95..ffb8f9d3d3 100644 --- a/packages/database/e2e/onDisconnect/setWithPriority.e2e.js +++ b/packages/database/e2e/onDisconnect/setWithPriority.e2e.js @@ -24,69 +24,159 @@ describe('database().ref().onDisconnect().setWithPriority()', function () { await wipe(TEST_PATH); }); - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); + describe('v8 compatibility', function () { + afterEach(async function () { + // Ensures the db is online before running each test + await firebase.database().goOnline(); + }); - it('throws if value is not a defined', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.setWithPriority(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); + it('throws if value is not a defined', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.setWithPriority(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be defined"); + return Promise.resolve(); + } + }); - it('throws if priority is not a valid type', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.setWithPriority(null, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'priority' must be a number, string or null value"); - return Promise.resolve(); - } - }); + it('throws if priority is not a valid type', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.setWithPriority(null, { foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'priority' must be a number, string or null value"); + return Promise.resolve(); + } + }); - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.setWithPriority(null, 1, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.setWithPriority(null, 1, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + xit('sets value with priority when disconnected', async function () { + const ref = firebase.database().ref(TEST_PATH); + + const value = Date.now(); - xit('sets value with priority when disconnected', async function () { - const ref = firebase.database().ref(TEST_PATH); + await ref.onDisconnect().setWithPriority(value, 3); + await firebase.database().goOffline(); + await firebase.database().goOnline(); - const value = Date.now(); + const snapshot = await ref.once('value'); + snapshot.exportVal()['.value'].should.eql(value); + snapshot.exportVal()['.priority'].should.eql(3); + }); - await ref.onDisconnect().setWithPriority(value, 3); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + it('calls back to the onComplete function', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH); - const snapshot = await ref.once('value'); - snapshot.exportVal()['.value'].should.eql(value); - snapshot.exportVal()['.priority'].should.eql(3); + // Set an initial value + await ref.set('foo'); + + await ref.onDisconnect().setWithPriority('bar', 2, callback); + await firebase.database().goOffline(); + await firebase.database().goOnline(); + + callback.should.be.calledOnce(); + }); }); - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + afterEach(async function () { + const { getDatabase, goOnline } = databaseModular; + const db = getDatabase(); + + // Ensures the db is online before running each test + await goOnline(db); + }); + + it('throws if value is not a defined', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.setWithPriority(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be defined"); + return Promise.resolve(); + } + }); + + it('throws if priority is not a valid type', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.setWithPriority(null, { foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'priority' must be a number, string or null value"); + return Promise.resolve(); + } + }); + + it('throws if onComplete is not a function', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.setWithPriority(null, 1, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + xit('sets value with priority when disconnected', async function () { + const { getDatabase, ref, onDisconnect, goOffline, goOnline, get } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const value = Date.now(); + + await onDisconnect(dbRef).setWithPriority(value, 3); + await goOffline(db); + await goOnline(db); + + const snapshot = await get(dbRef); + snapshot.exportVal()['.value'].should.eql(value); + snapshot.exportVal()['.priority'].should.eql(3); + }); + + it('calls back to the onComplete function', async function () { + const { getDatabase, ref, onDisconnect, set, goOffline, goOnline } = databaseModular; + const db = getDatabase(); + + const callback = sinon.spy(); + const dbRef = ref(db, TEST_PATH); - // Set an initial value - await ref.set('foo'); + // Set an initial value + await set(dbRef, 'foo'); - await ref.onDisconnect().setWithPriority('bar', 2, callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + await onDisconnect(dbRef).setWithPriority('bar', 2, callback); + await goOffline(db); + await goOnline(db); - callback.should.be.calledOnce(); + callback.should.be.calledOnce(); + }); }); }); diff --git a/packages/database/e2e/onDisconnect/setWithPriority.modular.e2e.js b/packages/database/e2e/onDisconnect/setWithPriority.modular.e2e.js deleted file mode 100644 index ffb8f9d3d3..0000000000 --- a/packages/database/e2e/onDisconnect/setWithPriority.modular.e2e.js +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/onDisconnectSetWithPriority`; - -describe('database().ref().onDisconnect().setWithPriority()', function () { - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); - - it('throws if value is not a defined', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.setWithPriority(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); - - it('throws if priority is not a valid type', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.setWithPriority(null, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'priority' must be a number, string or null value"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.setWithPriority(null, 1, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('sets value with priority when disconnected', async function () { - const ref = firebase.database().ref(TEST_PATH); - - const value = Date.now(); - - await ref.onDisconnect().setWithPriority(value, 3); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - const snapshot = await ref.once('value'); - snapshot.exportVal()['.value'].should.eql(value); - snapshot.exportVal()['.priority'].should.eql(3); - }); - - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); - - // Set an initial value - await ref.set('foo'); - - await ref.onDisconnect().setWithPriority('bar', 2, callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - callback.should.be.calledOnce(); - }); - }); - - describe('modular', function () { - afterEach(async function () { - const { getDatabase, goOnline } = databaseModular; - const db = getDatabase(); - - // Ensures the db is online before running each test - await goOnline(db); - }); - - it('throws if value is not a defined', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.setWithPriority(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); - - it('throws if priority is not a valid type', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.setWithPriority(null, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'priority' must be a number, string or null value"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.setWithPriority(null, 1, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('sets value with priority when disconnected', async function () { - const { getDatabase, ref, onDisconnect, goOffline, goOnline, get } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const value = Date.now(); - - await onDisconnect(dbRef).setWithPriority(value, 3); - await goOffline(db); - await goOnline(db); - - const snapshot = await get(dbRef); - snapshot.exportVal()['.value'].should.eql(value); - snapshot.exportVal()['.priority'].should.eql(3); - }); - - it('calls back to the onComplete function', async function () { - const { getDatabase, ref, onDisconnect, set, goOffline, goOnline } = databaseModular; - const db = getDatabase(); - - const callback = sinon.spy(); - const dbRef = ref(db, TEST_PATH); - - // Set an initial value - await set(dbRef, 'foo'); - - await onDisconnect(dbRef).setWithPriority('bar', 2, callback); - await goOffline(db); - await goOnline(db); - - callback.should.be.calledOnce(); - }); - }); -}); diff --git a/packages/database/e2e/onDisconnect/update.e2e.js b/packages/database/e2e/onDisconnect/update.e2e.js index 8873571cf5..aea53e7526 100644 --- a/packages/database/e2e/onDisconnect/update.e2e.js +++ b/packages/database/e2e/onDisconnect/update.e2e.js @@ -24,88 +24,201 @@ describe('database().ref().onDisconnect().update()', function () { await wipe(TEST_PATH); }); - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); + describe('v8 compatibility', function () { + afterEach(async function () { + // Ensures the db is online before running each test + await firebase.database().goOnline(); + }); - it('throws if values is not an object', async function () { - try { - await firebase.database().ref(TEST_PATH).onDisconnect().update('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object"); - return Promise.resolve(); - } - }); + it('throws if values is not an object', async function () { + try { + await firebase.database().ref(TEST_PATH).onDisconnect().update('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' must be an object"); + return Promise.resolve(); + } + }); - it('throws if values does not contain any values', async function () { - try { - await firebase.database().ref(TEST_PATH).onDisconnect().update({}); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object containing multiple values"); - return Promise.resolve(); - } - }); + it('throws if values does not contain any values', async function () { + try { + await firebase.database().ref(TEST_PATH).onDisconnect().update({}); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' must be an object containing multiple values"); + return Promise.resolve(); + } + }); + + it('throws if update paths are not valid', async function () { + try { + await firebase.database().ref(TEST_PATH).onDisconnect().update({ + $$$$: 'foo', + }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' contains an invalid path."); + return Promise.resolve(); + } + }); + + it('throws if onComplete is not a function', function () { + const ref = firebase.database().ref(TEST_PATH).onDisconnect(); + try { + ref.update({ foo: 'bar' }, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); - it('throws if update paths are not valid', async function () { - try { - await firebase.database().ref(TEST_PATH).onDisconnect().update({ - $$$$: 'foo', + xit('updates value when disconnected', async function () { + const ref = firebase.database().ref(TEST_PATH); + + const value = Date.now(); + await ref.set({ + foo: { + bar: 'baz', + }, }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' contains an invalid path."); - return Promise.resolve(); - } - }); - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.update({ foo: 'bar' }, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } + await ref.child('foo').onDisconnect().update({ + bar: value, + }); + await firebase.database().goOffline(); + await firebase.database().goOnline(); + + const snapshot = await ref.child('foo').once('value'); + snapshot.val().should.eql( + jet.contextify({ + bar: value, + }), + ); + }); + + it('calls back to the onComplete function', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH); + + // Set an initial value + await ref.set('foo'); + await ref.onDisconnect().update({ foo: 'bar' }, callback); + await firebase.database().goOffline(); + await firebase.database().goOnline(); + + callback.should.be.calledOnce(); + }); }); - xit('updates value when disconnected', async function () { - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + afterEach(async function () { + const { getDatabase, goOnline } = databaseModular; + const db = getDatabase(); - const value = Date.now(); - await ref.set({ - foo: { - bar: 'baz', - }, + // Ensures the db is online before running each test + await goOnline(db); }); - await ref.child('foo').onDisconnect().update({ - bar: value, + it('throws if values is not an object', async function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + try { + await onDisconnect(dbRef).update('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' must be an object"); + return Promise.resolve(); + } + }); + + it('throws if values does not contain any values', async function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + try { + await onDisconnect(dbRef).update({}); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' must be an object containing multiple values"); + return Promise.resolve(); + } + }); + + it('throws if update paths are not valid', async function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + try { + await onDisconnect(dbRef).update({ + $$$$: 'foo', + }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' contains an invalid path."); + return Promise.resolve(); + } + }); + + it('throws if onComplete is not a function', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const disconnect = onDisconnect(dbRef); + try { + disconnect.update({ foo: 'bar' }, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } }); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - const snapshot = await ref.child('foo').once('value'); - snapshot.val().should.eql( - jet.contextify({ + xit('updates value when disconnected', async function () { + const { getDatabase, ref, onDisconnect, child, goOnline, goOffline, get } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const value = Date.now(); + await dbRef.set({ + foo: { + bar: 'baz', + }, + }); + + await onDisconnect(child(dbRef, 'foo')).update({ bar: value, - }), - ); - }); + }); + await goOffline(db); + await goOnline(db); - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); + const snapshot = await get(child(dbRef, 'foo'), 'value'); + snapshot.val().should.eql( + jet.contextify({ + bar: value, + }), + ); + }); + + it('calls back to the onComplete function', async function () { + const { getDatabase, ref, onDisconnect, goOffline, goOnline } = databaseModular; + const db = getDatabase(); - // Set an initial value - await ref.set('foo'); - await ref.onDisconnect().update({ foo: 'bar' }, callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); + const callback = sinon.spy(); + const dbRef = ref(db, TEST_PATH); - callback.should.be.calledOnce(); + // Set an initial value + await dbRef.set('foo'); + await onDisconnect(dbRef).update({ foo: 'bar' }, callback); + await goOffline(db); + await goOnline(db); + + callback.should.be.calledOnce(); + }); }); }); diff --git a/packages/database/e2e/onDisconnect/update.modular.e2e.js b/packages/database/e2e/onDisconnect/update.modular.e2e.js deleted file mode 100644 index aea53e7526..0000000000 --- a/packages/database/e2e/onDisconnect/update.modular.e2e.js +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/onDisconnectUpdate`; - -describe('database().ref().onDisconnect().update()', function () { - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - afterEach(async function () { - // Ensures the db is online before running each test - await firebase.database().goOnline(); - }); - - it('throws if values is not an object', async function () { - try { - await firebase.database().ref(TEST_PATH).onDisconnect().update('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object"); - return Promise.resolve(); - } - }); - - it('throws if values does not contain any values', async function () { - try { - await firebase.database().ref(TEST_PATH).onDisconnect().update({}); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object containing multiple values"); - return Promise.resolve(); - } - }); - - it('throws if update paths are not valid', async function () { - try { - await firebase.database().ref(TEST_PATH).onDisconnect().update({ - $$$$: 'foo', - }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' contains an invalid path."); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', function () { - const ref = firebase.database().ref(TEST_PATH).onDisconnect(); - try { - ref.update({ foo: 'bar' }, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('updates value when disconnected', async function () { - const ref = firebase.database().ref(TEST_PATH); - - const value = Date.now(); - await ref.set({ - foo: { - bar: 'baz', - }, - }); - - await ref.child('foo').onDisconnect().update({ - bar: value, - }); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - const snapshot = await ref.child('foo').once('value'); - snapshot.val().should.eql( - jet.contextify({ - bar: value, - }), - ); - }); - - it('calls back to the onComplete function', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH); - - // Set an initial value - await ref.set('foo'); - await ref.onDisconnect().update({ foo: 'bar' }, callback); - await firebase.database().goOffline(); - await firebase.database().goOnline(); - - callback.should.be.calledOnce(); - }); - }); - - describe('modular', function () { - afterEach(async function () { - const { getDatabase, goOnline } = databaseModular; - const db = getDatabase(); - - // Ensures the db is online before running each test - await goOnline(db); - }); - - it('throws if values is not an object', async function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - try { - await onDisconnect(dbRef).update('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object"); - return Promise.resolve(); - } - }); - - it('throws if values does not contain any values', async function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - try { - await onDisconnect(dbRef).update({}); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object containing multiple values"); - return Promise.resolve(); - } - }); - - it('throws if update paths are not valid', async function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - try { - await onDisconnect(dbRef).update({ - $$$$: 'foo', - }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' contains an invalid path."); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const disconnect = onDisconnect(dbRef); - try { - disconnect.update({ foo: 'bar' }, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - xit('updates value when disconnected', async function () { - const { getDatabase, ref, onDisconnect, child, goOnline, goOffline, get } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const value = Date.now(); - await dbRef.set({ - foo: { - bar: 'baz', - }, - }); - - await onDisconnect(child(dbRef, 'foo')).update({ - bar: value, - }); - await goOffline(db); - await goOnline(db); - - const snapshot = await get(child(dbRef, 'foo'), 'value'); - snapshot.val().should.eql( - jet.contextify({ - bar: value, - }), - ); - }); - - it('calls back to the onComplete function', async function () { - const { getDatabase, ref, onDisconnect, goOffline, goOnline } = databaseModular; - const db = getDatabase(); - - const callback = sinon.spy(); - const dbRef = ref(db, TEST_PATH); - - // Set an initial value - await dbRef.set('foo'); - await onDisconnect(dbRef).update({ foo: 'bar' }, callback); - await goOffline(db); - await goOnline(db); - - callback.should.be.calledOnce(); - }); - }); -}); diff --git a/packages/database/e2e/query/endAt.e2e.js b/packages/database/e2e/query/endAt.e2e.js index 160ae5802a..619a18e250 100644 --- a/packages/database/e2e/query/endAt.e2e.js +++ b/packages/database/e2e/query/endAt.e2e.js @@ -27,109 +27,235 @@ describe('database().ref().endAt()', function () { await wipe(TEST_PATH); }); - it('throws if an value is undefined', async function () { - try { - await firebase.database().ref().endAt(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if an value is undefined', async function () { + try { + await firebase.database().ref().endAt(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be a number, string, boolean or null value"); + return Promise.resolve(); + } + }); - it('throws if an key is not a string', async function () { - try { - await firebase.database().ref().endAt('foo', 1234); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); + it('throws if an key is not a string', async function () { + try { + await firebase.database().ref().endAt('foo', 1234); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'key' must be a string value if defined"); + return Promise.resolve(); + } + }); - it('throws if a ending point has already been set', async function () { - try { - await firebase.database().ref().equalTo('foo').endAt('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Ending point was already set (by another call to endAt or equalTo)', - ); - return Promise.resolve(); - } - }); + it('throws if a ending point has already been set', async function () { + try { + await firebase.database().ref().equalTo('foo').endAt('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Ending point was already set (by another call to endAt or equalTo)', + ); + return Promise.resolve(); + } + }); - it('throws if ordering by key and the key param is set', async function () { - try { - await firebase.database().ref().orderByKey('foo').endAt('foo', 'bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', - ); - return Promise.resolve(); - } - }); + it('throws if ordering by key and the key param is set', async function () { + try { + await firebase.database().ref().orderByKey('foo').endAt('foo', 'bar'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', + ); + return Promise.resolve(); + } + }); - it('throws if ordering by key and the value param is not a string', async function () { - try { - await firebase.database().ref().orderByKey('foo').endAt(123); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', - ); - return Promise.resolve(); - } - }); + it('throws if ordering by key and the value param is not a string', async function () { + try { + await firebase.database().ref().orderByKey('foo').endAt(123); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', + ); + return Promise.resolve(); + } + }); - it('throws if ordering by priority and the value param is not priority type', async function () { - try { - await firebase.database().ref().orderByPriority().endAt(true); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', - ); - return Promise.resolve(); - } - }); + it('throws if ordering by priority and the value param is not priority type', async function () { + try { + await firebase.database().ref().orderByPriority().endAt(true); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', + ); + return Promise.resolve(); + } + }); + + it('snapshot value returns all when no ordering modifier is applied', async function () { + const ref = firebase.database().ref(TEST_PATH); + + await ref.set({ + a: 1, + b: 2, + c: 3, + d: 4, + }); - it('snapshot value returns all when no ordering modifier is applied', async function () { - const ref = firebase.database().ref(TEST_PATH); + const expected = ['a', 'b', 'c', 'd']; - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, + const snapshot = await ref.endAt(2).once('value'); + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); }); - const expected = ['a', 'b', 'c', 'd']; + it('ends at the correct value', async function () { + const ref = firebase.database().ref(TEST_PATH); + + await ref.set({ + a: 1, + b: 2, + c: 3, + d: 4, + }); - const snapshot = await ref.endAt(2).once('value'); + const snapshot = await ref.orderByValue().endAt(2).once('value'); - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); + const expected = ['a', 'b']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); }); }); - it('ends at the correct value', async function () { - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + it('throws if an value is undefined', async function () { + const { getDatabase, ref, query, endAt } = databaseModular; + + try { + query(ref(getDatabase()), endAt()); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be a number, string, boolean or null value"); + return Promise.resolve(); + } + }); + + it('throws if an key is not a string', async function () { + const { getDatabase, ref, query, endAt } = databaseModular; + + try { + query(ref(getDatabase()), endAt('foo', 1234)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'key' must be a string value if defined"); + return Promise.resolve(); + } + }); + + it('throws if a ending point has already been set', async function () { + const { getDatabase, ref, query, equalTo, endAt } = databaseModular; + + try { + query(ref(getDatabase()), equalTo('foo'), endAt('foo')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Ending point was already set (by another call to endAt or equalTo)', + ); + return Promise.resolve(); + } + }); + + it('throws if ordering by key and the key param is set', async function () { + const { getDatabase, ref, query, orderByKey, endAt } = databaseModular; + + try { + query(ref(getDatabase()), orderByKey('foo'), endAt('foo', 'bar')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', + ); + return Promise.resolve(); + } + }); + + it('throws if ordering by key and the value param is not a string', async function () { + const { getDatabase, ref, query, orderByKey, endAt } = databaseModular; + + try { + query(ref(getDatabase()), orderByKey('foo'), endAt(123)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', + ); + return Promise.resolve(); + } + }); + + it('throws if ordering by priority and the value param is not priority type', async function () { + const { getDatabase, ref, query, orderByPriority, endAt } = databaseModular; - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, + try { + query(ref(getDatabase()), orderByPriority(), endAt(true)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', + ); + return Promise.resolve(); + } }); - const snapshot = await ref.orderByValue().endAt(2).once('value'); + it('snapshot value returns all when no ordering modifier is applied', async function () { + const { getDatabase, ref, set, endAt, query, get } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + await set(dbRef, { + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const expected = ['a', 'b', 'c', 'd']; + + const snapshot = await get(query(dbRef, endAt(2))); + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); + }); + + it('ends at the correct value', async function () { + const { getDatabase, ref, set, endAt, query, get, orderByValue } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + await set(dbRef, { + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const expected = ['a', 'b']; - const expected = ['a', 'b']; + const snapshot = await get(query(dbRef, orderByValue(), endAt(2))); - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); }); }); }); diff --git a/packages/database/e2e/query/endAt.modular.e2e.js b/packages/database/e2e/query/endAt.modular.e2e.js deleted file mode 100644 index 619a18e250..0000000000 --- a/packages/database/e2e/query/endAt.modular.e2e.js +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/endAt`; - -describe('database().ref().endAt()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if an value is undefined', async function () { - try { - await firebase.database().ref().endAt(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); - - it('throws if an key is not a string', async function () { - try { - await firebase.database().ref().endAt('foo', 1234); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); - - it('throws if a ending point has already been set', async function () { - try { - await firebase.database().ref().equalTo('foo').endAt('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Ending point was already set (by another call to endAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the key param is set', async function () { - try { - await firebase.database().ref().orderByKey('foo').endAt('foo', 'bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the value param is not a string', async function () { - try { - await firebase.database().ref().orderByKey('foo').endAt(123); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by priority and the value param is not priority type', async function () { - try { - await firebase.database().ref().orderByPriority().endAt(true); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', - ); - return Promise.resolve(); - } - }); - - it('snapshot value returns all when no ordering modifier is applied', async function () { - const ref = firebase.database().ref(TEST_PATH); - - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const expected = ['a', 'b', 'c', 'd']; - - const snapshot = await ref.endAt(2).once('value'); - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - - it('ends at the correct value', async function () { - const ref = firebase.database().ref(TEST_PATH); - - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const snapshot = await ref.orderByValue().endAt(2).once('value'); - - const expected = ['a', 'b']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - }); - - describe('modular', function () { - it('throws if an value is undefined', async function () { - const { getDatabase, ref, query, endAt } = databaseModular; - - try { - query(ref(getDatabase()), endAt()); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); - - it('throws if an key is not a string', async function () { - const { getDatabase, ref, query, endAt } = databaseModular; - - try { - query(ref(getDatabase()), endAt('foo', 1234)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); - - it('throws if a ending point has already been set', async function () { - const { getDatabase, ref, query, equalTo, endAt } = databaseModular; - - try { - query(ref(getDatabase()), equalTo('foo'), endAt('foo')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Ending point was already set (by another call to endAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the key param is set', async function () { - const { getDatabase, ref, query, orderByKey, endAt } = databaseModular; - - try { - query(ref(getDatabase()), orderByKey('foo'), endAt('foo', 'bar')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the value param is not a string', async function () { - const { getDatabase, ref, query, orderByKey, endAt } = databaseModular; - - try { - query(ref(getDatabase()), orderByKey('foo'), endAt(123)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by priority and the value param is not priority type', async function () { - const { getDatabase, ref, query, orderByPriority, endAt } = databaseModular; - - try { - query(ref(getDatabase()), orderByPriority(), endAt(true)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', - ); - return Promise.resolve(); - } - }); - - it('snapshot value returns all when no ordering modifier is applied', async function () { - const { getDatabase, ref, set, endAt, query, get } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - await set(dbRef, { - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const expected = ['a', 'b', 'c', 'd']; - - const snapshot = await get(query(dbRef, endAt(2))); - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - - it('ends at the correct value', async function () { - const { getDatabase, ref, set, endAt, query, get, orderByValue } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - await set(dbRef, { - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const expected = ['a', 'b']; - - const snapshot = await get(query(dbRef, orderByValue(), endAt(2))); - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - }); -}); diff --git a/packages/database/e2e/query/equalTo.e2e.js b/packages/database/e2e/query/equalTo.e2e.js index 772be4cffb..4d68854d41 100644 --- a/packages/database/e2e/query/equalTo.e2e.js +++ b/packages/database/e2e/query/equalTo.e2e.js @@ -27,81 +27,175 @@ describe('database().ref().equalTo()', function () { await wipe(TEST_PATH); }); - it('throws if value is not a valid type', async function () { - try { - await firebase.database().ref().equalTo({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if value is not a valid type', async function () { + try { + await firebase.database().ref().equalTo({ foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be a number, string, boolean or null value"); + return Promise.resolve(); + } + }); - it('throws if key is not a string', async function () { - try { - await firebase.database().ref().equalTo('bar', 123); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); + it('throws if key is not a string', async function () { + try { + await firebase.database().ref().equalTo('bar', 123); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'key' must be a string value if defined"); + return Promise.resolve(); + } + }); - it('throws if a starting point has already been set', async function () { - try { - await firebase.database().ref().startAt('foo').equalTo('bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Starting point was already set (by another call to startAt or equalTo)', - ); - return Promise.resolve(); - } - }); + it('throws if a starting point has already been set', async function () { + try { + await firebase.database().ref().startAt('foo').equalTo('bar'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Starting point was already set (by another call to startAt or equalTo)', + ); + return Promise.resolve(); + } + }); - it('throws if a ending point has already been set', async function () { - try { - await firebase.database().ref().endAt('foo').equalTo('bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Ending point was already set (by another call to endAt or equalTo)', - ); - return Promise.resolve(); - } - }); + it('throws if a ending point has already been set', async function () { + try { + await firebase.database().ref().endAt('foo').equalTo('bar'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Ending point was already set (by another call to endAt or equalTo)', + ); + return Promise.resolve(); + } + }); - it('snapshot value is null when no ordering modifier is applied', async function () { - const ref = firebase.database().ref(TEST_PATH); + it('snapshot value is null when no ordering modifier is applied', async function () { + const ref = firebase.database().ref(TEST_PATH); - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, + await ref.set({ + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const snapshot = await ref.equalTo(2).once('value'); + should.equal(snapshot.val(), null); }); - const snapshot = await ref.equalTo(2).once('value'); - should.equal(snapshot.val(), null); + it('returns the correct equal to values', async function () { + const ref = firebase.database().ref(TEST_PATH); + + await ref.set({ + a: 1, + b: 2, + c: 3, + d: 4, + e: 2, + }); + + const snapshot = await ref.orderByValue().equalTo(2).once('value'); + + const expected = ['b', 'e']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); + }); }); - it('returns the correct equal to values', async function () { - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + it('throws if value is not a valid type', async function () { + const { getDatabase, ref, equalTo, query } = databaseModular; - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, - e: 2, + try { + query(ref(getDatabase()), equalTo({ foo: 'bar' })); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be a number, string, boolean or null value"); + return Promise.resolve(); + } }); - const snapshot = await ref.orderByValue().equalTo(2).once('value'); + it('throws if key is not a string', async function () { + const { getDatabase, ref, equalTo, query } = databaseModular; + + try { + query(ref(getDatabase()), equalTo('bar', 123)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'key' must be a string value if defined"); + return Promise.resolve(); + } + }); + + it('throws if a starting point has already been set', async function () { + const { getDatabase, ref, startAt, equalTo, query } = databaseModular; + + try { + query(ref(getDatabase()), startAt('foo'), equalTo('bar')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Starting point was already set (by another call to startAt or equalTo)', + ); + return Promise.resolve(); + } + }); + + it('throws if a ending point has already been set', async function () { + const { getDatabase, ref, endAt, equalTo, query } = databaseModular; + + try { + query(ref(getDatabase()), endAt('foo'), equalTo('bar')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Ending point was already set (by another call to endAt or equalTo)', + ); + return Promise.resolve(); + } + }); + + it('snapshot value is null when no ordering modifier is applied', async function () { + const { getDatabase, ref, set, equalTo, get, query } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + await set(dbRef, { + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const snapshot = await get(query(dbRef, equalTo(2))); + should.equal(snapshot.val(), null); + }); + + it('returns the correct equal to values', async function () { + const { getDatabase, ref, set, orderByValue, equalTo, get, query } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + await set(dbRef, { + a: 1, + b: 2, + c: 3, + d: 4, + e: 2, + }); + + const snapshot = await get(query(dbRef, orderByValue(), equalTo(2))); - const expected = ['b', 'e']; + const expected = ['b', 'e']; - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); }); }); }); diff --git a/packages/database/e2e/query/equalTo.modular.e2e.js b/packages/database/e2e/query/equalTo.modular.e2e.js deleted file mode 100644 index 4d68854d41..0000000000 --- a/packages/database/e2e/query/equalTo.modular.e2e.js +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/equalTo`; - -describe('database().ref().equalTo()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if value is not a valid type', async function () { - try { - await firebase.database().ref().equalTo({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); - - it('throws if key is not a string', async function () { - try { - await firebase.database().ref().equalTo('bar', 123); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); - - it('throws if a starting point has already been set', async function () { - try { - await firebase.database().ref().startAt('foo').equalTo('bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Starting point was already set (by another call to startAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('throws if a ending point has already been set', async function () { - try { - await firebase.database().ref().endAt('foo').equalTo('bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Ending point was already set (by another call to endAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('snapshot value is null when no ordering modifier is applied', async function () { - const ref = firebase.database().ref(TEST_PATH); - - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const snapshot = await ref.equalTo(2).once('value'); - should.equal(snapshot.val(), null); - }); - - it('returns the correct equal to values', async function () { - const ref = firebase.database().ref(TEST_PATH); - - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, - e: 2, - }); - - const snapshot = await ref.orderByValue().equalTo(2).once('value'); - - const expected = ['b', 'e']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - }); - - describe('modular', function () { - it('throws if value is not a valid type', async function () { - const { getDatabase, ref, equalTo, query } = databaseModular; - - try { - query(ref(getDatabase()), equalTo({ foo: 'bar' })); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); - - it('throws if key is not a string', async function () { - const { getDatabase, ref, equalTo, query } = databaseModular; - - try { - query(ref(getDatabase()), equalTo('bar', 123)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); - - it('throws if a starting point has already been set', async function () { - const { getDatabase, ref, startAt, equalTo, query } = databaseModular; - - try { - query(ref(getDatabase()), startAt('foo'), equalTo('bar')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Starting point was already set (by another call to startAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('throws if a ending point has already been set', async function () { - const { getDatabase, ref, endAt, equalTo, query } = databaseModular; - - try { - query(ref(getDatabase()), endAt('foo'), equalTo('bar')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Ending point was already set (by another call to endAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('snapshot value is null when no ordering modifier is applied', async function () { - const { getDatabase, ref, set, equalTo, get, query } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - await set(dbRef, { - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const snapshot = await get(query(dbRef, equalTo(2))); - should.equal(snapshot.val(), null); - }); - - it('returns the correct equal to values', async function () { - const { getDatabase, ref, set, orderByValue, equalTo, get, query } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - await set(dbRef, { - a: 1, - b: 2, - c: 3, - d: 4, - e: 2, - }); - - const snapshot = await get(query(dbRef, orderByValue(), equalTo(2))); - - const expected = ['b', 'e']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - }); -}); diff --git a/packages/database/e2e/query/get.modular.e2e.js b/packages/database/e2e/query/get.e2e.js similarity index 100% rename from packages/database/e2e/query/get.modular.e2e.js rename to packages/database/e2e/query/get.e2e.js diff --git a/packages/database/e2e/query/keepSynced.e2e.js b/packages/database/e2e/query/keepSynced.e2e.js index c567b54d35..ce7e5ba68e 100644 --- a/packages/database/e2e/query/keepSynced.e2e.js +++ b/packages/database/e2e/query/keepSynced.e2e.js @@ -16,19 +16,43 @@ */ describe('database().ref().keepSynced()', function () { - it('throws if bool is not a valid type', async function () { - try { - await firebase.database().ref().keepSynced('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bool' value must be a boolean value."); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if bool is not a valid type', async function () { + try { + await firebase.database().ref().keepSynced('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bool' value must be a boolean value."); + return Promise.resolve(); + } + }); + + it('toggles keepSynced on and off without throwing', async function () { + const ref = firebase.database().ref('noop').orderByValue(); + await ref.keepSynced(true); + await ref.keepSynced(false); + }); }); - it('toggles keepSynced on and off without throwing', async function () { - const ref = firebase.database().ref('noop').orderByValue(); - await ref.keepSynced(true); - await ref.keepSynced(false); + describe('modular', function () { + it('throws if bool is not a valid type', async function () { + const { getDatabase, ref, keepSynced } = databaseModular; + + try { + await keepSynced(ref(getDatabase()), 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'bool' value must be a boolean value."); + return Promise.resolve(); + } + }); + + it('toggles keepSynced on and off without throwing', async function () { + const { getDatabase, ref, orderByValue, query, keepSynced } = databaseModular; + + const dbRef = query(ref(getDatabase(), 'noop'), orderByValue()); + await keepSynced(dbRef, true); + await keepSynced(dbRef, false); + }); }); }); diff --git a/packages/database/e2e/query/keepSynced.modular.e2e.js b/packages/database/e2e/query/keepSynced.modular.e2e.js deleted file mode 100644 index ce7e5ba68e..0000000000 --- a/packages/database/e2e/query/keepSynced.modular.e2e.js +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe('database().ref().keepSynced()', function () { - describe('v8 compatibility', function () { - it('throws if bool is not a valid type', async function () { - try { - await firebase.database().ref().keepSynced('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bool' value must be a boolean value."); - return Promise.resolve(); - } - }); - - it('toggles keepSynced on and off without throwing', async function () { - const ref = firebase.database().ref('noop').orderByValue(); - await ref.keepSynced(true); - await ref.keepSynced(false); - }); - }); - - describe('modular', function () { - it('throws if bool is not a valid type', async function () { - const { getDatabase, ref, keepSynced } = databaseModular; - - try { - await keepSynced(ref(getDatabase()), 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'bool' value must be a boolean value."); - return Promise.resolve(); - } - }); - - it('toggles keepSynced on and off without throwing', async function () { - const { getDatabase, ref, orderByValue, query, keepSynced } = databaseModular; - - const dbRef = query(ref(getDatabase(), 'noop'), orderByValue()); - await keepSynced(dbRef, true); - await keepSynced(dbRef, false); - }); - }); -}); diff --git a/packages/database/e2e/query/limitToFirst.e2e.js b/packages/database/e2e/query/limitToFirst.e2e.js index 278a256ef6..25e558680f 100644 --- a/packages/database/e2e/query/limitToFirst.e2e.js +++ b/packages/database/e2e/query/limitToFirst.e2e.js @@ -27,63 +27,151 @@ describe('database().ref().limitToFirst()', function () { await wipe(TEST_PATH); }); - it('throws if limit is invalid', async function () { - try { - await firebase.database().ref().limitToFirst('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'limit' must be a positive integer value"); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if limit is invalid', async function () { + try { + await firebase.database().ref().limitToFirst('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'limit' must be a positive integer value"); + return Promise.resolve(); + } + }); + + it('throws if limit has already been set', async function () { + try { + await firebase.database().ref().limitToLast(2).limitToFirst(3); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Limit was already set (by another call to limitToFirst, or limitToLast)', + ); + return Promise.resolve(); + } + }); + + it('returns a limited array data set', async function () { + const ref = firebase.database().ref(`${TEST_PATH}`); + + const initial = { + 0: 'foo', + 1: 'bar', + 2: 'baz', + }; + + await ref.set(initial); + + return ref + .limitToFirst(2) + .once('value') + .then(snapshot => { + snapshot.val().should.eql(jet.contextify(['foo', 'bar'])); + return Promise.resolve(); + }); + }); + + it('returns a limited object data set', async function () { + const ref = firebase.database().ref(`${TEST_PATH}`); + + const initial = { + a: 'foo', + b: 'bar', + c: 'baz', + }; + + await ref.set(initial); + + return ref + .limitToFirst(2) + .once('value') + .then(snapshot => { + snapshot.val().should.eql( + jet.contextify({ + a: 'foo', + b: 'bar', + }), + ); + return Promise.resolve(); + }); + }); + + it('returns a null value when not possible to limit', async function () { + const ref = firebase.database().ref(`${TEST_PATH}`); + + const initial = 'foo'; + + await ref.set(initial); + + return ref + .limitToFirst(2) + .once('value') + .then(snapshot => { + should.equal(snapshot.val(), null); + return Promise.resolve(); + }); + }); }); - it('throws if limit has already been set', async function () { - try { - await firebase.database().ref().limitToLast(2).limitToFirst(3); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Limit was already set (by another call to limitToFirst, or limitToLast)', - ); - return Promise.resolve(); - } - }); + describe('modular', function () { + it('throws if limit is invalid', async function () { + const { getDatabase, ref, limitToFirst, query } = databaseModular; - it('returns a limited array data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); + try { + query(ref(getDatabase()), limitToFirst('foo')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'limit' must be a positive integer value"); + return Promise.resolve(); + } + }); + + it('throws if limit has already been set', async function () { + const { getDatabase, ref, limitToFirst, limitToLast, query } = databaseModular; + + try { + query(ref(getDatabase()), limitToLast(2), limitToFirst(3)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Limit was already set (by another call to limitToFirst, or limitToLast)', + ); + return Promise.resolve(); + } + }); - const initial = { - 0: 'foo', - 1: 'bar', - 2: 'baz', - }; + it('returns a limited array data set', async function () { + const { getDatabase, ref, set, get, limitToFirst, query } = databaseModular; - await ref.set(initial); + const dbRef = ref(getDatabase(), `${TEST_PATH}`); - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { + const initial = { + 0: 'foo', + 1: 'bar', + 2: 'baz', + }; + + await set(dbRef, initial); + + return get(query(dbRef, limitToFirst(2))).then(snapshot => { snapshot.val().should.eql(jet.contextify(['foo', 'bar'])); return Promise.resolve(); }); - }); + }); - it('returns a limited object data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); + it('returns a limited object data set', async function () { + const { getDatabase, ref, set, get, limitToFirst, query } = databaseModular; - const initial = { - a: 'foo', - b: 'bar', - c: 'baz', - }; + const dbRef = ref(getDatabase(), `${TEST_PATH}`); - await ref.set(initial); + const initial = { + a: 'foo', + b: 'bar', + c: 'baz', + }; - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { + await set(dbRef, initial); + + return get(query(dbRef, limitToFirst(2))).then(snapshot => { snapshot.val().should.eql( jet.contextify({ a: 'foo', @@ -92,21 +180,21 @@ describe('database().ref().limitToFirst()', function () { ); return Promise.resolve(); }); - }); + }); + + it('returns a null value when not possible to limit', async function () { + const { getDatabase, ref, set, get, limitToFirst, query } = databaseModular; - it('returns a null value when not possible to limit', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); + const dbRef = ref(getDatabase(), `${TEST_PATH}`); - const initial = 'foo'; + const initial = 'foo'; - await ref.set(initial); + await set(dbRef, initial); - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { + return get(query(dbRef, limitToFirst(2))).then(snapshot => { should.equal(snapshot.val(), null); return Promise.resolve(); }); + }); }); }); diff --git a/packages/database/e2e/query/limitToFirst.modular.e2e.js b/packages/database/e2e/query/limitToFirst.modular.e2e.js deleted file mode 100644 index 25e558680f..0000000000 --- a/packages/database/e2e/query/limitToFirst.modular.e2e.js +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/limitToFirst`; - -describe('database().ref().limitToFirst()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if limit is invalid', async function () { - try { - await firebase.database().ref().limitToFirst('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'limit' must be a positive integer value"); - return Promise.resolve(); - } - }); - - it('throws if limit has already been set', async function () { - try { - await firebase.database().ref().limitToLast(2).limitToFirst(3); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Limit was already set (by another call to limitToFirst, or limitToLast)', - ); - return Promise.resolve(); - } - }); - - it('returns a limited array data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); - - const initial = { - 0: 'foo', - 1: 'bar', - 2: 'baz', - }; - - await ref.set(initial); - - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { - snapshot.val().should.eql(jet.contextify(['foo', 'bar'])); - return Promise.resolve(); - }); - }); - - it('returns a limited object data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); - - const initial = { - a: 'foo', - b: 'bar', - c: 'baz', - }; - - await ref.set(initial); - - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { - snapshot.val().should.eql( - jet.contextify({ - a: 'foo', - b: 'bar', - }), - ); - return Promise.resolve(); - }); - }); - - it('returns a null value when not possible to limit', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); - - const initial = 'foo'; - - await ref.set(initial); - - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { - should.equal(snapshot.val(), null); - return Promise.resolve(); - }); - }); - }); - - describe('modular', function () { - it('throws if limit is invalid', async function () { - const { getDatabase, ref, limitToFirst, query } = databaseModular; - - try { - query(ref(getDatabase()), limitToFirst('foo')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'limit' must be a positive integer value"); - return Promise.resolve(); - } - }); - - it('throws if limit has already been set', async function () { - const { getDatabase, ref, limitToFirst, limitToLast, query } = databaseModular; - - try { - query(ref(getDatabase()), limitToLast(2), limitToFirst(3)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Limit was already set (by another call to limitToFirst, or limitToLast)', - ); - return Promise.resolve(); - } - }); - - it('returns a limited array data set', async function () { - const { getDatabase, ref, set, get, limitToFirst, query } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}`); - - const initial = { - 0: 'foo', - 1: 'bar', - 2: 'baz', - }; - - await set(dbRef, initial); - - return get(query(dbRef, limitToFirst(2))).then(snapshot => { - snapshot.val().should.eql(jet.contextify(['foo', 'bar'])); - return Promise.resolve(); - }); - }); - - it('returns a limited object data set', async function () { - const { getDatabase, ref, set, get, limitToFirst, query } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}`); - - const initial = { - a: 'foo', - b: 'bar', - c: 'baz', - }; - - await set(dbRef, initial); - - return get(query(dbRef, limitToFirst(2))).then(snapshot => { - snapshot.val().should.eql( - jet.contextify({ - a: 'foo', - b: 'bar', - }), - ); - return Promise.resolve(); - }); - }); - - it('returns a null value when not possible to limit', async function () { - const { getDatabase, ref, set, get, limitToFirst, query } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}`); - - const initial = 'foo'; - - await set(dbRef, initial); - - return get(query(dbRef, limitToFirst(2))).then(snapshot => { - should.equal(snapshot.val(), null); - return Promise.resolve(); - }); - }); - }); -}); diff --git a/packages/database/e2e/query/limitToLast.e2e.js b/packages/database/e2e/query/limitToLast.e2e.js index 616060a8fe..4fd2c227e8 100644 --- a/packages/database/e2e/query/limitToLast.e2e.js +++ b/packages/database/e2e/query/limitToLast.e2e.js @@ -27,63 +27,151 @@ describe('database().ref().limitToLast()', function () { await wipe(TEST_PATH); }); - it('throws if limit is invalid', async function () { - try { - await firebase.database().ref().limitToLast('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'limit' must be a positive integer value"); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if limit is invalid', async function () { + try { + await firebase.database().ref().limitToLast('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'limit' must be a positive integer value"); + return Promise.resolve(); + } + }); + + it('throws if limit has already been set', async function () { + try { + await firebase.database().ref().limitToFirst(3).limitToLast(2); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Limit was already set (by another call to limitToFirst, or limitToLast)', + ); + return Promise.resolve(); + } + }); + + it('returns a limited array data set', async function () { + const ref = firebase.database().ref(`${TEST_PATH}`); + + const initial = { + 0: 'foo', + 1: 'bar', + 2: 'baz', + }; + + await ref.set(initial); + + return ref + .limitToLast(2) + .once('value') + .then(snapshot => { + snapshot.val().should.eql(jet.contextify([null, 'bar', 'baz'])); + return Promise.resolve(); + }); + }); + + it('returns a limited object data set', async function () { + const ref = firebase.database().ref(`${TEST_PATH}`); + + const initial = { + a: 'foo', + b: 'bar', + c: 'baz', + }; + + await ref.set(initial); + + return ref + .limitToLast(2) + .once('value') + .then(snapshot => { + snapshot.val().should.eql( + jet.contextify({ + b: 'bar', + c: 'baz', + }), + ); + return Promise.resolve(); + }); + }); + + it('returns a null value when not possible to limit', async function () { + const ref = firebase.database().ref(`${TEST_PATH}`); + + const initial = 'foo'; + + await ref.set(initial); + + return ref + .limitToFirst(2) + .once('value') + .then(snapshot => { + should.equal(snapshot.val(), null); + return Promise.resolve(); + }); + }); }); - it('throws if limit has already been set', async function () { - try { - await firebase.database().ref().limitToFirst(3).limitToLast(2); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Limit was already set (by another call to limitToFirst, or limitToLast)', - ); - return Promise.resolve(); - } - }); + describe('modular', function () { + it('throws if limit is invalid', async function () { + const { getDatabase, ref, limitToLast, query } = databaseModular; - it('returns a limited array data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); + try { + query(ref(getDatabase()), limitToLast('foo')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'limit' must be a positive integer value"); + return Promise.resolve(); + } + }); + + it('throws if limit has already been set', async function () { + const { getDatabase, ref, limitToLast, limitToFirst, query } = databaseModular; + + try { + query(ref(getDatabase()), limitToFirst(3), limitToLast(2)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Limit was already set (by another call to limitToFirst, or limitToLast)', + ); + return Promise.resolve(); + } + }); - const initial = { - 0: 'foo', - 1: 'bar', - 2: 'baz', - }; + it('returns a limited array data set', async function () { + const { getDatabase, ref, set, get, limitToLast, query } = databaseModular; - await ref.set(initial); + const dbRef = ref(getDatabase(), `${TEST_PATH}`); - return ref - .limitToLast(2) - .once('value') - .then(snapshot => { + const initial = { + 0: 'foo', + 1: 'bar', + 2: 'baz', + }; + + await set(dbRef, initial); + + return get(query(dbRef, limitToLast(2))).then(snapshot => { snapshot.val().should.eql(jet.contextify([null, 'bar', 'baz'])); return Promise.resolve(); }); - }); + }); - it('returns a limited object data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); + it('returns a limited object data set', async function () { + const { getDatabase, ref, set, get, limitToLast, query } = databaseModular; - const initial = { - a: 'foo', - b: 'bar', - c: 'baz', - }; + const dbRef = ref(getDatabase(), `${TEST_PATH}`); - await ref.set(initial); + const initial = { + a: 'foo', + b: 'bar', + c: 'baz', + }; - return ref - .limitToLast(2) - .once('value') - .then(snapshot => { + await set(dbRef, initial); + + return get(query(dbRef, limitToLast(2))).then(snapshot => { snapshot.val().should.eql( jet.contextify({ b: 'bar', @@ -92,21 +180,21 @@ describe('database().ref().limitToLast()', function () { ); return Promise.resolve(); }); - }); + }); + + it('returns a null value when not possible to limit', async function () { + const { getDatabase, ref, set, get, limitToLast, query } = databaseModular; - it('returns a null value when not possible to limit', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); + const dbRef = ref(getDatabase(), `${TEST_PATH}`); - const initial = 'foo'; + const initial = 'foo'; - await ref.set(initial); + await set(dbRef, initial); - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { + return get(query(dbRef, limitToLast(2))).then(snapshot => { should.equal(snapshot.val(), null); return Promise.resolve(); }); + }); }); }); diff --git a/packages/database/e2e/query/limitToLast.modular.e2e.js b/packages/database/e2e/query/limitToLast.modular.e2e.js deleted file mode 100644 index 4fd2c227e8..0000000000 --- a/packages/database/e2e/query/limitToLast.modular.e2e.js +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/limitToLast`; - -describe('database().ref().limitToLast()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if limit is invalid', async function () { - try { - await firebase.database().ref().limitToLast('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'limit' must be a positive integer value"); - return Promise.resolve(); - } - }); - - it('throws if limit has already been set', async function () { - try { - await firebase.database().ref().limitToFirst(3).limitToLast(2); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Limit was already set (by another call to limitToFirst, or limitToLast)', - ); - return Promise.resolve(); - } - }); - - it('returns a limited array data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); - - const initial = { - 0: 'foo', - 1: 'bar', - 2: 'baz', - }; - - await ref.set(initial); - - return ref - .limitToLast(2) - .once('value') - .then(snapshot => { - snapshot.val().should.eql(jet.contextify([null, 'bar', 'baz'])); - return Promise.resolve(); - }); - }); - - it('returns a limited object data set', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); - - const initial = { - a: 'foo', - b: 'bar', - c: 'baz', - }; - - await ref.set(initial); - - return ref - .limitToLast(2) - .once('value') - .then(snapshot => { - snapshot.val().should.eql( - jet.contextify({ - b: 'bar', - c: 'baz', - }), - ); - return Promise.resolve(); - }); - }); - - it('returns a null value when not possible to limit', async function () { - const ref = firebase.database().ref(`${TEST_PATH}`); - - const initial = 'foo'; - - await ref.set(initial); - - return ref - .limitToFirst(2) - .once('value') - .then(snapshot => { - should.equal(snapshot.val(), null); - return Promise.resolve(); - }); - }); - }); - - describe('modular', function () { - it('throws if limit is invalid', async function () { - const { getDatabase, ref, limitToLast, query } = databaseModular; - - try { - query(ref(getDatabase()), limitToLast('foo')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'limit' must be a positive integer value"); - return Promise.resolve(); - } - }); - - it('throws if limit has already been set', async function () { - const { getDatabase, ref, limitToLast, limitToFirst, query } = databaseModular; - - try { - query(ref(getDatabase()), limitToFirst(3), limitToLast(2)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Limit was already set (by another call to limitToFirst, or limitToLast)', - ); - return Promise.resolve(); - } - }); - - it('returns a limited array data set', async function () { - const { getDatabase, ref, set, get, limitToLast, query } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}`); - - const initial = { - 0: 'foo', - 1: 'bar', - 2: 'baz', - }; - - await set(dbRef, initial); - - return get(query(dbRef, limitToLast(2))).then(snapshot => { - snapshot.val().should.eql(jet.contextify([null, 'bar', 'baz'])); - return Promise.resolve(); - }); - }); - - it('returns a limited object data set', async function () { - const { getDatabase, ref, set, get, limitToLast, query } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}`); - - const initial = { - a: 'foo', - b: 'bar', - c: 'baz', - }; - - await set(dbRef, initial); - - return get(query(dbRef, limitToLast(2))).then(snapshot => { - snapshot.val().should.eql( - jet.contextify({ - b: 'bar', - c: 'baz', - }), - ); - return Promise.resolve(); - }); - }); - - it('returns a null value when not possible to limit', async function () { - const { getDatabase, ref, set, get, limitToLast, query } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}`); - - const initial = 'foo'; - - await set(dbRef, initial); - - return get(query(dbRef, limitToLast(2))).then(snapshot => { - should.equal(snapshot.val(), null); - return Promise.resolve(); - }); - }); - }); -}); diff --git a/packages/database/e2e/query/onChildAdded.modular.e2e.js b/packages/database/e2e/query/onChildAdded.e2e.js similarity index 100% rename from packages/database/e2e/query/onChildAdded.modular.e2e.js rename to packages/database/e2e/query/onChildAdded.e2e.js diff --git a/packages/database/e2e/query/onChildChanged.modular.e2e.js b/packages/database/e2e/query/onChildChanged.e2e.js similarity index 100% rename from packages/database/e2e/query/onChildChanged.modular.e2e.js rename to packages/database/e2e/query/onChildChanged.e2e.js diff --git a/packages/database/e2e/query/onChildMoved.modular.e2e.js b/packages/database/e2e/query/onChildMoved.e2e.js similarity index 100% rename from packages/database/e2e/query/onChildMoved.modular.e2e.js rename to packages/database/e2e/query/onChildMoved.e2e.js diff --git a/packages/database/e2e/query/onChildRemoved.modular.e2e.js b/packages/database/e2e/query/onChildRemoved.e2e.js similarity index 100% rename from packages/database/e2e/query/onChildRemoved.modular.e2e.js rename to packages/database/e2e/query/onChildRemoved.e2e.js diff --git a/packages/database/e2e/query/onValue.modular.e2e.js b/packages/database/e2e/query/onValue.e2e.js similarity index 100% rename from packages/database/e2e/query/onValue.modular.e2e.js rename to packages/database/e2e/query/onValue.e2e.js diff --git a/packages/database/e2e/query/orderByChild.e2e.js b/packages/database/e2e/query/orderByChild.e2e.js index 28deb940fc..f4eefc9baf 100644 --- a/packages/database/e2e/query/orderByChild.e2e.js +++ b/packages/database/e2e/query/orderByChild.e2e.js @@ -27,51 +27,111 @@ describe('database().ref().orderByChild()', function () { await wipe(TEST_PATH); }); - it('throws if path is not a string value', async function () { - try { - await firebase.database().ref().orderByChild({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if path is not a string value', async function () { + try { + await firebase.database().ref().orderByChild({ foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + return Promise.resolve(); + } + }); - it('throws if path is an empty path', async function () { - try { - await firebase.database().ref().orderByChild('/'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' cannot be empty. Use orderByValue instead"); - return Promise.resolve(); - } - }); + it('throws if path is an empty path', async function () { + try { + await firebase.database().ref().orderByChild('/'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' cannot be empty. Use orderByValue instead"); + return Promise.resolve(); + } + }); + + it('throws if an orderBy call has already been set', async function () { + try { + await firebase.database().ref().orderByKey().orderByChild('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } + }); + + it('order by a child value', async function () { + const ref = firebase.database().ref(TEST_PATH); + + try { + const snapshot = await ref.child('query').orderByChild('number').once('value'); + + const expected = ['b', 'c', 'a']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByKey().orderByChild('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); - it('order by a child value', async function () { - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + it('throws if path is not a string value', async function () { + const { getDatabase, ref, orderByChild, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByChild({ foo: 'bar' })); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + return Promise.resolve(); + } + }); + + it('throws if path is an empty path', async function () { + const { getDatabase, ref, orderByChild, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByChild('/')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' cannot be empty. Use orderByValue instead"); + return Promise.resolve(); + } + }); + + it('throws if an orderBy call has already been set', async function () { + const { getDatabase, ref, orderByKey, orderByChild, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByKey(), orderByChild('foo')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } + }); + + it('order by a child value', async function () { + const { getDatabase, ref, get, child, orderByChild, query } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); - try { - const snapshot = await ref.child('query').orderByChild('number').once('value'); + try { + const snapshot = await get(query(child(dbRef, 'query'), orderByChild('number'))); - const expected = ['b', 'c', 'a']; + const expected = ['b', 'c', 'a']; - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); - return Promise.resolve(); - } catch (error) { - throw error; - } + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); }); diff --git a/packages/database/e2e/query/orderByChild.modular.e2e.js b/packages/database/e2e/query/orderByChild.modular.e2e.js deleted file mode 100644 index f4eefc9baf..0000000000 --- a/packages/database/e2e/query/orderByChild.modular.e2e.js +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/orderByChild`; - -describe('database().ref().orderByChild()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if path is not a string value', async function () { - try { - await firebase.database().ref().orderByChild({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); - - it('throws if path is an empty path', async function () { - try { - await firebase.database().ref().orderByChild('/'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' cannot be empty. Use orderByValue instead"); - return Promise.resolve(); - } - }); - - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByKey().orderByChild('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by a child value', async function () { - const ref = firebase.database().ref(TEST_PATH); - - try { - const snapshot = await ref.child('query').orderByChild('number').once('value'); - - const expected = ['b', 'c', 'a']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); - - describe('modular', function () { - it('throws if path is not a string value', async function () { - const { getDatabase, ref, orderByChild, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByChild({ foo: 'bar' })); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); - - it('throws if path is an empty path', async function () { - const { getDatabase, ref, orderByChild, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByChild('/')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' cannot be empty. Use orderByValue instead"); - return Promise.resolve(); - } - }); - - it('throws if an orderBy call has already been set', async function () { - const { getDatabase, ref, orderByKey, orderByChild, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByKey(), orderByChild('foo')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by a child value', async function () { - const { getDatabase, ref, get, child, orderByChild, query } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - try { - const snapshot = await get(query(child(dbRef, 'query'), orderByChild('number'))); - - const expected = ['b', 'c', 'a']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); -}); diff --git a/packages/database/e2e/query/orderByKey.e2e.js b/packages/database/e2e/query/orderByKey.e2e.js index 384e8f88af..940ce13dc1 100644 --- a/packages/database/e2e/query/orderByKey.e2e.js +++ b/packages/database/e2e/query/orderByKey.e2e.js @@ -27,31 +27,67 @@ describe('database().ref().orderByKey()', function () { await wipe(TEST_PATH); }); - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByChild('foo').orderByKey(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if an orderBy call has already been set', async function () { + try { + await firebase.database().ref().orderByChild('foo').orderByKey(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } + }); + + it('order by a key', async function () { + const ref = firebase.database().ref(TEST_PATH); + + try { + const snapshot = await ref.child('query').orderByKey().once('value'); + + const expected = ['a', 'b', 'c']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); + + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); - it('order by a key', async function () { - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + it('throws if an orderBy call has already been set', async function () { + const { getDatabase, ref, orderByChild, orderByKey, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByChild('foo'), orderByKey()); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } + }); + + it('order by a key', async function () { + const { getDatabase, ref, get, child, orderByKey, query } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); - try { - const snapshot = await ref.child('query').orderByKey().once('value'); + try { + const snapshot = await get(query(child(dbRef, 'query'), orderByKey())); - const expected = ['a', 'b', 'c']; + const expected = ['a', 'b', 'c']; - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); - return Promise.resolve(); - } catch (error) { - throw error; - } + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); }); diff --git a/packages/database/e2e/query/orderByKey.modular.e2e.js b/packages/database/e2e/query/orderByKey.modular.e2e.js deleted file mode 100644 index 940ce13dc1..0000000000 --- a/packages/database/e2e/query/orderByKey.modular.e2e.js +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/orderByKey`; - -describe('database().ref().orderByKey()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByChild('foo').orderByKey(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by a key', async function () { - const ref = firebase.database().ref(TEST_PATH); - - try { - const snapshot = await ref.child('query').orderByKey().once('value'); - - const expected = ['a', 'b', 'c']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); - - describe('modular', function () { - it('throws if an orderBy call has already been set', async function () { - const { getDatabase, ref, orderByChild, orderByKey, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByChild('foo'), orderByKey()); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by a key', async function () { - const { getDatabase, ref, get, child, orderByKey, query } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - try { - const snapshot = await get(query(child(dbRef, 'query'), orderByKey())); - - const expected = ['a', 'b', 'c']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); -}); diff --git a/packages/database/e2e/query/orderByPriority.e2e.js b/packages/database/e2e/query/orderByPriority.e2e.js index 2d03bb866b..a33c83651a 100644 --- a/packages/database/e2e/query/orderByPriority.e2e.js +++ b/packages/database/e2e/query/orderByPriority.e2e.js @@ -27,37 +27,79 @@ describe('database().ref().orderByPriority()', function () { await wipe(TEST_PATH); }); - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByChild('foo').orderByPriority(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if an orderBy call has already been set', async function () { + try { + await firebase.database().ref().orderByChild('foo').orderByPriority(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } + }); + + it('order by priority', async function () { + const ref = firebase.database().ref(TEST_PATH).child('query'); + + await Promise.all([ + ref.child('a').setPriority(2), + ref.child('b').setPriority(3), + ref.child('c').setPriority(1), + ]); + + try { + const snapshot = await ref.orderByPriority().once('value'); + + const expected = ['c', 'a', 'b']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); + + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); - it('order by priority', async function () { - const ref = firebase.database().ref(TEST_PATH).child('query'); + describe('modular', function () { + it('throws if an orderBy call has already been set', async function () { + const { getDatabase, ref, orderByChild, orderByPriority, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByChild('foo'), orderByPriority()); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } + }); + + it('order by priority', async function () { + const { getDatabase, ref, child, setPriority, orderByPriority, get, query } = databaseModular; + + const dbRef = child(ref(getDatabase(), TEST_PATH), 'query'); - await Promise.all([ - ref.child('a').setPriority(2), - ref.child('b').setPriority(3), - ref.child('c').setPriority(1), - ]); + await Promise.all([ + setPriority(child(dbRef, 'a'), 2), + setPriority(child(dbRef, 'b'), 3), + setPriority(child(dbRef, 'c'), 1), + ]); - try { - const snapshot = await ref.orderByPriority().once('value'); + try { + const snapshot = await get(query(dbRef, orderByPriority())); - const expected = ['c', 'a', 'b']; + const expected = ['c', 'a', 'b']; - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); - return Promise.resolve(); - } catch (error) { - throw error; - } + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); }); diff --git a/packages/database/e2e/query/orderByPriority.modular.e2e.js b/packages/database/e2e/query/orderByPriority.modular.e2e.js deleted file mode 100644 index a33c83651a..0000000000 --- a/packages/database/e2e/query/orderByPriority.modular.e2e.js +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/orderByPriority`; - -describe('database().ref().orderByPriority()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByChild('foo').orderByPriority(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by priority', async function () { - const ref = firebase.database().ref(TEST_PATH).child('query'); - - await Promise.all([ - ref.child('a').setPriority(2), - ref.child('b').setPriority(3), - ref.child('c').setPriority(1), - ]); - - try { - const snapshot = await ref.orderByPriority().once('value'); - - const expected = ['c', 'a', 'b']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); - - describe('modular', function () { - it('throws if an orderBy call has already been set', async function () { - const { getDatabase, ref, orderByChild, orderByPriority, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByChild('foo'), orderByPriority()); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by priority', async function () { - const { getDatabase, ref, child, setPriority, orderByPriority, get, query } = databaseModular; - - const dbRef = child(ref(getDatabase(), TEST_PATH), 'query'); - - await Promise.all([ - setPriority(child(dbRef, 'a'), 2), - setPriority(child(dbRef, 'b'), 3), - setPriority(child(dbRef, 'c'), 1), - ]); - - try { - const snapshot = await get(query(dbRef, orderByPriority())); - - const expected = ['c', 'a', 'b']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); -}); diff --git a/packages/database/e2e/query/orderByValue.e2e.js b/packages/database/e2e/query/orderByValue.e2e.js index a925a304fe..b70298040e 100644 --- a/packages/database/e2e/query/orderByValue.e2e.js +++ b/packages/database/e2e/query/orderByValue.e2e.js @@ -27,37 +27,79 @@ describe('database().ref().orderByValue()', function () { await wipe(TEST_PATH); }); - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByChild('foo').orderByValue(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if an orderBy call has already been set', async function () { + try { + await firebase.database().ref().orderByChild('foo').orderByValue(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } + }); + + it('order by value', async function () { + const ref = firebase.database().ref(TEST_PATH).child('query'); + + await ref.set({ + a: 2, + b: 3, + c: 1, + }); + + try { + const snapshot = await ref.orderByValue().once('value'); + + const expected = ['c', 'a', 'b']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); + + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); - it('order by value', async function () { - const ref = firebase.database().ref(TEST_PATH).child('query'); + describe('modular', function () { + it('throws if an orderBy call has already been set', async function () { + const { getDatabase, ref, orderByChild, orderByValue, query } = databaseModular; - await ref.set({ - a: 2, - b: 3, - c: 1, + try { + query(ref(getDatabase()), orderByChild('foo'), orderByValue()); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("You can't combine multiple orderBy calls"); + return Promise.resolve(); + } }); - try { - const snapshot = await ref.orderByValue().once('value'); + it('order by value', async function () { + const { getDatabase, ref, child, set, orderByValue, get, query } = databaseModular; - const expected = ['c', 'a', 'b']; + const childRef = child(ref(getDatabase(), TEST_PATH), 'query'); - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); + await set(childRef, { + a: 2, + b: 3, + c: 1, }); - return Promise.resolve(); - } catch (error) { - throw error; - } + try { + const snapshot = await get(query(childRef, orderByValue())); + + const expected = ['c', 'a', 'b']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); + + return Promise.resolve(); + } catch (error) { + throw error; + } + }); }); }); diff --git a/packages/database/e2e/query/orderByValue.modular.e2e.js b/packages/database/e2e/query/orderByValue.modular.e2e.js deleted file mode 100644 index b70298040e..0000000000 --- a/packages/database/e2e/query/orderByValue.modular.e2e.js +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/orderByValue`; - -describe('database().ref().orderByValue()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if an orderBy call has already been set', async function () { - try { - await firebase.database().ref().orderByChild('foo').orderByValue(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by value', async function () { - const ref = firebase.database().ref(TEST_PATH).child('query'); - - await ref.set({ - a: 2, - b: 3, - c: 1, - }); - - try { - const snapshot = await ref.orderByValue().once('value'); - - const expected = ['c', 'a', 'b']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); - - describe('modular', function () { - it('throws if an orderBy call has already been set', async function () { - const { getDatabase, ref, orderByChild, orderByValue, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByChild('foo'), orderByValue()); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("You can't combine multiple orderBy calls"); - return Promise.resolve(); - } - }); - - it('order by value', async function () { - const { getDatabase, ref, child, set, orderByValue, get, query } = databaseModular; - - const childRef = child(ref(getDatabase(), TEST_PATH), 'query'); - - await set(childRef, { - a: 2, - b: 3, - c: 1, - }); - - try { - const snapshot = await get(query(childRef, orderByValue())); - - const expected = ['c', 'a', 'b']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - - return Promise.resolve(); - } catch (error) { - throw error; - } - }); - }); -}); diff --git a/packages/database/e2e/query/query.e2e.js b/packages/database/e2e/query/query.e2e.js index ccf72f8543..c252a16eda 100644 --- a/packages/database/e2e/query/query.e2e.js +++ b/packages/database/e2e/query/query.e2e.js @@ -15,16 +15,35 @@ */ describe('DatabaseQuery/DatabaseQueryModifiers', function () { - it('should not mutate previous queries (#2691)', async function () { - const queryBefore = firebase.database().ref(); - queryBefore._modifiers._modifiers.length.should.equal(0); + describe('v8 compatibility', function () { + it('should not mutate previous queries (#2691)', async function () { + const queryBefore = firebase.database().ref(); + queryBefore._modifiers._modifiers.length.should.equal(0); - const queryAfter = queryBefore.orderByChild('age'); - queryBefore._modifiers._modifiers.length.should.equal(0); - queryAfter._modifiers._modifiers.length.should.equal(1); + const queryAfter = queryBefore.orderByChild('age'); + queryBefore._modifiers._modifiers.length.should.equal(0); + queryAfter._modifiers._modifiers.length.should.equal(1); - const queryAfterAfter = queryAfter.equalTo(30); - queryAfter._modifiers._modifiers.length.should.equal(1); - queryAfterAfter._modifiers._modifiers.length.should.equal(3); // adds startAt endAt internally + const queryAfterAfter = queryAfter.equalTo(30); + queryAfter._modifiers._modifiers.length.should.equal(1); + queryAfterAfter._modifiers._modifiers.length.should.equal(3); // adds startAt endAt internally + }); + }); + + describe('modular', function () { + it('should not mutate previous queries (#2691)', async function () { + const { getDatabase, ref, query, orderByChild, equalTo } = databaseModular; + + const queryBefore = ref(getDatabase()); + queryBefore._modifiers._modifiers.length.should.equal(0); + + const queryAfter = query(queryBefore, orderByChild('age')); + queryBefore._modifiers._modifiers.length.should.equal(0); + queryAfter._modifiers._modifiers.length.should.equal(1); + + const queryAfterAfter = query(queryAfter, equalTo(30)); + queryAfter._modifiers._modifiers.length.should.equal(1); + queryAfterAfter._modifiers._modifiers.length.should.equal(3); // adds startAt endAt internally + }); }); }); diff --git a/packages/database/e2e/query/query.modular.e2e.js b/packages/database/e2e/query/query.modular.e2e.js deleted file mode 100644 index c252a16eda..0000000000 --- a/packages/database/e2e/query/query.modular.e2e.js +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -describe('DatabaseQuery/DatabaseQueryModifiers', function () { - describe('v8 compatibility', function () { - it('should not mutate previous queries (#2691)', async function () { - const queryBefore = firebase.database().ref(); - queryBefore._modifiers._modifiers.length.should.equal(0); - - const queryAfter = queryBefore.orderByChild('age'); - queryBefore._modifiers._modifiers.length.should.equal(0); - queryAfter._modifiers._modifiers.length.should.equal(1); - - const queryAfterAfter = queryAfter.equalTo(30); - queryAfter._modifiers._modifiers.length.should.equal(1); - queryAfterAfter._modifiers._modifiers.length.should.equal(3); // adds startAt endAt internally - }); - }); - - describe('modular', function () { - it('should not mutate previous queries (#2691)', async function () { - const { getDatabase, ref, query, orderByChild, equalTo } = databaseModular; - - const queryBefore = ref(getDatabase()); - queryBefore._modifiers._modifiers.length.should.equal(0); - - const queryAfter = query(queryBefore, orderByChild('age')); - queryBefore._modifiers._modifiers.length.should.equal(0); - queryAfter._modifiers._modifiers.length.should.equal(1); - - const queryAfterAfter = query(queryAfter, equalTo(30)); - queryAfter._modifiers._modifiers.length.should.equal(1); - queryAfterAfter._modifiers._modifiers.length.should.equal(3); // adds startAt endAt internally - }); - }); -}); diff --git a/packages/database/e2e/query/startAt.e2e.js b/packages/database/e2e/query/startAt.e2e.js index 7849539213..13d30025da 100644 --- a/packages/database/e2e/query/startAt.e2e.js +++ b/packages/database/e2e/query/startAt.e2e.js @@ -27,104 +27,225 @@ describe('database().ref().startAt()', function () { await wipe(TEST_PATH); }); - it('throws if an value is undefined', async function () { - try { - await firebase.database().ref().startAt(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if an value is undefined', async function () { + try { + await firebase.database().ref().startAt(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be a number, string, boolean or null value"); + return Promise.resolve(); + } + }); - it('throws if an key is not a string', async function () { - try { - await firebase.database().ref().startAt('foo', 1234); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); + it('throws if an key is not a string', async function () { + try { + await firebase.database().ref().startAt('foo', 1234); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'key' must be a string value if defined"); + return Promise.resolve(); + } + }); - it('throws if a starting point has already been set', async function () { - try { - await firebase.database().ref().equalTo('foo').startAt('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Starting point was already set (by another call to startAt or equalTo)', - ); - return Promise.resolve(); - } - }); + it('throws if a starting point has already been set', async function () { + try { + await firebase.database().ref().equalTo('foo').startAt('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Starting point was already set (by another call to startAt or equalTo)', + ); + return Promise.resolve(); + } + }); - it('throws if ordering by key and the key param is set', async function () { - try { - await firebase.database().ref().orderByKey('foo').startAt('foo', 'bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', - ); - return Promise.resolve(); - } - }); + it('throws if ordering by key and the key param is set', async function () { + try { + await firebase.database().ref().orderByKey('foo').startAt('foo', 'bar'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', + ); + return Promise.resolve(); + } + }); - it('throws if ordering by key and the value param is not a string', async function () { - try { - await firebase.database().ref().orderByKey('foo').startAt(123); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', - ); - return Promise.resolve(); - } - }); + it('throws if ordering by key and the value param is not a string', async function () { + try { + await firebase.database().ref().orderByKey('foo').startAt(123); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', + ); + return Promise.resolve(); + } + }); - it('throws if ordering by priority and the value param is not priority type', async function () { - try { - await firebase.database().ref().orderByPriority().startAt(true); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', - ); - return Promise.resolve(); - } - }); + it('throws if ordering by priority and the value param is not priority type', async function () { + try { + await firebase.database().ref().orderByPriority().startAt(true); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', + ); + return Promise.resolve(); + } + }); - it('snapshot value is null when no ordering modifier is applied', async function () { - const ref = firebase.database().ref(TEST_PATH); + it('snapshot value is null when no ordering modifier is applied', async function () { + const ref = firebase.database().ref(TEST_PATH); - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, + await ref.set({ + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const snapshot = await ref.startAt(2).once('value'); + should.equal(snapshot.val(), null); }); - const snapshot = await ref.startAt(2).once('value'); - should.equal(snapshot.val(), null); + it('starts at the correct value', async function () { + const ref = firebase.database().ref(TEST_PATH); + + await ref.set({ + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const snapshot = await ref.orderByValue().startAt(2).once('value'); + + const expected = ['b', 'c', 'd']; + + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); + }); }); - it('starts at the correct value', async function () { - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + it('throws if an value is undefined', async function () { + const { getDatabase, ref, startAt, query } = databaseModular; + + try { + query(ref(getDatabase()), startAt()); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be a number, string, boolean or null value"); + return Promise.resolve(); + } + }); + + it('throws if an key is not a string', async function () { + const { getDatabase, ref, startAt, query } = databaseModular; + + try { + query(ref(getDatabase()), startAt('foo', 1234)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'key' must be a string value if defined"); + return Promise.resolve(); + } + }); + + it('throws if a starting point has already been set', async function () { + const { getDatabase, ref, equalTo, startAt, query } = databaseModular; + + try { + query(ref(getDatabase()), equalTo('foo'), startAt('foo')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'Starting point was already set (by another call to startAt or equalTo)', + ); + return Promise.resolve(); + } + }); + + it('throws if ordering by key and the key param is set', async function () { + const { getDatabase, ref, orderByKey, startAt, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByKey('foo'), startAt('foo', 'bar')); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', + ); + return Promise.resolve(); + } + }); + + it('throws if ordering by key and the value param is not a string', async function () { + const { getDatabase, ref, orderByKey, startAt, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByKey('foo'), startAt(123)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', + ); + return Promise.resolve(); + } + }); - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, + it('throws if ordering by priority and the value param is not priority type', async function () { + const { getDatabase, ref, orderByPriority, startAt, query } = databaseModular; + + try { + query(ref(getDatabase()), orderByPriority(), startAt(true)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', + ); + return Promise.resolve(); + } }); - const snapshot = await ref.orderByValue().startAt(2).once('value'); + it('snapshot value is null when no ordering modifier is applied', async function () { + const { getDatabase, ref, set, startAt, get, query } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + await set(dbRef, { + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const snapshot = await get(query(dbRef, startAt(2))); + should.equal(snapshot.val(), null); + }); + + it('starts at the correct value', async function () { + const { getDatabase, ref, set, startAt, get, query, orderByValue } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + await set(dbRef, { + a: 1, + b: 2, + c: 3, + d: 4, + }); + + const snapshot = await get(query(dbRef, orderByValue(), startAt(2))); - const expected = ['b', 'c', 'd']; + const expected = ['b', 'c', 'd']; - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); + snapshot.forEach((childSnapshot, i) => { + childSnapshot.key.should.eql(expected[i]); + }); }); }); }); diff --git a/packages/database/e2e/query/startAt.modular.e2e.js b/packages/database/e2e/query/startAt.modular.e2e.js deleted file mode 100644 index 13d30025da..0000000000 --- a/packages/database/e2e/query/startAt.modular.e2e.js +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/startAt`; - -describe('database().ref().startAt()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if an value is undefined', async function () { - try { - await firebase.database().ref().startAt(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); - - it('throws if an key is not a string', async function () { - try { - await firebase.database().ref().startAt('foo', 1234); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); - - it('throws if a starting point has already been set', async function () { - try { - await firebase.database().ref().equalTo('foo').startAt('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Starting point was already set (by another call to startAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the key param is set', async function () { - try { - await firebase.database().ref().orderByKey('foo').startAt('foo', 'bar'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the value param is not a string', async function () { - try { - await firebase.database().ref().orderByKey('foo').startAt(123); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by priority and the value param is not priority type', async function () { - try { - await firebase.database().ref().orderByPriority().startAt(true); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', - ); - return Promise.resolve(); - } - }); - - it('snapshot value is null when no ordering modifier is applied', async function () { - const ref = firebase.database().ref(TEST_PATH); - - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const snapshot = await ref.startAt(2).once('value'); - should.equal(snapshot.val(), null); - }); - - it('starts at the correct value', async function () { - const ref = firebase.database().ref(TEST_PATH); - - await ref.set({ - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const snapshot = await ref.orderByValue().startAt(2).once('value'); - - const expected = ['b', 'c', 'd']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - }); - - describe('modular', function () { - it('throws if an value is undefined', async function () { - const { getDatabase, ref, startAt, query } = databaseModular; - - try { - query(ref(getDatabase()), startAt()); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be a number, string, boolean or null value"); - return Promise.resolve(); - } - }); - - it('throws if an key is not a string', async function () { - const { getDatabase, ref, startAt, query } = databaseModular; - - try { - query(ref(getDatabase()), startAt('foo', 1234)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'key' must be a string value if defined"); - return Promise.resolve(); - } - }); - - it('throws if a starting point has already been set', async function () { - const { getDatabase, ref, equalTo, startAt, query } = databaseModular; - - try { - query(ref(getDatabase()), equalTo('foo'), startAt('foo')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'Starting point was already set (by another call to startAt or equalTo)', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the key param is set', async function () { - const { getDatabase, ref, orderByKey, startAt, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByKey('foo'), startAt('foo', 'bar')); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, you may only pass a value argument to startAt(), endAt(), or equalTo()', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by key and the value param is not a string', async function () { - const { getDatabase, ref, orderByKey, startAt, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByKey('foo'), startAt(123)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by key, the value of startAt(), endAt(), or equalTo() must be a string', - ); - return Promise.resolve(); - } - }); - - it('throws if ordering by priority and the value param is not priority type', async function () { - const { getDatabase, ref, orderByPriority, startAt, query } = databaseModular; - - try { - query(ref(getDatabase()), orderByPriority(), startAt(true)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'When ordering by priority, the first value of startAt(), endAt(), or equalTo() must be a valid priority value (null, a number, or a string)', - ); - return Promise.resolve(); - } - }); - - it('snapshot value is null when no ordering modifier is applied', async function () { - const { getDatabase, ref, set, startAt, get, query } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - await set(dbRef, { - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const snapshot = await get(query(dbRef, startAt(2))); - should.equal(snapshot.val(), null); - }); - - it('starts at the correct value', async function () { - const { getDatabase, ref, set, startAt, get, query, orderByValue } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - await set(dbRef, { - a: 1, - b: 2, - c: 3, - d: 4, - }); - - const snapshot = await get(query(dbRef, orderByValue(), startAt(2))); - - const expected = ['b', 'c', 'd']; - - snapshot.forEach((childSnapshot, i) => { - childSnapshot.key.should.eql(expected[i]); - }); - }); - }); -}); diff --git a/packages/database/e2e/query/toJSON.e2e.js b/packages/database/e2e/query/toJSON.e2e.js index 96a5e94f32..3efb80cae8 100644 --- a/packages/database/e2e/query/toJSON.e2e.js +++ b/packages/database/e2e/query/toJSON.e2e.js @@ -16,9 +16,22 @@ */ describe('database().ref().toJSON()', function () { - it('returns a string version of the current query path', async function () { - const res = firebase.database().ref('foo/bar/baz').toJSON(); - const expected = `${firebase.database()._customUrlOrRegion}/foo/bar/baz`; - should.equal(res, expected); + describe('v8 compatibility', function () { + it('returns a string version of the current query path', async function () { + const res = firebase.database().ref('foo/bar/baz').toJSON(); + const expected = `${firebase.database()._customUrlOrRegion}/foo/bar/baz`; + should.equal(res, expected); + }); + }); + + describe('modular', function () { + it('returns a string version of the current query path', async function () { + const { getDatabase, ref } = databaseModular; + + const db = getDatabase(); + const res = ref(db, 'foo/bar/baz').toJSON(); + const expected = `${db._customUrlOrRegion}/foo/bar/baz`; + should.equal(res, expected); + }); }); }); diff --git a/packages/database/e2e/query/toJSON.modular.e2e.js b/packages/database/e2e/query/toJSON.modular.e2e.js deleted file mode 100644 index 3efb80cae8..0000000000 --- a/packages/database/e2e/query/toJSON.modular.e2e.js +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe('database().ref().toJSON()', function () { - describe('v8 compatibility', function () { - it('returns a string version of the current query path', async function () { - const res = firebase.database().ref('foo/bar/baz').toJSON(); - const expected = `${firebase.database()._customUrlOrRegion}/foo/bar/baz`; - should.equal(res, expected); - }); - }); - - describe('modular', function () { - it('returns a string version of the current query path', async function () { - const { getDatabase, ref } = databaseModular; - - const db = getDatabase(); - const res = ref(db, 'foo/bar/baz').toJSON(); - const expected = `${db._customUrlOrRegion}/foo/bar/baz`; - should.equal(res, expected); - }); - }); -}); diff --git a/packages/database/e2e/reference/child.e2e.js b/packages/database/e2e/reference/child.e2e.js index 2827b7a8eb..d729dbc862 100644 --- a/packages/database/e2e/reference/child.e2e.js +++ b/packages/database/e2e/reference/child.e2e.js @@ -16,25 +16,59 @@ */ describe('database().ref().child()', function () { - it('throws if path is not a string', async function () { - try { - firebase.database().ref().child({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if path is not a string', async function () { + try { + firebase.database().ref().child({ foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + return Promise.resolve(); + } + }); + + it('throws if path is not a valid string', async function () { + try { + firebase.database().ref().child('$$$$$'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'firebase.database() Paths must be non-empty strings and can\'t contain ".", "#", "$", "[", or "]"', + ); + return Promise.resolve(); + } + }); }); - it('throws if path is not a valid string', async function () { - try { - firebase.database().ref().child('$$$$$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'firebase.database() Paths must be non-empty strings and can\'t contain ".", "#", "$", "[", or "]"', - ); - return Promise.resolve(); - } + describe('modular', function () { + it('throws if path is not a string', async function () { + const { getDatabase, ref, child } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db); + + try { + child(dbRef, { foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + return Promise.resolve(); + } + }); + + it('throws if path is not a valid string', async function () { + const { getDatabase, ref, child } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db); + + try { + child(dbRef, '$$$$$'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql( + 'firebase.database() Paths must be non-empty strings and can\'t contain ".", "#", "$", "[", or "]"', + ); + return Promise.resolve(); + } + }); }); }); diff --git a/packages/database/e2e/reference/child.modular.e2e.js b/packages/database/e2e/reference/child.modular.e2e.js deleted file mode 100644 index d729dbc862..0000000000 --- a/packages/database/e2e/reference/child.modular.e2e.js +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe('database().ref().child()', function () { - describe('v8 compatibility', function () { - it('throws if path is not a string', async function () { - try { - firebase.database().ref().child({ foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); - - it('throws if path is not a valid string', async function () { - try { - firebase.database().ref().child('$$$$$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'firebase.database() Paths must be non-empty strings and can\'t contain ".", "#", "$", "[", or "]"', - ); - return Promise.resolve(); - } - }); - }); - - describe('modular', function () { - it('throws if path is not a string', async function () { - const { getDatabase, ref, child } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db); - - try { - child(dbRef, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - return Promise.resolve(); - } - }); - - it('throws if path is not a valid string', async function () { - const { getDatabase, ref, child } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db); - - try { - child(dbRef, '$$$$$'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql( - 'firebase.database() Paths must be non-empty strings and can\'t contain ".", "#", "$", "[", or "]"', - ); - return Promise.resolve(); - } - }); - }); -}); diff --git a/packages/database/e2e/reference/key.e2e.js b/packages/database/e2e/reference/key.e2e.js index bcccdd22c1..0ed277b6ee 100644 --- a/packages/database/e2e/reference/key.e2e.js +++ b/packages/database/e2e/reference/key.e2e.js @@ -16,15 +16,36 @@ */ describe('database().ref().key', function () { - it('returns null when no reference path is provides', function () { - const ref = firebase.database().ref(); - should.equal(ref.key, null); + describe('v8 compatibility', function () { + it('returns null when no reference path is provides', function () { + const ref = firebase.database().ref(); + should.equal(ref.key, null); + }); + + it('return last token in reference path', function () { + const ref1 = firebase.database().ref('foo'); + const ref2 = firebase.database().ref('foo/bar/baz'); + ref1.key.should.equal('foo'); + ref2.key.should.equal('baz'); + }); }); - it('return last token in reference path', function () { - const ref1 = firebase.database().ref('foo'); - const ref2 = firebase.database().ref('foo/bar/baz'); - ref1.key.should.equal('foo'); - ref2.key.should.equal('baz'); + describe('modular', function () { + it('returns null when no reference path is provides', function () { + const { getDatabase, ref } = databaseModular; + + const dbRef = ref(getDatabase()); + should.equal(dbRef.key, null); + }); + + it('return last token in reference path', function () { + const { getDatabase, ref } = databaseModular; + + const db = getDatabase(); + const ref1 = ref(db, 'foo'); + const ref2 = ref(db, 'foo/bar/baz'); + ref1.key.should.equal('foo'); + ref2.key.should.equal('baz'); + }); }); }); diff --git a/packages/database/e2e/reference/key.modular.e2e.js b/packages/database/e2e/reference/key.modular.e2e.js deleted file mode 100644 index 0ed277b6ee..0000000000 --- a/packages/database/e2e/reference/key.modular.e2e.js +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe('database().ref().key', function () { - describe('v8 compatibility', function () { - it('returns null when no reference path is provides', function () { - const ref = firebase.database().ref(); - should.equal(ref.key, null); - }); - - it('return last token in reference path', function () { - const ref1 = firebase.database().ref('foo'); - const ref2 = firebase.database().ref('foo/bar/baz'); - ref1.key.should.equal('foo'); - ref2.key.should.equal('baz'); - }); - }); - - describe('modular', function () { - it('returns null when no reference path is provides', function () { - const { getDatabase, ref } = databaseModular; - - const dbRef = ref(getDatabase()); - should.equal(dbRef.key, null); - }); - - it('return last token in reference path', function () { - const { getDatabase, ref } = databaseModular; - - const db = getDatabase(); - const ref1 = ref(db, 'foo'); - const ref2 = ref(db, 'foo/bar/baz'); - ref1.key.should.equal('foo'); - ref2.key.should.equal('baz'); - }); - }); -}); diff --git a/packages/database/e2e/reference/onDisconnect.e2e.js b/packages/database/e2e/reference/onDisconnect.e2e.js index e308e7f6e2..8540faf7c4 100644 --- a/packages/database/e2e/reference/onDisconnect.e2e.js +++ b/packages/database/e2e/reference/onDisconnect.e2e.js @@ -18,8 +18,19 @@ // See onDisconnect directory for specific tests describe('database().ref().onDisconnect()', function () { - it('returns a new DatabaseOnDisconnect instance', function () { - const instance = firebase.database().ref().onDisconnect(); - instance.constructor.name.should.eql('DatabaseOnDisconnect'); + describe('v8 compatibility', function () { + it('returns a new DatabaseOnDisconnect instance', function () { + const instance = firebase.database().ref().onDisconnect(); + instance.constructor.name.should.eql('DatabaseOnDisconnect'); + }); + }); + + describe('modular', function () { + it('returns a new DatabaseOnDisconnect instance', function () { + const { getDatabase, ref, onDisconnect } = databaseModular; + + const instance = onDisconnect(ref(getDatabase())); + instance.constructor.name.should.eql('DatabaseOnDisconnect'); + }); }); }); diff --git a/packages/database/e2e/reference/onDisconnect.modular.e2e.js b/packages/database/e2e/reference/onDisconnect.modular.e2e.js deleted file mode 100644 index 8540faf7c4..0000000000 --- a/packages/database/e2e/reference/onDisconnect.modular.e2e.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -// See onDisconnect directory for specific tests - -describe('database().ref().onDisconnect()', function () { - describe('v8 compatibility', function () { - it('returns a new DatabaseOnDisconnect instance', function () { - const instance = firebase.database().ref().onDisconnect(); - instance.constructor.name.should.eql('DatabaseOnDisconnect'); - }); - }); - - describe('modular', function () { - it('returns a new DatabaseOnDisconnect instance', function () { - const { getDatabase, ref, onDisconnect } = databaseModular; - - const instance = onDisconnect(ref(getDatabase())); - instance.constructor.name.should.eql('DatabaseOnDisconnect'); - }); - }); -}); diff --git a/packages/database/e2e/reference/parent.e2e.js b/packages/database/e2e/reference/parent.e2e.js index 592b6e2f12..7dec1d8799 100644 --- a/packages/database/e2e/reference/parent.e2e.js +++ b/packages/database/e2e/reference/parent.e2e.js @@ -16,15 +16,36 @@ */ describe('database().ref().parent', function () { - it('returns null when no reference path is provides', function () { - const ref = firebase.database().ref(); - should.equal(ref.parent, null); + describe('v8 compatibility', function () { + it('returns null when no reference path is provides', function () { + const ref = firebase.database().ref(); + should.equal(ref.parent, null); + }); + + it('return last token in reference path', function () { + const ref1 = firebase.database().ref('/foo').parent; + const ref2 = firebase.database().ref('/foo/bar/baz').parent; + should.equal(ref1, null); + ref2.key.should.equal('bar'); + }); }); - it('return last token in reference path', function () { - const ref1 = firebase.database().ref('/foo').parent; - const ref2 = firebase.database().ref('/foo/bar/baz').parent; - should.equal(ref1, null); - ref2.key.should.equal('bar'); + describe('modular', function () { + it('returns null when no reference path is provides', function () { + const { getDatabase, ref } = databaseModular; + + const dbRef = ref(getDatabase()); + should.equal(dbRef.parent, null); + }); + + it('return last token in reference path', function () { + const { getDatabase, ref } = databaseModular; + + const db = getDatabase(); + const ref1 = ref(db, '/foo').parent; + const ref2 = ref(db, '/foo/bar/baz').parent; + should.equal(ref1, null); + ref2.key.should.equal('bar'); + }); }); }); diff --git a/packages/database/e2e/reference/parent.modular.e2e.js b/packages/database/e2e/reference/parent.modular.e2e.js deleted file mode 100644 index 7dec1d8799..0000000000 --- a/packages/database/e2e/reference/parent.modular.e2e.js +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe('database().ref().parent', function () { - describe('v8 compatibility', function () { - it('returns null when no reference path is provides', function () { - const ref = firebase.database().ref(); - should.equal(ref.parent, null); - }); - - it('return last token in reference path', function () { - const ref1 = firebase.database().ref('/foo').parent; - const ref2 = firebase.database().ref('/foo/bar/baz').parent; - should.equal(ref1, null); - ref2.key.should.equal('bar'); - }); - }); - - describe('modular', function () { - it('returns null when no reference path is provides', function () { - const { getDatabase, ref } = databaseModular; - - const dbRef = ref(getDatabase()); - should.equal(dbRef.parent, null); - }); - - it('return last token in reference path', function () { - const { getDatabase, ref } = databaseModular; - - const db = getDatabase(); - const ref1 = ref(db, '/foo').parent; - const ref2 = ref(db, '/foo/bar/baz').parent; - should.equal(ref1, null); - ref2.key.should.equal('bar'); - }); - }); -}); diff --git a/packages/database/e2e/reference/push.e2e.js b/packages/database/e2e/reference/push.e2e.js index 234c55a5ec..f57bff6b5d 100644 --- a/packages/database/e2e/reference/push.e2e.js +++ b/packages/database/e2e/reference/push.e2e.js @@ -20,72 +20,123 @@ const { PATH } = require('../helpers'); const TEST_PATH = `${PATH}/push`; describe('database().ref().push()', function () { - it('throws if on complete callback is not a function', function () { - try { - firebase.database().ref(TEST_PATH).push('foo', 'bar'); - return Promise.reject(new Error('Did not throw Error')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if on complete callback is not a function', function () { + try { + firebase.database().ref(TEST_PATH).push('foo', 'bar'); + return Promise.reject(new Error('Did not throw Error')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); - it('returns a promise when no value is passed', function () { - const ref = firebase.database().ref(`${TEST_PATH}/boop`); - const pushed = ref.push(); - return pushed - .then(childRef => { - pushed.ref.parent.toString().should.eql(ref.toString()); - pushed.toString().should.eql(childRef.toString()); - return pushed.once('value'); - }) - .then(snap => { - should.equal(snap.val(), null); - snap.ref.toString().should.eql(pushed.toString()); + it('returns a promise when no value is passed', function () { + const ref = firebase.database().ref(`${TEST_PATH}/boop`); + const pushed = ref.push(); + return pushed + .then(childRef => { + pushed.ref.parent.toString().should.eql(ref.toString()); + pushed.toString().should.eql(childRef.toString()); + return pushed.once('value'); + }) + .then(snap => { + should.equal(snap.val(), null); + snap.ref.toString().should.eql(pushed.toString()); + }); + }); + + it('returns a promise and sets the provided value', function () { + const ref = firebase.database().ref(`${TEST_PATH}/value`); + const pushed = ref.push(6); + return pushed + .then(childRef => { + pushed.ref.parent.toString().should.eql(ref.toString()); + pushed.toString().should.eql(childRef.toString()); + return pushed.once('value'); + }) + .then(snap => { + snap.val().should.equal(6); + snap.ref.toString().should.eql(pushed.toString()); + }); + }); + + it('returns a to the callback if provided once set', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(`${TEST_PATH}/callback`); + const value = Date.now(); + ref.push(value, () => { + callback(); }); - }); + await Utils.spyToBeCalledOnceAsync(callback); + }); - it('returns a promise and sets the provided value', function () { - const ref = firebase.database().ref(`${TEST_PATH}/value`); - const pushed = ref.push(6); - return pushed - .then(childRef => { - pushed.ref.parent.toString().should.eql(ref.toString()); - pushed.toString().should.eql(childRef.toString()); - return pushed.once('value'); - }) - .then(snap => { - snap.val().should.equal(6); - snap.ref.toString().should.eql(pushed.toString()); + it('throws if push errors', async function () { + const ref = firebase.database().ref('nope'); + return ref.push('foo').catch(error => { + error.message.should.containEql("doesn't have permission to access"); + return Promise.resolve(); }); - }); + }); - it('returns a to the callback if provided once set', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(`${TEST_PATH}/callback`); - const value = Date.now(); - ref.push(value, () => { - callback(); + it('returns an error to the callback', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref('nope'); + ref.push('foo', error => { + error.message.should.containEql("doesn't have permission to access"); + callback(); + }); + await Utils.spyToBeCalledOnceAsync(callback); + callback.should.be.calledOnce(); }); - await Utils.spyToBeCalledOnceAsync(callback); }); - it('throws if push errors', async function () { - const ref = firebase.database().ref('nope'); - return ref.push('foo').catch(error => { - error.message.should.containEql("doesn't have permission to access"); - return Promise.resolve(); + describe('modular', function () { + it('returns a promise when no value is passed', function () { + const { getDatabase, ref, push } = databaseModular; + + const dbRef = ref(getDatabase(), `${TEST_PATH}/boop`); + const pushed = push(dbRef); + return pushed + .then(childRef => { + pushed.ref.parent.toString().should.eql(dbRef.toString()); + pushed.toString().should.eql(childRef.toString()); + return pushed.once('value'); + }) + .then(snap => { + should.equal(snap.val(), null); + snap.ref.toString().should.eql(pushed.toString()); + }); }); - }); - it('returns an error to the callback', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref('nope'); - ref.push('foo', error => { - error.message.should.containEql("doesn't have permission to access"); - callback(); + it('returns a promise and sets the provided value', function () { + const { getDatabase, ref, push } = databaseModular; + + const dbRef = ref(getDatabase(), `${TEST_PATH}/value`); + const pushed = push(dbRef, 6); + return pushed + .then(childRef => { + pushed.ref.parent.toString().should.eql(dbRef.toString()); + pushed.toString().should.eql(childRef.toString()); + return pushed.once('value'); + }) + .then(snap => { + snap.val().should.equal(6); + snap.ref.toString().should.eql(pushed.toString()); + }); + }); + + it('throws if push errors', async function () { + const { getDatabase, ref, push } = databaseModular; + + const dbRef = ref(getDatabase(), 'nope'); + try { + await push(dbRef, 'foo'); + return Promise.reject(new Error('Did not throw Error')); + } catch (error) { + error.message.should.containEql("doesn't have permission to access"); + return Promise.resolve(); + } }); - await Utils.spyToBeCalledOnceAsync(callback); - callback.should.be.calledOnce(); }); }); diff --git a/packages/database/e2e/reference/push.modular.e2e.js b/packages/database/e2e/reference/push.modular.e2e.js deleted file mode 100644 index f57bff6b5d..0000000000 --- a/packages/database/e2e/reference/push.modular.e2e.js +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH } = require('../helpers'); - -const TEST_PATH = `${PATH}/push`; - -describe('database().ref().push()', function () { - describe('v8 compatibility', function () { - it('throws if on complete callback is not a function', function () { - try { - firebase.database().ref(TEST_PATH).push('foo', 'bar'); - return Promise.reject(new Error('Did not throw Error')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('returns a promise when no value is passed', function () { - const ref = firebase.database().ref(`${TEST_PATH}/boop`); - const pushed = ref.push(); - return pushed - .then(childRef => { - pushed.ref.parent.toString().should.eql(ref.toString()); - pushed.toString().should.eql(childRef.toString()); - return pushed.once('value'); - }) - .then(snap => { - should.equal(snap.val(), null); - snap.ref.toString().should.eql(pushed.toString()); - }); - }); - - it('returns a promise and sets the provided value', function () { - const ref = firebase.database().ref(`${TEST_PATH}/value`); - const pushed = ref.push(6); - return pushed - .then(childRef => { - pushed.ref.parent.toString().should.eql(ref.toString()); - pushed.toString().should.eql(childRef.toString()); - return pushed.once('value'); - }) - .then(snap => { - snap.val().should.equal(6); - snap.ref.toString().should.eql(pushed.toString()); - }); - }); - - it('returns a to the callback if provided once set', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(`${TEST_PATH}/callback`); - const value = Date.now(); - ref.push(value, () => { - callback(); - }); - await Utils.spyToBeCalledOnceAsync(callback); - }); - - it('throws if push errors', async function () { - const ref = firebase.database().ref('nope'); - return ref.push('foo').catch(error => { - error.message.should.containEql("doesn't have permission to access"); - return Promise.resolve(); - }); - }); - - it('returns an error to the callback', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref('nope'); - ref.push('foo', error => { - error.message.should.containEql("doesn't have permission to access"); - callback(); - }); - await Utils.spyToBeCalledOnceAsync(callback); - callback.should.be.calledOnce(); - }); - }); - - describe('modular', function () { - it('returns a promise when no value is passed', function () { - const { getDatabase, ref, push } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/boop`); - const pushed = push(dbRef); - return pushed - .then(childRef => { - pushed.ref.parent.toString().should.eql(dbRef.toString()); - pushed.toString().should.eql(childRef.toString()); - return pushed.once('value'); - }) - .then(snap => { - should.equal(snap.val(), null); - snap.ref.toString().should.eql(pushed.toString()); - }); - }); - - it('returns a promise and sets the provided value', function () { - const { getDatabase, ref, push } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/value`); - const pushed = push(dbRef, 6); - return pushed - .then(childRef => { - pushed.ref.parent.toString().should.eql(dbRef.toString()); - pushed.toString().should.eql(childRef.toString()); - return pushed.once('value'); - }) - .then(snap => { - snap.val().should.equal(6); - snap.ref.toString().should.eql(pushed.toString()); - }); - }); - - it('throws if push errors', async function () { - const { getDatabase, ref, push } = databaseModular; - - const dbRef = ref(getDatabase(), 'nope'); - try { - await push(dbRef, 'foo'); - return Promise.reject(new Error('Did not throw Error')); - } catch (error) { - error.message.should.containEql("doesn't have permission to access"); - return Promise.resolve(); - } - }); - }); -}); diff --git a/packages/database/e2e/reference/remove.e2e.js b/packages/database/e2e/reference/remove.e2e.js index 4b62800801..4fa18e76a2 100644 --- a/packages/database/e2e/reference/remove.e2e.js +++ b/packages/database/e2e/reference/remove.e2e.js @@ -20,21 +20,35 @@ const { PATH } = require('../helpers'); const TEST_PATH = `${PATH}/remove`; describe('database().ref().remove()', function () { - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref(TEST_PATH).remove('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } + describe('v8 compatibility', function () { + it('throws if onComplete is not a function', async function () { + try { + await firebase.database().ref(TEST_PATH).remove('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + it('removes a value at the path', async function () { + const ref = firebase.database().ref(TEST_PATH); + await ref.set('foo'); + await ref.remove(); + const snapshot = await ref.once('value'); + snapshot.exists().should.equal(false); + }); }); - it('removes a value at the path', async function () { - const ref = firebase.database().ref(TEST_PATH); - await ref.set('foo'); - await ref.remove(); - const snapshot = await ref.once('value'); - snapshot.exists().should.equal(false); + describe('modular', function () { + it('removes a value at the path', async function () { + const { getDatabase, ref, set, remove, get } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + await set(dbRef, 'foo'); + await remove(dbRef); + const snapshot = await get(dbRef); + snapshot.exists().should.equal(false); + }); }); }); diff --git a/packages/database/e2e/reference/remove.modular.e2e.js b/packages/database/e2e/reference/remove.modular.e2e.js deleted file mode 100644 index 4fa18e76a2..0000000000 --- a/packages/database/e2e/reference/remove.modular.e2e.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH } = require('../helpers'); - -const TEST_PATH = `${PATH}/remove`; - -describe('database().ref().remove()', function () { - describe('v8 compatibility', function () { - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref(TEST_PATH).remove('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('removes a value at the path', async function () { - const ref = firebase.database().ref(TEST_PATH); - await ref.set('foo'); - await ref.remove(); - const snapshot = await ref.once('value'); - snapshot.exists().should.equal(false); - }); - }); - - describe('modular', function () { - it('removes a value at the path', async function () { - const { getDatabase, ref, set, remove, get } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - await set(dbRef, 'foo'); - await remove(dbRef); - const snapshot = await get(dbRef); - snapshot.exists().should.equal(false); - }); - }); -}); diff --git a/packages/database/e2e/reference/root.e2e.js b/packages/database/e2e/reference/root.e2e.js index c7677ce9ee..227076830c 100644 --- a/packages/database/e2e/reference/root.e2e.js +++ b/packages/database/e2e/reference/root.e2e.js @@ -16,8 +16,19 @@ */ describe('database().ref().root', function () { - it('returns a root reference', function () { - const ref = firebase.database().ref('foo/bar/baz'); - should.equal(ref.root.key, null); + describe('v8 compatibility', function () { + it('returns a root reference', function () { + const ref = firebase.database().ref('foo/bar/baz'); + should.equal(ref.root.key, null); + }); + }); + + describe('modular', function () { + it('returns a root reference', function () { + const { getDatabase, ref } = databaseModular; + + const dbRef = ref(getDatabase(), 'foo/bar/baz'); + should.equal(dbRef.root.key, null); + }); }); }); diff --git a/packages/database/e2e/reference/root.modular.e2e.js b/packages/database/e2e/reference/root.modular.e2e.js deleted file mode 100644 index 227076830c..0000000000 --- a/packages/database/e2e/reference/root.modular.e2e.js +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -describe('database().ref().root', function () { - describe('v8 compatibility', function () { - it('returns a root reference', function () { - const ref = firebase.database().ref('foo/bar/baz'); - should.equal(ref.root.key, null); - }); - }); - - describe('modular', function () { - it('returns a root reference', function () { - const { getDatabase, ref } = databaseModular; - - const dbRef = ref(getDatabase(), 'foo/bar/baz'); - should.equal(dbRef.root.key, null); - }); - }); -}); diff --git a/packages/database/e2e/reference/set.e2e.js b/packages/database/e2e/reference/set.e2e.js index 3f49bbb5ed..adaaf39f43 100644 --- a/packages/database/e2e/reference/set.e2e.js +++ b/packages/database/e2e/reference/set.e2e.js @@ -19,7 +19,7 @@ const { PATH, seed, wipe } = require('../helpers'); const TEST_PATH = `${PATH}/set`; -describe('database().ref().set()', function () { +describe('set', function () { before(async function () { await seed(TEST_PATH); }); @@ -27,49 +27,92 @@ describe('database().ref().set()', function () { await wipe(TEST_PATH); }); - it('throws if no value is provided', async function () { - try { - await firebase.database().ref(TEST_PATH).set(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if no value is provided', async function () { + try { + await firebase.database().ref(TEST_PATH).set(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be defined"); + return Promise.resolve(); + } + }); - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref(TEST_PATH).set(null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', async function () { + try { + await firebase.database().ref(TEST_PATH).set(null, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); - it('sets a new value', async function () { - const value = Date.now(); - const ref = firebase.database().ref(TEST_PATH); - await ref.set(value); - const snapshot = await ref.once('value'); - snapshot.val().should.eql(value); - }); + it('sets a new value', async function () { + const value = Date.now(); + const ref = firebase.database().ref(TEST_PATH); + await ref.set(value); + const snapshot = await ref.once('value'); + snapshot.val().should.eql(value); + }); + + it('callback if function is passed', async function () { + const value = Date.now(); + return new Promise(async resolve => { + await firebase.database().ref(TEST_PATH).set(value, resolve); + }); + }); - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase.database().ref(TEST_PATH).set(value, resolve); + it('throws if permission defined', async function () { + const value = Date.now(); + try { + await firebase.database().ref('nope/foo').set(value); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + error.code.includes('database/permission-denied').should.be.true(); + return Promise.resolve(); + } }); }); - it('throws if permission defined', async function () { - const value = Date.now(); - try { - await firebase.database().ref('nope/foo').set(value); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.code.includes('database/permission-denied').should.be.true(); - return Promise.resolve(); - } + describe('modular', function () { + it('throws if no value is provided', async function () { + const { getDatabase, ref, set } = databaseModular; + const db = getDatabase(); + + try { + await set(ref(db, TEST_PATH)); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'value' must be defined"); + return Promise.resolve(); + } + }); + + it('sets a new value', async function () { + const { getDatabase, ref, set, get } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, TEST_PATH); + + const value = Date.now(); + await set(dbRef, value); + const snapshot = await get(dbRef); + snapshot.val().should.eql(value); + }); + + it('throws if permission defined', async function () { + const { getDatabase, ref, set } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, 'nope/foo'); + + const value = Date.now(); + try { + await set(dbRef, value); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + error.code.includes('database/permission-denied').should.be.true(); + return Promise.resolve(); + } + }); }); }); diff --git a/packages/database/e2e/reference/set.modular.e2e.js b/packages/database/e2e/reference/set.modular.e2e.js deleted file mode 100644 index adaaf39f43..0000000000 --- a/packages/database/e2e/reference/set.modular.e2e.js +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/set`; - -describe('set', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if no value is provided', async function () { - try { - await firebase.database().ref(TEST_PATH).set(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref(TEST_PATH).set(null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('sets a new value', async function () { - const value = Date.now(); - const ref = firebase.database().ref(TEST_PATH); - await ref.set(value); - const snapshot = await ref.once('value'); - snapshot.val().should.eql(value); - }); - - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase.database().ref(TEST_PATH).set(value, resolve); - }); - }); - - it('throws if permission defined', async function () { - const value = Date.now(); - try { - await firebase.database().ref('nope/foo').set(value); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.code.includes('database/permission-denied').should.be.true(); - return Promise.resolve(); - } - }); - }); - - describe('modular', function () { - it('throws if no value is provided', async function () { - const { getDatabase, ref, set } = databaseModular; - const db = getDatabase(); - - try { - await set(ref(db, TEST_PATH)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'value' must be defined"); - return Promise.resolve(); - } - }); - - it('sets a new value', async function () { - const { getDatabase, ref, set, get } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, TEST_PATH); - - const value = Date.now(); - await set(dbRef, value); - const snapshot = await get(dbRef); - snapshot.val().should.eql(value); - }); - - it('throws if permission defined', async function () { - const { getDatabase, ref, set } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, 'nope/foo'); - - const value = Date.now(); - try { - await set(dbRef, value); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.code.includes('database/permission-denied').should.be.true(); - return Promise.resolve(); - } - }); - }); -}); diff --git a/packages/database/e2e/reference/setPriority.e2e.js b/packages/database/e2e/reference/setPriority.e2e.js index f1bba1e334..8db16c14af 100644 --- a/packages/database/e2e/reference/setPriority.e2e.js +++ b/packages/database/e2e/reference/setPriority.e2e.js @@ -27,64 +27,128 @@ describe('database().ref().setPriority()', function () { await wipe(TEST_PATH); }); - it('throws if priority is not a valid type', async function () { - try { - await firebase.database().ref().setPriority({}); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'priority' must be a number, string or null value"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if priority is not a valid type', async function () { + try { + await firebase.database().ref().setPriority({}); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'priority' must be a number, string or null value"); + return Promise.resolve(); + } + }); - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref().setPriority(null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', async function () { + try { + await firebase.database().ref().setPriority(null, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); - it('should correctly set a priority for all non-null values', async function () { - await Promise.all( - Object.keys(CONTENT.TYPES).map(async dataRef => { - const ref = firebase.database().ref(`${TEST_PATH}/types/${dataRef}`); - await ref.setPriority(1); - const snapshot = await ref.once('value'); - if (snapshot.val() !== null) { - snapshot.getPriority().should.eql(1); - } - }), - ); - }); + it('should correctly set a priority for all non-null values', async function () { + await Promise.all( + Object.keys(CONTENT.TYPES).map(async dataRef => { + const ref = firebase.database().ref(`${TEST_PATH}/types/${dataRef}`); + await ref.setPriority(1); + const snapshot = await ref.once('value'); + if (snapshot.val() !== null) { + snapshot.getPriority().should.eql(1); + } + }), + ); + }); - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase.database().ref(`${TEST_PATH}/types/string`).set(value, resolve); + it('callback if function is passed', async function () { + const value = Date.now(); + return new Promise(async resolve => { + await firebase.database().ref(`${TEST_PATH}/types/string`).set(value, resolve); + }); + }); + + it('throws if setting priority on non-existent node', async function () { + try { + await firebase.database().ref('tests/siudfhsuidfj').setPriority(1); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + // WEB SDK: INVALID_PARAMETERS: could not set priority on non-existent node + // TODO Get this error? Native code = -999 Unknown + return Promise.resolve(); + } }); - }); - it('throws if setting priority on non-existent node', async function () { - try { - await firebase.database().ref('tests/siudfhsuidfj').setPriority(1); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - // WEB SDK: INVALID_PARAMETERS: could not set priority on non-existent node - // TODO Get this error? Native code = -999 Unknown - return Promise.resolve(); - } + it('throws if permission defined', async function () { + try { + await firebase.database().ref('nope/foo').setPriority(1); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + error.code.includes('database/permission-denied').should.be.true(); + return Promise.resolve(); + } + }); }); - it('throws if permission defined', async function () { - try { - await firebase.database().ref('nope/foo').setPriority(1); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.code.includes('database/permission-denied').should.be.true(); - return Promise.resolve(); - } + describe('modular', function () { + it('throws if priority is not a valid type', async function () { + const { getDatabase, ref, setPriority } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db); + + try { + await setPriority(dbRef, {}); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'priority' must be a number, string or null value"); + return Promise.resolve(); + } + }); + + it('should correctly set a priority for all non-null values', async function () { + const { getDatabase, ref, setPriority, get } = databaseModular; + + await Promise.all( + Object.keys(CONTENT.TYPES).map(async dataRef => { + const db = getDatabase(); + const dbRef = ref(db, `${TEST_PATH}/types/${dataRef}`); + + await setPriority(dbRef, 1); + const snapshot = await get(dbRef); + if (snapshot.val() !== null) { + snapshot.getPriority().should.eql(1); + } + }), + ); + }); + + it('throws if setting priority on non-existent node', async function () { + const { getDatabase, ref, setPriority } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, 'tests/siudfhsuidfj'); + + try { + await setPriority(dbRef, 1); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + // WEB SDK: INVALID_PARAMETERS: could not set priority on non-existent node + // TODO Get this error? Native code = -999 Unknown + return Promise.resolve(); + } + }); + + it('throws if permission defined', async function () { + const { getDatabase, ref, setPriority } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, 'nope/foo'); + + try { + await setPriority(dbRef, 1); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + error.code.includes('database/permission-denied').should.be.true(); + return Promise.resolve(); + } + }); }); }); diff --git a/packages/database/e2e/reference/setPriority.modular.e2e.js b/packages/database/e2e/reference/setPriority.modular.e2e.js deleted file mode 100644 index 8db16c14af..0000000000 --- a/packages/database/e2e/reference/setPriority.modular.e2e.js +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, CONTENT, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/priority`; - -describe('database().ref().setPriority()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if priority is not a valid type', async function () { - try { - await firebase.database().ref().setPriority({}); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'priority' must be a number, string or null value"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref().setPriority(null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('should correctly set a priority for all non-null values', async function () { - await Promise.all( - Object.keys(CONTENT.TYPES).map(async dataRef => { - const ref = firebase.database().ref(`${TEST_PATH}/types/${dataRef}`); - await ref.setPriority(1); - const snapshot = await ref.once('value'); - if (snapshot.val() !== null) { - snapshot.getPriority().should.eql(1); - } - }), - ); - }); - - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase.database().ref(`${TEST_PATH}/types/string`).set(value, resolve); - }); - }); - - it('throws if setting priority on non-existent node', async function () { - try { - await firebase.database().ref('tests/siudfhsuidfj').setPriority(1); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - // WEB SDK: INVALID_PARAMETERS: could not set priority on non-existent node - // TODO Get this error? Native code = -999 Unknown - return Promise.resolve(); - } - }); - - it('throws if permission defined', async function () { - try { - await firebase.database().ref('nope/foo').setPriority(1); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.code.includes('database/permission-denied').should.be.true(); - return Promise.resolve(); - } - }); - }); - - describe('modular', function () { - it('throws if priority is not a valid type', async function () { - const { getDatabase, ref, setPriority } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db); - - try { - await setPriority(dbRef, {}); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'priority' must be a number, string or null value"); - return Promise.resolve(); - } - }); - - it('should correctly set a priority for all non-null values', async function () { - const { getDatabase, ref, setPriority, get } = databaseModular; - - await Promise.all( - Object.keys(CONTENT.TYPES).map(async dataRef => { - const db = getDatabase(); - const dbRef = ref(db, `${TEST_PATH}/types/${dataRef}`); - - await setPriority(dbRef, 1); - const snapshot = await get(dbRef); - if (snapshot.val() !== null) { - snapshot.getPriority().should.eql(1); - } - }), - ); - }); - - it('throws if setting priority on non-existent node', async function () { - const { getDatabase, ref, setPriority } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, 'tests/siudfhsuidfj'); - - try { - await setPriority(dbRef, 1); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - // WEB SDK: INVALID_PARAMETERS: could not set priority on non-existent node - // TODO Get this error? Native code = -999 Unknown - return Promise.resolve(); - } - }); - - it('throws if permission defined', async function () { - const { getDatabase, ref, setPriority } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, 'nope/foo'); - - try { - await setPriority(dbRef, 1); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.code.includes('database/permission-denied').should.be.true(); - return Promise.resolve(); - } - }); - }); -}); diff --git a/packages/database/e2e/reference/setWithPriority.e2e.js b/packages/database/e2e/reference/setWithPriority.e2e.js index 24b3508454..6e4c156535 100644 --- a/packages/database/e2e/reference/setWithPriority.e2e.js +++ b/packages/database/e2e/reference/setWithPriority.e2e.js @@ -27,52 +27,96 @@ describe('database().ref().setWithPriority()', function () { await wipe(TEST_PATH); }); - it('throws if newVal is not defined', async function () { - try { - await firebase.database().ref().setWithPriority(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'newVal' must be defined"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if newVal is not defined', async function () { + try { + await firebase.database().ref().setWithPriority(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'newVal' must be defined"); + return Promise.resolve(); + } + }); - it('throws if newPriority is incorrect type', async function () { - try { - await firebase.database().ref().setWithPriority(null, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'newPriority' must be a number, string or null value"); - return Promise.resolve(); - } - }); + it('throws if newPriority is incorrect type', async function () { + try { + await firebase.database().ref().setWithPriority(null, { foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'newPriority' must be a number, string or null value"); + return Promise.resolve(); + } + }); - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref().setWithPriority(null, null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', async function () { + try { + await firebase.database().ref().setWithPriority(null, null, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + it('callback if function is passed', async function () { + const value = Date.now(); + return new Promise(async resolve => { + await firebase + .database() + .ref(`${TEST_PATH}/setValueWithCallback`) + .setWithPriority(value, 2, resolve); + }); + }); - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase - .database() - .ref(`${TEST_PATH}/setValueWithCallback`) - .setWithPriority(value, 2, resolve); + it('sets with a new value and priority', async function () { + const value = Date.now(); + const ref = firebase.database().ref(`${TEST_PATH}/setValue`); + await ref.setWithPriority(value, 2); + const snapshot = await ref.once('value'); + snapshot.val().should.eql(value); + snapshot.getPriority().should.eql(2); }); }); - it('sets with a new value and priority', async function () { - const value = Date.now(); - const ref = firebase.database().ref(`${TEST_PATH}/setValue`); - await ref.setWithPriority(value, 2); - const snapshot = await ref.once('value'); - snapshot.val().should.eql(value); - snapshot.getPriority().should.eql(2); + describe('modular', function () { + it('throws if newVal is not defined', async function () { + const { getDatabase, ref, setWithPriority } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db); + + try { + await setWithPriority(dbRef); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'newVal' must be defined"); + return Promise.resolve(); + } + }); + + it('throws if newPriority is incorrect type', async function () { + const { getDatabase, ref, setWithPriority } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db); + + try { + await setWithPriority(dbRef, null, { foo: 'bar' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'newPriority' must be a number, string or null value"); + return Promise.resolve(); + } + }); + + it('sets with a new value and priority', async function () { + const { getDatabase, ref, setWithPriority, get } = databaseModular; + const db = getDatabase(); + const dbRef = ref(db, `${TEST_PATH}/setValue`); + + const value = Date.now(); + await setWithPriority(dbRef, value, 2); + const snapshot = await get(dbRef); + snapshot.val().should.eql(value); + snapshot.getPriority().should.eql(2); + }); }); }); diff --git a/packages/database/e2e/reference/setWithPriority.modular.e2e.js b/packages/database/e2e/reference/setWithPriority.modular.e2e.js deleted file mode 100644 index 6e4c156535..0000000000 --- a/packages/database/e2e/reference/setWithPriority.modular.e2e.js +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/setWithPriority`; - -describe('database().ref().setWithPriority()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if newVal is not defined', async function () { - try { - await firebase.database().ref().setWithPriority(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'newVal' must be defined"); - return Promise.resolve(); - } - }); - - it('throws if newPriority is incorrect type', async function () { - try { - await firebase.database().ref().setWithPriority(null, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'newPriority' must be a number, string or null value"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref().setWithPriority(null, null, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase - .database() - .ref(`${TEST_PATH}/setValueWithCallback`) - .setWithPriority(value, 2, resolve); - }); - }); - - it('sets with a new value and priority', async function () { - const value = Date.now(); - const ref = firebase.database().ref(`${TEST_PATH}/setValue`); - await ref.setWithPriority(value, 2); - const snapshot = await ref.once('value'); - snapshot.val().should.eql(value); - snapshot.getPriority().should.eql(2); - }); - }); - - describe('modular', function () { - it('throws if newVal is not defined', async function () { - const { getDatabase, ref, setWithPriority } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db); - - try { - await setWithPriority(dbRef); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'newVal' must be defined"); - return Promise.resolve(); - } - }); - - it('throws if newPriority is incorrect type', async function () { - const { getDatabase, ref, setWithPriority } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db); - - try { - await setWithPriority(dbRef, null, { foo: 'bar' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'newPriority' must be a number, string or null value"); - return Promise.resolve(); - } - }); - - it('sets with a new value and priority', async function () { - const { getDatabase, ref, setWithPriority, get } = databaseModular; - const db = getDatabase(); - const dbRef = ref(db, `${TEST_PATH}/setValue`); - - const value = Date.now(); - await setWithPriority(dbRef, value, 2); - const snapshot = await get(dbRef); - snapshot.val().should.eql(value); - snapshot.getPriority().should.eql(2); - }); - }); -}); diff --git a/packages/database/e2e/reference/transaction.e2e.js b/packages/database/e2e/reference/transaction.e2e.js index c581cefcf3..64f67cb27b 100644 --- a/packages/database/e2e/reference/transaction.e2e.js +++ b/packages/database/e2e/reference/transaction.e2e.js @@ -28,83 +28,172 @@ describe('database().ref().transaction()', function () { await wipe(TEST_PATH); }); - it('throws if no transactionUpdate is provided', async function () { - try { - await firebase.database().ref(TEST_PATH).transaction(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'transactionUpdate' must be a function"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if no transactionUpdate is provided', async function () { + try { + await firebase.database().ref(TEST_PATH).transaction(); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'transactionUpdate' must be a function"); + return Promise.resolve(); + } + }); - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref().transaction(NOOP, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); + it('throws if onComplete is not a function', async function () { + try { + await firebase.database().ref().transaction(NOOP, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); - it('throws if applyLocally is not a boolean', async function () { - try { - await firebase.database().ref().transaction(NOOP, NOOP, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'applyLocally' must be a boolean value if provided"); - return Promise.resolve(); - } - }); + it('throws if applyLocally is not a boolean', async function () { + try { + await firebase.database().ref().transaction(NOOP, NOOP, 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'applyLocally' must be a boolean value if provided"); + return Promise.resolve(); + } + }); + + // FIXME this test works in isolation but not running in the suite? + xit('updates the value via a transaction', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/transactionUpdate`); + await ref.set(1); + await Utils.sleep(2000); + const { committed, snapshot } = await ref.transaction(value => { + return value + 1; + }); - // FIXME this test works in isolation but not running in the suite? - xit('updates the value via a transaction', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/transactionUpdate`); - await ref.set(1); - await Utils.sleep(2000); - const { committed, snapshot } = await ref.transaction(value => { - return value + 1; + should.equal(committed, true, 'Transaction did not commit.'); + snapshot.val().should.equal(2); }); - should.equal(committed, true, 'Transaction did not commit.'); - snapshot.val().should.equal(2); - }); + it('aborts transaction if undefined returned', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/transactionAbort`); + await ref.set(1); - it('aborts transaction if undefined returned', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/transactionAbort`); - await ref.set(1); - - return new Promise((resolve, reject) => { - ref.transaction( - () => { - return undefined; - }, - (error, committed) => { - if (error) { - return reject(error); - } - - if (!committed) { - return resolve(); - } + return new Promise((resolve, reject) => { + ref.transaction( + () => { + return undefined; + }, + (error, committed) => { + if (error) { + return reject(error); + } - return reject(new Error('Transaction did not abort commit.')); - }, - ); + if (!committed) { + return resolve(); + } + + return reject(new Error('Transaction did not abort commit.')); + }, + ); + }); }); - }); - // FIXME flaky on android local against emulator? - it('passes valid data through the callback', async function () { - if (device.getPlatform() === 'ios') { - const ref = firebase.database().ref(`${TEST_PATH}/transactionCallback`); - await ref.set(1); + // FIXME flaky on android local against emulator? + it('passes valid data through the callback', async function () { + if (device.getPlatform() === 'ios') { + const ref = firebase.database().ref(`${TEST_PATH}/transactionCallback`); + await ref.set(1); + + return new Promise((resolve, reject) => { + ref.transaction( + $ => { + return $ + 1; + }, + (error, committed, snapshot) => { + if (error) { + return reject(error); + } + + if (!committed) { + return reject(new Error('Transaction aborted when it should not have done')); + } + + should.equal(snapshot.val(), 2); + return resolve(); + }, + ); + }); + } else { + this.skip(); + } + }); + + // FIXME flaky on android local against emulator? + it('throws when an error occurs', async function () { + if (device.getPlatform() === 'ios') { + const ref = firebase.database().ref('nope'); + + try { + await ref.transaction($ => { + return $ + 1; + }); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + error.message.should.containEql( + "Client doesn't have permission to access the desired data", + ); + return Promise.resolve(); + } + } else { + this.skip(); + } + }); + + // FIXME flaky on android in CI? works most of the time... + it('passes error back to the callback', async function () { + if (device.getPlatform() === 'ios' || !global.isCI) { + const ref = firebase.database().ref('nope'); + + return new Promise((resolve, reject) => { + ref + .transaction( + $ => { + return $ + 1; + }, + (error, committed, snapshot) => { + if (snapshot !== null) { + return reject(new Error('Snapshot should not be available')); + } + + if (committed === true) { + return reject(new Error('Transaction should not have committed')); + } + + error.message.should.containEql( + "Client doesn't have permission to access the desired data", + ); + return resolve(); + }, + ) + .catch(e => { + return reject(e); + }); + }); + } else { + this.skip(); + } + }); + + it('sets a value if one does not exist', async function () { + const ref = firebase.database().ref(`${TEST_PATH}/transactionCreate`); + const value = Date.now(); return new Promise((resolve, reject) => { ref.transaction( $ => { - return $ + 1; + if ($ === null) { + return { foo: value }; + } + + throw new Error('Value should not exist'); }, (error, committed, snapshot) => { if (error) { @@ -112,104 +201,124 @@ describe('database().ref().transaction()', function () { } if (!committed) { - return reject(new Error('Transaction aborted when it should not have done')); + return reject(new Error('Transaction should have committed')); } - should.equal(snapshot.val(), 2); + snapshot.val().should.eql( + jet.contextify({ + foo: value, + }), + ); return resolve(); }, ); }); - } else { - this.skip(); - } + }); }); - // FIXME flaky on android local against emulator? - it('throws when an error occurs', async function () { - if (device.getPlatform() === 'ios') { - const ref = firebase.database().ref('nope'); + describe('modular', function () { + it('throws if no transactionUpdate is provided', async function () { + const { getDatabase, ref, runTransaction } = databaseModular; try { - await ref.transaction($ => { - return $ + 1; - }); - return Promise.reject(new Error('Did not throw error.')); + await runTransaction(ref(getDatabase(), TEST_PATH)); + return Promise.reject(new Error('Did not throw an Error.')); } catch (error) { - error.message.should.containEql( - "Client doesn't have permission to access the desired data", - ); + error.message.should.containEql("'transactionUpdate' must be a function"); return Promise.resolve(); } - } else { - this.skip(); - } - }); + }); - // FIXME flaky on android in CI? works most of the time... - it('passes error back to the callback', async function () { - if (device.getPlatform() === 'ios' || !global.isCI) { - const ref = firebase.database().ref('nope'); + it('throws if options.applyLocally is not a boolean', async function () { + const { getDatabase, ref, runTransaction } = databaseModular; - return new Promise((resolve, reject) => { - ref - .transaction( - $ => { - return $ + 1; - }, - (error, committed, snapshot) => { - if (snapshot !== null) { - return reject(new Error('Snapshot should not be available')); - } + try { + await runTransaction(ref(getDatabase(), TEST_PATH), NOOP, { applyLocally: 'foo' }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'applyLocally' must be a boolean value if provided"); + return Promise.resolve(); + } + }); - if (committed === true) { - return reject(new Error('Transaction should not have committed')); - } + // FIXME this test works in isolation but not running in the suite? + xit('updates the value via a transaction', async function () { + const { getDatabase, set, ref, runTransaction } = databaseModular; - error.message.should.containEql( - "Client doesn't have permission to access the desired data", - ); - return resolve(); - }, - ) - .catch(e => { - return reject(e); - }); + const dbRef = ref(getDatabase(), `${TEST_PATH}/transactionUpdate`); + await set(dbRef, 1); + await Utils.sleep(2000); + const { committed, snapshot } = await runTransaction(dbRef, value => { + return value + 1; }); - } else { - this.skip(); - } - }); - it('sets a value if one does not exist', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/transactionCreate`); - const value = Date.now(); - - return new Promise((resolve, reject) => { - ref.transaction( - $ => { - if ($ === null) { - return { foo: value }; - } - - throw new Error('Value should not exist'); - }, - (error, committed, snapshot) => { - if (error) { - return reject(error); - } - - if (!committed) { - return reject(new Error('Transaction should have committed')); - } - - snapshot.val().should.eql( - jet.contextify({ - foo: value, - }), + should.equal(committed, true, 'Transaction did not commit.'); + snapshot.val().should.equal(2); + }); + + it('aborts transaction if undefined returned', async function () { + const { getDatabase, set, ref, runTransaction } = databaseModular; + + const dbRef = ref(getDatabase(), `${TEST_PATH}/transactionAbort`); + await set(dbRef, 1); + + const { committed } = await runTransaction(dbRef, () => { + return undefined; + }); + + if (!committed) { + return; + } + + throw new Error('Transaction did not abort commit.'); + }); + + // FIXME flaky on android local against emulator? + it('throws when an error occurs', async function () { + if (device.getPlatform() === 'ios') { + const { getDatabase, ref, runTransaction } = databaseModular; + + const dbRef = ref(getDatabase(), 'nope'); + + try { + await runTransaction(dbRef, $ => { + return $ + 1; + }); + return Promise.reject(new Error('Did not throw error.')); + } catch (error) { + error.message.should.containEql( + "Client doesn't have permission to access the desired data", ); - return resolve(); - }, + return Promise.resolve(); + } + } else { + this.skip(); + } + }); + + // FIXME runs in isolation but not in suite. Crashes on iOS, and gets stuck on Android. + xit('sets a value if one does not exist', async function () { + const { getDatabase, ref, runTransaction } = databaseModular; + + const dbRef = ref(getDatabase(), `${TEST_PATH}/transactionCreate`); + const value = Date.now(); + + const { committed, snapshot } = await runTransaction(dbRef, $ => { + if ($ === null) { + return { foo: value }; + } + + throw new Error('Value should not exist'); + }); + + if (!committed) { + throw new Error('Transaction should have committed'); + } + + snapshot.val().should.eql( + jet.contextify({ + foo: value, + }), ); }); }); diff --git a/packages/database/e2e/reference/transaction.modular.e2e.js b/packages/database/e2e/reference/transaction.modular.e2e.js deleted file mode 100644 index 64f67cb27b..0000000000 --- a/packages/database/e2e/reference/transaction.modular.e2e.js +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/transaction`; -const NOOP = () => {}; - -describe('database().ref().transaction()', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('throws if no transactionUpdate is provided', async function () { - try { - await firebase.database().ref(TEST_PATH).transaction(); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'transactionUpdate' must be a function"); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref().transaction(NOOP, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('throws if applyLocally is not a boolean', async function () { - try { - await firebase.database().ref().transaction(NOOP, NOOP, 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'applyLocally' must be a boolean value if provided"); - return Promise.resolve(); - } - }); - - // FIXME this test works in isolation but not running in the suite? - xit('updates the value via a transaction', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/transactionUpdate`); - await ref.set(1); - await Utils.sleep(2000); - const { committed, snapshot } = await ref.transaction(value => { - return value + 1; - }); - - should.equal(committed, true, 'Transaction did not commit.'); - snapshot.val().should.equal(2); - }); - - it('aborts transaction if undefined returned', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/transactionAbort`); - await ref.set(1); - - return new Promise((resolve, reject) => { - ref.transaction( - () => { - return undefined; - }, - (error, committed) => { - if (error) { - return reject(error); - } - - if (!committed) { - return resolve(); - } - - return reject(new Error('Transaction did not abort commit.')); - }, - ); - }); - }); - - // FIXME flaky on android local against emulator? - it('passes valid data through the callback', async function () { - if (device.getPlatform() === 'ios') { - const ref = firebase.database().ref(`${TEST_PATH}/transactionCallback`); - await ref.set(1); - - return new Promise((resolve, reject) => { - ref.transaction( - $ => { - return $ + 1; - }, - (error, committed, snapshot) => { - if (error) { - return reject(error); - } - - if (!committed) { - return reject(new Error('Transaction aborted when it should not have done')); - } - - should.equal(snapshot.val(), 2); - return resolve(); - }, - ); - }); - } else { - this.skip(); - } - }); - - // FIXME flaky on android local against emulator? - it('throws when an error occurs', async function () { - if (device.getPlatform() === 'ios') { - const ref = firebase.database().ref('nope'); - - try { - await ref.transaction($ => { - return $ + 1; - }); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.message.should.containEql( - "Client doesn't have permission to access the desired data", - ); - return Promise.resolve(); - } - } else { - this.skip(); - } - }); - - // FIXME flaky on android in CI? works most of the time... - it('passes error back to the callback', async function () { - if (device.getPlatform() === 'ios' || !global.isCI) { - const ref = firebase.database().ref('nope'); - - return new Promise((resolve, reject) => { - ref - .transaction( - $ => { - return $ + 1; - }, - (error, committed, snapshot) => { - if (snapshot !== null) { - return reject(new Error('Snapshot should not be available')); - } - - if (committed === true) { - return reject(new Error('Transaction should not have committed')); - } - - error.message.should.containEql( - "Client doesn't have permission to access the desired data", - ); - return resolve(); - }, - ) - .catch(e => { - return reject(e); - }); - }); - } else { - this.skip(); - } - }); - - it('sets a value if one does not exist', async function () { - const ref = firebase.database().ref(`${TEST_PATH}/transactionCreate`); - const value = Date.now(); - - return new Promise((resolve, reject) => { - ref.transaction( - $ => { - if ($ === null) { - return { foo: value }; - } - - throw new Error('Value should not exist'); - }, - (error, committed, snapshot) => { - if (error) { - return reject(error); - } - - if (!committed) { - return reject(new Error('Transaction should have committed')); - } - - snapshot.val().should.eql( - jet.contextify({ - foo: value, - }), - ); - return resolve(); - }, - ); - }); - }); - }); - - describe('modular', function () { - it('throws if no transactionUpdate is provided', async function () { - const { getDatabase, ref, runTransaction } = databaseModular; - - try { - await runTransaction(ref(getDatabase(), TEST_PATH)); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'transactionUpdate' must be a function"); - return Promise.resolve(); - } - }); - - it('throws if options.applyLocally is not a boolean', async function () { - const { getDatabase, ref, runTransaction } = databaseModular; - - try { - await runTransaction(ref(getDatabase(), TEST_PATH), NOOP, { applyLocally: 'foo' }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'applyLocally' must be a boolean value if provided"); - return Promise.resolve(); - } - }); - - // FIXME this test works in isolation but not running in the suite? - xit('updates the value via a transaction', async function () { - const { getDatabase, set, ref, runTransaction } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/transactionUpdate`); - await set(dbRef, 1); - await Utils.sleep(2000); - const { committed, snapshot } = await runTransaction(dbRef, value => { - return value + 1; - }); - - should.equal(committed, true, 'Transaction did not commit.'); - snapshot.val().should.equal(2); - }); - - it('aborts transaction if undefined returned', async function () { - const { getDatabase, set, ref, runTransaction } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/transactionAbort`); - await set(dbRef, 1); - - const { committed } = await runTransaction(dbRef, () => { - return undefined; - }); - - if (!committed) { - return; - } - - throw new Error('Transaction did not abort commit.'); - }); - - // FIXME flaky on android local against emulator? - it('throws when an error occurs', async function () { - if (device.getPlatform() === 'ios') { - const { getDatabase, ref, runTransaction } = databaseModular; - - const dbRef = ref(getDatabase(), 'nope'); - - try { - await runTransaction(dbRef, $ => { - return $ + 1; - }); - return Promise.reject(new Error('Did not throw error.')); - } catch (error) { - error.message.should.containEql( - "Client doesn't have permission to access the desired data", - ); - return Promise.resolve(); - } - } else { - this.skip(); - } - }); - - // FIXME runs in isolation but not in suite. Crashes on iOS, and gets stuck on Android. - xit('sets a value if one does not exist', async function () { - const { getDatabase, ref, runTransaction } = databaseModular; - - const dbRef = ref(getDatabase(), `${TEST_PATH}/transactionCreate`); - const value = Date.now(); - - const { committed, snapshot } = await runTransaction(dbRef, $ => { - if ($ === null) { - return { foo: value }; - } - - throw new Error('Value should not exist'); - }); - - if (!committed) { - throw new Error('Transaction should have committed'); - } - - snapshot.val().should.eql( - jet.contextify({ - foo: value, - }), - ); - }); - }); -}); diff --git a/packages/database/e2e/reference/update.e2e.js b/packages/database/e2e/reference/update.e2e.js index 3dc34639e7..dd20261136 100644 --- a/packages/database/e2e/reference/update.e2e.js +++ b/packages/database/e2e/reference/update.e2e.js @@ -24,73 +24,127 @@ describe('database().ref().update()', function () { await firebase.database().ref(TEST_PATH).remove(); }); - it('throws if values is not an object', async function () { - try { - await firebase.database().ref(TEST_PATH).update('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object"); - return Promise.resolve(); - } - }); + describe('v8 compatibility', function () { + it('throws if values is not an object', async function () { + try { + await firebase.database().ref(TEST_PATH).update('foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' must be an object"); + return Promise.resolve(); + } + }); + + it('throws if update paths are not valid', async function () { + try { + await firebase.database().ref(TEST_PATH).update({ + $$$$: 'foo', + }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' contains an invalid path."); + return Promise.resolve(); + } + }); - it('throws if update paths are not valid', async function () { - try { - await firebase.database().ref(TEST_PATH).update({ - $$$$: 'foo', + it('throws if onComplete is not a function', async function () { + try { + await firebase.database().ref(TEST_PATH).update( + { + foo: 'bar', + }, + 'foo', + ); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'onComplete' must be a function if provided"); + return Promise.resolve(); + } + }); + + it('updates values', async function () { + const value = Date.now(); + const ref = firebase.database().ref(TEST_PATH); + await ref.update({ + foo: value, }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' contains an invalid path."); - return Promise.resolve(); - } - }); + const snapshot = await ref.once('value'); + snapshot.val().should.eql( + jet.contextify({ + foo: value, + }), + ); - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref(TEST_PATH).update( - { - foo: 'bar', - }, - 'foo', + await ref.update({}); // empty update should pass, but no side effects + const snapshot2 = await ref.once('value'); + snapshot2.val().should.eql( + jet.contextify({ + foo: value, + }), ); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } + }); + + it('callback if function is passed', async function () { + const value = Date.now(); + return new Promise(async resolve => { + await firebase.database().ref(TEST_PATH).update( + { + foo: value, + }, + resolve, + ); + }); + }); }); - it('updates values', async function () { - const value = Date.now(); - const ref = firebase.database().ref(TEST_PATH); - await ref.update({ - foo: value, + describe('modular', function () { + it('throws if values is not an object', async function () { + const { getDatabase, ref, update } = databaseModular; + + try { + await update(ref(getDatabase(), TEST_PATH), 'foo'); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' must be an object"); + return Promise.resolve(); + } }); - const snapshot = await ref.once('value'); - snapshot.val().should.eql( - jet.contextify({ - foo: value, - }), - ); - await ref.update({}); // empty update should pass, but no side effects - const snapshot2 = await ref.once('value'); - snapshot2.val().should.eql( - jet.contextify({ + it('throws if update paths are not valid', async function () { + const { getDatabase, ref, update } = databaseModular; + + try { + await update(ref(getDatabase(), TEST_PATH), { + $$$$: 'foo', + }); + return Promise.reject(new Error('Did not throw an Error.')); + } catch (error) { + error.message.should.containEql("'values' contains an invalid path."); + return Promise.resolve(); + } + }); + + it('updates values', async function () { + const { getDatabase, ref, update, get } = databaseModular; + + const value = Date.now(); + const dbRef = ref(getDatabase(), TEST_PATH); + await update(dbRef, { foo: value, - }), - ); - }); + }); + const snapshot = await get(dbRef); + snapshot.val().should.eql( + jet.contextify({ + foo: value, + }), + ); - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase.database().ref(TEST_PATH).update( - { + await update(dbRef, {}); // empty update should pass, but no side effects + const snapshot2 = await get(dbRef); + snapshot2.val().should.eql( + jet.contextify({ foo: value, - }, - resolve, + }), ); }); }); diff --git a/packages/database/e2e/reference/update.modular.e2e.js b/packages/database/e2e/reference/update.modular.e2e.js deleted file mode 100644 index dd20261136..0000000000 --- a/packages/database/e2e/reference/update.modular.e2e.js +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH } = require('../helpers'); - -const TEST_PATH = `${PATH}/update`; - -describe('database().ref().update()', function () { - after(async function () { - await firebase.database().ref(TEST_PATH).remove(); - }); - - describe('v8 compatibility', function () { - it('throws if values is not an object', async function () { - try { - await firebase.database().ref(TEST_PATH).update('foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object"); - return Promise.resolve(); - } - }); - - it('throws if update paths are not valid', async function () { - try { - await firebase.database().ref(TEST_PATH).update({ - $$$$: 'foo', - }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' contains an invalid path."); - return Promise.resolve(); - } - }); - - it('throws if onComplete is not a function', async function () { - try { - await firebase.database().ref(TEST_PATH).update( - { - foo: 'bar', - }, - 'foo', - ); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'onComplete' must be a function if provided"); - return Promise.resolve(); - } - }); - - it('updates values', async function () { - const value = Date.now(); - const ref = firebase.database().ref(TEST_PATH); - await ref.update({ - foo: value, - }); - const snapshot = await ref.once('value'); - snapshot.val().should.eql( - jet.contextify({ - foo: value, - }), - ); - - await ref.update({}); // empty update should pass, but no side effects - const snapshot2 = await ref.once('value'); - snapshot2.val().should.eql( - jet.contextify({ - foo: value, - }), - ); - }); - - it('callback if function is passed', async function () { - const value = Date.now(); - return new Promise(async resolve => { - await firebase.database().ref(TEST_PATH).update( - { - foo: value, - }, - resolve, - ); - }); - }); - }); - - describe('modular', function () { - it('throws if values is not an object', async function () { - const { getDatabase, ref, update } = databaseModular; - - try { - await update(ref(getDatabase(), TEST_PATH), 'foo'); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' must be an object"); - return Promise.resolve(); - } - }); - - it('throws if update paths are not valid', async function () { - const { getDatabase, ref, update } = databaseModular; - - try { - await update(ref(getDatabase(), TEST_PATH), { - $$$$: 'foo', - }); - return Promise.reject(new Error('Did not throw an Error.')); - } catch (error) { - error.message.should.containEql("'values' contains an invalid path."); - return Promise.resolve(); - } - }); - - it('updates values', async function () { - const { getDatabase, ref, update, get } = databaseModular; - - const value = Date.now(); - const dbRef = ref(getDatabase(), TEST_PATH); - await update(dbRef, { - foo: value, - }); - const snapshot = await get(dbRef); - snapshot.val().should.eql( - jet.contextify({ - foo: value, - }), - ); - - await update(dbRef, {}); // empty update should pass, but no side effects - const snapshot2 = await get(dbRef); - snapshot2.val().should.eql( - jet.contextify({ - foo: value, - }), - ); - }); - }); -}); diff --git a/packages/database/e2e/snapshot/snapshot.e2e.js b/packages/database/e2e/snapshot/snapshot.e2e.js index 853cb2eaa1..65d4bdcf20 100644 --- a/packages/database/e2e/snapshot/snapshot.e2e.js +++ b/packages/database/e2e/snapshot/snapshot.e2e.js @@ -27,228 +27,507 @@ describe('database()...snapshot', function () { await wipe(TEST_PATH); }); - it('returns the snapshot key', async function () { - const snapshot = await firebase.database().ref(TEST_PATH).child('types/boolean').once('value'); + describe('v8 compatibility', function () { + it('returns the snapshot key', async function () { + const snapshot = await firebase + .database() + .ref(TEST_PATH) + .child('types/boolean') + .once('value'); + + snapshot.key.should.equal('boolean'); + }); - snapshot.key.should.equal('boolean'); - }); + it('returns the snapshot reference', async function () { + const snapshot = await firebase + .database() + .ref(TEST_PATH) + .child('types/boolean') + .once('value'); - it('returns the snapshot reference', async function () { - const snapshot = await firebase.database().ref(TEST_PATH).child('types/boolean').once('value'); + snapshot.ref.key.should.equal('boolean'); + }); - snapshot.ref.key.should.equal('boolean'); - }); + it('returns the correct boolean for exists', async function () { + const snapshot1 = await firebase + .database() + .ref(TEST_PATH) + .child('types/boolean') + .once('value'); - it('returns the correct boolean for exists', async function () { - const snapshot1 = await firebase.database().ref(TEST_PATH).child('types/boolean').once('value'); + const snapshot2 = await firebase.database().ref(TEST_PATH).child('types/nope').once('value'); - const snapshot2 = await firebase.database().ref(TEST_PATH).child('types/nope').once('value'); + snapshot1.exists().should.equal(true); + snapshot2.exists().should.equal(false); + }); - snapshot1.exists().should.equal(true); - snapshot2.exists().should.equal(false); - }); + it('exports a valid object', async function () { + const snapshot = await firebase.database().ref(TEST_PATH).child('types/string').once('value'); - it('exports a valid object', async function () { - const snapshot = await firebase.database().ref(TEST_PATH).child('types/string').once('value'); + const exported = snapshot.exportVal(); - const exported = snapshot.exportVal(); + exported.should.have.property('.value'); + exported.should.have.property('.priority'); + exported['.value'].should.equal('foobar'); + should.equal(exported['.priority'], null); + }); - exported.should.have.property('.value'); - exported.should.have.property('.priority'); - exported['.value'].should.equal('foobar'); - should.equal(exported['.priority'], null); - }); + it('exports a valid object with a object value', async function () { + const snapshot = await firebase.database().ref(TEST_PATH).child('types/object').once('value'); - it('exports a valid object with a object value', async function () { - const snapshot = await firebase.database().ref(TEST_PATH).child('types/object').once('value'); + const exported = snapshot.exportVal(); - const exported = snapshot.exportVal(); + exported.should.have.property('.value'); + exported.should.have.property('.priority'); + exported['.value'].should.eql(jet.contextify(CONTENT.TYPES.object)); + should.equal(exported['.priority'], null); + }); - exported.should.have.property('.value'); - exported.should.have.property('.priority'); - exported['.value'].should.eql(jet.contextify(CONTENT.TYPES.object)); - should.equal(exported['.priority'], null); - }); + it('forEach throws if action param is not a function', async function () { + const ref = firebase.database().ref(TEST_PATH).child('unorderedList'); - it('forEach throws if action param is not a function', async function () { - const ref = firebase.database().ref(TEST_PATH).child('unorderedList'); + await ref.set({ + a: 3, + b: 1, + c: 2, + }); - await ref.set({ - a: 3, - b: 1, - c: 2, + const snapshot = await ref.orderByValue().once('value'); + + try { + snapshot.forEach('foo'); + } catch (error) { + error.message.should.containEql("'action' must be a function"); + } }); - const snapshot = await ref.orderByValue().once('value'); + it('forEach returns an ordered list of snapshots', async function () { + const ref = firebase.database().ref(TEST_PATH).child('unorderedList'); - try { - snapshot.forEach('foo'); - } catch (error) { - error.message.should.containEql("'action' must be a function"); - } - }); + await ref.set({ + a: 3, + b: 1, + c: 2, + }); - it('forEach returns an ordered list of snapshots', async function () { - const ref = firebase.database().ref(TEST_PATH).child('unorderedList'); + const snapshot = await ref.orderByValue().once('value'); + const expected = ['b', 'c', 'a']; - await ref.set({ - a: 3, - b: 1, - c: 2, + snapshot.forEach((childSnap, i) => { + childSnap.val().should.equal(i + 1); + childSnap.key.should.equal(expected[i]); + }); }); - const snapshot = await ref.orderByValue().once('value'); - const expected = ['b', 'c', 'a']; + it('forEach works with arrays', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH).child('types'); - snapshot.forEach((childSnap, i) => { - childSnap.val().should.equal(i + 1); - childSnap.key.should.equal(expected[i]); + const snapshot = await ref.once('value'); + + snapshot.child('array').forEach((childSnap, i) => { + callback(); + childSnap.val().should.equal(i); + childSnap.key.should.equal(i.toString()); + }); + + callback.should.be.callCount(snapshot.child('array').numChildren()); }); - }); - it('forEach works with arrays', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types'); + it('forEach works with objects and cancels when returning true', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH).child('types/object').orderByKey(); + + const snapshot = await ref.once('value'); - const snapshot = await ref.once('value'); + snapshot.forEach(childSnap => { + callback(); + childSnap.key.should.equal('bar'); + childSnap.val().should.equal('baz'); + return true; + }); - snapshot.child('array').forEach((childSnap, i) => { - callback(); - childSnap.val().should.equal(i); - childSnap.key.should.equal(i.toString()); + callback.should.be.calledOnce(); }); - callback.should.be.callCount(snapshot.child('array').numChildren()); - }); + it('forEach works with arrays and cancels when returning true', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH).child('types'); - it('forEach works with objects and cancels when returning true', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types/object').orderByKey(); + const snapshot = await ref.once('value'); - const snapshot = await ref.once('value'); + snapshot.child('array').forEach(childSnap => { + callback(); + childSnap.val().should.equal(0); + childSnap.key.should.equal('0'); + return true; + }); - snapshot.forEach(childSnap => { - callback(); - childSnap.key.should.equal('bar'); - childSnap.val().should.equal('baz'); - return true; + callback.should.be.calledOnce(); }); - callback.should.be.calledOnce(); - }); + it('forEach returns false when no child keys', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH).child('types/boolean'); - it('forEach works with arrays and cancels when returning true', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types'); + const snapshot = await ref.once('value'); - const snapshot = await ref.once('value'); + const bool = snapshot.forEach(() => { + callback(); + }); - snapshot.child('array').forEach(childSnap => { - callback(); - childSnap.val().should.equal(0); - childSnap.key.should.equal('0'); - return true; + bool.should.equal(false); + callback.should.be.callCount(0); }); - callback.should.be.calledOnce(); - }); + it('forEach cancels iteration when returning true', async function () { + const callback = sinon.spy(); + const ref = firebase.database().ref(TEST_PATH).child('types/array'); - it('forEach returns false when no child keys', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types/boolean'); + const snapshot = await ref.orderByValue().once('value'); - const snapshot = await ref.once('value'); + const cancelled = snapshot.forEach(childSnap => { + callback(childSnap.val()); + return true; + }); - const bool = snapshot.forEach(() => { - callback(); + cancelled.should.equal(true); + callback.should.be.callCount(1); + callback.getCall(0).args[0].should.equal(0); }); - bool.should.equal(false); - callback.should.be.callCount(0); - }); + it('getPriority returns the correct value', async function () { + const ref = firebase.database().ref(TEST_PATH).child('getPriority'); - it('forEach cancels iteration when returning true', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types/array'); + await ref.setWithPriority('foo', 'bar'); + const snapshot = await ref.once('value'); - const snapshot = await ref.orderByValue().once('value'); + snapshot.getPriority().should.equal('bar'); + }); + + it('hasChild throws if path is not a string value', async function () { + const ref = firebase.database().ref(TEST_PATH).child('types/boolean'); - const cancelled = snapshot.forEach(childSnap => { - callback(childSnap.val()); - return true; + const snapshot = await ref.once('value'); + + try { + snapshot.hasChild({ foo: 'bar' }); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + } }); - cancelled.should.equal(true); - callback.should.be.callCount(1); - callback.getCall(0).args[0].should.equal(0); - }); + it('hasChild returns the correct boolean value', async function () { + const ref = firebase.database().ref(TEST_PATH); - it('getPriority returns the correct value', async function () { - const ref = firebase.database().ref(TEST_PATH).child('getPriority'); + const snapshot1 = await ref.child('types/boolean').once('value'); + const snapshot2 = await ref.child('types').once('value'); - await ref.setWithPriority('foo', 'bar'); - const snapshot = await ref.once('value'); + snapshot1.hasChild('foo').should.equal(false); + snapshot2.hasChild('boolean').should.equal(true); + }); - snapshot.getPriority().should.equal('bar'); - }); + it('hasChildren returns the correct boolean value', async function () { + const ref = firebase.database().ref(TEST_PATH); + const snapshot = await ref.child('types/object').once('value'); + snapshot.hasChildren().should.equal(true); + }); - it('hasChild throws if path is not a string value', async function () { - const ref = firebase.database().ref(TEST_PATH).child('types/boolean'); + it('numChildren returns the correct number value', async function () { + const ref = firebase.database().ref(TEST_PATH); - const snapshot = await ref.once('value'); + const snapshot1 = await ref.child('types/boolean').once('value'); + const snapshot2 = await ref.child('types/array').once('value'); + const snapshot3 = await ref.child('types/object').once('value'); - try { - snapshot.hasChild({ foo: 'bar' }); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - } - }); + snapshot1.numChildren().should.equal(0); + snapshot2.numChildren().should.equal(CONTENT.TYPES.array.length); + snapshot3.numChildren().should.equal(Object.keys(CONTENT.TYPES.object).length); + }); - it('hasChild returns the correct boolean value', async function () { - const ref = firebase.database().ref(TEST_PATH); + it('toJSON returns the value of the snapshot', async function () { + const ref = firebase.database().ref(TEST_PATH); - const snapshot1 = await ref.child('types/boolean').once('value'); - const snapshot2 = await ref.child('types').once('value'); + const snapshot1 = await ref.child('types/string').once('value'); + const snapshot2 = await ref.child('types/object').once('value'); - snapshot1.hasChild('foo').should.equal(false); - snapshot2.hasChild('boolean').should.equal(true); - }); + snapshot1.toJSON().should.equal('foobar'); + snapshot2.toJSON().should.eql(jet.contextify(CONTENT.TYPES.object)); + }); + + it('val returns the value of the snapshot', async function () { + const ref = firebase.database().ref(TEST_PATH); - it('hasChildren returns the correct boolean value', async function () { - const ref = firebase.database().ref(TEST_PATH); - const snapshot = await ref.child('types/object').once('value'); - snapshot.hasChildren().should.equal(true); + const snapshot1 = await ref.child('types/string').once('value'); + const snapshot2 = await ref.child('types/object').once('value'); + + snapshot1.val().should.equal('foobar'); + snapshot2.val().should.eql(jet.contextify(CONTENT.TYPES.object)); + }); }); - it('numChildren returns the correct number value', async function () { - const ref = firebase.database().ref(TEST_PATH); + describe('modular', function () { + it('returns the snapshot key', async function () { + const { getDatabase, ref, child, get } = databaseModular; - const snapshot1 = await ref.child('types/boolean').once('value'); - const snapshot2 = await ref.child('types/array').once('value'); - const snapshot3 = await ref.child('types/object').once('value'); + const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/boolean')); - snapshot1.numChildren().should.equal(0); - snapshot2.numChildren().should.equal(CONTENT.TYPES.array.length); - snapshot3.numChildren().should.equal(Object.keys(CONTENT.TYPES.object).length); - }); + snapshot.key.should.equal('boolean'); + }); - it('toJSON returns the value of the snapshot', async function () { - const ref = firebase.database().ref(TEST_PATH); + it('returns the snapshot reference', async function () { + const { getDatabase, ref, child, get } = databaseModular; - const snapshot1 = await ref.child('types/string').once('value'); - const snapshot2 = await ref.child('types/object').once('value'); + const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/boolean')); - snapshot1.toJSON().should.equal('foobar'); - snapshot2.toJSON().should.eql(jet.contextify(CONTENT.TYPES.object)); - }); + snapshot.ref.key.should.equal('boolean'); + }); + + it('returns the correct boolean for exists', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const snapshot1 = await get(child(ref(getDatabase(), TEST_PATH), 'types/boolean')); + + const snapshot2 = await get(child(ref(getDatabase(), TEST_PATH), 'types/nope')); + + snapshot1.exists().should.equal(true); + snapshot2.exists().should.equal(false); + }); + + it('exports a valid object', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/string')); + + const exported = snapshot.exportVal(); + + exported.should.have.property('.value'); + exported.should.have.property('.priority'); + exported['.value'].should.equal('foobar'); + should.equal(exported['.priority'], null); + }); - it('val returns the value of the snapshot', async function () { - const ref = firebase.database().ref(TEST_PATH); + it('exports a valid object with a object value', async function () { + const { getDatabase, ref, child, get } = databaseModular; - const snapshot1 = await ref.child('types/string').once('value'); - const snapshot2 = await ref.child('types/object').once('value'); + const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/object')); + + const exported = snapshot.exportVal(); + + exported.should.have.property('.value'); + exported.should.have.property('.priority'); + exported['.value'].should.eql(jet.contextify(CONTENT.TYPES.object)); + should.equal(exported['.priority'], null); + }); + + it('forEach throws if action param is not a function', async function () { + const { getDatabase, ref, child, set, get, orderByValue, query } = databaseModular; + + const dbRef = child(ref(getDatabase(), TEST_PATH), 'unorderedList'); + + await set(dbRef, { + a: 3, + b: 1, + c: 2, + }); + + const snapshot = await get(query(dbRef, orderByValue())); + + try { + snapshot.forEach('foo'); + } catch (error) { + error.message.should.containEql("'action' must be a function"); + } + }); + + it('forEach returns an ordered list of snapshots', async function () { + const { getDatabase, ref, child, set, get, orderByValue, query } = databaseModular; + + const dbRef = child(ref(getDatabase(), TEST_PATH), 'unorderedList'); + + await set(dbRef, { + a: 3, + b: 1, + c: 2, + }); + + const snapshot = await get(query(dbRef, orderByValue())); + const expected = ['b', 'c', 'a']; + + snapshot.forEach((childSnap, i) => { + childSnap.val().should.equal(i + 1); + childSnap.key.should.equal(expected[i]); + }); + }); - snapshot1.val().should.equal('foobar'); - snapshot2.val().should.eql(jet.contextify(CONTENT.TYPES.object)); + it('forEach works with arrays', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const callback = sinon.spy(); + const dbRef = child(ref(getDatabase(), TEST_PATH), 'types'); + + const snapshot = await get(dbRef); + + snapshot.child('array').forEach((childSnap, i) => { + callback(); + childSnap.val().should.equal(i); + childSnap.key.should.equal(i.toString()); + }); + + callback.should.be.callCount(snapshot.child('array').numChildren()); + }); + + it('forEach works with objects and cancels when returning true', async function () { + const { getDatabase, ref, child, orderByKey, query, get } = databaseModular; + + const callback = sinon.spy(); + const dbRef = query(child(ref(getDatabase(), TEST_PATH), 'types/object'), orderByKey()); + + const snapshot = await get(dbRef); + + snapshot.forEach(childSnap => { + callback(); + childSnap.key.should.equal('bar'); + childSnap.val().should.equal('baz'); + return true; + }); + + callback.should.be.calledOnce(); + }); + + it('forEach works with arrays and cancels when returning true', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const callback = sinon.spy(); + const dbRef = child(ref(getDatabase(), TEST_PATH), 'types'); + + const snapshot = await get(dbRef); + + snapshot.child('array').forEach(childSnap => { + callback(); + childSnap.val().should.equal(0); + childSnap.key.should.equal('0'); + return true; + }); + + callback.should.be.calledOnce(); + }); + + it('forEach returns false when no child keys', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const callback = sinon.spy(); + const dbRef = child(ref(getDatabase(), TEST_PATH), 'types/boolean'); + + const snapshot = await get(dbRef); + + const bool = snapshot.forEach(() => { + callback(); + }); + + bool.should.equal(false); + callback.should.be.callCount(0); + }); + + it('forEach cancels iteration when returning true', async function () { + const { getDatabase, ref, child, orderByValue, query, get } = databaseModular; + + const callback = sinon.spy(); + const dbRef = child(ref(getDatabase(), TEST_PATH), 'types/array'); + + const snapshot = await get(query(dbRef, orderByValue())); + + const cancelled = snapshot.forEach(childSnap => { + callback(childSnap.val()); + return true; + }); + + cancelled.should.equal(true); + callback.should.be.callCount(1); + callback.getCall(0).args[0].should.equal(0); + }); + + it('getPriority returns the correct value', async function () { + const { getDatabase, ref, child, setWithPriority, get } = databaseModular; + + const dbRef = child(ref(getDatabase(), TEST_PATH), 'getPriority'); + + await setWithPriority(dbRef, 'foo', 'bar'); + const snapshot = await get(dbRef); + + snapshot.getPriority().should.equal('bar'); + }); + + it('hasChild throws if path is not a string value', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const dbRef = child(ref(getDatabase(), TEST_PATH), 'types/boolean'); + + const snapshot = await get(dbRef); + + try { + snapshot.hasChild({ foo: 'bar' }); + } catch (error) { + error.message.should.containEql("'path' must be a string value"); + } + }); + + it('hasChild returns the correct boolean value', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + const snapshot1 = await get(child(dbRef, 'types/boolean')); + const snapshot2 = await get(child(dbRef, 'types')); + + snapshot1.hasChild('foo').should.equal(false); + snapshot2.hasChild('boolean').should.equal(true); + }); + + it('hasChildren returns the correct boolean value', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + const snapshot = await get(child(dbRef, 'types/object')); + snapshot.hasChildren().should.equal(true); + }); + + it('numChildren returns the correct number value', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + const snapshot1 = await get(child(dbRef, 'types/boolean')); + const snapshot2 = await get(child(dbRef, 'types/array')); + const snapshot3 = await get(child(dbRef, 'types/object')); + + snapshot1.numChildren().should.equal(0); + snapshot2.numChildren().should.equal(CONTENT.TYPES.array.length); + snapshot3.numChildren().should.equal(Object.keys(CONTENT.TYPES.object).length); + }); + + it('toJSON returns the value of the snapshot', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + const snapshot1 = await get(child(dbRef, 'types/string')); + const snapshot2 = await get(child(dbRef, 'types/object')); + + snapshot1.toJSON().should.equal('foobar'); + snapshot2.toJSON().should.eql(jet.contextify(CONTENT.TYPES.object)); + }); + + it('val returns the value of the snapshot', async function () { + const { getDatabase, ref, child, get } = databaseModular; + + const dbRef = ref(getDatabase(), TEST_PATH); + + const snapshot1 = await get(child(dbRef, 'types/string')); + const snapshot2 = await get(child(dbRef, 'types/object')); + + snapshot1.val().should.equal('foobar'); + snapshot2.val().should.eql(jet.contextify(CONTENT.TYPES.object)); + }); }); }); diff --git a/packages/database/e2e/snapshot/snapshot.modular.e2e.js b/packages/database/e2e/snapshot/snapshot.modular.e2e.js deleted file mode 100644 index 65d4bdcf20..0000000000 --- a/packages/database/e2e/snapshot/snapshot.modular.e2e.js +++ /dev/null @@ -1,533 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -const { PATH, CONTENT, seed, wipe } = require('../helpers'); - -const TEST_PATH = `${PATH}/snapshot`; - -describe('database()...snapshot', function () { - before(async function () { - await seed(TEST_PATH); - }); - after(async function () { - await wipe(TEST_PATH); - }); - - describe('v8 compatibility', function () { - it('returns the snapshot key', async function () { - const snapshot = await firebase - .database() - .ref(TEST_PATH) - .child('types/boolean') - .once('value'); - - snapshot.key.should.equal('boolean'); - }); - - it('returns the snapshot reference', async function () { - const snapshot = await firebase - .database() - .ref(TEST_PATH) - .child('types/boolean') - .once('value'); - - snapshot.ref.key.should.equal('boolean'); - }); - - it('returns the correct boolean for exists', async function () { - const snapshot1 = await firebase - .database() - .ref(TEST_PATH) - .child('types/boolean') - .once('value'); - - const snapshot2 = await firebase.database().ref(TEST_PATH).child('types/nope').once('value'); - - snapshot1.exists().should.equal(true); - snapshot2.exists().should.equal(false); - }); - - it('exports a valid object', async function () { - const snapshot = await firebase.database().ref(TEST_PATH).child('types/string').once('value'); - - const exported = snapshot.exportVal(); - - exported.should.have.property('.value'); - exported.should.have.property('.priority'); - exported['.value'].should.equal('foobar'); - should.equal(exported['.priority'], null); - }); - - it('exports a valid object with a object value', async function () { - const snapshot = await firebase.database().ref(TEST_PATH).child('types/object').once('value'); - - const exported = snapshot.exportVal(); - - exported.should.have.property('.value'); - exported.should.have.property('.priority'); - exported['.value'].should.eql(jet.contextify(CONTENT.TYPES.object)); - should.equal(exported['.priority'], null); - }); - - it('forEach throws if action param is not a function', async function () { - const ref = firebase.database().ref(TEST_PATH).child('unorderedList'); - - await ref.set({ - a: 3, - b: 1, - c: 2, - }); - - const snapshot = await ref.orderByValue().once('value'); - - try { - snapshot.forEach('foo'); - } catch (error) { - error.message.should.containEql("'action' must be a function"); - } - }); - - it('forEach returns an ordered list of snapshots', async function () { - const ref = firebase.database().ref(TEST_PATH).child('unorderedList'); - - await ref.set({ - a: 3, - b: 1, - c: 2, - }); - - const snapshot = await ref.orderByValue().once('value'); - const expected = ['b', 'c', 'a']; - - snapshot.forEach((childSnap, i) => { - childSnap.val().should.equal(i + 1); - childSnap.key.should.equal(expected[i]); - }); - }); - - it('forEach works with arrays', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types'); - - const snapshot = await ref.once('value'); - - snapshot.child('array').forEach((childSnap, i) => { - callback(); - childSnap.val().should.equal(i); - childSnap.key.should.equal(i.toString()); - }); - - callback.should.be.callCount(snapshot.child('array').numChildren()); - }); - - it('forEach works with objects and cancels when returning true', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types/object').orderByKey(); - - const snapshot = await ref.once('value'); - - snapshot.forEach(childSnap => { - callback(); - childSnap.key.should.equal('bar'); - childSnap.val().should.equal('baz'); - return true; - }); - - callback.should.be.calledOnce(); - }); - - it('forEach works with arrays and cancels when returning true', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types'); - - const snapshot = await ref.once('value'); - - snapshot.child('array').forEach(childSnap => { - callback(); - childSnap.val().should.equal(0); - childSnap.key.should.equal('0'); - return true; - }); - - callback.should.be.calledOnce(); - }); - - it('forEach returns false when no child keys', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types/boolean'); - - const snapshot = await ref.once('value'); - - const bool = snapshot.forEach(() => { - callback(); - }); - - bool.should.equal(false); - callback.should.be.callCount(0); - }); - - it('forEach cancels iteration when returning true', async function () { - const callback = sinon.spy(); - const ref = firebase.database().ref(TEST_PATH).child('types/array'); - - const snapshot = await ref.orderByValue().once('value'); - - const cancelled = snapshot.forEach(childSnap => { - callback(childSnap.val()); - return true; - }); - - cancelled.should.equal(true); - callback.should.be.callCount(1); - callback.getCall(0).args[0].should.equal(0); - }); - - it('getPriority returns the correct value', async function () { - const ref = firebase.database().ref(TEST_PATH).child('getPriority'); - - await ref.setWithPriority('foo', 'bar'); - const snapshot = await ref.once('value'); - - snapshot.getPriority().should.equal('bar'); - }); - - it('hasChild throws if path is not a string value', async function () { - const ref = firebase.database().ref(TEST_PATH).child('types/boolean'); - - const snapshot = await ref.once('value'); - - try { - snapshot.hasChild({ foo: 'bar' }); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - } - }); - - it('hasChild returns the correct boolean value', async function () { - const ref = firebase.database().ref(TEST_PATH); - - const snapshot1 = await ref.child('types/boolean').once('value'); - const snapshot2 = await ref.child('types').once('value'); - - snapshot1.hasChild('foo').should.equal(false); - snapshot2.hasChild('boolean').should.equal(true); - }); - - it('hasChildren returns the correct boolean value', async function () { - const ref = firebase.database().ref(TEST_PATH); - const snapshot = await ref.child('types/object').once('value'); - snapshot.hasChildren().should.equal(true); - }); - - it('numChildren returns the correct number value', async function () { - const ref = firebase.database().ref(TEST_PATH); - - const snapshot1 = await ref.child('types/boolean').once('value'); - const snapshot2 = await ref.child('types/array').once('value'); - const snapshot3 = await ref.child('types/object').once('value'); - - snapshot1.numChildren().should.equal(0); - snapshot2.numChildren().should.equal(CONTENT.TYPES.array.length); - snapshot3.numChildren().should.equal(Object.keys(CONTENT.TYPES.object).length); - }); - - it('toJSON returns the value of the snapshot', async function () { - const ref = firebase.database().ref(TEST_PATH); - - const snapshot1 = await ref.child('types/string').once('value'); - const snapshot2 = await ref.child('types/object').once('value'); - - snapshot1.toJSON().should.equal('foobar'); - snapshot2.toJSON().should.eql(jet.contextify(CONTENT.TYPES.object)); - }); - - it('val returns the value of the snapshot', async function () { - const ref = firebase.database().ref(TEST_PATH); - - const snapshot1 = await ref.child('types/string').once('value'); - const snapshot2 = await ref.child('types/object').once('value'); - - snapshot1.val().should.equal('foobar'); - snapshot2.val().should.eql(jet.contextify(CONTENT.TYPES.object)); - }); - }); - - describe('modular', function () { - it('returns the snapshot key', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/boolean')); - - snapshot.key.should.equal('boolean'); - }); - - it('returns the snapshot reference', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/boolean')); - - snapshot.ref.key.should.equal('boolean'); - }); - - it('returns the correct boolean for exists', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const snapshot1 = await get(child(ref(getDatabase(), TEST_PATH), 'types/boolean')); - - const snapshot2 = await get(child(ref(getDatabase(), TEST_PATH), 'types/nope')); - - snapshot1.exists().should.equal(true); - snapshot2.exists().should.equal(false); - }); - - it('exports a valid object', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/string')); - - const exported = snapshot.exportVal(); - - exported.should.have.property('.value'); - exported.should.have.property('.priority'); - exported['.value'].should.equal('foobar'); - should.equal(exported['.priority'], null); - }); - - it('exports a valid object with a object value', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const snapshot = await get(child(ref(getDatabase(), TEST_PATH), 'types/object')); - - const exported = snapshot.exportVal(); - - exported.should.have.property('.value'); - exported.should.have.property('.priority'); - exported['.value'].should.eql(jet.contextify(CONTENT.TYPES.object)); - should.equal(exported['.priority'], null); - }); - - it('forEach throws if action param is not a function', async function () { - const { getDatabase, ref, child, set, get, orderByValue, query } = databaseModular; - - const dbRef = child(ref(getDatabase(), TEST_PATH), 'unorderedList'); - - await set(dbRef, { - a: 3, - b: 1, - c: 2, - }); - - const snapshot = await get(query(dbRef, orderByValue())); - - try { - snapshot.forEach('foo'); - } catch (error) { - error.message.should.containEql("'action' must be a function"); - } - }); - - it('forEach returns an ordered list of snapshots', async function () { - const { getDatabase, ref, child, set, get, orderByValue, query } = databaseModular; - - const dbRef = child(ref(getDatabase(), TEST_PATH), 'unorderedList'); - - await set(dbRef, { - a: 3, - b: 1, - c: 2, - }); - - const snapshot = await get(query(dbRef, orderByValue())); - const expected = ['b', 'c', 'a']; - - snapshot.forEach((childSnap, i) => { - childSnap.val().should.equal(i + 1); - childSnap.key.should.equal(expected[i]); - }); - }); - - it('forEach works with arrays', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const callback = sinon.spy(); - const dbRef = child(ref(getDatabase(), TEST_PATH), 'types'); - - const snapshot = await get(dbRef); - - snapshot.child('array').forEach((childSnap, i) => { - callback(); - childSnap.val().should.equal(i); - childSnap.key.should.equal(i.toString()); - }); - - callback.should.be.callCount(snapshot.child('array').numChildren()); - }); - - it('forEach works with objects and cancels when returning true', async function () { - const { getDatabase, ref, child, orderByKey, query, get } = databaseModular; - - const callback = sinon.spy(); - const dbRef = query(child(ref(getDatabase(), TEST_PATH), 'types/object'), orderByKey()); - - const snapshot = await get(dbRef); - - snapshot.forEach(childSnap => { - callback(); - childSnap.key.should.equal('bar'); - childSnap.val().should.equal('baz'); - return true; - }); - - callback.should.be.calledOnce(); - }); - - it('forEach works with arrays and cancels when returning true', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const callback = sinon.spy(); - const dbRef = child(ref(getDatabase(), TEST_PATH), 'types'); - - const snapshot = await get(dbRef); - - snapshot.child('array').forEach(childSnap => { - callback(); - childSnap.val().should.equal(0); - childSnap.key.should.equal('0'); - return true; - }); - - callback.should.be.calledOnce(); - }); - - it('forEach returns false when no child keys', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const callback = sinon.spy(); - const dbRef = child(ref(getDatabase(), TEST_PATH), 'types/boolean'); - - const snapshot = await get(dbRef); - - const bool = snapshot.forEach(() => { - callback(); - }); - - bool.should.equal(false); - callback.should.be.callCount(0); - }); - - it('forEach cancels iteration when returning true', async function () { - const { getDatabase, ref, child, orderByValue, query, get } = databaseModular; - - const callback = sinon.spy(); - const dbRef = child(ref(getDatabase(), TEST_PATH), 'types/array'); - - const snapshot = await get(query(dbRef, orderByValue())); - - const cancelled = snapshot.forEach(childSnap => { - callback(childSnap.val()); - return true; - }); - - cancelled.should.equal(true); - callback.should.be.callCount(1); - callback.getCall(0).args[0].should.equal(0); - }); - - it('getPriority returns the correct value', async function () { - const { getDatabase, ref, child, setWithPriority, get } = databaseModular; - - const dbRef = child(ref(getDatabase(), TEST_PATH), 'getPriority'); - - await setWithPriority(dbRef, 'foo', 'bar'); - const snapshot = await get(dbRef); - - snapshot.getPriority().should.equal('bar'); - }); - - it('hasChild throws if path is not a string value', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const dbRef = child(ref(getDatabase(), TEST_PATH), 'types/boolean'); - - const snapshot = await get(dbRef); - - try { - snapshot.hasChild({ foo: 'bar' }); - } catch (error) { - error.message.should.containEql("'path' must be a string value"); - } - }); - - it('hasChild returns the correct boolean value', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - const snapshot1 = await get(child(dbRef, 'types/boolean')); - const snapshot2 = await get(child(dbRef, 'types')); - - snapshot1.hasChild('foo').should.equal(false); - snapshot2.hasChild('boolean').should.equal(true); - }); - - it('hasChildren returns the correct boolean value', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - const snapshot = await get(child(dbRef, 'types/object')); - snapshot.hasChildren().should.equal(true); - }); - - it('numChildren returns the correct number value', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - const snapshot1 = await get(child(dbRef, 'types/boolean')); - const snapshot2 = await get(child(dbRef, 'types/array')); - const snapshot3 = await get(child(dbRef, 'types/object')); - - snapshot1.numChildren().should.equal(0); - snapshot2.numChildren().should.equal(CONTENT.TYPES.array.length); - snapshot3.numChildren().should.equal(Object.keys(CONTENT.TYPES.object).length); - }); - - it('toJSON returns the value of the snapshot', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - const snapshot1 = await get(child(dbRef, 'types/string')); - const snapshot2 = await get(child(dbRef, 'types/object')); - - snapshot1.toJSON().should.equal('foobar'); - snapshot2.toJSON().should.eql(jet.contextify(CONTENT.TYPES.object)); - }); - - it('val returns the value of the snapshot', async function () { - const { getDatabase, ref, child, get } = databaseModular; - - const dbRef = ref(getDatabase(), TEST_PATH); - - const snapshot1 = await get(child(dbRef, 'types/string')); - const snapshot2 = await get(child(dbRef, 'types/object')); - - snapshot1.val().should.equal('foobar'); - snapshot2.val().should.eql(jet.contextify(CONTENT.TYPES.object)); - }); - }); -});