Firebase mock: fix for property ServerValue.TIMESTAMP

This commit is contained in:
gianantoniopini
2020-12-22 17:08:37 +01:00
parent b9014eb6cb
commit a9c6ed4cdb
3 changed files with 49 additions and 4 deletions

View File

@ -157,9 +157,12 @@ const database = () => {
}; };
}; };
database.ServerValue = { database.ServerValue = {};
TIMESTAMP: Date.now(), Object.defineProperty(database.ServerValue, 'TIMESTAMP', {
}; get() {
return new Date().getTime();
},
});
export default class firebase { export default class firebase {
static auth = auth; static auth = auth;

View File

@ -78,7 +78,7 @@ describe('builder', () => {
await ref.update({ await ref.update({
...resume, ...resume,
updatedAt: firebaseMock.database().ServerValue.TIMESTAMP, updatedAt: firebaseMock.database.ServerValue.TIMESTAMP,
}); });
await waitFor(() => await waitFor(() =>

View File

@ -0,0 +1,42 @@
import { cleanup, waitFor } from '@testing-library/react';
import firebaseMock from 'gatsby-plugin-firebase';
beforeEach(() => {
firebaseMock.database().__init();
});
afterEach(cleanup);
describe('builder', () => {
let resumeId = null;
let resume = null;
beforeEach(async () => {
resumeId = firebaseMock.database().__demoResumeId;
resume = (
await firebaseMock.database().ref(`resumes/${resumeId}`).once('value')
).val();
});
it('test 1', async () => {
const now = new Date().getTime();
const newInputValue = 'test street 123';
resume.profile.address.line1 = newInputValue;
const ref = firebaseMock.database().ref(`resumes/${resumeId}`);
const functionSpy = jest.spyOn(ref, 'update');
await ref.update({
...resume,
updatedAt: firebaseMock.database.ServerValue.TIMESTAMP,
});
await waitFor(() => expect(functionSpy).toHaveBeenCalledTimes(1), {
timeout: 4000,
});
const functionCallArgument = functionSpy.mock.calls[0][0];
expect(functionCallArgument.id).toBe(resume.id);
expect(functionCallArgument.profile.address.line1).toBe(newInputValue);
expect(functionCallArgument.updatedAt).toBeGreaterThanOrEqual(now);
});
});