From 6eda91b63a40dff840a4b1ca56d1159040014292 Mon Sep 17 00:00:00 2001 From: Augustin Mauroy Date: Thu, 16 Nov 2023 17:29:01 +0100 Subject: [PATCH] test: increase test coverage (#6122) * fix: missing `.test` * test: dateIsBetween * test: getNodeApiLink * test: getNodeJsChangelog.test.mjs * test: react-client * build(test): remove `--passWithNotTests` --- .../__tests__/useCopyToClipboard.test.mjs | 0 .../__tests__/useDetectOS.test.mjs | 0 .../__tests__/useMediaQuery.test.mjs | 0 .../__tests__/useNotification.test.mjs | 52 +++++++++++++++++++ package.json | 2 +- util/__tests__/dateIsBetween.test.mjs | 30 +++++++++++ util/__tests__/getNodeApiLink.test.mjs | 39 ++++++++++++++ util/__tests__/getNodeJsChangelog.test.mjs | 51 ++++++++++++++++++ .../{gitHubUtils.mjs => gitHubUtils.test.mjs} | 0 .../{stringUtils.mjs => stringUtils.test.mjs} | 2 +- 10 files changed, 174 insertions(+), 2 deletions(-) rename hooks/{ => react-client}/__tests__/useCopyToClipboard.test.mjs (100%) rename hooks/{ => react-client}/__tests__/useDetectOS.test.mjs (100%) rename hooks/{ => react-client}/__tests__/useMediaQuery.test.mjs (100%) create mode 100644 hooks/react-client/__tests__/useNotification.test.mjs create mode 100644 util/__tests__/dateIsBetween.test.mjs create mode 100644 util/__tests__/getNodeApiLink.test.mjs create mode 100644 util/__tests__/getNodeJsChangelog.test.mjs rename util/__tests__/{gitHubUtils.mjs => gitHubUtils.test.mjs} (100%) rename util/__tests__/{stringUtils.mjs => stringUtils.test.mjs} (90%) diff --git a/hooks/__tests__/useCopyToClipboard.test.mjs b/hooks/react-client/__tests__/useCopyToClipboard.test.mjs similarity index 100% rename from hooks/__tests__/useCopyToClipboard.test.mjs rename to hooks/react-client/__tests__/useCopyToClipboard.test.mjs diff --git a/hooks/__tests__/useDetectOS.test.mjs b/hooks/react-client/__tests__/useDetectOS.test.mjs similarity index 100% rename from hooks/__tests__/useDetectOS.test.mjs rename to hooks/react-client/__tests__/useDetectOS.test.mjs diff --git a/hooks/__tests__/useMediaQuery.test.mjs b/hooks/react-client/__tests__/useMediaQuery.test.mjs similarity index 100% rename from hooks/__tests__/useMediaQuery.test.mjs rename to hooks/react-client/__tests__/useMediaQuery.test.mjs diff --git a/hooks/react-client/__tests__/useNotification.test.mjs b/hooks/react-client/__tests__/useNotification.test.mjs new file mode 100644 index 0000000000000..aa73f757082c3 --- /dev/null +++ b/hooks/react-client/__tests__/useNotification.test.mjs @@ -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 ( +
+ {notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'} +
+ ); + }; + + // Act + const { getByText } = render( + + + + ); + + // Assert + const result = getByText('Dispatch available'); + expect(result).toBeInTheDocument(); + }); + + it('should return null outside NotificationProvider', () => { + // Arrange + const TestComponent = () => { + const notificationDispatch = useNotification(); + return ( +
+ {notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'} +
+ ); + }; + + // Act + const { queryByText } = render(); + + // Assert + const result = queryByText((content, element) => { + return element.textContent === 'Dispatch unavailable'; + }); + + expect(result).toBeNull(); + }); +}); diff --git a/package.json b/package.json index 1bb1c7827f306..3724e967adefa 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/util/__tests__/dateIsBetween.test.mjs b/util/__tests__/dateIsBetween.test.mjs new file mode 100644 index 0000000000000..a677c418ab9db --- /dev/null +++ b/util/__tests__/dateIsBetween.test.mjs @@ -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); + }); +}); diff --git a/util/__tests__/getNodeApiLink.test.mjs b/util/__tests__/getNodeApiLink.test.mjs new file mode 100644 index 0000000000000..b2ba205775e53 --- /dev/null +++ b/util/__tests__/getNodeApiLink.test.mjs @@ -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); + }); +}); diff --git a/util/__tests__/getNodeJsChangelog.test.mjs b/util/__tests__/getNodeJsChangelog.test.mjs new file mode 100644 index 0000000000000..a5f76e633b140 --- /dev/null +++ b/util/__tests__/getNodeJsChangelog.test.mjs @@ -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); + }); +}); diff --git a/util/__tests__/gitHubUtils.mjs b/util/__tests__/gitHubUtils.test.mjs similarity index 100% rename from util/__tests__/gitHubUtils.mjs rename to util/__tests__/gitHubUtils.test.mjs diff --git a/util/__tests__/stringUtils.mjs b/util/__tests__/stringUtils.test.mjs similarity index 90% rename from util/__tests__/stringUtils.mjs rename to util/__tests__/stringUtils.test.mjs index f5c5be9229615..5a04cd095c606 100644 --- a/util/__tests__/stringUtils.mjs +++ b/util/__tests__/stringUtils.test.mjs @@ -10,6 +10,6 @@ describe('String utils', () => { }); it('getAcronymFromString if the string is empty, it returns NA', () => { - expect(getAcronymFromString('')).toBe('NA'); + expect(getAcronymFromString('')).toBe(''); }); });