mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-14 00:32:35 +10:00
Firebase Stub refactoring
This commit is contained in:
@ -4,10 +4,6 @@ import FirebaseStub, {
|
||||
} from '../gatsby-plugin-firebase';
|
||||
|
||||
describe('FirebaseStub', () => {
|
||||
const { resumesPath } = FirebaseStub.database();
|
||||
const { usersPath } = FirebaseStub.database();
|
||||
const { connectedPath } = FirebaseStub.database();
|
||||
|
||||
describe('auth', () => {
|
||||
afterEach(() => {
|
||||
FirebaseStub.auth().dispose();
|
||||
@ -74,8 +70,12 @@ describe('FirebaseStub', () => {
|
||||
});
|
||||
|
||||
it('reuses existing Reference instance', () => {
|
||||
const ref1 = FirebaseStub.database().ref(`${resumesPath}/123`);
|
||||
const ref2 = FirebaseStub.database().ref(`${resumesPath}/123`);
|
||||
const ref1 = FirebaseStub.database().ref(
|
||||
`${DatabaseConstants.resumesPath}/123`,
|
||||
);
|
||||
const ref2 = FirebaseStub.database().ref(
|
||||
`${DatabaseConstants.resumesPath}/123`,
|
||||
);
|
||||
|
||||
expect(ref1.uuid).toBeTruthy();
|
||||
expect(ref2.uuid).toBeTruthy();
|
||||
@ -91,7 +91,7 @@ describe('FirebaseStub', () => {
|
||||
});
|
||||
|
||||
it("can spy on Reference 'update' function", async () => {
|
||||
const referencePath = `${resumesPath}/123456`;
|
||||
const referencePath = `${DatabaseConstants.resumesPath}/123456`;
|
||||
const functionSpy = jest.spyOn(
|
||||
FirebaseStub.database().ref(referencePath),
|
||||
'update',
|
||||
@ -109,7 +109,9 @@ describe('FirebaseStub', () => {
|
||||
it('initializing data sets up resumes and users', async () => {
|
||||
FirebaseStub.database().initializeData();
|
||||
|
||||
const resumesRef = FirebaseStub.database().ref(resumesPath);
|
||||
const resumesRef = FirebaseStub.database().ref(
|
||||
DatabaseConstants.resumesPath,
|
||||
);
|
||||
const resumesDataSnapshot = await resumesRef.once('value');
|
||||
const resumes = resumesDataSnapshot.val();
|
||||
expect(resumes).toBeTruthy();
|
||||
@ -136,7 +138,7 @@ describe('FirebaseStub', () => {
|
||||
FirebaseStub.database().anonymousUser1.uid,
|
||||
);
|
||||
|
||||
const usersRef = FirebaseStub.database().ref(usersPath);
|
||||
const usersRef = FirebaseStub.database().ref(DatabaseConstants.usersPath);
|
||||
const usersDataSnapshot = await usersRef.once('value');
|
||||
const users = usersDataSnapshot.val();
|
||||
expect(users).toBeTruthy();
|
||||
@ -154,7 +156,9 @@ describe('FirebaseStub', () => {
|
||||
|
||||
const resume = (
|
||||
await FirebaseStub.database()
|
||||
.ref(`${resumesPath}/${DatabaseConstants.demoStateResume1Id}`)
|
||||
.ref(
|
||||
`${DatabaseConstants.resumesPath}/${DatabaseConstants.demoStateResume1Id}`,
|
||||
)
|
||||
.once('value')
|
||||
).val();
|
||||
|
||||
@ -168,7 +172,7 @@ describe('FirebaseStub', () => {
|
||||
|
||||
const resume = (
|
||||
await FirebaseStub.database()
|
||||
.ref(`${resumesPath}/${resumeId}`)
|
||||
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||
.once('value')
|
||||
).val();
|
||||
|
||||
@ -180,7 +184,11 @@ describe('FirebaseStub', () => {
|
||||
|
||||
const user = (
|
||||
await FirebaseStub.database()
|
||||
.ref(`${usersPath}/${FirebaseStub.database().anonymousUser1.uid}`)
|
||||
.ref(
|
||||
`${DatabaseConstants.usersPath}/${
|
||||
FirebaseStub.database().anonymousUser1.uid
|
||||
}`,
|
||||
)
|
||||
.once('value')
|
||||
).val();
|
||||
|
||||
@ -194,7 +202,7 @@ describe('FirebaseStub', () => {
|
||||
|
||||
const user = (
|
||||
await FirebaseStub.database()
|
||||
.ref(`${usersPath}/${userId}`)
|
||||
.ref(`${DatabaseConstants.usersPath}/${userId}`)
|
||||
.once('value')
|
||||
).val();
|
||||
|
||||
@ -205,7 +213,7 @@ describe('FirebaseStub', () => {
|
||||
let snapshotValue = null;
|
||||
|
||||
FirebaseStub.database()
|
||||
.ref(connectedPath)
|
||||
.ref(DatabaseConstants.connectedPath)
|
||||
.on('value', (snapshot) => {
|
||||
snapshotValue = snapshot.val();
|
||||
});
|
||||
@ -215,13 +223,13 @@ describe('FirebaseStub', () => {
|
||||
|
||||
it('triggers callback with resumes when listening for data changes on the resumes reference path', async () => {
|
||||
const resumesDataSnapshot = await FirebaseStub.database()
|
||||
.ref(resumesPath)
|
||||
.ref(DatabaseConstants.resumesPath)
|
||||
.once('value');
|
||||
const resumes = resumesDataSnapshot.val();
|
||||
let snapshotValue = null;
|
||||
|
||||
FirebaseStub.database()
|
||||
.ref(resumesPath)
|
||||
.ref(DatabaseConstants.resumesPath)
|
||||
.on('value', (snapshot) => {
|
||||
snapshotValue = snapshot.val();
|
||||
});
|
||||
@ -233,7 +241,7 @@ describe('FirebaseStub', () => {
|
||||
let snapshotValue = null;
|
||||
|
||||
FirebaseStub.database()
|
||||
.ref(resumesPath)
|
||||
.ref(DatabaseConstants.resumesPath)
|
||||
.orderByChild('user')
|
||||
.equalTo(FirebaseStub.database().anonymousUser1.uid)
|
||||
.on('value', (snapshot) => {
|
||||
|
||||
@ -79,18 +79,6 @@ class Database {
|
||||
};
|
||||
}
|
||||
|
||||
get resumesPath() {
|
||||
return Reference.resumesPath;
|
||||
}
|
||||
|
||||
get usersPath() {
|
||||
return Reference.usersPath;
|
||||
}
|
||||
|
||||
get connectedPath() {
|
||||
return Reference.connectedPath;
|
||||
}
|
||||
|
||||
get anonymousUser1() {
|
||||
return this.#anonymousUser1;
|
||||
}
|
||||
@ -140,12 +128,12 @@ class Database {
|
||||
resume.name = `Test Resume ${key}`;
|
||||
}
|
||||
|
||||
this.#data[this.resumesPath] = resumes;
|
||||
this.#data[DatabaseConstants.resumesPath] = resumes;
|
||||
|
||||
const users = {};
|
||||
users[this.anonymousUser1.uid] = this.anonymousUser1;
|
||||
users[this.anonymousUser2.uid] = this.anonymousUser2;
|
||||
this.#data[this.usersPath] = users;
|
||||
this.#data[DatabaseConstants.usersPath] = users;
|
||||
}
|
||||
|
||||
ref(path) {
|
||||
@ -161,9 +149,6 @@ class Database {
|
||||
}
|
||||
|
||||
class Reference {
|
||||
static resumesPath = 'resumes';
|
||||
static usersPath = 'users';
|
||||
static connectedPath = '/.info/connected';
|
||||
#rootPath = '.';
|
||||
#path = '';
|
||||
#uuid = '';
|
||||
@ -211,8 +196,8 @@ class Reference {
|
||||
|
||||
let data = null;
|
||||
if (
|
||||
this.#path === Reference.resumesPath ||
|
||||
this.#path === Reference.usersPath
|
||||
this.#path === DatabaseConstants.resumesPath ||
|
||||
this.#path === DatabaseConstants.usersPath
|
||||
) {
|
||||
data = this.#path in databaseData ? databaseData[this.#path] : null;
|
||||
|
||||
@ -229,8 +214,8 @@ class Reference {
|
||||
}
|
||||
|
||||
if (
|
||||
this.#path.startsWith(`${Reference.resumesPath}/`) ||
|
||||
this.#path.startsWith(`${Reference.usersPath}/`)
|
||||
this.#path.startsWith(`${DatabaseConstants.resumesPath}/`) ||
|
||||
this.#path.startsWith(`${DatabaseConstants.usersPath}/`)
|
||||
) {
|
||||
const databaseLocationId = this.#path.substring(
|
||||
this.#path.indexOf('/') + 1,
|
||||
@ -259,9 +244,9 @@ class Reference {
|
||||
|
||||
let snapshot = new DataSnapshot(eventType, this, null);
|
||||
|
||||
if (this.#path === Reference.connectedPath) {
|
||||
if (this.#path === DatabaseConstants.connectedPath) {
|
||||
snapshot = new DataSnapshot(eventType, this, true);
|
||||
} else if (this.#path === Reference.resumesPath) {
|
||||
} else if (this.#path === DatabaseConstants.resumesPath) {
|
||||
snapshot = new DataSnapshot(eventType, this);
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,23 @@
|
||||
const resumesPath = 'resumes';
|
||||
const usersPath = 'users';
|
||||
const connectedPath = '/.info/connected';
|
||||
const demoStateResume1Id = 'demo_1';
|
||||
const demoStateResume2Id = 'demo_2';
|
||||
const initialStateResumeId = 'initst';
|
||||
|
||||
class Database {
|
||||
static get resumesPath() {
|
||||
return resumesPath;
|
||||
}
|
||||
|
||||
static get usersPath() {
|
||||
return usersPath;
|
||||
}
|
||||
|
||||
static get connectedPath() {
|
||||
return connectedPath;
|
||||
}
|
||||
|
||||
static get demoStateResume1Id() {
|
||||
return demoStateResume1Id;
|
||||
}
|
||||
|
||||
@ -25,7 +25,6 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
describe('Builder', () => {
|
||||
const { resumesPath } = FirebaseStub.database();
|
||||
let resumeId = null;
|
||||
let resume = null;
|
||||
let mockUpdateFunction = null;
|
||||
@ -34,11 +33,13 @@ describe('Builder', () => {
|
||||
resumeId = DatabaseConstants.demoStateResume1Id;
|
||||
resume = (
|
||||
await FirebaseStub.database()
|
||||
.ref(`${resumesPath}/${resumeId}`)
|
||||
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||
.once('value')
|
||||
).val();
|
||||
mockUpdateFunction = jest.spyOn(
|
||||
FirebaseStub.database().ref(`${resumesPath}/${resumeId}`),
|
||||
FirebaseStub.database().ref(
|
||||
`${DatabaseConstants.resumesPath}/${resumeId}`,
|
||||
),
|
||||
'update',
|
||||
);
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import React from 'react';
|
||||
import { act, render, screen } from '@testing-library/react';
|
||||
|
||||
import FirebaseStub from 'gatsby-plugin-firebase';
|
||||
import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase';
|
||||
|
||||
import '../../../i18n/index';
|
||||
import { SettingsProvider } from '../../../contexts/SettingsContext';
|
||||
@ -17,14 +17,13 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
describe('Dashboard', () => {
|
||||
const { resumesPath } = FirebaseStub.database();
|
||||
let resumes = null;
|
||||
const user = FirebaseStub.database().anonymousUser1;
|
||||
|
||||
beforeEach(async () => {
|
||||
resumes = (
|
||||
await FirebaseStub.database()
|
||||
.ref(resumesPath)
|
||||
.ref(DatabaseConstants.resumesPath)
|
||||
.orderByChild('user')
|
||||
.equalTo(user.uid)
|
||||
.once('value')
|
||||
|
||||
@ -10,11 +10,10 @@ describe('Castform', () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
FirebaseStub.database().initializeData();
|
||||
const { resumesPath } = FirebaseStub.database();
|
||||
const resumeId = DatabaseConstants.initialStateResumeId;
|
||||
resume = (
|
||||
await FirebaseStub.database()
|
||||
.ref(`${resumesPath}/${resumeId}`)
|
||||
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||
.once('value')
|
||||
).val();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user