mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-15 01:01:43 +10:00
Firebase stub: new implementation in place
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import FirebaseStub from '../gatsby-plugin-firebase_2';
|
import FirebaseStub from '../gatsby-plugin-firebase';
|
||||||
|
|
||||||
describe('database', () => {
|
describe('database', () => {
|
||||||
it('reuses existing Database instance', () => {
|
it('reuses existing Database instance', () => {
|
||||||
@ -1,118 +1,236 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
const __testUser = {
|
class Auth {
|
||||||
email: 'test.user@noemail.com',
|
static #instance = undefined;
|
||||||
name: 'Test User',
|
#uuid = '';
|
||||||
uid: 'testuser123',
|
#onAuthStateChangedObservers = [];
|
||||||
};
|
|
||||||
let __onAuthStateChangedObservers = [];
|
|
||||||
let __resumesDictionary = {};
|
|
||||||
let __databaseRefUpdateCalls = [];
|
|
||||||
|
|
||||||
const auth = () => {
|
constructor() {
|
||||||
const __init = () => {
|
if (Auth.#instance) {
|
||||||
__onAuthStateChangedObservers = [];
|
return Auth.#instance;
|
||||||
};
|
}
|
||||||
|
|
||||||
const onAuthStateChanged = (observer) => {
|
Auth.#instance = this;
|
||||||
__onAuthStateChangedObservers.push(observer);
|
|
||||||
|
this.#uuid = uuidv4();
|
||||||
|
}
|
||||||
|
|
||||||
|
get uuid() {
|
||||||
|
return this.#uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
get onAuthStateChangedObservers() {
|
||||||
|
return this.#onAuthStateChangedObservers;
|
||||||
|
}
|
||||||
|
|
||||||
|
clearOnAuthStateChangedObservers() {
|
||||||
|
this.#onAuthStateChangedObservers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
onAuthStateChanged(observer) {
|
||||||
|
this.#onAuthStateChangedObservers.push(observer);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
__onAuthStateChangedObservers = __onAuthStateChangedObservers.filter(
|
this.#onAuthStateChangedObservers = this.#onAuthStateChangedObservers.filter(
|
||||||
(observer) => observer !== observer,
|
(observer) => observer !== observer,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async signInAnonymously() {
|
||||||
|
this.#onAuthStateChangedObservers.forEach((observer) =>
|
||||||
|
observer(Database.testUser),
|
||||||
|
);
|
||||||
|
|
||||||
|
return Promise.resolve(Database.testUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = {};
|
||||||
|
|
||||||
const signInAnonymously = async () => {
|
constructor() {
|
||||||
__onAuthStateChangedObservers.forEach((observer) => observer(__testUser));
|
if (Database.#instance) {
|
||||||
|
return Database.#instance;
|
||||||
|
}
|
||||||
|
|
||||||
var result = await Promise.resolve(__testUser);
|
Database.#instance = this;
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
this.#uuid = uuidv4();
|
||||||
__init,
|
}
|
||||||
onAuthStateChanged,
|
|
||||||
signInAnonymously,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const database = () => {
|
get testUser() {
|
||||||
const __demoResumeId = 'demore';
|
return Database.testUser;
|
||||||
const __emptyResumeId = 'mtre01';
|
}
|
||||||
|
|
||||||
const __init = () => {
|
get demoResumeId() {
|
||||||
__resumesDictionary = {};
|
return 'demore';
|
||||||
__databaseRefUpdateCalls = [];
|
}
|
||||||
|
get emptyResumeId() {
|
||||||
|
return 'mtre01';
|
||||||
|
}
|
||||||
|
|
||||||
const demoResume = __readFile('../src/data/demoState.json');
|
get uuid() {
|
||||||
__resumesDictionary[__demoResumeId] = demoResume;
|
return this.#uuid;
|
||||||
const emptyResume = __readFile('../src/data/initialState.json');
|
}
|
||||||
__resumesDictionary[__emptyResumeId] = emptyResume;
|
|
||||||
|
|
||||||
for (var key in __resumesDictionary) {
|
static readFile(fileRelativePath) {
|
||||||
const resume = __resumesDictionary[key];
|
const fileAbsolutePath = path.resolve(__dirname, fileRelativePath);
|
||||||
|
const fileBuffer = fs.readFileSync(fileAbsolutePath);
|
||||||
|
const fileData = JSON.parse(fileBuffer);
|
||||||
|
return fileData;
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeData() {
|
||||||
|
const resumes = {};
|
||||||
|
const demoResume = Database.readFile('../src/data/demoState.json');
|
||||||
|
resumes[this.demoResumeId] = demoResume;
|
||||||
|
const emptyResume = Database.readFile('../src/data/initialState.json');
|
||||||
|
resumes[this.emptyResumeId] = emptyResume;
|
||||||
|
|
||||||
|
for (var key in resumes) {
|
||||||
|
const resume = resumes[key];
|
||||||
|
|
||||||
resume.id = key;
|
resume.id = key;
|
||||||
resume.name = `Test Resume ${key}`;
|
resume.name = `Test Resume ${key}`;
|
||||||
resume.user = __testUser.uid;
|
resume.user = this.testUser.uid;
|
||||||
|
|
||||||
let date = new Date('December 15, 2020 11:20:25');
|
let date = new Date('December 15, 2020 11:20:25');
|
||||||
resume.updatedAt = date.valueOf();
|
resume.updatedAt = date.valueOf();
|
||||||
date.setMonth(date.getMonth() - 2);
|
date.setMonth(date.getMonth() - 2);
|
||||||
resume.createdAt = date.valueOf();
|
resume.createdAt = date.valueOf();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
const __readFile = (fileRelativePath) => {
|
this.#data[Database.resumesPath] = resumes;
|
||||||
const fileAbsolutePath = path.resolve(__dirname, fileRelativePath);
|
|
||||||
const fileBuffer = fs.readFileSync(fileAbsolutePath);
|
|
||||||
const fileData = JSON.parse(fileBuffer);
|
|
||||||
return fileData;
|
|
||||||
};
|
|
||||||
|
|
||||||
const ref = (path) => {
|
const users = {};
|
||||||
if (!path) {
|
users[this.testUser.uid] = this.testUser;
|
||||||
throw new Error('Not implemented.');
|
this.#data[Database.usersPath] = users;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resumesPath = path.startsWith('resumes/');
|
ref(path) {
|
||||||
const usersPath = path.startsWith('users/');
|
const newRef = new Reference(path, () => this.#data);
|
||||||
if (!resumesPath && !usersPath) {
|
const existingRef = this.#references[newRef.path];
|
||||||
throw new Error('Unknown Reference path.');
|
if (existingRef) {
|
||||||
|
return existingRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
const databaseLocationId = path.substring(path.indexOf('/') + 1);
|
this.#references[newRef.path] = newRef;
|
||||||
|
return newRef;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Reference {
|
||||||
|
#rootPath = '.';
|
||||||
|
#path = '';
|
||||||
|
#uuid = '';
|
||||||
|
#dataSnapshots = {};
|
||||||
|
#getDatabaseData = () => null;
|
||||||
|
|
||||||
|
constructor(path, getDatabaseData) {
|
||||||
|
if (typeof path === 'undefined' || path === null) {
|
||||||
|
this.#path = this.#rootPath;
|
||||||
|
} else if (typeof path !== 'string') {
|
||||||
|
throw new Error('path should be a string.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!getDatabaseData) {
|
||||||
|
throw new Error('getDatabaseData must be provided.');
|
||||||
|
} else if (typeof getDatabaseData !== 'function') {
|
||||||
|
throw new Error('getDatabaseData should be a function.');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#path = path;
|
||||||
|
this.#uuid = uuidv4();
|
||||||
|
this.#getDatabaseData = getDatabaseData;
|
||||||
|
}
|
||||||
|
|
||||||
|
get path() {
|
||||||
|
return this.#path;
|
||||||
|
}
|
||||||
|
|
||||||
|
get uuid() {
|
||||||
|
return this.#uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
getData() {
|
||||||
|
const databaseData = this.#getDatabaseData();
|
||||||
|
|
||||||
|
if (!databaseData) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.#path === this.#rootPath) {
|
||||||
|
return databaseData;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.#path === Database.resumesPath ||
|
||||||
|
this.#path === Database.usersPath
|
||||||
|
) {
|
||||||
|
return this.#path in databaseData ? databaseData[this.#path] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.#path.startsWith(`${Database.resumesPath}/`) ||
|
||||||
|
this.#path.startsWith(`${Database.usersPath}/`)
|
||||||
|
) {
|
||||||
|
const databaseLocationId = this.#path.substring(
|
||||||
|
this.#path.indexOf('/') + 1,
|
||||||
|
);
|
||||||
if (!databaseLocationId) {
|
if (!databaseLocationId) {
|
||||||
throw new Error('Unknown database location id.');
|
throw new Error('Unknown database location id.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const once = async (eventType) => {
|
const pathWithoutId = this.#path.substring(0, this.#path.indexOf('/'));
|
||||||
if (!eventType) {
|
if (pathWithoutId in databaseData) {
|
||||||
throw new Error('Event type must be provided.');
|
return databaseLocationId in databaseData[pathWithoutId]
|
||||||
}
|
? databaseData[pathWithoutId][databaseLocationId]
|
||||||
|
|
||||||
if (eventType !== 'value') {
|
|
||||||
throw new Error('Unknown event type.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const val = () => {
|
|
||||||
if (resumesPath) {
|
|
||||||
return __resumesDictionary[databaseLocationId]
|
|
||||||
? __resumesDictionary[databaseLocationId]
|
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usersPath) {
|
|
||||||
return __testUser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
}
|
||||||
|
|
||||||
return Promise.resolve({ val });
|
async once(eventType) {
|
||||||
|
const newDataSnapshot = new DataSnapshot(eventType, this);
|
||||||
|
const existingDataSnapshot = this.#dataSnapshots[newDataSnapshot.eventType];
|
||||||
|
if (existingDataSnapshot) {
|
||||||
|
return Promise.resolve(existingDataSnapshot);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#dataSnapshots[newDataSnapshot.eventType] = newDataSnapshot;
|
||||||
|
return Promise.resolve(newDataSnapshot);
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(value) {
|
||||||
|
return Promise.resolve(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
const update = async (value) => {
|
||||||
|
if (resumesPath) {
|
||||||
|
if (value === null) {
|
||||||
|
delete __resumesDictionary[databaseLocationId];
|
||||||
|
} else {
|
||||||
|
__resumesDictionary[databaseLocationId] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const set = (value) => {
|
const set = (value) => {
|
||||||
@ -126,46 +244,59 @@ const database = () => {
|
|||||||
|
|
||||||
return Promise.resolve(true);
|
return Promise.resolve(true);
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
const update = async (value) => {
|
class DataSnapshot {
|
||||||
if (resumesPath) {
|
#eventType = '';
|
||||||
if (value === null) {
|
#reference = null;
|
||||||
delete __resumesDictionary[databaseLocationId];
|
|
||||||
} else {
|
constructor(eventType, reference) {
|
||||||
__resumesDictionary[databaseLocationId] = value;
|
if (!eventType) {
|
||||||
}
|
throw new Error('eventType must be provided.');
|
||||||
|
} else if (typeof eventType !== 'string') {
|
||||||
|
throw new Error('eventType should be a string.');
|
||||||
}
|
}
|
||||||
|
|
||||||
__databaseRefUpdateCalls.push(value);
|
this.#eventType = eventType;
|
||||||
|
|
||||||
return Promise.resolve(true);
|
if (!reference) {
|
||||||
};
|
throw new Error('reference must be provided.');
|
||||||
|
} else if (!(reference instanceof Reference)) {
|
||||||
|
throw new Error('reference must be an instance of the Reference class.');
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
this.#reference = reference;
|
||||||
once,
|
}
|
||||||
set,
|
|
||||||
update,
|
|
||||||
__updateCalls: __databaseRefUpdateCalls,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
get eventType() {
|
||||||
__demoResumeId,
|
return this.#eventType;
|
||||||
__emptyResumeId,
|
}
|
||||||
__init,
|
|
||||||
ref,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
database.ServerValue = {};
|
val() {
|
||||||
Object.defineProperty(database.ServerValue, 'TIMESTAMP', {
|
if (this.eventType === 'value') {
|
||||||
|
return this.#reference.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FirebaseStub {
|
||||||
|
static auth() {
|
||||||
|
return new Auth();
|
||||||
|
}
|
||||||
|
|
||||||
|
static database() {
|
||||||
|
return new Database();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FirebaseStub.database.ServerValue = {};
|
||||||
|
Object.defineProperty(FirebaseStub.database.ServerValue, 'TIMESTAMP', {
|
||||||
get() {
|
get() {
|
||||||
return new Date().getTime();
|
return new Date().getTime();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default class firebase {
|
export default FirebaseStub;
|
||||||
static auth = auth;
|
|
||||||
|
|
||||||
static database = database;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,302 +0,0 @@
|
|||||||
import path from 'path';
|
|
||||||
import fs from 'fs';
|
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
|
||||||
|
|
||||||
class Auth {
|
|
||||||
static #instance = undefined;
|
|
||||||
#uuid = '';
|
|
||||||
#onAuthStateChangedObservers = [];
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
if (Auth.#instance) {
|
|
||||||
return Auth.#instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
Auth.#instance = this;
|
|
||||||
|
|
||||||
this.#uuid = uuidv4();
|
|
||||||
}
|
|
||||||
|
|
||||||
get uuid() {
|
|
||||||
return this.#uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
get onAuthStateChangedObservers() {
|
|
||||||
return this.#onAuthStateChangedObservers;
|
|
||||||
}
|
|
||||||
|
|
||||||
clearOnAuthStateChangedObservers() {
|
|
||||||
this.#onAuthStateChangedObservers = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
onAuthStateChanged(observer) {
|
|
||||||
this.#onAuthStateChangedObservers.push(observer);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
this.#onAuthStateChangedObservers = this.#onAuthStateChangedObservers.filter(
|
|
||||||
(observer) => observer !== observer,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async signInAnonymously() {
|
|
||||||
this.#onAuthStateChangedObservers.forEach((observer) =>
|
|
||||||
observer(Database.testUser),
|
|
||||||
);
|
|
||||||
|
|
||||||
return Promise.resolve(Database.testUser);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = {};
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
if (Database.#instance) {
|
|
||||||
return Database.#instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
Database.#instance = this;
|
|
||||||
|
|
||||||
this.#uuid = uuidv4();
|
|
||||||
}
|
|
||||||
|
|
||||||
get testUser() {
|
|
||||||
return Database.testUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
get demoResumeId() {
|
|
||||||
return 'demore';
|
|
||||||
}
|
|
||||||
get emptyResumeId() {
|
|
||||||
return 'mtre01';
|
|
||||||
}
|
|
||||||
|
|
||||||
get uuid() {
|
|
||||||
return this.#uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
static readFile(fileRelativePath) {
|
|
||||||
const fileAbsolutePath = path.resolve(__dirname, fileRelativePath);
|
|
||||||
const fileBuffer = fs.readFileSync(fileAbsolutePath);
|
|
||||||
const fileData = JSON.parse(fileBuffer);
|
|
||||||
return fileData;
|
|
||||||
}
|
|
||||||
|
|
||||||
initializeData() {
|
|
||||||
const resumes = {};
|
|
||||||
const demoResume = Database.readFile('../src/data/demoState.json');
|
|
||||||
resumes[this.demoResumeId] = demoResume;
|
|
||||||
const emptyResume = Database.readFile('../src/data/initialState.json');
|
|
||||||
resumes[this.emptyResumeId] = emptyResume;
|
|
||||||
|
|
||||||
for (var key in resumes) {
|
|
||||||
const resume = resumes[key];
|
|
||||||
|
|
||||||
resume.id = key;
|
|
||||||
resume.name = `Test Resume ${key}`;
|
|
||||||
resume.user = this.testUser.uid;
|
|
||||||
|
|
||||||
let date = new Date('December 15, 2020 11:20:25');
|
|
||||||
resume.updatedAt = date.valueOf();
|
|
||||||
date.setMonth(date.getMonth() - 2);
|
|
||||||
resume.createdAt = date.valueOf();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#data[Database.resumesPath] = resumes;
|
|
||||||
|
|
||||||
const users = {};
|
|
||||||
users[this.testUser.uid] = this.testUser;
|
|
||||||
this.#data[Database.usersPath] = users;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref(path) {
|
|
||||||
const newRef = new Reference(path, () => this.#data);
|
|
||||||
const existingRef = this.#references[newRef.path];
|
|
||||||
if (existingRef) {
|
|
||||||
return existingRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#references[newRef.path] = newRef;
|
|
||||||
return newRef;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Reference {
|
|
||||||
#rootPath = '.';
|
|
||||||
#path = '';
|
|
||||||
#uuid = '';
|
|
||||||
#dataSnapshots = {};
|
|
||||||
#getDatabaseData = () => null;
|
|
||||||
|
|
||||||
constructor(path, getDatabaseData) {
|
|
||||||
if (typeof path === 'undefined' || path === null) {
|
|
||||||
this.#path = this.#rootPath;
|
|
||||||
} else if (typeof path !== 'string') {
|
|
||||||
throw new Error('path should be a string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!getDatabaseData) {
|
|
||||||
throw new Error('getDatabaseData must be provided.');
|
|
||||||
} else if (typeof getDatabaseData !== 'function') {
|
|
||||||
throw new Error('getDatabaseData should be a function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#path = path;
|
|
||||||
this.#uuid = uuidv4();
|
|
||||||
this.#getDatabaseData = getDatabaseData;
|
|
||||||
}
|
|
||||||
|
|
||||||
get path() {
|
|
||||||
return this.#path;
|
|
||||||
}
|
|
||||||
|
|
||||||
get uuid() {
|
|
||||||
return this.#uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
getData() {
|
|
||||||
const databaseData = this.#getDatabaseData();
|
|
||||||
|
|
||||||
if (!databaseData) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.#path === this.#rootPath) {
|
|
||||||
return databaseData;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
this.#path === Database.resumesPath ||
|
|
||||||
this.#path === Database.usersPath
|
|
||||||
) {
|
|
||||||
return this.#path in databaseData ? databaseData[this.#path] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
this.#path.startsWith(`${Database.resumesPath}/`) ||
|
|
||||||
this.#path.startsWith(`${Database.usersPath}/`)
|
|
||||||
) {
|
|
||||||
const databaseLocationId = this.#path.substring(
|
|
||||||
this.#path.indexOf('/') + 1,
|
|
||||||
);
|
|
||||||
if (!databaseLocationId) {
|
|
||||||
throw new Error('Unknown database location id.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const pathWithoutId = this.#path.substring(0, this.#path.indexOf('/'));
|
|
||||||
if (pathWithoutId in databaseData) {
|
|
||||||
return databaseLocationId in databaseData[pathWithoutId]
|
|
||||||
? databaseData[pathWithoutId][databaseLocationId]
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async once(eventType) {
|
|
||||||
const newDataSnapshot = new DataSnapshot(eventType, this);
|
|
||||||
const existingDataSnapshot = this.#dataSnapshots[newDataSnapshot.eventType];
|
|
||||||
if (existingDataSnapshot) {
|
|
||||||
return Promise.resolve(existingDataSnapshot);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#dataSnapshots[newDataSnapshot.eventType] = newDataSnapshot;
|
|
||||||
return Promise.resolve(newDataSnapshot);
|
|
||||||
}
|
|
||||||
|
|
||||||
async update(value) {
|
|
||||||
return Promise.resolve(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
const update = async (value) => {
|
|
||||||
if (resumesPath) {
|
|
||||||
if (value === null) {
|
|
||||||
delete __resumesDictionary[databaseLocationId];
|
|
||||||
} else {
|
|
||||||
__resumesDictionary[databaseLocationId] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
const set = (value) => {
|
|
||||||
if (resumesPath) {
|
|
||||||
if (value === null) {
|
|
||||||
delete __resumesDictionary[databaseLocationId];
|
|
||||||
} else {
|
|
||||||
__resumesDictionary[databaseLocationId] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve(true);
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
class DataSnapshot {
|
|
||||||
#eventType = '';
|
|
||||||
#reference = null;
|
|
||||||
|
|
||||||
constructor(eventType, reference) {
|
|
||||||
if (!eventType) {
|
|
||||||
throw new Error('eventType must be provided.');
|
|
||||||
} else if (typeof eventType !== 'string') {
|
|
||||||
throw new Error('eventType should be a string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#eventType = eventType;
|
|
||||||
|
|
||||||
if (!reference) {
|
|
||||||
throw new Error('reference must be provided.');
|
|
||||||
} else if (!(reference instanceof Reference)) {
|
|
||||||
throw new Error('reference must be an instance of the Reference class.');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#reference = reference;
|
|
||||||
}
|
|
||||||
|
|
||||||
get eventType() {
|
|
||||||
return this.#eventType;
|
|
||||||
}
|
|
||||||
|
|
||||||
val() {
|
|
||||||
if (this.eventType === 'value') {
|
|
||||||
return this.#reference.getData();
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FirebaseStub {
|
|
||||||
static auth() {
|
|
||||||
return new Auth();
|
|
||||||
}
|
|
||||||
|
|
||||||
static database() {
|
|
||||||
return new Database();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FirebaseStub.database.ServerValue = {};
|
|
||||||
Object.defineProperty(FirebaseStub.database.ServerValue, 'TIMESTAMP', {
|
|
||||||
get() {
|
|
||||||
return new Date().getTime();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export default FirebaseStub;
|
|
||||||
@ -1,9 +1,9 @@
|
|||||||
import { cleanup, waitFor } from '@testing-library/react';
|
import { cleanup, waitFor } from '@testing-library/react';
|
||||||
|
|
||||||
import firebaseMock from 'gatsby-plugin-firebase';
|
import FirebaseStub from 'gatsby-plugin-firebase';
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
firebaseMock.database().__init();
|
FirebaseStub.database().initializeData();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(cleanup);
|
afterEach(cleanup);
|
||||||
@ -13,9 +13,9 @@ describe('builder', () => {
|
|||||||
let resume = null;
|
let resume = null;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
resumeId = firebaseMock.database().__demoResumeId;
|
resumeId = FirebaseStub.database().demoResumeId;
|
||||||
resume = (
|
resume = (
|
||||||
await firebaseMock.database().ref(`resumes/${resumeId}`).once('value')
|
await FirebaseStub.database().ref(`resumes/${resumeId}`).once('value')
|
||||||
).val();
|
).val();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -23,17 +23,19 @@ describe('builder', () => {
|
|||||||
const now = new Date().getTime();
|
const now = new Date().getTime();
|
||||||
const newInputValue = 'test street 123';
|
const newInputValue = 'test street 123';
|
||||||
resume.profile.address.line1 = newInputValue;
|
resume.profile.address.line1 = newInputValue;
|
||||||
const ref = firebaseMock.database().ref(`resumes/${resumeId}`);
|
const functionSpy = jest.spyOn(
|
||||||
const functionSpy = jest.spyOn(ref, 'update');
|
FirebaseStub.database().ref(`resumes/${resumeId}`),
|
||||||
|
'update',
|
||||||
|
);
|
||||||
|
|
||||||
await ref.update({
|
await FirebaseStub.database()
|
||||||
|
.ref(`resumes/${resumeId}`)
|
||||||
|
.update({
|
||||||
...resume,
|
...resume,
|
||||||
updatedAt: firebaseMock.database.ServerValue.TIMESTAMP,
|
updatedAt: FirebaseStub.database.ServerValue.TIMESTAMP,
|
||||||
});
|
});
|
||||||
|
|
||||||
await waitFor(() => expect(functionSpy).toHaveBeenCalledTimes(1), {
|
await waitFor(() => expect(functionSpy).toHaveBeenCalledTimes(1));
|
||||||
timeout: 4000,
|
|
||||||
});
|
|
||||||
const functionCallArgument = functionSpy.mock.calls[0][0];
|
const functionCallArgument = functionSpy.mock.calls[0][0];
|
||||||
expect(functionCallArgument.id).toBe(resume.id);
|
expect(functionCallArgument.id).toBe(resume.id);
|
||||||
expect(functionCallArgument.profile.address.line1).toBe(newInputValue);
|
expect(functionCallArgument.profile.address.line1).toBe(newInputValue);
|
||||||
|
|||||||
Reference in New Issue
Block a user