Skip to content

Commit

Permalink
test: add library and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Apr 16, 2024
1 parent 66612bc commit 58a5c8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/search-modal/SearchUI.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
fireEvent,
render,
waitFor,
within,
getByLabelText as getByLabelTextIn,
} from '@testing-library/react';
import fetchMock from 'fetch-mock-jest';
Expand Down Expand Up @@ -136,23 +137,25 @@ describe('<SearchUI />', () => {
// Now we should see the results:
expect(queryByText('Enter a keyword')).toBeNull();
// The result:
expect(getByText('1 result found')).toBeInTheDocument();
expect(getByText('2 results found')).toBeInTheDocument();
expect(getByText(mockResultDisplayName)).toBeInTheDocument();
// Breadcrumbs showing where the result came from:
expect(getByText('The Little Unit That Could')).toBeInTheDocument();

const resultItem = getByRole('button', { name: /The Little Unit That Could/ });

// Clicking the "Open in new window" button should open the result in a new window:
const { open } = window;
window.open = jest.fn();
fireEvent.click(getByRole('button', { name: 'Open in new window' }));
fireEvent.click(within(resultItem).getByRole('button', { name: 'Open in new window' }));
expect(window.open).toHaveBeenCalledWith(
'/course/course-v1:edx+TestCourse+24?show=block-v1%3Aedx%2BTestCourse%2B24%2Btype%40html%2Bblock%40test_html',
'_blank',
);
window.open = open;

// Clicking in the result should navigate to the result's URL:
fireEvent.click(getByRole('button', { name: /The Little Unit That Could/ }));
fireEvent.click(resultItem);
expect(mockNavigate).toHaveBeenCalledWith('/course/course-v1:edx+TestCourse+24?show=block-v1%3Aedx%2BTestCourse%2B24%2Btype%40html%2Bblock%40test_html');
});

Expand All @@ -176,7 +179,7 @@ describe('<SearchUI />', () => {
// Now we should see the results:
expect(queryByText('Enter a keyword')).toBeNull();
// The result:
expect(getByText('1 result found')).toBeInTheDocument();
expect(getByText('2 results found')).toBeInTheDocument();
expect(getByText(mockResultDisplayName)).toBeInTheDocument();
// Breadcrumbs showing where the result came from:
expect(getByText('The Little Unit That Could')).toBeInTheDocument();
Expand All @@ -201,7 +204,7 @@ describe('<SearchUI />', () => {
return (requestedFilter?.length === 1); // the filter is: 'context_key = "course-v1:org+test+123"'
});
// Now we should see the results:
expect(getByText('1 result found')).toBeInTheDocument();
expect(getByText('2 results found')).toBeInTheDocument();
expect(getByText(mockResultDisplayName)).toBeInTheDocument();
});

Expand Down
22 changes: 20 additions & 2 deletions src/search-modal/__mocks__/search-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,34 @@
"ESDC Skills and Competencies > Work Context > Physical Work Environment > Environmental Conditions > Biological Agents"
]
}
},
{
"display_name": "Text1",
"block_id": "a1fa8bdd-dc67-4976-9bf5-0ea75a9bca3d",
"content": {
"html_content": " Test "
},
"id": "lborg1libafter1htmla1fa8bdd-dc67-4976-9bf5-0ea75a9bca3d-8c5b26ce",
"usage_key": "lb:org1:libafter1:html:a1fa8bdd-dc67-4976-9bf5-0ea75a9bca3d",
"block_type": "html",
"context_key": "lib:org1:libafter1",
"org": "edx",
"breadcrumbs": [
{
"display_name": "libafter2"
}
],
"type": "library_block"
}
],
"query": "learn",
"processingTimeMs": 1,
"limit": 2,
"offset": 0,
"estimatedTotalHits": 1,
"estimatedTotalHits": 2,
"facetDistribution": {
"block_type": {
"html": 1,
"html": 2,
"problem": 16,
"vertical": 2,
"video": 1
Expand Down

0 comments on commit 58a5c8c

Please sign in to comment.