Dashboard page: unit tests refactoring

This commit is contained in:
gianantoniopini
2021-01-27 10:35:58 +01:00
parent a20fe10c06
commit 5d9527e46e

View File

@ -43,10 +43,8 @@ describe('Dashboard', () => {
}; };
const waitForModalWindowToHaveBeenClosed = async () => { const waitForModalWindowToHaveBeenClosed = async () => {
await waitFor(() => await waitForElementToBeRemoved(() =>
screen.queryByRole('textbox', { name: /name/i }) screen.getByRole('textbox', { name: /name/i }),
? Promise.reject()
: Promise.resolve(),
); );
}; };
@ -54,6 +52,17 @@ describe('Dashboard', () => {
fireEvent.click(notification); fireEvent.click(notification);
}; };
const findAndDismissNotification = async () => {
const notification = await screen.findByRole('alert');
dismissNotification(notification);
};
const fnWaitForLoadingScreenToDisappear = async () => {
await waitForElementToBeRemoved(() =>
screen.getByTestId(loadingScreenTestId),
);
};
async function setup(waitForLoadingScreenToDisappear = true) { async function setup(waitForLoadingScreenToDisappear = true) {
FirebaseStub.database().initializeData(); FirebaseStub.database().initializeData();
@ -86,9 +95,7 @@ describe('Dashboard', () => {
); );
if (waitForLoadingScreenToDisappear) { if (waitForLoadingScreenToDisappear) {
await waitForElementToBeRemoved(() => await fnWaitForLoadingScreenToDisappear();
screen.getByTestId(loadingScreenTestId),
);
} }
} }
@ -205,17 +212,17 @@ describe('Dashboard', () => {
}); });
it('renders loading message', async () => { it('renders loading message', async () => {
await waitFor(() =>
expect( expect(
screen.getByRole('button', { screen.getByRole('button', {
name: /loading/i, name: /loading/i,
}), }),
).toBeInTheDocument(); ).toBeInTheDocument(),
await waitFor(() => );
expect( await waitForElementToBeRemoved(() =>
screen.queryByRole('button', { screen.getByRole('button', {
name: /loading/i, name: /loading/i,
}), }),
).toBeNull(),
); );
await waitForModalWindowToHaveBeenClosed(); await waitForModalWindowToHaveBeenClosed();
@ -224,7 +231,7 @@ describe('Dashboard', () => {
it('closes modal window', async () => { it('closes modal window', async () => {
await waitFor(() => await waitFor(() =>
expect(screen.queryByRole('textbox', { name: /name/i })).toBeNull(), expect(waitForModalWindowToHaveBeenClosed()).resolves.toBeUndefined(),
); );
await waitForResumeToBeRenderedInPreview(resumeName); await waitForResumeToBeRenderedInPreview(resumeName);
@ -289,11 +296,22 @@ describe('Dashboard', () => {
); );
}; };
const waitForResumeToDisappearFromPreview = async (resumeName) => {
await waitFor(() =>
screen.queryByText(resumeName) ? Promise.reject() : Promise.resolve(),
);
};
beforeEach(async () => { beforeEach(async () => {
await setup(); await setup();
[resumeToDelete, undeletedResume] = Object.values(userResumes); [resumeToDelete] = Object.values(userResumes).filter(
(resume) => resume.id === DatabaseConstants.demoStateResume1Id,
);
resumeToDeleteId = resumeToDelete.id; resumeToDeleteId = resumeToDelete.id;
[undeletedResume] = Object.values(userResumes).filter(
(resume) => resume.id === DatabaseConstants.initialStateResumeId,
);
mockDatabaseRemoveFunction = jest.spyOn( mockDatabaseRemoveFunction = jest.spyOn(
FirebaseStub.database().ref( FirebaseStub.database().ref(
@ -319,10 +337,14 @@ describe('Dashboard', () => {
}); });
it('removes it from database and preview', async () => { it('removes it from database and preview', async () => {
await findAndDismissNotification();
await expectDatabaseRemoveToHaveCompleted(); await expectDatabaseRemoveToHaveCompleted();
await waitFor(() => await waitFor(() =>
expect(screen.queryByText(resumeToDelete.name)).toBeNull(), expect(
waitForResumeToDisappearFromPreview(resumeToDelete.name),
).resolves.toBeUndefined(),
); );
await expectResumeToBeRenderedInPreview(undeletedResume.name); await expectResumeToBeRenderedInPreview(undeletedResume.name);
}); });
@ -338,13 +360,16 @@ describe('Dashboard', () => {
dismissNotification(notification); dismissNotification(notification);
await waitForDatabaseRemoveToHaveCompleted(); await waitForDatabaseRemoveToHaveCompleted();
await waitForResumeToDisappearFromPreview(resumeToDelete.name);
}); });
it('closes menu', async () => { it('closes menu', async () => {
const menuItems = screen.queryAllByRole('menuitem'); const menuItems = screen.queryAllByRole('menuitem');
expect(menuItems).toHaveLength(0); expect(menuItems).toHaveLength(0);
await findAndDismissNotification();
await waitForDatabaseRemoveToHaveCompleted(); await waitForDatabaseRemoveToHaveCompleted();
await waitForResumeToDisappearFromPreview(resumeToDelete.name);
}); });
}); });
@ -375,7 +400,9 @@ describe('Dashboard', () => {
beforeEach(async () => { beforeEach(async () => {
await setup(); await setup();
[resumeToRename] = Object.values(userResumes); [resumeToRename] = Object.values(userResumes).filter(
(resume) => resume.id === DatabaseConstants.demoStateResume1Id,
);
resumeToRenameId = resumeToRename.id; resumeToRenameId = resumeToRename.id;
/* /*
@ -444,10 +471,7 @@ describe('Dashboard', () => {
it('renders loading screen', async () => { it('renders loading screen', async () => {
expect(screen.getByTestId(loadingScreenTestId)).toBeInTheDocument(); expect(screen.getByTestId(loadingScreenTestId)).toBeInTheDocument();
await fnWaitForLoadingScreenToDisappear();
await waitForElementToBeRemoved(() =>
screen.getByTestId(loadingScreenTestId),
);
await waitForResumeToBeRenderedInPreview( await waitForResumeToBeRenderedInPreview(
Object.values(userResumes)[0].name, Object.values(userResumes)[0].name,