Builder.test: modified draft version of test for data update

This commit is contained in:
gianantoniopini
2020-12-21 15:16:35 +01:00
parent 7450b1c683
commit 9dc1f1b89b
2 changed files with 18 additions and 9 deletions

View File

@ -8,6 +8,7 @@ const __testUser = {
}; };
let __onAuthStateChangedObservers = []; let __onAuthStateChangedObservers = [];
let __resumesDictionary = {}; let __resumesDictionary = {};
let __databaseRefUpdateCalls = [];
const auth = () => { const auth = () => {
const __init = () => { const __init = () => {
@ -44,6 +45,7 @@ const database = () => {
const __init = () => { const __init = () => {
__resumesDictionary = {}; __resumesDictionary = {};
__databaseRefUpdateCalls = [];
const demoResume = __readFile('../src/data/demoState.json'); const demoResume = __readFile('../src/data/demoState.json');
__resumesDictionary[__demoResumeId] = demoResume; __resumesDictionary[__demoResumeId] = demoResume;
@ -134,6 +136,8 @@ const database = () => {
} }
} }
__databaseRefUpdateCalls.push(value);
return Promise.resolve(); return Promise.resolve();
}; };
@ -141,6 +145,7 @@ const database = () => {
once, once,
set, set,
update, update,
__updateCalls: __databaseRefUpdateCalls,
}; };
}; };

View File

@ -74,6 +74,7 @@ describe('builder', () => {
resume.profile.address.line1 = newInputValue; resume.profile.address.line1 = newInputValue;
const ref = firebaseMock.database().ref(`resumes/${resumeId}`); const ref = firebaseMock.database().ref(`resumes/${resumeId}`);
const firebaseMockUpdateFunction = jest.spyOn(ref, 'update'); const firebaseMockUpdateFunction = jest.spyOn(ref, 'update');
const now = Date.now();
await ref.update({ await ref.update({
...resume, ...resume,
@ -87,26 +88,29 @@ describe('builder', () => {
firebaseMockUpdateFunction.mock.calls[0][0]; firebaseMockUpdateFunction.mock.calls[0][0];
expect(firebaseMockUpdateFunctionCallArgument.id).toBe(resume.id); expect(firebaseMockUpdateFunctionCallArgument.id).toBe(resume.id);
expect(firebaseMockUpdateFunctionCallArgument.profile.address.line1).toBe(newInputValue); expect(firebaseMockUpdateFunctionCallArgument.profile.address.line1).toBe(newInputValue);
expect(firebaseMockUpdateFunctionCallArgument.updatedAt).toBeGreaterThanOrEqual(now);
*/ */
const input = screen.getByLabelText(new RegExp('address line 1', 'i')); const input = screen.getByLabelText(new RegExp('address line 1', 'i'));
const newInputValue = 'test street 123'; const newInputValue = 'test street 123';
const ref = firebaseMock.database().ref(`resumes/${resumeId}`); const now = Date.now();
const firebaseMockUpdateFunction = jest.spyOn(ref, 'update');
fireEvent.change(input, { target: { value: newInputValue } }); fireEvent.change(input, { target: { value: newInputValue } });
expect(input.value).toBe(newInputValue); expect(input.value).toBe(newInputValue);
await waitFor(() => const databaseRef = firebaseMock.database().ref(`resumes/${resume.id}`);
expect(firebaseMockUpdateFunction).toHaveBeenCalledTimes(1), await waitFor(() => expect(databaseRef.__updateCalls.length).toBe(1), {
); timeout: 4000,
const firebaseMockUpdateFunctionCallArgument = });
firebaseMockUpdateFunction.mock.calls[0][0]; const databaseRefUpdateCallArgument = databaseRef.__updateCalls[0];
expect(firebaseMockUpdateFunctionCallArgument.id).toBe(resume.id); expect(databaseRefUpdateCallArgument.id).toBe(resume.id);
expect(firebaseMockUpdateFunctionCallArgument.profile.address.line1).toBe( expect(databaseRefUpdateCallArgument.profile.address.line1).toBe(
newInputValue, newInputValue,
); );
expect(databaseRefUpdateCallArgument.updatedAt).toBeGreaterThanOrEqual(
now,
);
}); });
}); });
}); });