From e0019f8b7d3047bffc488bbccfa10131cf77c9aa Mon Sep 17 00:00:00 2001 From: Xavier FACQ Date: Thu, 5 Sep 2024 15:07:17 +0200 Subject: [PATCH] Add code coverage --- .../__snapshots__/adoptiumNews.test.tsx.snap | 31 +++++++++++++++++++ .../__tests__/adoptiumNews.test.tsx | 24 ++++++++++++++ src/components/AdoptiumNews/index.tsx | 10 ++++-- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/components/AdoptiumNews/__tests__/__snapshots__/adoptiumNews.test.tsx.snap diff --git a/src/components/AdoptiumNews/__tests__/__snapshots__/adoptiumNews.test.tsx.snap b/src/components/AdoptiumNews/__tests__/__snapshots__/adoptiumNews.test.tsx.snap new file mode 100644 index 000000000..14fca4788 --- /dev/null +++ b/src/components/AdoptiumNews/__tests__/__snapshots__/adoptiumNews.test.tsx.snap @@ -0,0 +1,31 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`AdoptiumNews component > renders correctly with provided news 1`] = ` +
+
+
+

+ Adoptium Summit 2024 +

+
+

+ September 10, 2024 +

+

+ Text +

+
+
+
+
+`; diff --git a/src/components/AdoptiumNews/__tests__/adoptiumNews.test.tsx b/src/components/AdoptiumNews/__tests__/adoptiumNews.test.tsx index 0be3f629e..b2c401353 100644 --- a/src/components/AdoptiumNews/__tests__/adoptiumNews.test.tsx +++ b/src/components/AdoptiumNews/__tests__/adoptiumNews.test.tsx @@ -3,10 +3,34 @@ import { render } from '@testing-library/react'; import { describe, expect, it } from 'vitest' import AdoptiumNews from '..'; +const mockedItems = [ + { + title: "Mocked News 1", + body: "Be a part of the Adoptium", + date: new Date('2024-09-10'), + startDisplayAt: new Date('2024-01-01'), + stopDisplayAfter: new Date('2050-12-31'), + }, + { + title: "Mocked News 2", + body: "I love Adoptium.
Go here", + callToActionLink: 'https://adoptium.net/', + date: new Date('2024-09-10'), + startDisplayAt: new Date('2024-01-01'), + stopDisplayAfter: new Date('2050-12-31'), + } +] + describe('AdoptiumNews component', () => { it('renders correctly', () => { const { container } = render(); // expect container to either be null or contain a div with the class of alert expect(container).toBeNull || expect(container.querySelector('div.text-pink')).toBeTruthy(); }); + + it('renders correctly with provided news', () => { + const { container } = render(); + + expect(container).toMatchSnapshot(); + }); }); diff --git a/src/components/AdoptiumNews/index.tsx b/src/components/AdoptiumNews/index.tsx index 196174419..926a9adb1 100644 --- a/src/components/AdoptiumNews/index.tsx +++ b/src/components/AdoptiumNews/index.tsx @@ -31,18 +31,24 @@ const eventDateOptions = { timeZone: "UTC" } +interface Props { + providedAdoptiumNewsList?: AdoptiumNewsItem[]; + } + function compareNewsByStartDisplayAt(a: AdoptiumNewsItem, b: AdoptiumNewsItem) { return a.startDisplayAt.getTime() - b.startDisplayAt.getTime(); } -const AdoptiumNews = () => { +const AdoptiumNews = ({ providedAdoptiumNewsList }: Props) => { + + const adoptiumNewsListToDisplay = providedAdoptiumNewsList ? providedAdoptiumNewsList : adoptiumNewsList; const { language } = useI18next(); const now = Date.now(); return ( - adoptiumNewsList + adoptiumNewsListToDisplay .filter(adoptiumNews => adoptiumNews.startDisplayAt.getTime() <= now && adoptiumNews.stopDisplayAfter.getTime() > now) .sort(compareNewsByStartDisplayAt) .map((adoptiumNews, index) => {