mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-14 08:42:08 +10:00
FirebaseStub refactoring
This commit is contained in:
@ -1,9 +1,9 @@
|
|||||||
import FirebaseStub from '../gatsby-plugin-firebase';
|
import FirebaseStub, { AuthConstants } from '../gatsby-plugin-firebase';
|
||||||
|
|
||||||
describe('FirebaseStub', () => {
|
describe('FirebaseStub', () => {
|
||||||
const resumesPath = FirebaseStub.database().resumesPath;
|
const { resumesPath } = FirebaseStub.database();
|
||||||
const usersPath = FirebaseStub.database().usersPath;
|
const { usersPath } = FirebaseStub.database();
|
||||||
const connectedPath = FirebaseStub.database().connectedPath;
|
const { connectedPath } = FirebaseStub.database();
|
||||||
|
|
||||||
describe('auth', () => {
|
describe('auth', () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@ -23,7 +23,7 @@ describe('FirebaseStub', () => {
|
|||||||
const user = await FirebaseStub.auth().signInAnonymously();
|
const user = await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
expect(user).toBeTruthy();
|
expect(user).toBeTruthy();
|
||||||
expect(user).toEqual(FirebaseStub.auth().anonymousUser1);
|
expect(user).toEqual(AuthConstants.anonymousUser1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls onAuthStateChanged observer with anonymous user 1 when signing in anonymously', async () => {
|
it('calls onAuthStateChanged observer with anonymous user 1 when signing in anonymously', async () => {
|
||||||
@ -41,7 +41,7 @@ describe('FirebaseStub', () => {
|
|||||||
await FirebaseStub.auth().signInAnonymously();
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
expect(user).toBeTruthy();
|
expect(user).toBeTruthy();
|
||||||
expect(user).toEqual(FirebaseStub.auth().anonymousUser1);
|
expect(user).toEqual(AuthConstants.anonymousUser1);
|
||||||
expect(error).toBeNull();
|
expect(error).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -2,22 +2,10 @@ import path from 'path';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
import { AuthConstants } from './gatsby-plugin-firebase/constants';
|
||||||
|
|
||||||
class Auth {
|
class Auth {
|
||||||
static #instance = undefined;
|
static #instance = undefined;
|
||||||
static anonymousUser1 = {
|
|
||||||
displayName: 'Anonymous User 1',
|
|
||||||
email: 'anonymous1@noemail.com',
|
|
||||||
isAnonymous: true,
|
|
||||||
name: 'Anonymous 1',
|
|
||||||
uid: 'anonym123',
|
|
||||||
};
|
|
||||||
static anonymousUser2 = {
|
|
||||||
displayName: 'Anonymous User 2',
|
|
||||||
email: 'anonymous2@noemail.com',
|
|
||||||
isAnonymous: true,
|
|
||||||
name: 'Anonymous 2',
|
|
||||||
uid: 'anonym456',
|
|
||||||
};
|
|
||||||
#uuid = '';
|
#uuid = '';
|
||||||
#onAuthStateChangedObservers = [];
|
#onAuthStateChangedObservers = [];
|
||||||
|
|
||||||
@ -39,14 +27,6 @@ class Auth {
|
|||||||
return this.#onAuthStateChangedObservers;
|
return this.#onAuthStateChangedObservers;
|
||||||
}
|
}
|
||||||
|
|
||||||
get anonymousUser1() {
|
|
||||||
return Auth.anonymousUser1;
|
|
||||||
}
|
|
||||||
|
|
||||||
get anonymousUser2() {
|
|
||||||
return Auth.anonymousUser2;
|
|
||||||
}
|
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
this.#onAuthStateChangedObservers = [];
|
this.#onAuthStateChangedObservers = [];
|
||||||
}
|
}
|
||||||
@ -62,7 +42,7 @@ class Auth {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async signInAnonymously() {
|
async signInAnonymously() {
|
||||||
const user = this.anonymousUser1;
|
const user = AuthConstants.anonymousUser1;
|
||||||
|
|
||||||
this.#onAuthStateChangedObservers.forEach((observer) => observer(user));
|
this.#onAuthStateChangedObservers.forEach((observer) => observer(user));
|
||||||
|
|
||||||
@ -90,12 +70,12 @@ class Database {
|
|||||||
|
|
||||||
this.#uuid = uuidv4();
|
this.#uuid = uuidv4();
|
||||||
this.#anonymousUser1 = {
|
this.#anonymousUser1 = {
|
||||||
uid: Auth.anonymousUser1.uid,
|
uid: AuthConstants.anonymousUser1.uid,
|
||||||
isAnonymous: Auth.anonymousUser1.isAnonymous,
|
isAnonymous: AuthConstants.anonymousUser1.isAnonymous,
|
||||||
};
|
};
|
||||||
this.#anonymousUser2 = {
|
this.#anonymousUser2 = {
|
||||||
uid: Auth.anonymousUser2.uid,
|
uid: AuthConstants.anonymousUser2.uid,
|
||||||
isAnonymous: Auth.anonymousUser2.isAnonymous,
|
isAnonymous: AuthConstants.anonymousUser2.isAnonymous,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,3 +372,4 @@ Object.defineProperty(FirebaseStub.database.ServerValue, 'TIMESTAMP', {
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default FirebaseStub;
|
export default FirebaseStub;
|
||||||
|
export { AuthConstants };
|
||||||
|
|||||||
1
__mocks__/gatsby-plugin-firebase/constants.js
Normal file
1
__mocks__/gatsby-plugin-firebase/constants.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
export { Auth as AuthConstants } from './constants/auth';
|
||||||
19
__mocks__/gatsby-plugin-firebase/constants/auth.js
Normal file
19
__mocks__/gatsby-plugin-firebase/constants/auth.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
class Auth {
|
||||||
|
static anonymousUser1 = {
|
||||||
|
displayName: 'Anonymous User 1',
|
||||||
|
email: 'anonymous1@noemail.com',
|
||||||
|
isAnonymous: true,
|
||||||
|
name: 'Anonymous 1',
|
||||||
|
uid: 'anonym123',
|
||||||
|
};
|
||||||
|
|
||||||
|
static anonymousUser2 = {
|
||||||
|
displayName: 'Anonymous User 2',
|
||||||
|
email: 'anonymous2@noemail.com',
|
||||||
|
isAnonymous: true,
|
||||||
|
name: 'Anonymous 2',
|
||||||
|
uid: 'anonym456',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Auth };
|
||||||
Reference in New Issue
Block a user