Skip to content

Commit

Permalink
Add code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierfacq committed Sep 5, 2024
1 parent 327185d commit e0019f8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`AdoptiumNews component > renders correctly with provided news 1`] = `
<div>
<div
class="p-3 mt-4 mb-4 bg-light rounded-3 text-start"
>
<div
class="container py-5"
>
<h2
class="text-pink"
>
Adoptium Summit 2024
</h2>
<div>
<p
class="m-0 fw-bold"
>
September 10, 2024
</p>
<p
class="text-muted lh-sm"
>
Text
</p>
</div>
</div>
</div>
</div>
`;
24 changes: 24 additions & 0 deletions src/components/AdoptiumNews/__tests__/adoptiumNews.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/><callToActionLink>Go here</callToActionLink>",
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(<AdoptiumNews />);
// 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(<AdoptiumNews items={mockedItems} />);

expect(container).toMatchSnapshot();
});
});
10 changes: 8 additions & 2 deletions src/components/AdoptiumNews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Check warning on line 40 in src/components/AdoptiumNews/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/AdoptiumNews/index.tsx#L38-L40

Added lines #L38 - L40 were not covered by tests

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) => {
Expand Down

0 comments on commit e0019f8

Please sign in to comment.