From 45edbded874b9ebb341d799858842a1f4eda081a Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Sun, 20 Dec 2020 17:36:24 +0100 Subject: [PATCH] Builder.test: modified draft version of test for data update --- __mocks__/gatsby-plugin-firebase.js | 2 -- src/pages/app/__tests__/builder.test.js | 46 +++++++++++++++++-------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/__mocks__/gatsby-plugin-firebase.js b/__mocks__/gatsby-plugin-firebase.js index fc423c6f..76cdcd73 100644 --- a/__mocks__/gatsby-plugin-firebase.js +++ b/__mocks__/gatsby-plugin-firebase.js @@ -126,8 +126,6 @@ const database = () => { }; const update = async (value) => { - console.log('update'); - console.log(value); if (resumesPath) { if (value === null) { delete __resumesDictionary[databaseLocationId]; diff --git a/src/pages/app/__tests__/builder.test.js b/src/pages/app/__tests__/builder.test.js index 56fea655..449e3512 100644 --- a/src/pages/app/__tests__/builder.test.js +++ b/src/pages/app/__tests__/builder.test.js @@ -69,28 +69,44 @@ describe('builder', () => { describe('updates data', () => { it('when input value is changed', async () => { + /* + const newInputValue = 'test street 123'; + resume.profile.address.line1 = newInputValue; + const ref = firebaseMock.database().ref(`resumes/${resumeId}`); + const firebaseMockUpdateFunction = jest.spyOn(ref, 'update'); + + await ref.update({ + ...resume, + updatedAt: firebaseMock.database().ServerValue.TIMESTAMP, + }); + + await waitFor(() => + expect(firebaseMockUpdateFunction).toHaveBeenCalledTimes(1), + ); + const firebaseMockUpdateFunctionCallArgument = + firebaseMockUpdateFunction.mock.calls[0][0]; + expect(firebaseMockUpdateFunctionCallArgument.id).toBe(resume.id); + expect(firebaseMockUpdateFunctionCallArgument.profile.address.line1).toBe(newInputValue); + */ + const input = screen.getByLabelText(new RegExp('address line 1', 'i')); const newInputValue = 'test street 123'; - const spy = jest.spyOn( - firebaseMock.database().ref(`resumes/${resumeId}`), - 'update', - ); + const ref = firebaseMock.database().ref(`resumes/${resumeId}`); + const firebaseMockUpdateFunction = jest.spyOn(ref, 'update'); fireEvent.change(input, { target: { value: newInputValue } }); expect(input.value).toBe(newInputValue); - await waitFor(() => expect(spy).toHaveBeenCalledTimes(1), { - timeout: 4000, - }); - //const promise = spy.mock.results[0].value; - //await spy(); - //expect(spy).toHaveBeenCalledTimes(1); - - resume = ( - await firebaseMock.database().ref(`resumes/${resumeId}`).once('value') - ).val(); - expect(resume.profile.address.line1).toBe(newInputValue); + await waitFor(() => + expect(firebaseMockUpdateFunction).toHaveBeenCalledTimes(1), + ); + const firebaseMockUpdateFunctionCallArgument = + firebaseMockUpdateFunction.mock.calls[0][0]; + expect(firebaseMockUpdateFunctionCallArgument.id).toBe(resume.id); + expect(firebaseMockUpdateFunctionCallArgument.profile.address.line1).toBe( + newInputValue, + ); }); }); });