From ce05e7e89898e420dd9c44ad0ec5dab6e8c2dbfd Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Wed, 9 Sep 2020 11:46:42 +0200 Subject: [PATCH] Fix tests --- .../AuditList/AuditListTable.test.tsx | 5 +--- .../src/components/AuditList/index.test.tsx | 16 +++++-------- .../src/components/AuditView/index.test.tsx | 24 ++++++++++++------- .../src/components/CreateAudit/index.test.tsx | 6 ++--- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx b/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx index 039b397083..e67f939ba1 100644 --- a/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx +++ b/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx @@ -58,10 +58,7 @@ describe('AuditListTable', () => { if (!website) throw new Error('https://anchor.fm must be present in fixture'); expect(link).toBeInTheDocument(); - expect(link).toHaveAttribute( - 'href', - `/lighthouse/audit/${website.lastAudit.id}`, - ); + expect(link).toHaveAttribute('href', `/audit/${website.lastAudit.id}`); }); it('renders the dates that are available for a given row', () => { diff --git a/plugins/lighthouse/src/components/AuditList/index.test.tsx b/plugins/lighthouse/src/components/AuditList/index.test.tsx index 076bb5819a..34fd760801 100644 --- a/plugins/lighthouse/src/components/AuditList/index.test.tsx +++ b/plugins/lighthouse/src/components/AuditList/index.test.tsx @@ -63,7 +63,7 @@ describe('AuditList', () => { expect(element).toBeInTheDocument(); }); - it('renders a link to create a new audit', async () => { + it('renders a button to create a new audit', async () => { const rendered = render( wrapInTestApp( @@ -71,12 +71,8 @@ describe('AuditList', () => { , ), ); - const element = await rendered.findByText('Create Audit'); - expect(element).toBeInTheDocument(); - expect(element.parentElement).toHaveAttribute( - 'href', - '/lighthouse/create-audit', - ); + const button = await rendered.findByText('Create Audit'); + expect(button).toBeInTheDocument(); }); describe('pagination', () => { @@ -87,7 +83,7 @@ describe('AuditList', () => { , - { routeEntries: ['/lighthouse?page=2'] }, + { routeEntries: ['?page=2'] }, ), ); expect(mockFetch).toHaveBeenLastCalledWith( @@ -137,13 +133,13 @@ describe('AuditList', () => { , - { routeEntries: ['/lighthouse?page=2'] }, + { routeEntries: ['?page=2'] }, ), ); const element = await rendered.findByLabelText(/Go to page 1/); fireEvent.click(element); - expect(useNavigate()).toHaveBeenCalledWith(`/lighthouse?page=1`); + expect(useNavigate()).toHaveBeenCalledWith(`?page=1`); }); }); }); diff --git a/plugins/lighthouse/src/components/AuditView/index.test.tsx b/plugins/lighthouse/src/components/AuditView/index.test.tsx index b2c15cd8b7..742be67fed 100644 --- a/plugins/lighthouse/src/components/AuditView/index.test.tsx +++ b/plugins/lighthouse/src/components/AuditView/index.test.tsx @@ -18,15 +18,17 @@ jest.mock('react-router-dom', () => { const actual = jest.requireActual('react-router-dom'); + const mockNavigation = jest.fn(); return { ...actual, useParams: jest.fn(() => ({})), + useNavigate: jest.fn(() => mockNavigation), }; }); import React from 'react'; import mockFetch from 'jest-fetch-mock'; -import { render } from '@testing-library/react'; +import { render, fireEvent } from '@testing-library/react'; import { wrapInTestApp } from '@backstage/test-utils'; import { ApiRegistry, ApiProvider } from '@backstage/core'; @@ -34,11 +36,13 @@ import AuditView from '.'; import { lighthouseApiRef, LighthouseRestApi, Audit, Website } from '../../api'; import { formatTime } from '../../utils'; import * as data from '../../__fixtures__/website-response.json'; +import { act } from 'react-dom/test-utils'; const { useParams }: { useParams: jest.Mock } = jest.requireMock( 'react-router-dom', ); const websiteResponse = data as Website; +const { useNavigate } = jest.requireMock('react-router-dom'); describe('AuditView', () => { let apis: ApiRegistry; @@ -70,7 +74,7 @@ describe('AuditView', () => { expect(iframe).toHaveAttribute('src', `https://lighthouse/v1/audits/${id}`); }); - it('renders a link to create a new audit for this website', async () => { + it('renders a button to click to create a new audit for this website', async () => { const rendered = render( wrapInTestApp( @@ -79,13 +83,15 @@ describe('AuditView', () => { ), ); - const button = await rendered.findByText('Create Audit'); + const button = await rendered.findByText('Create New Audit'); expect(button).toBeInTheDocument(); - expect(button.parentElement).toHaveAttribute( - 'href', - `/lighthouse/create-audit?url=${encodeURIComponent( - 'https://spotify.com', - )}`, + + act(() => { + fireEvent.click(button); + }); + + expect(useNavigate()).toHaveBeenCalledWith( + `../../create-audit?url=${encodeURIComponent('https://spotify.com')}`, ); }); @@ -151,7 +157,7 @@ describe('AuditView', () => { expect( rendered.getByText(formatTime(a.timeCreated)).parentElement ?.parentElement, - ).toHaveAttribute('href', `/lighthouse/audit/${a.id}`); + ).toHaveAttribute('href', `/audit/${a.id}`); }); }); }); diff --git a/plugins/lighthouse/src/components/CreateAudit/index.test.tsx b/plugins/lighthouse/src/components/CreateAudit/index.test.tsx index aaf376923f..b2348bf5ff 100644 --- a/plugins/lighthouse/src/components/CreateAudit/index.test.tsx +++ b/plugins/lighthouse/src/components/CreateAudit/index.test.tsx @@ -78,9 +78,7 @@ describe('CreateAudit', () => { , { - routeEntries: [ - `/lighthouse/create-audit?url=${encodeURIComponent(url)}`, - ], + routeEntries: [`/create-audit?url=${encodeURIComponent(url)}`], }, ), ); @@ -137,7 +135,7 @@ describe('CreateAudit', () => { await wait(() => expect(rendered.getByLabelText(/URL/)).toBeEnabled()); - expect(useNavigate()).toHaveBeenCalledWith('/lighthouse'); + expect(useNavigate()).toHaveBeenCalledWith('..'); }); });