-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added prequery event handler to search (#373)
* feat: added prequery event handler to search
- Loading branch information
1 parent
b549a6f
commit 5705327
Showing
6 changed files
with
120 additions
and
51 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/catalog-search/src/PrequerySearchSuggestionItem.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react'; | ||
import { Image } from '@openedx/paragon'; | ||
import { Link } from 'react-router-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const PrequerySearchSuggestionItem = ({ | ||
url, hit, optimizelyPrequerySuggestionClickHandler, | ||
}) => ( | ||
<Link | ||
to={url} | ||
key={hit.title} | ||
className="prequery-item pr-4 d-flex flex-column" | ||
onClick={() => optimizelyPrequerySuggestionClickHandler(hit.key)} | ||
> | ||
<div className="d-flex align-items-center justify-content-start"> | ||
<Image className="prequery-image mr-2" src={hit.card_image_url} /> | ||
<div className="d-flex flex-column"> | ||
{/* eslint-disable-next-line react/no-danger, no-underscore-dangle */} | ||
<div dangerouslySetInnerHTML={{ __html: hit._highlightResult.title.value }} /> | ||
<div className="x-small d-flex"> | ||
<span> | ||
{hit.partners[0]?.name} | ||
</span> | ||
<span> | </span> | ||
<span className="text-capitalize">{hit.learning_type}</span> | ||
</div> | ||
</div> | ||
</div> | ||
</Link> | ||
); | ||
|
||
PrequerySearchSuggestionItem.propTypes = { | ||
url: PropTypes.string.isRequired, | ||
hit: PropTypes.shape({ | ||
key: PropTypes.string, | ||
title: PropTypes.string, | ||
_highlightResult: PropTypes.shape({ title: PropTypes.shape({ value: PropTypes.string }) }), | ||
card_image_url: PropTypes.string, | ||
partners: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
name: PropTypes.string, | ||
}), | ||
), | ||
learning_type: PropTypes.string, | ||
}).isRequired, | ||
optimizelyPrequerySuggestionClickHandler: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default PrequerySearchSuggestionItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/catalog-search/src/tests/PrequerySearchSuggestionItem.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 '@testing-library/jest-dom/extend-expect'; | ||
import PrequerySearchSuggestionItem from '../PrequerySearchSuggestionItem'; | ||
|
||
describe('<PrequerySearchSuggestionItem />', () => { | ||
test('sends optimizely event on prequery click', () => { | ||
const mockData = { | ||
url: '/test-enterprise/course/edX+courseX', | ||
optimizelyPrequerySuggestionClickHandler: jest.fn(), | ||
hit: { | ||
partners: [{ | ||
name: 'edX', | ||
}], | ||
learning_type: 'course', | ||
content_type: 'course', | ||
card_image_url: 'url.com', | ||
key: 'edX+courseX', | ||
title: 'basket weaving', | ||
_highlightResult: { title: { value: 'basket weaving' } }, | ||
}, | ||
}; | ||
|
||
renderWithRouter(<PrequerySearchSuggestionItem | ||
url={mockData.url} | ||
optimizelyPrequerySuggestionClickHandler={mockData.optimizelyPrequerySuggestionClickHandler} | ||
hit={mockData.hit} | ||
/>); | ||
expect(screen.getByRole('link', { name: 'basket weaving edX | course' })) | ||
.toHaveAttribute('href', '/test-enterprise/course/edX+courseX'); | ||
userEvent.click(screen.getByText('basket weaving')); | ||
expect(mockData.optimizelyPrequerySuggestionClickHandler.mock.calls.length).toBe(1); | ||
}); | ||
}); |