Skip to content

Commit

Permalink
fix: prevent error when there are no results for a given filter (#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
germanolleunlp authored Dec 19, 2023
1 parent b3a06fd commit 06d79ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/course-home/courseware-search/CoursewareResultsFilter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const CoursewareSearchResultsFilter = ({ intl }) => {
const lastSearch = useModel('contentSearchResults', courseId);
const { filter: filterKeyword, setFilter } = useCoursewareSearchParams();

if (!lastSearch || !lastSearch?.results?.length) { return null; }
if (!lastSearch) { return null; }

const { results: data = [] } = lastSearch;

Expand All @@ -40,7 +40,7 @@ export const CoursewareSearchResultsFilter = ({ intl }) => {
const filters = useMemo(() => Object.keys(allowedFilterKeys).map((key) => ({
key,
label: intl.formatMessage(messages[`filter:${key}`]),
count: results[key].length,
count: results[key]?.length || 0,
})), [results]);

const activeKey = allowedFilterKeys[filterKeyword] ? filterKeyword : allFilterKey;
Expand Down
29 changes: 27 additions & 2 deletions src/course-home/courseware-search/CoursewareResultsFilter.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ describe('CoursewareSearchResultsFilter', () => {

describe('</CoursewareSearchResultsFilter />', () => {
beforeEach(() => {
useCoursewareSearchParams.mockReturnValue(coursewareSearch);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should render', async () => {
useCoursewareSearchParams.mockReturnValue(coursewareSearch);
it('should render without errors', async () => {
useModel.mockReturnValue(searchResultsFactory());

await renderComponent();
Expand All @@ -71,5 +74,27 @@ describe('CoursewareSearchResultsFilter', () => {
expect(screen.queryByTestId('courseware-search-results-tabs-other')).toBeInTheDocument();
});
});

describe('when there are not results', () => {
it('should render without errors', async () => {
useModel.mockReturnValue(searchResultsFactory('blah', {
results: [],
filters: [],
total: 0,
maxScore: null,
ms: 5,
}));

await renderComponent();

await waitFor(() => {
expect(screen.queryByTestId('courseware-search-results-tabs-all')).toBeInTheDocument();
expect(screen.queryByTestId('courseware-search-results-tabs-text')).toBeInTheDocument();
expect(screen.queryByTestId('courseware-search-results-tabs-video')).toBeInTheDocument();
expect(screen.queryByTestId('courseware-search-results-tabs-sequence')).toBeInTheDocument();
expect(screen.queryByTestId('courseware-search-results-tabs-other')).toBeInTheDocument();
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const iconTypeMapping = {

const defaultIcon = Article;

const CoursewareSearchResults = ({ results }) => {
const CoursewareSearchResults = ({ results = [] }) => {
if (!results?.length) {
return <CoursewareSearchEmpty />;
}
Expand Down

0 comments on commit 06d79ce

Please sign in to comment.