mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-14 00:32:35 +10:00
FirebaseStub: refactoring
This commit is contained in:
@ -1,5 +1,60 @@
|
||||
import FirebaseStub from '../gatsby-plugin-firebase';
|
||||
|
||||
describe('auth', () => {
|
||||
afterEach(() => {
|
||||
FirebaseStub.auth().dispose();
|
||||
});
|
||||
|
||||
it('reuses existing Auth instance', () => {
|
||||
const auth1 = FirebaseStub.auth();
|
||||
const auth2 = FirebaseStub.auth();
|
||||
|
||||
expect(auth1.uuid).toBeTruthy();
|
||||
expect(auth2.uuid).toBeTruthy();
|
||||
expect(auth1.uuid).toEqual(auth2.uuid);
|
||||
});
|
||||
|
||||
it('returns anonymous user when signing in anonymously', async () => {
|
||||
const user = await FirebaseStub.auth().signInAnonymously();
|
||||
|
||||
expect(user).toBeTruthy();
|
||||
expect(user).toEqual(FirebaseStub.auth().anonymousUser);
|
||||
});
|
||||
|
||||
it('calls onAuthStateChanged observer with anonymous user when signing in anonymously', async () => {
|
||||
let user = null;
|
||||
let error = null;
|
||||
FirebaseStub.auth().onAuthStateChanged(
|
||||
(_user) => {
|
||||
user = _user;
|
||||
},
|
||||
(_error) => {
|
||||
error = _error;
|
||||
},
|
||||
);
|
||||
|
||||
await FirebaseStub.auth().signInAnonymously();
|
||||
|
||||
expect(user).toBeTruthy();
|
||||
expect(user).toEqual(FirebaseStub.auth().anonymousUser);
|
||||
expect(error).toBeNull();
|
||||
});
|
||||
|
||||
it('onAuthStateChanged unsubscribe removes observer', () => {
|
||||
const observer = () => {};
|
||||
const unsubscribe = FirebaseStub.auth().onAuthStateChanged(observer);
|
||||
expect(unsubscribe).toBeTruthy();
|
||||
expect(FirebaseStub.auth().onAuthStateChangedObservers.length).toEqual(1);
|
||||
expect(FirebaseStub.auth().onAuthStateChangedObservers[0]).toEqual(
|
||||
observer,
|
||||
);
|
||||
|
||||
unsubscribe();
|
||||
|
||||
expect(FirebaseStub.auth().onAuthStateChangedObservers.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('database', () => {
|
||||
it('reuses existing Database instance', () => {
|
||||
const database1 = FirebaseStub.database();
|
||||
@ -63,9 +118,9 @@ describe('database', () => {
|
||||
const users = usersDataSnapshot.val();
|
||||
expect(users).toBeTruthy();
|
||||
expect(Object.keys(users).length).toEqual(1);
|
||||
const testUser = users[FirebaseStub.database().testUser.uid];
|
||||
expect(testUser).toBeTruthy();
|
||||
expect(testUser.uid).toEqual(FirebaseStub.database().testUser.uid);
|
||||
const anonymousUser = users[FirebaseStub.database().anonymousUser.uid];
|
||||
expect(anonymousUser).toBeTruthy();
|
||||
expect(anonymousUser).toEqual(FirebaseStub.database().anonymousUser);
|
||||
});
|
||||
|
||||
it('retrieves resume if it exists', async () => {
|
||||
@ -97,12 +152,12 @@ describe('database', () => {
|
||||
|
||||
const user = (
|
||||
await FirebaseStub.database()
|
||||
.ref(`users/${FirebaseStub.database().testUser.uid}`)
|
||||
.ref(`users/${FirebaseStub.database().anonymousUser.uid}`)
|
||||
.once('value')
|
||||
).val();
|
||||
|
||||
expect(user).toBeTruthy();
|
||||
expect(user.uid).toEqual(FirebaseStub.database().testUser.uid);
|
||||
expect(user).toEqual(FirebaseStub.database().anonymousUser);
|
||||
});
|
||||
|
||||
it('retrieves null if user does not exist', async () => {
|
||||
@ -116,58 +171,3 @@ describe('database', () => {
|
||||
expect(user).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('auth', () => {
|
||||
afterEach(() => {
|
||||
FirebaseStub.auth().clearOnAuthStateChangedObservers();
|
||||
});
|
||||
|
||||
it('reuses existing Auth instance', () => {
|
||||
const auth1 = FirebaseStub.auth();
|
||||
const auth2 = FirebaseStub.auth();
|
||||
|
||||
expect(auth1.uuid).toBeTruthy();
|
||||
expect(auth2.uuid).toBeTruthy();
|
||||
expect(auth1.uuid).toEqual(auth2.uuid);
|
||||
});
|
||||
|
||||
it('returns test user when signing in anonymously', async () => {
|
||||
const user = await FirebaseStub.auth().signInAnonymously();
|
||||
|
||||
expect(user).toBeTruthy();
|
||||
expect(user).toEqual(FirebaseStub.database().testUser);
|
||||
});
|
||||
|
||||
it('calls onAuthStateChanged observer with test user when signing in anonymously', async () => {
|
||||
let user = null;
|
||||
let error = null;
|
||||
FirebaseStub.auth().onAuthStateChanged(
|
||||
(_user) => {
|
||||
user = _user;
|
||||
},
|
||||
(_error) => {
|
||||
error = _error;
|
||||
},
|
||||
);
|
||||
|
||||
await FirebaseStub.auth().signInAnonymously();
|
||||
|
||||
expect(user).toBeTruthy();
|
||||
expect(user).toEqual(FirebaseStub.database().testUser);
|
||||
expect(error).toBeNull();
|
||||
});
|
||||
|
||||
it('onAuthStateChanged unsubscribe removes observer', () => {
|
||||
const observer = () => {};
|
||||
const unsubscribe = FirebaseStub.auth().onAuthStateChanged(observer);
|
||||
expect(unsubscribe).toBeTruthy();
|
||||
expect(FirebaseStub.auth().onAuthStateChangedObservers.length).toEqual(1);
|
||||
expect(FirebaseStub.auth().onAuthStateChangedObservers[0]).toEqual(
|
||||
observer,
|
||||
);
|
||||
|
||||
unsubscribe();
|
||||
|
||||
expect(FirebaseStub.auth().onAuthStateChangedObservers.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
@ -4,6 +4,13 @@ import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
class Auth {
|
||||
static #instance = undefined;
|
||||
static anonymousUser = {
|
||||
displayName: 'Anonymous User 1',
|
||||
email: 'anonymous.user@noemail.com',
|
||||
isAnonymous: true,
|
||||
name: 'Anonymous 1',
|
||||
uid: 'anonym123',
|
||||
};
|
||||
#uuid = '';
|
||||
#onAuthStateChangedObservers = [];
|
||||
|
||||
@ -25,7 +32,11 @@ class Auth {
|
||||
return this.#onAuthStateChangedObservers;
|
||||
}
|
||||
|
||||
clearOnAuthStateChangedObservers() {
|
||||
get anonymousUser() {
|
||||
return Auth.anonymousUser;
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.#onAuthStateChangedObservers = [];
|
||||
}
|
||||
|
||||
@ -41,25 +52,21 @@ class Auth {
|
||||
|
||||
async signInAnonymously() {
|
||||
this.#onAuthStateChangedObservers.forEach((observer) =>
|
||||
observer(Database.testUser),
|
||||
observer(this.anonymousUser),
|
||||
);
|
||||
|
||||
return Promise.resolve(Database.testUser);
|
||||
return Promise.resolve(this.anonymousUser);
|
||||
}
|
||||
}
|
||||
|
||||
class Database {
|
||||
static testUser = {
|
||||
email: 'test.user@noemail.com',
|
||||
name: 'Test User',
|
||||
uid: 'testuser123',
|
||||
};
|
||||
static resumesPath = 'resumes';
|
||||
static usersPath = 'users';
|
||||
static #instance = undefined;
|
||||
#uuid = '';
|
||||
#data = {};
|
||||
#references = {};
|
||||
#anonymousUser = undefined;
|
||||
|
||||
constructor() {
|
||||
if (Database.#instance) {
|
||||
@ -69,10 +76,14 @@ class Database {
|
||||
Database.#instance = this;
|
||||
|
||||
this.#uuid = uuidv4();
|
||||
this.#anonymousUser = {
|
||||
uid: Auth.anonymousUser.uid,
|
||||
isAnonymous: Auth.anonymousUser.isAnonymous,
|
||||
};
|
||||
}
|
||||
|
||||
get testUser() {
|
||||
return Database.testUser;
|
||||
get anonymousUser() {
|
||||
return this.#anonymousUser;
|
||||
}
|
||||
|
||||
get demoResumeId() {
|
||||
@ -105,7 +116,7 @@ class Database {
|
||||
|
||||
resume.id = key;
|
||||
resume.name = `Test Resume ${key}`;
|
||||
resume.user = this.testUser.uid;
|
||||
resume.user = this.anonymousUser.uid;
|
||||
|
||||
let date = new Date('December 15, 2020 11:20:25');
|
||||
resume.updatedAt = date.valueOf();
|
||||
@ -116,7 +127,7 @@ class Database {
|
||||
this.#data[Database.resumesPath] = resumes;
|
||||
|
||||
const users = {};
|
||||
users[this.testUser.uid] = this.testUser;
|
||||
users[this.anonymousUser.uid] = this.anonymousUser;
|
||||
this.#data[Database.usersPath] = users;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user