Skip to content

Commit

Permalink
Improve test coverage (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
samwisekind authored Dec 24, 2024
1 parent 42020ce commit 1afd26d
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 16 deletions.
4 changes: 2 additions & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sonar.projectKey=CRRU-UK_website
sonar.organization=crru

sonar.sources=.
sonar.tests=.
sonar.sources=src/
sonar.tests=src/
sonar.test.inclusions=**/*.test.ts,**/*.test.tsx
sonar.coverage.exclusions=src/helpers/types.ts,src/helpers/constants.ts,src/pages/**/*
sonar.cpd.exclusions=**/*.test.ts,**/*.test.tsx,src/helpers/types.ts,src/helpers/constants.ts,src/pages/**/*
Expand Down
20 changes: 20 additions & 0 deletions src/components/Breadcrumbs/Breadcrumbs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import '@testing-library/jest-dom';

import type {
Props,
} from './Breadcrumbs';

import { act } from 'react';
import { render } from '@testing-library/react';
import { axe, toHaveNoViolations } from 'jest-axe';
Expand Down Expand Up @@ -46,3 +50,19 @@ it('Passes accessibility with optional props', async () => act(async () => {

expect(results).toHaveNoViolations();
}));

it.each([
'wide',
'inline',
])('Passes accessibility with different styles', async (style) => act(async () => {
const { container } = render(
<Breadcrumbs
style={style as Props['style']}
items={[]}
/>,
);

const results = await axe(container);

expect(results).toHaveNoViolations();
}));
2 changes: 1 addition & 1 deletion src/components/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import sitemap from '@/data/sitemap.json';

import styles from './Breadcrumbs.module.scss';

interface Props {
export interface Props {
items: Array<SitemapItem>,
style?: 'normal' | 'wide' | 'inline',
}
Expand Down
33 changes: 30 additions & 3 deletions src/components/Filters/Filters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,32 @@ beforeAll(() => {
expect.extend(toHaveNoViolations);
});

it('Passes accessibility with default props', async () => act(async () => {
it('Passes accessibility with default search props', async () => act(async () => {
const { container } = render(
<Filters />,
<Filters
search={{
callback: jest.fn(),
}}
/>,
);

const results = await axe(container);

expect(results).toHaveNoViolations();
}));

it('Passes accessibility with default dropdown props', async () => act(async () => {
const { container } = render(
<Filters
dropdowns={[{
name: 'test name',
options: [{
text: 'test text',
value: 'text-value',
}],
callback: jest.fn(),
}]}
/>,
);

const results = await axe(container);
Expand All @@ -23,7 +46,11 @@ it('Passes accessibility with default props', async () => act(async () => {
it('Passes accessibility with optional props', async () => act(async () => {
const { container } = render(
<Filters
search={{ callback: jest.fn(), label: 'mocked label', defaultValue: 'mocked default value' }}
search={{
callback: jest.fn(),
label: 'mocked label',
defaultValue: 'mocked default value',
}}
dropdowns={[{
name: 'test name',
defaultValue: 'mocked default value',
Expand Down
24 changes: 24 additions & 0 deletions src/components/Timeline/Timeline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ it('Passes accessibility with default props', async () => act(async () => {

expect(results).toHaveNoViolations();
}));

it('Displays single timeline', async () => act(async () => {
const { container } = render(
<Timeline
items={['item 1']}
/>,
);

const results = await axe(container);

expect(results).toHaveNoViolations();
}));

it('Returns null for no items', async () => act(async () => {
const { container } = render(
<Timeline
items={[]}
/>,
);

const results = await axe(container);

expect(results).toHaveNoViolations();
}));
13 changes: 7 additions & 6 deletions src/helpers/getCatalogue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,24 @@ afterEach(() => {
});

describe('getCatalogueList', () => {
it('Returns catalogue entries', async () => {
it.each([
['bottlenose-dolphin', 'catalogueBottlenoseDolphin'],
['minke-whale', 'catalogueMinkeWhale']
])('Returns catalogue entries (%p)', async (catalogue, contentType) => {
(contentfulDeliveryClient.getEntries as jest.Mock).mockImplementation(() => ({
total: 100,
items: mockedEntries,
}));

const result = await getCatalogueList(
// @ts-expect-error String of enum value
'bottlenose-dolphin',
catalogue,
{ page: 2 },
);

expect(contentfulDeliveryClient.getEntries).toHaveBeenCalledTimes(1);
expect(contentfulDeliveryClient.getEntries).toHaveBeenNthCalledWith(1, {
content_type: 'catalogueBottlenoseDolphin',
content_type: contentType,
order: ['fields.id'],
limit: 30,
skip: 30,
Expand Down Expand Up @@ -396,9 +399,7 @@ describe('getMinkeWhaleCatalogueItem', () => {
items: [],
}));

const result = await getBottlenoseDolphinCatalogueItem('mocked-slug');

expect(contentfulDeliveryClient.getEntries).toHaveBeenCalledTimes(1);
const result = await getMinkeWhaleCatalogueItem('mocked-slug');

expect(result).toBe(null);
});
Expand Down
15 changes: 13 additions & 2 deletions src/helpers/getNews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ afterEach(() => {
jest.clearAllMocks();
});

it('Queries news articles', async () => {
it('Gets news articles', async () => {
const result = await getNews({ limit: 12 });

expect(contentfulDeliveryClient.getEntries).toHaveBeenCalledTimes(1);
Expand All @@ -45,7 +45,18 @@ it('Queries news articles', async () => {
expect(result).toStrictEqual([mockedEntries]);
});

it('Queries new articles with no keywords', async () => {
it('Gets news articles with default limit', async () => {
await getNews({});

expect(contentfulDeliveryClient.getEntries).toHaveBeenCalledTimes(1);
expect(contentfulDeliveryClient.getEntries).toHaveBeenNthCalledWith(1, {
content_type: 'newsArticle',
order: ['-fields.date'],
limit: 1000,
});
});

it('Gets new articles with no keywords', async () => {
(contentfulDeliveryClient.getEntries as jest.Mock)
.mockImplementation(() => ({
items: [{
Expand Down
2 changes: 0 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { NextPage, GetServerSideProps } from 'next';
import type { Asset, Entry } from 'contentful';

Expand Down

0 comments on commit 1afd26d

Please sign in to comment.