Skip to content

Commit 6892633

Browse files
test: transform snapshot into rtl test 1/2 (#1756)
* test: remove snapshots and use rtl tests * test: improve coverage on search results test
1 parent 56a73ee commit 6892633

File tree

6 files changed

+56
-1486
lines changed

6 files changed

+56
-1486
lines changed

src/__snapshots__/index.test.jsx.snap

Lines changed: 0 additions & 228 deletions
This file was deleted.

src/course-home/courseware-search/CoursewareSearchEmpty.test.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
screen,
66
} from '../../setupTest';
77
import CoursewareSearchEmpty from './CoursewareSearchEmpty';
8+
import messages from './messages';
89

910
function renderComponent() {
1011
const { container } = render(<CoursewareSearchEmpty />);
@@ -16,9 +17,12 @@ describe('CoursewareSearchEmpty', () => {
1617
initializeMockApp();
1718
});
1819

19-
it('should match the snapshot', () => {
20+
it('render empty results text and corresponding classes', () => {
2021
renderComponent();
21-
22-
expect(screen.getByTestId('no-results')).toMatchSnapshot();
22+
const emptyText = screen.getByText(messages.searchResultsNone.defaultMessage);
23+
expect(emptyText).toBeInTheDocument();
24+
expect(emptyText).toHaveClass('courseware-search-results__empty');
25+
expect(emptyText).toHaveAttribute('data-testid', 'no-results');
26+
expect(emptyText.parentElement).toHaveClass('courseware-search-results');
2327
});
2428
});

src/course-home/courseware-search/CoursewareSearchResults.test.jsx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import CoursewareSearchResults from './CoursewareSearchResults';
88
import messages from './messages';
99
import searchResultsFactory from './test-data/search-results-factory';
10+
import * as mock from './test-data/mocked-response.json';
1011

1112
jest.mock('react-redux');
1213

@@ -34,8 +35,53 @@ describe('CoursewareSearchResults', () => {
3435
renderComponent({ results });
3536
});
3637

37-
it('should match the snapshot', () => {
38-
expect(screen.getByTestId('search-results')).toMatchSnapshot();
38+
it('should render complete list', () => {
39+
const courses = screen.getAllByRole('link');
40+
expect(courses.length).toBe(mock.results.length);
41+
});
42+
43+
it('should render correct link for internal course', () => {
44+
const courses = screen.getAllByRole('link');
45+
const firstCourse = courses[0];
46+
const firstCourseTitle = firstCourse.querySelector('.courseware-search-results__title span');
47+
expect(firstCourseTitle.innerHTML).toEqual(mock.results[0].data.content.display_name);
48+
expect(firstCourse.href).toContain(mock.results[0].data.url);
49+
expect(firstCourse).not.toHaveAttribute('target', '_blank');
50+
expect(firstCourse).not.toHaveAttribute('rel', 'nofollow');
51+
});
52+
53+
it('should render correct link if is External url course', () => {
54+
const courses = screen.getAllByRole('link');
55+
const externalCourse = courses[courses.length - 1];
56+
const externalCourseTitle = externalCourse.querySelector('.courseware-search-results__title span');
57+
expect(externalCourseTitle.innerHTML).toEqual(mock.results[mock.results.length - 1].data.content.display_name);
58+
expect(externalCourse.href).toContain(mock.results[mock.results.length - 1].data.url);
59+
expect(externalCourse).toHaveAttribute('target', '_blank');
60+
expect(externalCourse).toHaveAttribute('rel', 'nofollow');
61+
const icon = externalCourse.querySelector('svg');
62+
expect(icon).toBeInTheDocument();
63+
});
64+
65+
it('should render location breadcrumbs', () => {
66+
const breadcrumbs = screen.getAllByText(mock.results[0].data.location[0]);
67+
expect(breadcrumbs.length).toBeGreaterThan(0);
68+
const firstBreadcrumb = breadcrumbs[0].closest('li');
69+
expect(firstBreadcrumb).toBeInTheDocument();
70+
expect(firstBreadcrumb.querySelector('div').textContent).toBe(mock.results[0].data.location[0]);
71+
expect(firstBreadcrumb.nextSibling.querySelector('div').textContent).toBe(mock.results[0].data.location[1]);
72+
});
73+
});
74+
75+
describe('when results are provided with content hits', () => {
76+
beforeEach(() => {
77+
const { results } = searchResultsFactory('Passing');
78+
renderComponent({ results });
79+
});
80+
81+
it('should render content hits', () => {
82+
const contentHits = screen.getByText('1');
83+
expect(contentHits).toBeInTheDocument();
84+
expect(contentHits.tagName).toBe('EM');
3985
});
4086
});
4187
});

src/course-home/courseware-search/__snapshots__/CoursewareSearchEmpty.test.jsx.snap

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)