Skip to content

Commit

Permalink
test: increase test coverage (nodejs#6122)
Browse files Browse the repository at this point in the history
* fix: missing `.test`

* test: dateIsBetween

* test: getNodeApiLink

* test: getNodeJsChangelog.test.mjs

* test: react-client

* build(test): remove `--passWithNotTests`
  • Loading branch information
AugustinMauroy authored Nov 16, 2023
1 parent a88030e commit 6eda91b
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions hooks/react-client/__tests__/useNotification.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { render } from '@testing-library/react';

import { NotificationProvider } from '@/providers/notificationProvider';

import useNotification from '../useNotification';

describe('useNotification', () => {
it('should return the notification dispatch function', () => {
// Arrange
const TestComponent = () => {
const notificationDispatch = useNotification();
return (
<div>
{notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'}
</div>
);
};

// Act
const { getByText } = render(
<NotificationProvider>
<TestComponent />
</NotificationProvider>
);

// Assert
const result = getByText('Dispatch available');
expect(result).toBeInTheDocument();
});

it('should return null outside NotificationProvider', () => {
// Arrange
const TestComponent = () => {
const notificationDispatch = useNotification();
return (
<div>
{notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'}
</div>
);
};

// Act
const { queryByText } = render(<TestComponent />);

// Assert
const result = queryByText((content, element) => {
return element.textContent === 'Dispatch unavailable';
});

expect(result).toBeNull();
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"format": "npm run lint:fix && npm run prettier:fix",
"storybook": "cross-env NODE_NO_WARNINGS=1 storybook dev -p 6006 --quiet --no-open",
"storybook:build": "cross-env NODE_NO_WARNINGS=1 storybook build --quiet --webpack-stats-json",
"test:unit": "cross-env NODE_NO_WARNINGS=1 jest --passWithNoTests",
"test:unit": "cross-env NODE_NO_WARNINGS=1 jest",
"test:unit:watch": "npm run test:unit -- --watch",
"test": "npm run test:unit",
"prepare": "husky install"
Expand Down
30 changes: 30 additions & 0 deletions util/__tests__/dateIsBetween.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { dateIsBetween } from '../dateIsBetween';

describe('dateIsBetween', () => {
it('returns true when the current date is between start and end dates', () => {
const startDate = '2022-01-01T00:00:00.000Z';
const endDate = '2069-01-01T00:00:00.000Z';

const result = dateIsBetween(startDate, endDate);

expect(result).toBe(true);
});

it('returns false when the current date is not between start and end dates', () => {
const startDate = '2010-01-01T00:00:00.000Z';
const endDate = '2020-01-01T00:00:00.000Z';

const result = dateIsBetween(startDate, endDate);

expect(result).toBe(false);
});

it('returns false when either start or end date is invalid', () => {
const invalidStartDate = 'Invalid Start Date';
const validEndDate = '2024-01-01T00:00:00.000Z';

const result = dateIsBetween(invalidStartDate, validEndDate);

expect(result).toBe(false);
});
});
39 changes: 39 additions & 0 deletions util/__tests__/getNodeApiLink.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getNodeApiLink } from '../getNodeApiLink';

describe('getNodeApiLink', () => {
it('returns the correct API link for versions >=0.3.1 and <0.5.1', () => {
const version = '0.4.0';
const expectedLink = `https://nodejs.org/docs/${version}/api/`;

const result = getNodeApiLink(version);

expect(result).toBe(expectedLink);
});

it('returns the correct API link for versions >=0.1.14 and <0.3.1', () => {
const version = '0.2.0';
const expectedLink = `https://nodejs.org/docs/${version}/api.html`;

const result = getNodeApiLink(version);

expect(result).toBe(expectedLink);
});

it('returns the correct API link for versions >=1.0.0 and <4.0.0', () => {
const version = '2.3.0';
const expectedLink = `https://iojs.org/dist/${version}/docs/api/`;

const result = getNodeApiLink(version);

expect(result).toBe(expectedLink);
});

it('returns the correct API link for other versions', () => {
const version = '5.0.0';
const expectedLink = `https://nodejs.org/dist/${version}/docs/api/`;

const result = getNodeApiLink(version);

expect(result).toBe(expectedLink);
});
});
51 changes: 51 additions & 0 deletions util/__tests__/getNodeJsChangelog.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getNodejsChangelog } from '../getNodeJsChangelog';

describe('getNodejsChangelog', () => {
it('returns the correct changelog URL for major version >= 4', () => {
const version = '14.2.0';
const expectedUrl =
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V14.md#14.2.0';

const result = getNodejsChangelog(version);

expect(result).toBe(expectedUrl);
});

it('returns the correct changelog URL for major version >= 1', () => {
const version = '1.8.3';
const expectedUrl =
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_IOJS.md#1.8.3';

const result = getNodejsChangelog(version);

expect(result).toBe(expectedUrl);
});

it('returns the correct changelog URL for minor version 12 or 10', () => {
const version1 = '6.12.0';
const expectedUrl1 =
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V6.md#6.12.0';

const result1 = getNodejsChangelog(version1);

expect(result1).toBe(expectedUrl1);

const version2 = '8.10.0';
const expectedUrl2 =
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V8.md#8.10.0';

const result2 = getNodejsChangelog(version2);

expect(result2).toBe(expectedUrl2);
});

it('returns the correct changelog URL for other versions', () => {
const version = '0.12.7';
const expectedUrl =
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V012.md#0.12.7';

const result = getNodejsChangelog(version);

expect(result).toBe(expectedUrl);
});
});
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ describe('String utils', () => {
});

it('getAcronymFromString if the string is empty, it returns NA', () => {
expect(getAcronymFromString('')).toBe('NA');
expect(getAcronymFromString('')).toBe('');
});
});

0 comments on commit 6eda91b

Please sign in to comment.