Skip to content

Commit

Permalink
test: provide context for Components
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdvl committed Oct 27, 2023
1 parent 1816b43 commit 1bb1e19
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
25 changes: 22 additions & 3 deletions dotcom-rendering/src/components/MostViewedRight.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from '@testing-library/react';
import { useApi as useApi_ } from '../lib/useApi';
import { ConfigProvider } from './ConfigContext';
import { mockTab1 } from './MostViewed.mocks';
import { MostViewedRight } from './MostViewedRight';

Expand All @@ -17,7 +18,13 @@ describe('MostViewedList', () => {
it('should call the api and render the response as expected', () => {
useApi.mockReturnValue(response);

const { asFragment, getAllByText } = render(<MostViewedRight />);
const { asFragment, getAllByText } = render(
<ConfigProvider
value={{ renderingTarget: 'Web', darkModeAvailable: false }}
>
<MostViewedRight />
</ConfigProvider>,
);

// Calls api once only
expect(useApi).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -56,7 +63,13 @@ describe('MostViewedList', () => {
it('should implement a limit on the number of items', () => {
useApi.mockReturnValue(response);

const { getAllByText } = render(<MostViewedRight limitItems={3} />);
const { getAllByText } = render(
<ConfigProvider
value={{ renderingTarget: 'Web', darkModeAvailable: false }}
>
<MostViewedRight limitItems={3} />
</ConfigProvider>,
);

// Calls api once only
expect(useApi).toHaveBeenCalledTimes(1);
Expand All @@ -74,7 +87,13 @@ describe('MostViewedList', () => {
it('should show a byline when this property is set to true', async () => {
useApi.mockReturnValue(response);

const { getByText } = render(<MostViewedRight />);
const { getByText } = render(
<ConfigProvider
value={{ renderingTarget: 'Web', darkModeAvailable: false }}
>
<MostViewedRight />
</ConfigProvider>,
);

expect(
getByText('Jennifer Rankin and Daniel Boffey'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ArticleDesign, ArticleDisplay, ArticleSpecial } from '@guardian/libs';
import { renderToString } from 'react-dom/server';
import { ConfigProvider } from './ConfigContext';
import { RichLinkComponent } from './RichLinkComponent.importable';

describe('RichLinkComponent', () => {
Expand All @@ -11,18 +12,22 @@ describe('RichLinkComponent', () => {
};
expect(() =>
renderToString(
<RichLinkComponent
element={{
_type: 'model.dotcomrendering.pageElements.RichLinkBlockElement',
elementId: '',
url: '',
text: '',
prefix: '',
}}
ajaxUrl={''}
richLinkIndex={0}
format={format}
/>,
<ConfigProvider
value={{ renderingTarget: 'Web', darkModeAvailable: false }}
>
<RichLinkComponent
element={{
_type: 'model.dotcomrendering.pageElements.RichLinkBlockElement',
elementId: '',
url: '',
text: '',
prefix: '',
}}
ajaxUrl={''}
richLinkIndex={0}
format={format}
/>
</ConfigProvider>,
),
).not.toThrow();
});
Expand Down

0 comments on commit 1bb1e19

Please sign in to comment.