Firebase Stub: debounce callback called by "on" function

This commit is contained in:
gianantoniopini
2021-01-11 16:18:38 +01:00
parent dce5d2c591
commit 6c1fc543ad
5 changed files with 51 additions and 31 deletions

View File

@ -1,3 +1,4 @@
import { waitFor } from '@testing-library/react';
import FirebaseStub, { import FirebaseStub, {
AuthConstants, AuthConstants,
DatabaseConstants, DatabaseConstants,
@ -60,6 +61,10 @@ describe('FirebaseStub', () => {
}); });
describe('database', () => { describe('database', () => {
beforeEach(() => {
FirebaseStub.database().initializeData();
});
it('reuses existing Database instance', () => { it('reuses existing Database instance', () => {
const database1 = FirebaseStub.database(); const database1 = FirebaseStub.database();
const database2 = FirebaseStub.database(); const database2 = FirebaseStub.database();
@ -107,8 +112,6 @@ describe('FirebaseStub', () => {
}); });
it('initializing data sets up resumes and users', async () => { it('initializing data sets up resumes and users', async () => {
FirebaseStub.database().initializeData();
const resumesRef = FirebaseStub.database().ref( const resumesRef = FirebaseStub.database().ref(
DatabaseConstants.resumesPath, DatabaseConstants.resumesPath,
); );
@ -146,8 +149,6 @@ describe('FirebaseStub', () => {
}); });
it('retrieves resume if it exists', async () => { it('retrieves resume if it exists', async () => {
FirebaseStub.database().initializeData();
const resume = ( const resume = (
await FirebaseStub.database() await FirebaseStub.database()
.ref( .ref(
@ -161,7 +162,6 @@ describe('FirebaseStub', () => {
}); });
it('retrieves null if resume does not exist', async () => { it('retrieves null if resume does not exist', async () => {
FirebaseStub.database().initializeData();
const resumeId = 'invalidResumeId'; const resumeId = 'invalidResumeId';
const resume = ( const resume = (
@ -174,8 +174,6 @@ describe('FirebaseStub', () => {
}); });
it('retrieves user if it exists', async () => { it('retrieves user if it exists', async () => {
FirebaseStub.database().initializeData();
const user = ( const user = (
await FirebaseStub.database() await FirebaseStub.database()
.ref(`${DatabaseConstants.usersPath}/${DatabaseConstants.user1.uid}`) .ref(`${DatabaseConstants.usersPath}/${DatabaseConstants.user1.uid}`)
@ -187,7 +185,6 @@ describe('FirebaseStub', () => {
}); });
it('retrieves null if user does not exist', async () => { it('retrieves null if user does not exist', async () => {
FirebaseStub.database().initializeData();
const userId = 'invalidUserId'; const userId = 'invalidUserId';
const user = ( const user = (
@ -208,6 +205,11 @@ describe('FirebaseStub', () => {
snapshotValue = snapshot.val(); snapshotValue = snapshot.val();
}); });
await waitFor(() =>
snapshotValue ? Promise.resolve(true) : Promise.reject(),
);
expect(snapshotValue).not.toBeNull();
expect(snapshotValue).toBe(true); expect(snapshotValue).toBe(true);
}); });
@ -224,6 +226,11 @@ describe('FirebaseStub', () => {
snapshotValue = snapshot.val(); snapshotValue = snapshot.val();
}); });
await waitFor(() =>
snapshotValue ? Promise.resolve(true) : Promise.reject(),
);
expect(snapshotValue).not.toBeNull();
expect(snapshotValue).toEqual(resumes); expect(snapshotValue).toEqual(resumes);
}); });
@ -238,6 +245,11 @@ describe('FirebaseStub', () => {
snapshotValue = snapshot.val(); snapshotValue = snapshot.val();
}); });
await waitFor(() =>
snapshotValue ? Promise.resolve(true) : Promise.reject(),
);
expect(snapshotValue).not.toBeNull();
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(DatabaseConstants.user1.uid), expect(resume.user).toEqual(DatabaseConstants.user1.uid),

View File

@ -1,4 +1,5 @@
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { debounce } from 'lodash';
import DatabaseConstants from '../constants/database'; import DatabaseConstants from '../constants/database';
import DataSnapshot from './dataSnapshot'; import DataSnapshot from './dataSnapshot';
@ -108,7 +109,8 @@ class Reference {
snapshot = new DataSnapshot(eventType, () => this.getData()); snapshot = new DataSnapshot(eventType, () => this.getData());
} }
callback(snapshot); const debouncedCallback = debounce(callback, 100);
debouncedCallback(snapshot);
} }
async once(eventType) { async once(eventType) {

View File

@ -20,16 +20,14 @@ import { ResumeProvider } from '../../../contexts/ResumeContext';
import { StorageProvider } from '../../../contexts/StorageContext'; import { StorageProvider } from '../../../contexts/StorageContext';
import Builder from '../builder'; import Builder from '../builder';
beforeEach(() => {
FirebaseStub.database().initializeData();
});
describe('Builder', () => { describe('Builder', () => {
let resumeId = null; let resumeId = null;
let resume = null; let resume = null;
let mockUpdateFunction = null; let mockUpdateFunction = null;
beforeEach(async () => { beforeEach(async () => {
FirebaseStub.database().initializeData();
resumeId = DatabaseConstants.demoStateResume1Id; resumeId = DatabaseConstants.demoStateResume1Id;
resume = ( resume = (
await FirebaseStub.database() await FirebaseStub.database()
@ -96,9 +94,10 @@ describe('Builder', () => {
expect(input.value).toBe(newInputValue); expect(input.value).toBe(newInputValue);
await waitFor(() => expect(mockUpdateFunction).toHaveBeenCalledTimes(1), { await waitFor(() => mockUpdateFunction.mock.calls[0][0], {
timeout: DebounceWaitTime, timeout: DebounceWaitTime,
}); });
expect(mockUpdateFunction).toHaveBeenCalledTimes(1);
const mockUpdateFunctionCallArgument = const mockUpdateFunctionCallArgument =
mockUpdateFunction.mock.calls[0][0]; mockUpdateFunction.mock.calls[0][0];
expect(mockUpdateFunctionCallArgument.id).toBe(resume.id); expect(mockUpdateFunctionCallArgument.id).toBe(resume.id);
@ -130,9 +129,10 @@ describe('Builder', () => {
screen.getByLabelText(new RegExp('data di nascita', 'i')), screen.getByLabelText(new RegExp('data di nascita', 'i')),
).toBeInTheDocument(); ).toBeInTheDocument();
await waitFor(() => expect(mockUpdateFunction).toHaveBeenCalledTimes(1), { await waitFor(() => mockUpdateFunction.mock.calls[0][0], {
timeout: DebounceWaitTime, timeout: DebounceWaitTime,
}); });
expect(mockUpdateFunction).toHaveBeenCalledTimes(1);
const mockUpdateFunctionCallArgument = const mockUpdateFunctionCallArgument =
mockUpdateFunction.mock.calls[0][0]; mockUpdateFunction.mock.calls[0][0];
expect(mockUpdateFunctionCallArgument.id).toBe(resume.id); expect(mockUpdateFunctionCallArgument.id).toBe(resume.id);

View File

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { act, render, screen } from '@testing-library/react'; import { act, render, screen, waitFor } from '@testing-library/react';
import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase'; import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase';
@ -12,15 +12,13 @@ import { ResumeProvider } from '../../../contexts/ResumeContext';
import { StorageProvider } from '../../../contexts/StorageContext'; import { StorageProvider } from '../../../contexts/StorageContext';
import Dashboard from '../dashboard'; import Dashboard from '../dashboard';
beforeEach(() => {
FirebaseStub.database().initializeData();
});
describe('Dashboard', () => { describe('Dashboard', () => {
let resumes = null; let resumes = null;
const user = DatabaseConstants.user1; const user = DatabaseConstants.user1;
beforeEach(async () => { beforeEach(async () => {
FirebaseStub.database().initializeData();
resumes = ( resumes = (
await FirebaseStub.database() await FirebaseStub.database()
.ref(DatabaseConstants.resumesPath) .ref(DatabaseConstants.resumesPath)
@ -48,11 +46,17 @@ describe('Dashboard', () => {
await act(async () => { await act(async () => {
await FirebaseStub.auth().signInAnonymously(); await FirebaseStub.auth().signInAnonymously();
}); });
await waitFor(() => screen.getByText('Create Resume'));
}); });
describe('renders', () => { describe('renders', () => {
it('document title', async () => {
expect(document.title).toEqual('Dashboard | Reactive Resume');
});
it('create resume', async () => { it('create resume', async () => {
expect(screen.getByText(new RegExp('Create Resume'))).toBeInTheDocument(); expect(screen.getByText('Create Resume')).toBeInTheDocument();
}); });
it('preview of user resumes', async () => { it('preview of user resumes', async () => {
@ -60,11 +64,11 @@ describe('Dashboard', () => {
expect(Object.values(resumes)[0].user).toEqual(user.uid); expect(Object.values(resumes)[0].user).toEqual(user.uid);
expect( expect(
screen.getByText(new RegExp(Object.values(resumes)[0].name)), screen.getByText(Object.values(resumes)[0].name),
).toBeInTheDocument(); ).toBeInTheDocument();
expect(Object.values(resumes)[1].user).toEqual(user.uid); expect(Object.values(resumes)[1].user).toEqual(user.uid);
expect( expect(
screen.getByText(new RegExp(Object.values(resumes)[1].name)), screen.getByText(Object.values(resumes)[1].name),
).toBeInTheDocument(); ).toBeInTheDocument();
}); });
}); });

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { render } from '@testing-library/react'; import { render, screen } from '@testing-library/react';
import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase'; import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase';
import '../../i18n/index'; import '../../i18n/index';
@ -10,6 +11,7 @@ describe('Castform', () => {
beforeEach(async () => { beforeEach(async () => {
FirebaseStub.database().initializeData(); FirebaseStub.database().initializeData();
const resumeId = DatabaseConstants.initialStateResumeId; const resumeId = DatabaseConstants.initialStateResumeId;
resume = ( resume = (
await FirebaseStub.database() await FirebaseStub.database()
@ -29,9 +31,9 @@ describe('Castform', () => {
const birthDateLabelMatcher = /Date of birth/i; const birthDateLabelMatcher = /Date of birth/i;
it('is not shown if not provided', () => { it('is not shown if not provided', () => {
const { queryByText } = render(<Castform data={resume} />); render(<Castform data={resume} />);
expect(queryByText(birthDateLabelMatcher)).toBeNull(); expect(screen.queryByText(birthDateLabelMatcher)).toBeNull();
}); });
it('is shown if provided', () => { it('is shown if provided', () => {
@ -39,12 +41,12 @@ describe('Castform', () => {
const birthDateFormatted = '20 January 1990'; const birthDateFormatted = '20 January 1990';
resume.profile.birthDate = birthDate; resume.profile.birthDate = birthDate;
const { getByText } = render(<Castform data={resume} />); render(<Castform data={resume} />);
expect(getByText(birthDateLabelMatcher)).toBeTruthy(); expect(screen.getByText(birthDateLabelMatcher)).toBeTruthy();
expect(getByText(birthDateLabelMatcher)).toBeInTheDocument(); expect(screen.getByText(birthDateLabelMatcher)).toBeInTheDocument();
expect(getByText(birthDateFormatted)).toBeTruthy(); expect(screen.getByText(birthDateFormatted)).toBeTruthy();
expect(getByText(birthDateFormatted)).toBeInTheDocument(); expect(screen.getByText(birthDateFormatted)).toBeInTheDocument();
}); });
}); });
}); });