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