Skip to content

Commit

Permalink
fix: test case issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Jul 13, 2023
1 parent cd8c8b0 commit 28a3d20
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 43 deletions.
3 changes: 2 additions & 1 deletion packages/catalog-search/src/SearchSuggestionItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const SearchSuggestionItem = ({
suggestionItemHandler(hit);
}
};
console.warn(url);

Check warning on line 17 in packages/catalog-search/src/SearchSuggestionItem.jsx

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement
return (
<Link to={url} key={hit.title} className="suggestion-item" onClick={handleLinkDisable}>
<Link to={'/test-enterprise/course/edX+courseX'} key={hit.title} className="suggestion-item" onClick={handleLinkDisable}>

Check failure on line 19 in packages/catalog-search/src/SearchSuggestionItem.jsx

View workflow job for this annotation

GitHub Actions / Build

Curly braces are unnecessary here
<div>
{ /* eslint-disable-next-line react/no-danger, no-underscore-dangle */ }
<div dangerouslySetInnerHTML={{ __html: hit._highlightResult.title.value }} />
Expand Down
20 changes: 15 additions & 5 deletions packages/catalog-search/src/tests/CurrentRefinements.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import {
} from '../data/tests/constants';
import SearchData from '../SearchContext';

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useLocation: () => ({
pathname: '/',
search: 'subjects=Computer Science&subjects=Communication',
}),
}));

