From c2ac9594efbf6553f022e6f1376b732f16ffcbe7 Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Thu, 7 Jan 2021 17:28:43 +0100 Subject: [PATCH] Firebase Stub refactoring --- __mocks__/gatsby-plugin-firebase.js | 47 +--------------------- __mocks__/gatsby-plugin-firebase/auth.js | 51 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 __mocks__/gatsby-plugin-firebase/auth.js diff --git a/__mocks__/gatsby-plugin-firebase.js b/__mocks__/gatsby-plugin-firebase.js index 3278a857..f8dc7845 100644 --- a/__mocks__/gatsby-plugin-firebase.js +++ b/__mocks__/gatsby-plugin-firebase.js @@ -6,52 +6,7 @@ import { AuthConstants, DatabaseConstants, } from './gatsby-plugin-firebase/constants'; - -class Auth { - static #instance = undefined; - #uuid = ''; - #onAuthStateChangedObservers = []; - - constructor() { - if (Auth.#instance) { - return Auth.#instance; - } - - Auth.#instance = this; - - this.#uuid = uuidv4(); - } - - get uuid() { - return this.#uuid; - } - - get onAuthStateChangedObservers() { - return this.#onAuthStateChangedObservers; - } - - dispose() { - this.#onAuthStateChangedObservers = []; - } - - onAuthStateChanged(observer) { - this.#onAuthStateChangedObservers.push(observer); - - return () => { - this.#onAuthStateChangedObservers = this.#onAuthStateChangedObservers.filter( - (observer) => observer !== observer, - ); - }; - } - - async signInAnonymously() { - const user = AuthConstants.anonymousUser1; - - this.#onAuthStateChangedObservers.forEach((observer) => observer(user)); - - return Promise.resolve(user); - } -} +import Auth from './gatsby-plugin-firebase/auth'; class Database { static #instance = undefined; diff --git a/__mocks__/gatsby-plugin-firebase/auth.js b/__mocks__/gatsby-plugin-firebase/auth.js new file mode 100644 index 00000000..aa1206c1 --- /dev/null +++ b/__mocks__/gatsby-plugin-firebase/auth.js @@ -0,0 +1,51 @@ +import { v4 as uuidv4 } from 'uuid'; + +import { AuthConstants } from './constants'; + +class Auth { + static #instance = undefined; + #uuid = ''; + #onAuthStateChangedObservers = []; + + constructor() { + if (Auth.#instance) { + return Auth.#instance; + } + + Auth.#instance = this; + + this.#uuid = uuidv4(); + } + + get uuid() { + return this.#uuid; + } + + get onAuthStateChangedObservers() { + return this.#onAuthStateChangedObservers; + } + + dispose() { + this.#onAuthStateChangedObservers = []; + } + + onAuthStateChanged(observer) { + this.#onAuthStateChangedObservers.push(observer); + + return () => { + this.#onAuthStateChangedObservers = this.#onAuthStateChangedObservers.filter( + (observer) => observer !== observer, + ); + }; + } + + async signInAnonymously() { + const user = AuthConstants.anonymousUser1; + + this.#onAuthStateChangedObservers.forEach((observer) => observer(user)); + + return Promise.resolve(user); + } +} + +export default Auth;