From dce5d2c591007641cecad9bbc1e90e397d9cf307 Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Fri, 8 Jan 2021 14:15:46 +0100 Subject: [PATCH] Firebase Stub: resolving ESLint errors --- .../database/dataSnapshot.js | 16 ++++++------- .../database/reference.js | 23 ++++++++++--------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/__mocks__/gatsby-plugin-firebase/database/dataSnapshot.js b/__mocks__/gatsby-plugin-firebase/database/dataSnapshot.js index c8069fe5..255bad24 100644 --- a/__mocks__/gatsby-plugin-firebase/database/dataSnapshot.js +++ b/__mocks__/gatsby-plugin-firebase/database/dataSnapshot.js @@ -1,7 +1,5 @@ -import Reference from './reference'; - class DataSnapshot { - constructor(eventType, reference, value = undefined) { + constructor(eventType, getDataCallback, value = undefined) { if (!eventType) { throw new Error('eventType must be provided.'); } else if (typeof eventType !== 'string') { @@ -10,13 +8,13 @@ class DataSnapshot { this.eventTypeField = eventType; - if (!reference) { - throw new Error('reference must be provided.'); - } else if (!(reference instanceof Reference)) { - throw new Error('reference must be an instance of the Reference class.'); + if (!getDataCallback) { + throw new Error('getDataCallback must be provided.'); + } else if (typeof getDataCallback !== 'function') { + throw new Error('getDataCallback should be a function.'); } - this.referenceField = reference; + this.getDataCallbackField = getDataCallback; this.valueField = value; } @@ -33,7 +31,7 @@ class DataSnapshot { if (this.eventType === 'value') { return typeof this.value !== 'undefined' ? this.value - : this.referenceField.getData(); + : this.getDataCallbackField(); } return undefined; diff --git a/__mocks__/gatsby-plugin-firebase/database/reference.js b/__mocks__/gatsby-plugin-firebase/database/reference.js index 0888d736..f4deb3cf 100644 --- a/__mocks__/gatsby-plugin-firebase/database/reference.js +++ b/__mocks__/gatsby-plugin-firebase/database/reference.js @@ -6,7 +6,7 @@ import DataSnapshot from './dataSnapshot'; const rootPath = '.'; class Reference { - constructor(path, getDatabaseData) { + constructor(path, getDatabaseDataCallback) { if (typeof path === 'undefined' || path === null) { this.pathField = rootPath; } else if (typeof path !== 'string') { @@ -16,15 +16,16 @@ class Reference { } this.uuidField = uuidv4(); + this.dataSnapshotsField = {}; - if (!getDatabaseData) { - throw new Error('getDatabaseData must be provided.'); - } else if (typeof getDatabaseData !== 'function') { - throw new Error('getDatabaseData should be a function.'); + if (!getDatabaseDataCallback) { + throw new Error('getDatabaseDataCallback must be provided.'); + } else if (typeof getDatabaseDataCallback !== 'function') { + throw new Error('getDatabaseDataCallback should be a function.'); } - this.getDatabaseDataField = getDatabaseData; + this.getDatabaseDataCallbackField = getDatabaseDataCallback; this.orderByChildPathField = ''; this.equalToValueField = ''; @@ -39,7 +40,7 @@ class Reference { } getData() { - const databaseData = this.getDatabaseDataField(); + const databaseData = this.getDatabaseDataCallbackField(); if (!databaseData) { return null; @@ -99,19 +100,19 @@ class Reference { return; } - let snapshot = new DataSnapshot(eventType, this, null); + let snapshot = new DataSnapshot(eventType, () => this.getData(), null); if (this.path === DatabaseConstants.connectedPath) { - snapshot = new DataSnapshot(eventType, this, true); + snapshot = new DataSnapshot(eventType, () => this.getData(), true); } else if (this.path === DatabaseConstants.resumesPath) { - snapshot = new DataSnapshot(eventType, this); + snapshot = new DataSnapshot(eventType, () => this.getData()); } callback(snapshot); } async once(eventType) { - const newDataSnapshot = new DataSnapshot(eventType, this); + const newDataSnapshot = new DataSnapshot(eventType, () => this.getData()); const existingDataSnapshot = this.dataSnapshotsField[ newDataSnapshot.eventType ];