From 78dc0c4af2254ee2642baefd14926ff22ae68833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Wed, 24 Apr 2024 15:04:13 -0300 Subject: [PATCH] test: add lib content tests --- src/search-modal/SearchResult.jsx | 4 +-- src/search-modal/SearchUI.test.jsx | 36 ++++++++++++++++++- src/search-modal/__mocks__/search-result.json | 2 +- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/search-modal/SearchResult.jsx b/src/search-modal/SearchResult.jsx index 620608b8be..7fb4cb2599 100644 --- a/src/search-modal/SearchResult.jsx +++ b/src/search-modal/SearchResult.jsx @@ -41,7 +41,7 @@ function getItemIcon(blockType) { */ function getLibraryHitUrl(hit, libraryAuthoringMfeUrl) { const { contextKey } = hit; - return `${libraryAuthoringMfeUrl}library/${contextKey}`; + return `${libraryAuthoringMfeUrl}/library/${contextKey}`; } /** @@ -218,7 +218,7 @@ const SearchResult = ({ hit }) => { diff --git a/src/search-modal/SearchUI.test.jsx b/src/search-modal/SearchUI.test.jsx index 4fa79346f4..1486643fec 100644 --- a/src/search-modal/SearchUI.test.jsx +++ b/src/search-modal/SearchUI.test.jsx @@ -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'; @@ -89,6 +93,7 @@ describe('', () => { 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() ?? ''); @@ -185,7 +190,13 @@ describe('', () => { 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(); const { getByRole } = rendered; fireEvent.change(getByRole('searchbox'), { target: { value: 'giraffe' } }); @@ -284,6 +295,29 @@ describe('', () => { + '?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', () => { diff --git a/src/search-modal/__mocks__/search-result.json b/src/search-modal/__mocks__/search-result.json index 6a4f184d71..5371f3d5c7 100644 --- a/src/search-modal/__mocks__/search-result.json +++ b/src/search-modal/__mocks__/search-result.json @@ -58,7 +58,7 @@ } }, { - "display_name": "Text1", + "display_name": "Library Content", "block_id": "a1fa8bdd-dc67-4976-9bf5-0ea75a9bca3d", "content": { "html_content": " Test "