Fix tests

This commit is contained in:
Marcus Eide
2020-09-09 11:46:42 +02:00
parent d32a911c43
commit ce05e7e898
4 changed files with 24 additions and 27 deletions
@@ -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', () => {
@@ -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(
<ApiProvider apis={apis}>
@@ -71,12 +71,8 @@ describe('AuditList', () => {
</ApiProvider>,
),
);
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', () => {
<ApiProvider apis={apis}>
<AuditList />
</ApiProvider>,
{ routeEntries: ['/lighthouse?page=2'] },
{ routeEntries: ['?page=2'] },
),
);
expect(mockFetch).toHaveBeenLastCalledWith(
@@ -137,13 +133,13 @@ describe('AuditList', () => {
<ApiProvider apis={apis}>
<AuditList />
</ApiProvider>,
{ 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`);
});
});
});
@@ -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(
<ApiProvider apis={apis}>
@@ -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}`);
});
});
});
@@ -78,9 +78,7 @@ describe('CreateAudit', () => {
<CreateAudit />
</ApiProvider>,
{
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('..');
});
});