Skip to content

Commit 6eda91b

Browse files
test: increase test coverage (nodejs#6122)
* fix: missing `.test` * test: dateIsBetween * test: getNodeApiLink * test: getNodeJsChangelog.test.mjs * test: react-client * build(test): remove `--passWithNotTests`
1 parent a88030e commit 6eda91b

10 files changed

+174
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { render } from '@testing-library/react';
2+
3+
import { NotificationProvider } from '@/providers/notificationProvider';
4+
5+
import useNotification from '../useNotification';
6+
7+
describe('useNotification', () => {
8+
it('should return the notification dispatch function', () => {
9+
// Arrange
10+
const TestComponent = () => {
11+
const notificationDispatch = useNotification();
12+
return (
13+
<div>
14+
{notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'}
15+
</div>
16+
);
17+
};
18+
19+
// Act
20+
const { getByText } = render(
21+
<NotificationProvider>
22+
<TestComponent />
23+
</NotificationProvider>
24+
);
25+
26+
// Assert
27+
const result = getByText('Dispatch available');
28+
expect(result).toBeInTheDocument();
29+
});
30+
31+
it('should return null outside NotificationProvider', () => {
32+
// Arrange
33+
const TestComponent = () => {
34+
const notificationDispatch = useNotification();
35+
return (
36+
<div>
37+
{notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'}
38+
</div>
39+
);
40+
};
41+
42+
// Act
43+
const { queryByText } = render(<TestComponent />);
44+
45+
// Assert
46+
const result = queryByText((content, element) => {
47+
return element.textContent === 'Dispatch unavailable';
48+
});
49+
50+
expect(result).toBeNull();
51+
});
52+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"format": "npm run lint:fix && npm run prettier:fix",
3434
"storybook": "cross-env NODE_NO_WARNINGS=1 storybook dev -p 6006 --quiet --no-open",
3535
"storybook:build": "cross-env NODE_NO_WARNINGS=1 storybook build --quiet --webpack-stats-json",
36-
"test:unit": "cross-env NODE_NO_WARNINGS=1 jest --passWithNoTests",
36+
"test:unit": "cross-env NODE_NO_WARNINGS=1 jest",
3737
"test:unit:watch": "npm run test:unit -- --watch",
3838
"test": "npm run test:unit",
3939
"prepare": "husky install"

util/__tests__/dateIsBetween.test.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { dateIsBetween } from '../dateIsBetween';
2+
3+
describe('dateIsBetween', () => {
4+
it('returns true when the current date is between start and end dates', () => {
5+
const startDate = '2022-01-01T00:00:00.000Z';
6+
const endDate = '2069-01-01T00:00:00.000Z';
7+
8+
const result = dateIsBetween(startDate, endDate);
9+
10+
expect(result).toBe(true);
11+
});
12+
13+
it('returns false when the current date is not between start and end dates', () => {
14+
const startDate = '2010-01-01T00:00:00.000Z';
15+
const endDate = '2020-01-01T00:00:00.000Z';
16+
17+
const result = dateIsBetween(startDate, endDate);
18+
19+
expect(result).toBe(false);
20+
});
21+
22+
it('returns false when either start or end date is invalid', () => {
23+
const invalidStartDate = 'Invalid Start Date';
24+
const validEndDate = '2024-01-01T00:00:00.000Z';
25+
26+
const result = dateIsBetween(invalidStartDate, validEndDate);
27+
28+
expect(result).toBe(false);
29+
});
30+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { getNodeApiLink } from '../getNodeApiLink';
2+
3+
describe('getNodeApiLink', () => {
4+
it('returns the correct API link for versions >=0.3.1 and <0.5.1', () => {
5+
const version = '0.4.0';
6+
const expectedLink = `https://nodejs.org/docs/${version}/api/`;
7+
8+
const result = getNodeApiLink(version);
9+
10+
expect(result).toBe(expectedLink);
11+
});
12+
13+
it('returns the correct API link for versions >=0.1.14 and <0.3.1', () => {
14+
const version = '0.2.0';
15+
const expectedLink = `https://nodejs.org/docs/${version}/api.html`;
16+
17+
const result = getNodeApiLink(version);
18+
19+
expect(result).toBe(expectedLink);
20+
});
21+
22+
it('returns the correct API link for versions >=1.0.0 and <4.0.0', () => {
23+
const version = '2.3.0';
24+
const expectedLink = `https://iojs.org/dist/${version}/docs/api/`;
25+
26+
const result = getNodeApiLink(version);
27+
28+
expect(result).toBe(expectedLink);
29+
});
30+
31+
it('returns the correct API link for other versions', () => {
32+
const version = '5.0.0';
33+
const expectedLink = `https://nodejs.org/dist/${version}/docs/api/`;
34+
35+
const result = getNodeApiLink(version);
36+
37+
expect(result).toBe(expectedLink);
38+
});
39+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { getNodejsChangelog } from '../getNodeJsChangelog';
2+
3+
describe('getNodejsChangelog', () => {
4+
it('returns the correct changelog URL for major version >= 4', () => {
5+
const version = '14.2.0';
6+
const expectedUrl =
7+
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V14.md#14.2.0';
8+
9+
const result = getNodejsChangelog(version);
10+
11+
expect(result).toBe(expectedUrl);
12+
});
13+
14+
it('returns the correct changelog URL for major version >= 1', () => {
15+
const version = '1.8.3';
16+
const expectedUrl =
17+
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_IOJS.md#1.8.3';
18+
19+
const result = getNodejsChangelog(version);
20+
21+
expect(result).toBe(expectedUrl);
22+
});
23+
24+
it('returns the correct changelog URL for minor version 12 or 10', () => {
25+
const version1 = '6.12.0';
26+
const expectedUrl1 =
27+
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V6.md#6.12.0';
28+
29+
const result1 = getNodejsChangelog(version1);
30+
31+
expect(result1).toBe(expectedUrl1);
32+
33+
const version2 = '8.10.0';
34+
const expectedUrl2 =
35+
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V8.md#8.10.0';
36+
37+
const result2 = getNodejsChangelog(version2);
38+
39+
expect(result2).toBe(expectedUrl2);
40+
});
41+
42+
it('returns the correct changelog URL for other versions', () => {
43+
const version = '0.12.7';
44+
const expectedUrl =
45+
'https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V012.md#0.12.7';
46+
47+
const result = getNodejsChangelog(version);
48+
49+
expect(result).toBe(expectedUrl);
50+
});
51+
});
File renamed without changes.

util/__tests__/stringUtils.mjs renamed to util/__tests__/stringUtils.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ describe('String utils', () => {
1010
});
1111

1212
it('getAcronymFromString if the string is empty, it returns NA', () => {
13-
expect(getAcronymFromString('')).toBe('NA');
13+
expect(getAcronymFromString('')).toBe('');
1414
});
1515
});

0 commit comments

Comments
 (0)