From fbdef5f40eb4816f36ce1de004a7f54638d01eda Mon Sep 17 00:00:00 2001 From: German Date: Tue, 19 Dec 2023 12:27:09 -0300 Subject: [PATCH] fix: prevent error when there are no results for a given filter --- .../CoursewareResultsFilter.jsx | 4 +-- .../CoursewareResultsFilter.test.jsx | 29 +++++++++++++++++-- .../CoursewareSearchResults.jsx | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/course-home/courseware-search/CoursewareResultsFilter.jsx b/src/course-home/courseware-search/CoursewareResultsFilter.jsx index 7f1f8cac17..7f4217b731 100644 --- a/src/course-home/courseware-search/CoursewareResultsFilter.jsx +++ b/src/course-home/courseware-search/CoursewareResultsFilter.jsx @@ -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; @@ -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; diff --git a/src/course-home/courseware-search/CoursewareResultsFilter.test.jsx b/src/course-home/courseware-search/CoursewareResultsFilter.test.jsx index 26c56f01ae..28251e1139 100644 --- a/src/course-home/courseware-search/CoursewareResultsFilter.test.jsx +++ b/src/course-home/courseware-search/CoursewareResultsFilter.test.jsx @@ -54,11 +54,14 @@ describe('CoursewareSearchResultsFilter', () => { describe('', () => { 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(); @@ -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(); + }); + }); + }); }); }); diff --git a/src/course-home/courseware-search/CoursewareSearchResults.jsx b/src/course-home/courseware-search/CoursewareSearchResults.jsx index 5c7bbfde25..04f9344b77 100644 --- a/src/course-home/courseware-search/CoursewareSearchResults.jsx +++ b/src/course-home/courseware-search/CoursewareSearchResults.jsx @@ -15,7 +15,7 @@ const iconTypeMapping = { const defaultIcon = Article; -const CoursewareSearchResults = ({ results }) => { +const CoursewareSearchResults = ({ results = [] }) => { if (!results?.length) { return ; }