mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-13 16:22:59 +10:00
Firebase Stub: resolving ESLint errors
This commit is contained in:
@ -119,36 +119,30 @@ describe('FirebaseStub', () => {
|
|||||||
const demoStateResume1 = resumes[DatabaseConstants.demoStateResume1Id];
|
const demoStateResume1 = resumes[DatabaseConstants.demoStateResume1Id];
|
||||||
expect(demoStateResume1).toBeTruthy();
|
expect(demoStateResume1).toBeTruthy();
|
||||||
expect(demoStateResume1.id).toEqual(DatabaseConstants.demoStateResume1Id);
|
expect(demoStateResume1.id).toEqual(DatabaseConstants.demoStateResume1Id);
|
||||||
expect(demoStateResume1.user).toEqual(
|
expect(demoStateResume1.user).toEqual(DatabaseConstants.user1.uid);
|
||||||
FirebaseStub.database().anonymousUser1.uid,
|
|
||||||
);
|
|
||||||
const demoStateResume2 = resumes[DatabaseConstants.demoStateResume2Id];
|
const demoStateResume2 = resumes[DatabaseConstants.demoStateResume2Id];
|
||||||
expect(demoStateResume2).toBeTruthy();
|
expect(demoStateResume2).toBeTruthy();
|
||||||
expect(demoStateResume2.id).toEqual(DatabaseConstants.demoStateResume2Id);
|
expect(demoStateResume2.id).toEqual(DatabaseConstants.demoStateResume2Id);
|
||||||
expect(demoStateResume2.user).toEqual(
|
expect(demoStateResume2.user).toEqual(DatabaseConstants.user2.uid);
|
||||||
FirebaseStub.database().anonymousUser2.uid,
|
|
||||||
);
|
|
||||||
const initialStateResume =
|
const initialStateResume =
|
||||||
resumes[DatabaseConstants.initialStateResumeId];
|
resumes[DatabaseConstants.initialStateResumeId];
|
||||||
expect(initialStateResume).toBeTruthy();
|
expect(initialStateResume).toBeTruthy();
|
||||||
expect(initialStateResume.id).toEqual(
|
expect(initialStateResume.id).toEqual(
|
||||||
DatabaseConstants.initialStateResumeId,
|
DatabaseConstants.initialStateResumeId,
|
||||||
);
|
);
|
||||||
expect(initialStateResume.user).toEqual(
|
expect(initialStateResume.user).toEqual(DatabaseConstants.user1.uid);
|
||||||
FirebaseStub.database().anonymousUser1.uid,
|
|
||||||
);
|
|
||||||
|
|
||||||
const usersRef = FirebaseStub.database().ref(DatabaseConstants.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();
|
||||||
expect(Object.keys(users)).toHaveLength(2);
|
expect(Object.keys(users)).toHaveLength(2);
|
||||||
const anonymousUser1 = users[FirebaseStub.database().anonymousUser1.uid];
|
const anonymousUser1 = users[DatabaseConstants.user1.uid];
|
||||||
expect(anonymousUser1).toBeTruthy();
|
expect(anonymousUser1).toBeTruthy();
|
||||||
expect(anonymousUser1).toEqual(FirebaseStub.database().anonymousUser1);
|
expect(anonymousUser1).toEqual(DatabaseConstants.user1);
|
||||||
const anonymousUser2 = users[FirebaseStub.database().anonymousUser2.uid];
|
const anonymousUser2 = users[DatabaseConstants.user2.uid];
|
||||||
expect(anonymousUser2).toBeTruthy();
|
expect(anonymousUser2).toBeTruthy();
|
||||||
expect(anonymousUser2).toEqual(FirebaseStub.database().anonymousUser2);
|
expect(anonymousUser2).toEqual(DatabaseConstants.user2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('retrieves resume if it exists', async () => {
|
it('retrieves resume if it exists', async () => {
|
||||||
@ -184,16 +178,12 @@ describe('FirebaseStub', () => {
|
|||||||
|
|
||||||
const user = (
|
const user = (
|
||||||
await FirebaseStub.database()
|
await FirebaseStub.database()
|
||||||
.ref(
|
.ref(`${DatabaseConstants.usersPath}/${DatabaseConstants.user1.uid}`)
|
||||||
`${DatabaseConstants.usersPath}/${
|
|
||||||
FirebaseStub.database().anonymousUser1.uid
|
|
||||||
}`,
|
|
||||||
)
|
|
||||||
.once('value')
|
.once('value')
|
||||||
).val();
|
).val();
|
||||||
|
|
||||||
expect(user).toBeTruthy();
|
expect(user).toBeTruthy();
|
||||||
expect(user).toEqual(FirebaseStub.database().anonymousUser1);
|
expect(user).toEqual(DatabaseConstants.user1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('retrieves null if user does not exist', async () => {
|
it('retrieves null if user does not exist', async () => {
|
||||||
@ -243,14 +233,14 @@ describe('FirebaseStub', () => {
|
|||||||
FirebaseStub.database()
|
FirebaseStub.database()
|
||||||
.ref(DatabaseConstants.resumesPath)
|
.ref(DatabaseConstants.resumesPath)
|
||||||
.orderByChild('user')
|
.orderByChild('user')
|
||||||
.equalTo(FirebaseStub.database().anonymousUser1.uid)
|
.equalTo(DatabaseConstants.user1.uid)
|
||||||
.on('value', (snapshot) => {
|
.on('value', (snapshot) => {
|
||||||
snapshotValue = snapshot.val();
|
snapshotValue = snapshot.val();
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(Object.keys(snapshotValue)).toHaveLength(2);
|
expect(Object.keys(snapshotValue)).toHaveLength(2);
|
||||||
Object.values(snapshotValue).forEach((resume) =>
|
Object.values(snapshotValue).forEach((resume) =>
|
||||||
expect(resume.user).toEqual(FirebaseStub.database().anonymousUser1.uid),
|
expect(resume.user).toEqual(DatabaseConstants.user1.uid),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
import Auth from './gatsby-plugin-firebase/auth/auth';
|
import Auth from './gatsby-plugin-firebase/auth/auth';
|
||||||
import Database from './gatsby-plugin-firebase/database/database';
|
import Database from './gatsby-plugin-firebase/database/database';
|
||||||
import {
|
import AuthConstants from './gatsby-plugin-firebase/constants/auth';
|
||||||
AuthConstants,
|
import DatabaseConstants from './gatsby-plugin-firebase/constants/database';
|
||||||
DatabaseConstants,
|
|
||||||
} from './gatsby-plugin-firebase/constants';
|
|
||||||
|
|
||||||
class FirebaseStub {
|
class FirebaseStub {
|
||||||
static auth() {
|
static auth() {
|
||||||
@ -11,7 +9,7 @@ class FirebaseStub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static database() {
|
static database() {
|
||||||
return new Database();
|
return Database.instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +0,0 @@
|
|||||||
import AuthConstants from './constants/auth';
|
|
||||||
import DatabaseConstants from './constants/database';
|
|
||||||
|
|
||||||
export { AuthConstants, DatabaseConstants };
|
|
||||||
@ -1,10 +1,22 @@
|
|||||||
|
import AuthConstants from './auth';
|
||||||
|
|
||||||
const resumesPath = 'resumes';
|
const resumesPath = 'resumes';
|
||||||
const usersPath = 'users';
|
const usersPath = 'users';
|
||||||
const connectedPath = '/.info/connected';
|
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';
|
||||||
|
|
||||||
|
const user1 = {
|
||||||
|
uid: AuthConstants.anonymousUser1.uid,
|
||||||
|
isAnonymous: AuthConstants.anonymousUser1.isAnonymous,
|
||||||
|
};
|
||||||
|
const user2 = {
|
||||||
|
uid: AuthConstants.anonymousUser2.uid,
|
||||||
|
isAnonymous: AuthConstants.anonymousUser2.isAnonymous,
|
||||||
|
};
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
static get resumesPath() {
|
static get resumesPath() {
|
||||||
return resumesPath;
|
return resumesPath;
|
||||||
@ -29,6 +41,14 @@ class Database {
|
|||||||
static get initialStateResumeId() {
|
static get initialStateResumeId() {
|
||||||
return initialStateResumeId;
|
return initialStateResumeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static get user1() {
|
||||||
|
return user1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static get user2() {
|
||||||
|
return user2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Database;
|
export default Database;
|
||||||
|
|||||||
@ -2,102 +2,85 @@ 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, DatabaseConstants } from '../constants';
|
import DatabaseConstants from '../constants/database';
|
||||||
import Reference from './reference';
|
import Reference from './reference';
|
||||||
|
|
||||||
class Database {
|
const singleton = Symbol('');
|
||||||
static #instance = undefined;
|
const singletonEnforcer = Symbol('');
|
||||||
#uuid = '';
|
|
||||||
#data = {};
|
|
||||||
#references = {};
|
|
||||||
#anonymousUser1 = undefined;
|
|
||||||
#anonymousUser2 = undefined;
|
|
||||||
|
|
||||||
constructor() {
|
const readFile = (fileRelativePath) => {
|
||||||
if (Database.#instance) {
|
|
||||||
return Database.#instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
Database.#instance = this;
|
|
||||||
|
|
||||||
this.#uuid = uuidv4();
|
|
||||||
this.#anonymousUser1 = {
|
|
||||||
uid: AuthConstants.anonymousUser1.uid,
|
|
||||||
isAnonymous: AuthConstants.anonymousUser1.isAnonymous,
|
|
||||||
};
|
|
||||||
this.#anonymousUser2 = {
|
|
||||||
uid: AuthConstants.anonymousUser2.uid,
|
|
||||||
isAnonymous: AuthConstants.anonymousUser2.isAnonymous,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
get anonymousUser1() {
|
|
||||||
return this.#anonymousUser1;
|
|
||||||
}
|
|
||||||
|
|
||||||
get anonymousUser2() {
|
|
||||||
return this.#anonymousUser2;
|
|
||||||
}
|
|
||||||
|
|
||||||
get uuid() {
|
|
||||||
return this.#uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
static readFile(fileRelativePath) {
|
|
||||||
const fileAbsolutePath = path.resolve(__dirname, fileRelativePath);
|
const fileAbsolutePath = path.resolve(__dirname, fileRelativePath);
|
||||||
const fileBuffer = fs.readFileSync(fileAbsolutePath);
|
const fileBuffer = fs.readFileSync(fileAbsolutePath);
|
||||||
const fileData = JSON.parse(fileBuffer);
|
const fileData = JSON.parse(fileBuffer);
|
||||||
return fileData;
|
return fileData;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Database {
|
||||||
|
constructor(enforcer) {
|
||||||
|
if (enforcer !== singletonEnforcer) {
|
||||||
|
throw new Error('Cannot construct singleton');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.uuidField = uuidv4();
|
||||||
|
this.dataField = {};
|
||||||
|
this.referencesField = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
static get instance() {
|
||||||
|
if (!this[singleton]) {
|
||||||
|
this[singleton] = new Database(singletonEnforcer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this[singleton];
|
||||||
|
}
|
||||||
|
|
||||||
|
get uuid() {
|
||||||
|
return this.uuidField;
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeData() {
|
initializeData() {
|
||||||
const resumes = {};
|
const resumes = {};
|
||||||
|
|
||||||
const demoStateResume1 = Database.readFile(
|
const demoStateResume1 = readFile('../../../src/data/demoState.json');
|
||||||
'../../../src/data/demoState.json',
|
const date = new Date('December 15, 2020 11:20:25');
|
||||||
);
|
|
||||||
let date = new Date('December 15, 2020 11:20:25');
|
|
||||||
demoStateResume1.updatedAt = date.valueOf();
|
demoStateResume1.updatedAt = date.valueOf();
|
||||||
date.setMonth(date.getMonth() - 2);
|
date.setMonth(date.getMonth() - 2);
|
||||||
demoStateResume1.createdAt = date.valueOf();
|
demoStateResume1.createdAt = date.valueOf();
|
||||||
demoStateResume1.user = this.anonymousUser1.uid;
|
demoStateResume1.user = DatabaseConstants.user1.uid;
|
||||||
resumes[DatabaseConstants.demoStateResume1Id] = demoStateResume1;
|
resumes[DatabaseConstants.demoStateResume1Id] = demoStateResume1;
|
||||||
|
|
||||||
const demoStateResume2 = JSON.parse(JSON.stringify(demoStateResume1));
|
const demoStateResume2 = JSON.parse(JSON.stringify(demoStateResume1));
|
||||||
demoStateResume2.user = this.anonymousUser2.uid;
|
demoStateResume2.user = DatabaseConstants.user2.uid;
|
||||||
resumes[DatabaseConstants.demoStateResume2Id] = demoStateResume2;
|
resumes[DatabaseConstants.demoStateResume2Id] = demoStateResume2;
|
||||||
|
|
||||||
const initialStateResume = Database.readFile(
|
const initialStateResume = readFile('../../../src/data/initialState.json');
|
||||||
'../../../src/data/initialState.json',
|
|
||||||
);
|
|
||||||
initialStateResume.updatedAt = date.valueOf();
|
initialStateResume.updatedAt = date.valueOf();
|
||||||
initialStateResume.createdAt = date.valueOf();
|
initialStateResume.createdAt = date.valueOf();
|
||||||
initialStateResume.user = this.anonymousUser1.uid;
|
initialStateResume.user = DatabaseConstants.user1.uid;
|
||||||
resumes[DatabaseConstants.initialStateResumeId] = initialStateResume;
|
resumes[DatabaseConstants.initialStateResumeId] = initialStateResume;
|
||||||
|
|
||||||
for (var key in resumes) {
|
Object.keys(resumes).forEach((key) => {
|
||||||
const resume = resumes[key];
|
const resume = resumes[key];
|
||||||
|
|
||||||
resume.id = key;
|
resume.id = key;
|
||||||
resume.name = `Test Resume ${key}`;
|
resume.name = `Test Resume ${key}`;
|
||||||
}
|
});
|
||||||
|
|
||||||
this.#data[DatabaseConstants.resumesPath] = resumes;
|
this.dataField[DatabaseConstants.resumesPath] = resumes;
|
||||||
|
|
||||||
const users = {};
|
const users = {};
|
||||||
users[this.anonymousUser1.uid] = this.anonymousUser1;
|
users[DatabaseConstants.user1.uid] = DatabaseConstants.user1;
|
||||||
users[this.anonymousUser2.uid] = this.anonymousUser2;
|
users[DatabaseConstants.user2.uid] = DatabaseConstants.user2;
|
||||||
this.#data[DatabaseConstants.usersPath] = users;
|
this.dataField[DatabaseConstants.usersPath] = users;
|
||||||
}
|
}
|
||||||
|
|
||||||
ref(path) {
|
ref(referencePath) {
|
||||||
const newRef = new Reference(path, () => this.#data);
|
const newRef = new Reference(referencePath, () => this.dataField);
|
||||||
const existingRef = this.#references[newRef.path];
|
const existingRef = this.referencesField[newRef.path];
|
||||||
if (existingRef) {
|
if (existingRef) {
|
||||||
return existingRef;
|
return existingRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#references[newRef.path] = newRef;
|
this.referencesField[newRef.path] = newRef;
|
||||||
return newRef;
|
return newRef;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
import { DatabaseConstants } from '../constants';
|
import DatabaseConstants from '../constants/database';
|
||||||
import DataSnapshot from './dataSnapshot';
|
import DataSnapshot from './dataSnapshot';
|
||||||
|
|
||||||
const rootPath = '.';
|
const rootPath = '.';
|
||||||
|
|||||||
@ -63,7 +63,7 @@ describe('Builder', () => {
|
|||||||
await FirebaseStub.auth().signInAnonymously();
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
});
|
});
|
||||||
|
|
||||||
await waitFor(() => expect(mockUpdateFunction).toHaveBeenCalledTimes(1), {
|
await waitFor(() => mockUpdateFunction.mock.calls[0][0], {
|
||||||
timeout: DebounceWaitTime,
|
timeout: DebounceWaitTime,
|
||||||
});
|
});
|
||||||
mockUpdateFunction.mockClear();
|
mockUpdateFunction.mockClear();
|
||||||
|
|||||||
@ -18,7 +18,7 @@ beforeEach(() => {
|
|||||||
|
|
||||||
describe('Dashboard', () => {
|
describe('Dashboard', () => {
|
||||||
let resumes = null;
|
let resumes = null;
|
||||||
const user = FirebaseStub.database().anonymousUser1;
|
const user = DatabaseConstants.user1;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
resumes = (
|
resumes = (
|
||||||
|
|||||||
Reference in New Issue
Block a user