Skip to content

Commit

Permalink
test: add lib content tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Apr 24, 2024
1 parent 949b3c6 commit 78dc0c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/search-modal/SearchResult.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function getItemIcon(blockType) {
*/
function getLibraryHitUrl(hit, libraryAuthoringMfeUrl) {
const { contextKey } = hit;
return `${libraryAuthoringMfeUrl}library/${contextKey}`;
return `${libraryAuthoringMfeUrl}/library/${contextKey}`;
}

/**
Expand Down Expand Up @@ -218,7 +218,7 @@ const SearchResult = ({ hit }) => {
<IconButton
src={OpenInNew}
iconAs={Icon}
disabled={noRedirectUrl}
disabled={noRedirectUrl ? true : undefined}
onClick={openContextInNewWindow}
alt={intl.formatMessage(messages.openInNewWindow)}
/>
Expand Down
36 changes: 35 additions & 1 deletion src/search-modal/SearchUI.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
import fetchMock from 'fetch-mock-jest';

import initializeStore from '../store';
import { executeThunk } from '../utils';
import { getStudioHomeApiUrl } from '../studio-home/data/api';
import { fetchStudioHomeData } from '../studio-home/data/thunks';
import { generateGetStudioHomeDataApiResponse } from '../studio-home/factories/mockApiResponses';
import mockResult from './__mocks__/search-result.json';
import mockEmptyResult from './__mocks__/empty-search-result.json';
import mockTagsFacetResult from './__mocks__/facet-search.json';
Expand Down Expand Up @@ -89,6 +93,7 @@ describe('<SearchUI />', () => {
index_name: 'studio',
api_key: 'test-key',
});

// The Meilisearch client-side API uses fetch, not Axios.
fetchMock.post(searchEndpoint, (_url, req) => {
const requestData = JSON.parse(req.body?.toString() ?? '');
Expand Down Expand Up @@ -185,7 +190,13 @@ describe('<SearchUI />', () => {
describe('results', () => {
/** @type {import('@testing-library/react').RenderResult} */
let rendered;
beforeEach(() => {
beforeEach(async () => {
const data = generateGetStudioHomeDataApiResponse();
data.redirectToLibraryAuthoringMfe = true;
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, data);

await executeThunk(fetchStudioHomeData(), store.dispatch);

rendered = render(<Wrap><SearchUI {...defaults} /></Wrap>);
const { getByRole } = rendered;
fireEvent.change(getByRole('searchbox'), { target: { value: 'giraffe' } });
Expand Down Expand Up @@ -284,6 +295,29 @@ describe('<SearchUI />', () => {
+ '?show=block-v1%3ASampleTaxonomyOrg1%2BSTC1%2B2023_1%2Btype%40html%2Bblock%400b2d1c0722f742489602b6d8645205f4',
);
});

test('click lib component result navigates to the context', async () => {
const { findByRole } = rendered;

const resultItem = await findByRole('button', { name: /Library Content/ });

// Clicking the "Open in new window" button should open the result in a new window:
const { open, location } = window;
window.open = jest.fn();
fireEvent.click(within(resultItem).getByRole('button', { name: 'Open in new window' }));
expect(window.open).toHaveBeenCalledWith(
'http://localhost:3001/library/lib:org1:libafter1',
'_blank',
);
window.open = open;

// @ts-ignore
window.location = { href: '' };
// Clicking in the result should navigate to the result's URL:
fireEvent.click(resultItem);
expect(window.location.href = 'http://localhost:3001/library/lib:org1:libafter1');
window.location = location;
});
});

describe('filters', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/search-modal/__mocks__/search-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}
},
{
"display_name": "Text1",
"display_name": "Library Content",
"block_id": "a1fa8bdd-dc67-4976-9bf5-0ea75a9bca3d",
"content": {
"html_content": " Test "
Expand Down

0 comments on commit 78dc0c4

Please sign in to comment.