From eb54a7f69df638bedd93df240b90a167306b7877 Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Tue, 11 May 2021 15:19:05 +0200 Subject: [PATCH] Delete Account: started adding unit tests --- __mocks__/gatsby-plugin-firebase.js | 8 ++- .../constants/functions.js | 15 ++++++ .../functions/functions.js | 54 +++++++++++++++++++ .../functions/httpsCallableResult.js | 19 +++++++ .../builder.settings.deleteAccount.test.js | 36 +++++++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 __mocks__/gatsby-plugin-firebase/constants/functions.js create mode 100644 __mocks__/gatsby-plugin-firebase/functions/functions.js create mode 100644 __mocks__/gatsby-plugin-firebase/functions/httpsCallableResult.js create mode 100644 src/pages/app/__tests__/builder.settings.deleteAccount.test.js diff --git a/__mocks__/gatsby-plugin-firebase.js b/__mocks__/gatsby-plugin-firebase.js index 0e0b5955..556b9917 100644 --- a/__mocks__/gatsby-plugin-firebase.js +++ b/__mocks__/gatsby-plugin-firebase.js @@ -1,7 +1,9 @@ import Auth from './gatsby-plugin-firebase/auth/auth'; import Database from './gatsby-plugin-firebase/database/database'; +import Functions from './gatsby-plugin-firebase/functions/functions'; import AuthConstants from './gatsby-plugin-firebase/constants/auth'; import DatabaseConstants from './gatsby-plugin-firebase/constants/database'; +import FunctionsConstants from './gatsby-plugin-firebase/constants/functions'; class FirebaseStub { static auth() { @@ -11,6 +13,10 @@ class FirebaseStub { static database() { return Database.instance; } + + static functions() { + return Functions.instance; + } } FirebaseStub.database.ServerValue = {}; @@ -21,4 +27,4 @@ Object.defineProperty(FirebaseStub.database.ServerValue, 'TIMESTAMP', { }); export default FirebaseStub; -export { AuthConstants, DatabaseConstants }; +export { AuthConstants, DatabaseConstants, FunctionsConstants }; diff --git a/__mocks__/gatsby-plugin-firebase/constants/functions.js b/__mocks__/gatsby-plugin-firebase/constants/functions.js new file mode 100644 index 00000000..6ab0c54b --- /dev/null +++ b/__mocks__/gatsby-plugin-firebase/constants/functions.js @@ -0,0 +1,15 @@ +const deleteUserFunctionName = 'deleteUser'; + +const defaultDelayInMilliseconds = 2000; + +class Functions { + static get deleteUserFunctionName() { + return deleteUserFunctionName; + } + + static get defaultDelayInMilliseconds() { + return defaultDelayInMilliseconds; + } +} + +export default Functions; diff --git a/__mocks__/gatsby-plugin-firebase/functions/functions.js b/__mocks__/gatsby-plugin-firebase/functions/functions.js new file mode 100644 index 00000000..3a4d6b1d --- /dev/null +++ b/__mocks__/gatsby-plugin-firebase/functions/functions.js @@ -0,0 +1,54 @@ +/* eslint-disable no-underscore-dangle */ +import { v4 as uuidv4 } from 'uuid'; + +import FunctionsConstants from '../constants/functions'; +import HttpsCallableResult from './httpsCallableResult'; +import { delay } from '../../../src/utils/index'; + +const singleton = Symbol(''); +const singletonEnforcer = Symbol(''); + +const deleteUser = async () => { + await delay(FunctionsConstants.defaultDelayInMilliseconds); + + return new HttpsCallableResult(true); +}; + +class Functions { + constructor(enforcer) { + if (enforcer !== singletonEnforcer) { + throw new Error('Cannot construct singleton'); + } + + this._uuid = uuidv4(); + + this._httpsCallables = {}; + this._httpsCallables[ + FunctionsConstants.deleteUserFunctionName + ] = deleteUser; + } + + static get instance() { + if (!this[singleton]) { + this[singleton] = new Functions(singletonEnforcer); + } + + return this[singleton]; + } + + get uuid() { + return this._uuid; + } + + httpsCallable(name) { + if (!name) { + throw new Error('name must be provided.'); + } else if (typeof name !== 'string') { + throw new Error('name should be a string.'); + } + + return this._httpsCallables[name]; + } +} + +export default Functions; diff --git a/__mocks__/gatsby-plugin-firebase/functions/httpsCallableResult.js b/__mocks__/gatsby-plugin-firebase/functions/httpsCallableResult.js new file mode 100644 index 00000000..d35ba4ee --- /dev/null +++ b/__mocks__/gatsby-plugin-firebase/functions/httpsCallableResult.js @@ -0,0 +1,19 @@ +/* eslint-disable no-underscore-dangle */ +import { v4 as uuidv4 } from 'uuid'; + +class HttpsCallableResult { + constructor(data) { + this._uuid = uuidv4(); + this._data = data; + } + + get data() { + return this._data; + } + + get uuid() { + return this._uuid; + } +} + +export default HttpsCallableResult; diff --git a/src/pages/app/__tests__/builder.settings.deleteAccount.test.js b/src/pages/app/__tests__/builder.settings.deleteAccount.test.js new file mode 100644 index 00000000..4720dcac --- /dev/null +++ b/src/pages/app/__tests__/builder.settings.deleteAccount.test.js @@ -0,0 +1,36 @@ +import { fireEvent, screen } from '@testing-library/react'; + +import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase'; + +import { setupAndWait } from './helpers/builder'; + +const testTimeoutInMilliseconds = 20000; +jest.setTimeout(testTimeoutInMilliseconds); + +async function setup() { + const resumeId = DatabaseConstants.demoStateResume1Id; + await setupAndWait(resumeId, true, true); + + const button = screen.getByRole('button', { + name: /Delete Account/i, + }); + + const mockFirebaseFunctionsHttpsCallable = jest.spyOn( + FirebaseStub.functions(), + 'httpsCallable', + ); + + return { + button, + mockFirebaseFunctionsHttpsCallable, + }; +} + +test('prompts for confirmation', async () => { + const { button, mockFirebaseFunctionsHttpsCallable } = await setup(); + + fireEvent.click(button); + + expect(button).toHaveTextContent('Are you sure?'); + expect(mockFirebaseFunctionsHttpsCallable).not.toHaveBeenCalled(); +});