Skip to content

Commit

Permalink
fix: test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Jul 14, 2023
1 parent ff415bd commit f67f7c1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/catalog-search/src/tests/FacetListBase.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SearchData from '../SearchContext';
const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(),
useNavigate: () => mockedNavigator,
}));
Expand Down
27 changes: 14 additions & 13 deletions packages/catalog-search/src/tests/SearchSuggestionItem.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ 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';

const mockedNavigator = jest.fn();

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

describe('<SeachSuggestionItem />', () => {
Expand Down Expand Up @@ -87,14 +83,19 @@ describe('<SeachSuggestionItem />', () => {
},
};

const { container, history } = renderWithRouter(<SearchSuggestionItem
url={mockData.program.url}
suggestionItemHandler={mockData.program.suggestionItemHandler}
hit={mockData.program.hit}
disableSuggestionRedirect={mockData.program.disableSuggestionRedirect}
/>);
const { container } = renderWithRouter(
<SearchSuggestionItem
url={mockData.program.url}
suggestionItemHandler={mockData.program.suggestionItemHandler}
hit={mockData.program.hit}
disableSuggestionRedirect={mockData.program.disableSuggestionRedirect}
/>,
);
userEvent.click(container.getElementsByClassName('suggestion-item')[0]);
expect(history.location.pathname).toBe(mockData.program.url);

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

test('fires callback on click if disableSuggestionRedirect is true', () => {
Expand Down
24 changes: 20 additions & 4 deletions packages/catalog-search/src/tests/SearchSuggestions.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ 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 @@ -58,6 +64,10 @@ const fakeSuggestionsDataEmptyAuthoringOrgs = {
const handleSubmit = jest.fn();

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

test('renders all data', () => {
renderWithRouter(<SearchSuggestions
enterpriseSlug="test-enterprise"
Expand Down Expand Up @@ -99,17 +109,20 @@ describe('<SeachSuggestions />', () => {
expect(handleSubmit.mock.calls.length).toBe(1);
});

test.only('redirects to correct page on course click', () => {
test('redirects to correct page on course click', () => {
const { container } = renderWithRouter(
<SearchSuggestions
enterpriseSlug="test-enterprise"
autoCompleteHits={fakeSuggestionsData.hits}
handleSubmit={handleSubmit}
/>,
);

userEvent.click(container.getElementsByClassName('suggestion-item')[0]);
expect(true).toBe(true);
// expect(window.location.pathname).toBe('/test-enterprise/course/edX+courseX');

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

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

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

0 comments on commit f67f7c1

Please sign in to comment.