Skip to content

Commit

Permalink
Get featured resources test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Oct 30, 2024
1 parent aa63148 commit 6f78ff3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ function FeaturedResources({header, models, ...props}: ResourcesProps) {
const seenTimes = 1 + Number(window.localStorage[storageKey] || 0);
const model = Object.assign(
{
isNew: seenTimes <= 3,
onClick: () => {
window.localStorage[storageKey] = 5;
model.isNew = false;
}
isNew: seenTimes <= 3
// There was an onClick defined here that nothing used
},
res
);
Expand Down
6 changes: 6 additions & 0 deletions src/app/pages/details/common/resource-box/resource-boxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ function CommonsHubBox() {
);
}

// There's more, but this is all we need for now
export type ResourceModel = {
heading: string;
description?: string;
link: {
text: string;
url: string;
}
};

export default function ResourceBoxes({
Expand Down
58 changes: 58 additions & 0 deletions test/src/pages/details/common/featured-resources.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import {render, screen} from '@testing-library/preact';
import FeaturedResourcesSection from '~/pages/details/common/featured-resources/featured-resources';
import ShellContextProvider from '../../../../helpers/shell-context';
import {MemoryRouter} from 'react-router-dom';
import userEvent from '@testing-library/user-event';

const mockUseUserContext = jest.fn();

jest.mock('~/contexts/user', () => ({
...jest.requireActual('~/contexts/user'),
__esModule: true,
default: () => mockUseUserContext()
}));

const resourceModels = [
{
heading: 'one',
link: {
text: 'link1',
url: 'url1'
}
},
{heading: 'two',
link: {
text: 'link2',
url: 'url2'
}
}
];

describe('details/featured-resources', () => {
const user = userEvent.setup();
const originalError = console.error;

it('renders', async () => {
mockUseUserContext.mockReturnValue({
userStatus: {
isInstructor: true
}
});
render(
<ShellContextProvider>
<MemoryRouter initialEntries={['/path?Instructor']}>
<FeaturedResourcesSection
header="section header"
models={resourceModels}
data-analytics-content-list="featured-resource"
/>
</MemoryRouter>
</ShellContextProvider>
);
console.error = jest.fn();
await user.click(screen.getByRole('link', {name: 'Go to one'}));
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Not implemented: navigation'), undefined);
console.error = originalError;
});
});

0 comments on commit 6f78ff3

Please sign in to comment.