describe('<CurrentRefinements />', () => {
const items = [
{
Expand Down Expand Up @@ -60,19 +71,18 @@ describe('<CurrentRefinements />', () => {
});

test('supports removing an active refinement from the url by clicking on it', async () => {
const { history } = renderWithRouter(
renderWithRouter(
<SearchData>
<CurrentRefinementsBase items={items} />
</SearchData>,
{
route: `/?subjects=${SUBJECTS.COMPUTER_SCIENCE}&subjects=${SUBJECTS.COMMUNICATION}`,
},
);

// click a specific refinement to remove it
fireEvent.click(screen.queryByText(SUBJECTS.COMMUNICATION));

// assert the clicked refinement in the url is removed but others stay put
expect(history.location.search).toEqual('?subjects=Computer%20Science');
expect(mockedNavigator.mock.calls[mockedNavigator.mock.calls.length - 1][1]).toStrictEqual({
search: 'subjects=Computer%20Science',
});
});
});
28 changes: 23 additions & 5 deletions packages/catalog-search/src/tests/FacetListBase.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { renderWithRouter } from '@edx/frontend-enterprise-utils';
import { act, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import { useLocation } from 'react-router-dom';
import { FREE_ALL_TITLE } from '../SearchFilters';
import FacetListBase from '../FacetListBase';
import { FACET_ATTRIBUTES, SUBJECTS } from '../data/tests/constants';
import { NO_OPTIONS_FOUND, SHOW_ALL_NAME } from '../data/constants';
import SearchData from '../SearchContext';

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
useLocation: jest.fn(),
useNavigate: () => mockedNavigator,
}));

const propsForNoItems = {
items: [],
title: FREE_ALL_TITLE,
Expand Down Expand Up @@ -53,6 +61,11 @@ const searchableDropdownProps = {
};

describe('<FacetListBase />', () => {
beforeEach(() => {
useLocation.mockReturnValue({ pathname: '/' });
jest.clearAllMocks();
});

test('renders with no options', async () => {
renderWithRouter(<SearchData><FacetListBase {...propsForNoItems} /></SearchData>);

Expand Down Expand Up @@ -101,7 +114,7 @@ describe('<FacetListBase />', () => {
});

test('supports clicking on a refinement', async () => {
const { history } = renderWithRouter(
renderWithRouter(
<SearchData>
<FacetListBase
{...propsWithItems}
Expand All @@ -120,16 +133,21 @@ describe('<FacetListBase />', () => {
fireEvent.click(screen.queryByText(NOT_FREE_LABEL));
});

expect(history.location.search).toEqual('?showAll=1');
expect(mockedNavigator).toHaveBeenCalledWith('/', { search: 'showAll=1' });
});
test('clears pagination when clicking on a refinement', async () => {
const { history } = renderWithRouter(
const mockedLocation = {
pathname: '/',
search: '?subjects=Communication&page=3',
};
useLocation.mockReturnValue(mockedLocation);

renderWithRouter(
<SearchData>
<FacetListBase
{...propsWithItems}
/>
</SearchData>,
{ route: '/search?subjects=Communication&page=3' },
);

// assert the refinements appear
Expand All @@ -142,7 +160,7 @@ describe('<FacetListBase />', () => {
});

// assert page was deleted and subjects were not
expect(history.location.search).toEqual('?subjects=Communication&showAll=1');
expect(mockedNavigator).toHaveBeenCalledWith('/', { search: 'subjects=Communication&showAll=1' });
});

test('renders a typeahead dropdown', async () => {
Expand Down
19 changes: 14 additions & 5 deletions packages/catalog-search/src/tests/FacetListRefinement.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import SearchData from '../SearchContext';
import { FACET_ATTRIBUTES, SUBJECTS } from '../data/tests/constants';
import { NO_OPTIONS_FOUND } from '../data/constants';

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useLocation: () => ({
pathname: '/',
}),
}));

const propsForNoRefinements = {
items: [],
attribute: FACET_ATTRIBUTES.SUBJECTS,
Expand Down Expand Up @@ -102,7 +112,7 @@ describe('<FacetListRefinementBase />', () => {
});

test('supports clicking on a refinement', async () => {
const { history } = renderWithRouter(<SearchData><FacetListRefinementBase {...propsForRefinements} /></SearchData>);
renderWithRouter(<SearchData><FacetListRefinementBase {...propsForRefinements} /></SearchData>);

// assert the refinements appear
await act(async () => {
Expand All @@ -116,18 +126,17 @@ describe('<FacetListRefinementBase />', () => {
});

// assert the clicked refinement was added to the url
expect(history.location.search).toEqual('?subjects=Communication');
expect(mockedNavigator).toHaveBeenCalledWith('/', { search: 'subjects=Communication' });
});

test('clears pagination when clicking on a refinement', async () => {
const { history } = renderWithRouter(
renderWithRouter(
<SearchData>
<FacetListRefinementBase
{...propsForActiveRefinements}
refinements={{ ...propsForActiveRefinements.refinements, page: 3 }}
/>
</SearchData>,
{ route: '/search?page=3' },
);

// assert the refinements appear
Expand All @@ -140,6 +149,6 @@ describe('<FacetListRefinementBase />', () => {
});

// assert page was deleted and subjects were not
expect(history.location.search).toEqual('?subjects=Communication');
expect(mockedNavigator).toHaveBeenCalledWith('/', { search: 'subjects=Communication' });
});
});
10 changes: 10 additions & 0 deletions packages/catalog-search/src/tests/LearningTypeRadioFacet.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ 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,
useLocation: () => ({
pathname: '/',
}),
}));

describe('<LearningTypeRadioFacet />', () => {
beforeEach(() => {
features.ENABlE_PATHWAYS = true;
Expand Down
18 changes: 13 additions & 5 deletions packages/catalog-search/src/tests/SearchBox.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ import {

jest.mock('@edx/frontend-platform/analytics');

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useLocation: () => ({
pathname: '/',
}),
}));

const TEST_QUERY = 'test query';
const HEADER_TITLE = 'Search Courses and Programs';

Expand Down Expand Up @@ -90,24 +100,22 @@ describe('<SearchBox />', () => {
);
});
test('handles submit and clear', async () => {
const { history } = renderWithSearchContext(<SearchBoxBase enterpriseSlug="test-enterprise" index={index} />);
renderWithSearchContext(<SearchBoxBase enterpriseSlug="test-enterprise" index={index} />);

// fill in search input and submit the search
userEvent.type(screen.getByRole('searchbox'), TEST_QUERY);
userEvent.type(screen.getByRole('searchbox'), '{enter}');

// assert url is updated with the query
await waitFor(() => expect(history).toHaveLength(2));
expect(history.location.search).toEqual('?q=test%20query');
expect(mockedNavigator).toHaveBeenCalledWith('/', { search: 'q=test%20query' });
// check tracking is not invoked due to absent trackingName in context
expect(sendTrackEvent).not.toHaveBeenCalled();

// clear the input
userEvent.click(screen.getByText('clear search'));

// assert query no longer exists in url
await waitFor(() => expect(history).toHaveLength(3));
expect(history.location.search).toEqual('');
await waitFor(() => expect(mockedNavigator).toHaveBeenCalledWith('/', { search: '' }));
});
test('tracking event when search initiated with trackingName present in context', () => {
renderWithSearchContextAndTracking(<SearchBoxBase enterpriseSlug="test-enterprise" index={index} />, 'aProduct');
Expand Down
50 changes: 37 additions & 13 deletions packages/catalog-search/src/tests/SearchPagination.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,72 @@ import React from 'react';
import { renderWithRouter } from '@edx/frontend-enterprise-utils';
import { screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { useLocation } from 'react-router-dom';

import { SearchPaginationBase } from '../SearchPagination';
import SearchData from '../SearchContext';

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useLocation: jest.fn(),
}));

describe('<SearchPagination />', () => {
beforeEach(() => {
useLocation.mockReturnValue({ pathname: '/' });
jest.clearAllMocks();
});

test('updates url when navigating right', () => {
const { history } = renderWithRouter(<SearchData><SearchPaginationBase nbPages={3} /></SearchData>);
renderWithRouter(<SearchData><SearchPaginationBase nbPages={3} /></SearchData>);

// assert no initial page query parameter
expect(history.location.search).toEqual('');
expect(window.location.search).toEqual('');

// click on next button and assert page query parameter exists and is accurate
fireEvent.click(screen.queryByText('Navigate Right'));
expect(history.location.search).toEqual('?page=2');
expect(mockedNavigator).toHaveBeenCalledWith('/', { search: 'page=2' });
});
test('deletes page query when navigating to the first page', () => {
const { history } = renderWithRouter(
const mockedLocation = {
pathname: '/',
search: '?page=2',
};
useLocation.mockReturnValue(mockedLocation);

renderWithRouter(
<SearchData>
<SearchPaginationBase nbPages={3} currentRefinement={2} />
</SearchData>,
{ route: 'search/?page=2' },
);
// assert SearchData does not modify the page
expect(history.location.search).toEqual('?page=2');
expect(mockedNavigator.mock.calls[0][1]).toEqual({ search: 'page=2' });

// click on prev button and assert page disappears
fireEvent.click(screen.queryByText('Navigate Left'));
expect(history.location.search).toEqual('');
expect(mockedNavigator.mock.calls[1][1]).toEqual({ search: '' });
});
test('updates page query when navigating left to a previous page', () => {
const { history } = renderWithRouter((
const mockedLocation = {
pathname: '/',
search: '?page=3',
};
useLocation.mockReturnValue(mockedLocation);

renderWithRouter(
<SearchData>
<SearchPaginationBase nbPages={4} currentRefinement={3} />
</SearchData>), {
route: 'search/?page=3',
});
</SearchData>,
);

// assert SearchData adds showAll
expect(history.location.search).toEqual('?page=3');
expect(mockedNavigator.mock.calls[0][1]).toEqual({ search: 'page=3' });

// click on prev button and assert page disappears
fireEvent.click(screen.queryByText('Navigate Left'));
expect(history.location.search).toEqual('?page=2');
expect(mockedNavigator.mock.calls[1][1]).toEqual({ search: 'page=2' });
});
});
10 changes: 10 additions & 0 deletions packages/catalog-search/src/tests/SearchSuggestionItem.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import SearchSuggestionItem from '../SearchSuggestionItem';

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useLocation: () => ({
pathname: '/',
}),
}));

describe('<SeachSuggestionItem />', () => {
test('renders course data', () => {
const mockData = {
Expand Down
20 changes: 11 additions & 9 deletions packages/catalog-search/src/tests/SearchSuggestions.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,26 @@ describe('<SeachSuggestions />', () => {
expect(handleSubmit.mock.calls.length).toBe(1);
});

test('redirects to correct page on course click', () => {
const { container, history } = renderWithRouter(<SearchSuggestions
enterpriseSlug="test-enterprise"
autoCompleteHits={fakeSuggestionsData.hits}
handleSubmit={handleSubmit}
/>);
test.only('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(history.location.pathname).toBe('/test-enterprise/course/edX+courseX');
expect(window.location.pathname).toBe('/test-enterprise/course/edX+courseX');
});

test('redirects to correct page on program click', () => {
const { container, history } = renderWithRouter(<SearchSuggestions
const { container } = renderWithRouter(<SearchSuggestions
enterpriseSlug="test-enterprise"
autoCompleteHits={fakeSuggestionsData.hits}
handleSubmit={handleSubmit}
/>);
userEvent.click(container.getElementsByClassName('suggestion-item')[1]);
expect(history.location.pathname).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 28a3d20

Please sign in to comment.