Skip to content

Commit

Permalink
refactor: update test cases with Link
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Aug 8, 2023
1 parent c10a055 commit 02931ab
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import LearningTypeRadioFacet from '../LearningTypeRadioFacet';
import { features } from '../config';
import { renderWithSearchContext } from './utils';

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useNavigate: () => jest.fn(),
useLocation: () => ({
pathname: '/',
}),
Expand Down
15 changes: 1 addition & 14 deletions packages/catalog-search/src/tests/SearchSuggestionItem.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@ import { renderWithRouter } from '@edx/frontend-enterprise-utils';
import React from 'react';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Link } from 'react-router-dom';
import SearchSuggestionItem from '../SearchSuggestionItem';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
Link: jest.fn(({ to, onClick, children }) => <a href={to} className="suggestion-item" onClick={onClick}>{children}</a>),
}));

describe('<SeachSuggestionItem />', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('renders course data', () => {
const mockData = {
course: {
Expand Down Expand Up @@ -96,10 +86,7 @@ describe('<SeachSuggestionItem />', () => {
/>,
);
userEvent.click(container.getElementsByClassName('suggestion-item')[0]);

expect(Link).toHaveBeenCalled();
const [[props]] = Link.mock.calls;
expect(props.to).toBe(mockData.program.url);
expect(window.location.pathname).toBe(mockData.program.url);
});

test('fires callback on click if disableSuggestionRedirect is true', () => {
Expand Down
20 changes: 2 additions & 18 deletions packages/catalog-search/src/tests/SearchSuggestions.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import { renderWithRouter } from '@edx/frontend-enterprise-utils';
import React from 'react';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Link } from 'react-router-dom';
import SearchSuggestions from '../SearchSuggestions';
import { COURSE_TYPE_EXECUTIVE_EDUCATION } from '../data/constants';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
Link: jest.fn(({ to, children }) => <a href={to} className="suggestion-item">{children}</a>),
}));

const fakeSuggestionsData = {
nbHits: 2,
hits: [
Expand Down Expand Up @@ -64,10 +58,6 @@ const fakeSuggestionsDataEmptyAuthoringOrgs = {
const handleSubmit = jest.fn();

describe('<SeachSuggestions />', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('renders all data', () => {
renderWithRouter(<SearchSuggestions
enterpriseSlug="test-enterprise"
Expand Down Expand Up @@ -119,10 +109,7 @@ describe('<SeachSuggestions />', () => {
);

userEvent.click(container.getElementsByClassName('suggestion-item')[0]);

expect(Link).toHaveBeenCalled();
const [props] = Link.mock.calls[0];
expect(props.to).toBe('/test-enterprise/course/edX+courseX');
expect(window.location.pathname).toBe('/test-enterprise/course/edX+courseX');
});

test('redirects to correct page on program click', () => {
Expand All @@ -132,10 +119,7 @@ describe('<SeachSuggestions />', () => {
handleSubmit={handleSubmit}
/>);
userEvent.click(container.getElementsByClassName('suggestion-item')[1]);

expect(Link).toHaveBeenCalled();
const [props] = Link.mock.calls[1];
expect(props.to).toBe('/test-enterprise/program/456');
expect(window.location.pathname).toBe('/test-enterprise/program/456');
});
test('properly handles exec ed content', () => {
const { container } = renderWithRouter(<SearchSuggestions
Expand Down

0 comments on commit 02931ab

Please sign in to comment.