Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Apr 24, 2024
1 parent 1bbca0a commit 64c630e
Show file tree
Hide file tree
Showing 2 changed files with 302 additions and 24 deletions.
134 changes: 112 additions & 22 deletions src/search-modal/SearchUI.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
waitFor,
within,
getByLabelText as getByLabelTextIn,
screen,
} from '@testing-library/react';
import fetchMock from 'fetch-mock-jest';

Expand Down Expand Up @@ -150,30 +151,11 @@ describe('<SearchUI />', () => {
// Now we should see the results:
expect(queryByText('Enter a keyword')).toBeNull();
// The result:
expect(getByText('2 results found')).toBeInTheDocument();
expect(getByText('6 results found')).toBeInTheDocument();
expect(getByText(mockResultDisplayName)).toBeInTheDocument();
// Breadcrumbs showing where the result came from:
expect(getByText('TheCourse / Section 2 / Subsection 3 / The Little Unit That Could')).toBeInTheDocument();

Check failure on line 157 in src/search-modal/SearchUI.test.jsx

View workflow job for this annotation

GitHub Actions / tests

Block must not be padded by blank lines

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(within(resultItem).getByRole('button', { name: 'Open in new window' }));
expect(window.open).toHaveBeenCalledWith(
'/course/course-v1:edx+TestCourse+24/container/block-v1:edx+TestCourse+24+type@vertical+block@vertical_3_1'
+ '?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(resultItem);
expect(mockNavigate).toHaveBeenCalledWith(
'/course/course-v1:edx+TestCourse+24/container/block-v1:edx+TestCourse+24+type@vertical+block@vertical_3_1'
+ '?show=block-v1%3Aedx%2BTestCourse%2B24%2Btype%40html%2Bblock%40test_html',
);
});

it('defaults to searching "This Course" if used in a course', async () => {
Expand All @@ -196,12 +178,120 @@ describe('<SearchUI />', () => {
// Now we should see the results:
expect(queryByText('Enter a keyword')).toBeNull();
// The result:
expect(getByText('2 results found')).toBeInTheDocument();
expect(getByText('6 results found')).toBeInTheDocument();
expect(getByText(mockResultDisplayName)).toBeInTheDocument();
// Breadcrumbs showing where the result came from:
expect(getByText('TheCourse / Section 2 / Subsection 3 / The Little Unit That Could')).toBeInTheDocument();
});

describe('results', () => {
/** @type {import('@testing-library/react').RenderResult} */
let rendered;
beforeEach(() => {
rendered = render(<Wrap><SearchUI {...defaults} /></Wrap>);
const { getByRole } = rendered;
fireEvent.change(getByRole('searchbox'), { target: { value: 'giraffe' } });
});

test('click section result navigates to the context', async () => {
const { findAllByRole } = rendered;

screen.logTestingPlaygroundURL();
const [resultItem] = await findAllByRole('button', { name: /Section 1/ });

// Clicking the "Open in new window" button should open the result in a new window:
const { open } = window;
window.open = jest.fn();
fireEvent.click(within(resultItem).getByRole('button', { name: 'Open in new window' }));
expect(window.open).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1'
+ '?show=block-v1%3ASampleTaxonomyOrg1%2BSTC1%2B2023_1%2Btype%40chapter%2Bblock%40c7077c8cafcf420dbc0b440bf27bad04',
'_blank',
);
window.open = open;

// Clicking in the result should navigate to the result's URL:
fireEvent.click(resultItem);
expect(mockNavigate).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1'
+ '?show=block-v1%3ASampleTaxonomyOrg1%2BSTC1%2B2023_1%2Btype%40chapter%2Bblock%40c7077c8cafcf420dbc0b440bf27bad04',
);
});

test('click subsection result navigates to the context', async () => {
const { findAllByRole } = rendered;

screen.logTestingPlaygroundURL();
const [resultItem] = await findAllByRole('button', { name: /Subsection 1.1/ });

// Clicking the "Open in new window" button should open the result in a new window:
const { open } = window;
window.open = jest.fn();
fireEvent.click(within(resultItem).getByRole('button', { name: 'Open in new window' }));
expect(window.open).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1'
+ '?show=block-v1%3ASampleTaxonomyOrg1%2BSTC1%2B2023_1%2Btype%40sequential%2Bblock%4092e3e9ca156c44fa8a735f0e9e7c854f',
'_blank',
);
window.open = open;

// Clicking in the result should navigate to the result's URL:
fireEvent.click(resultItem);
expect(mockNavigate).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1'
+ '?show=block-v1%3ASampleTaxonomyOrg1%2BSTC1%2B2023_1%2Btype%40sequential%2Bblock%4092e3e9ca156c44fa8a735f0e9e7c854f',
);
});

test('click unit result navigates to the context', async () => {
const { findAllByRole } = rendered;

screen.logTestingPlaygroundURL();
const [resultItem] = await findAllByRole('button', { name: /Unit 1.1.1/ });

// Clicking the "Open in new window" button should open the result in a new window:
const { open } = window;
window.open = jest.fn();
fireEvent.click(within(resultItem).getByRole('button', { name: 'Open in new window' }));
expect(window.open).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1/container/block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b',
'_blank',
);
window.open = open;

// Clicking in the result should navigate to the result's URL:
fireEvent.click(resultItem);
expect(mockNavigate).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1/container/block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b',
);
});

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

screen.logTestingPlaygroundURL();
const [resultItem] = await findAllByRole('button', { name: /Announcement/ });

// Clicking the "Open in new window" button should open the result in a new window:
const { open } = window;
window.open = jest.fn();
fireEvent.click(within(resultItem).getByRole('button', { name: 'Open in new window' }));
expect(window.open).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1/container/block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b'
+ '?show=block-v1%3ASampleTaxonomyOrg1%2BSTC1%2B2023_1%2Btype%40html%2Bblock%400b2d1c0722f742489602b6d8645205f4',
'_blank',
);
window.open = open;

// Clicking in the result should navigate to the result's URL:
fireEvent.click(resultItem);
expect(mockNavigate).toHaveBeenCalledWith(
'/course/course-v1:SampleTaxonomyOrg1+STC1+2023_1/container/block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b'
+ '?show=block-v1%3ASampleTaxonomyOrg1%2BSTC1%2B2023_1%2Btype%40html%2Bblock%400b2d1c0722f742489602b6d8645205f4',
);
});
});

describe('filters', () => {
/** @type {import('@testing-library/react').RenderResult} */
let rendered;
Expand Down Expand Up @@ -231,7 +321,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('2 results found')).toBeInTheDocument();
expect(getByText('6 results found')).toBeInTheDocument();
expect(getByText(mockResultDisplayName)).toBeInTheDocument();
});

Expand Down
192 changes: 190 additions & 2 deletions src/search-modal/__mocks__/search-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,201 @@
}
],
"type": "library_block"
},
{
"display_name": "Section 1",
"block_id": "c7077c8cafcf420dbc0b440bf27bad04",
"content": {},
"id": "block-v1sampletaxonomyorg1stc12023_1typechapterblockc7077c8cafcf420dbc0b440bf27bad04-2af9d1ac",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04",
"block_type": "chapter",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": 6,
"_formatted": {
"display_name": "Section 1",
"block_id": "c7077c8cafcf420dbc0b440bf27bad04",
"content": {},
"id": "block-v1sampletaxonomyorg1stc12023_1typechapterblockc7077c8cafcf420dbc0b440bf27bad04-2af9d1ac",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04",
"block_type": "chapter",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": "6"
}
},
{
"display_name": "Subsection 1.1",
"block_id": "92e3e9ca156c44fa8a735f0e9e7c854f",
"content": {},
"id": "block-v1sampletaxonomyorg1stc12023_1typesequentialblock92e3e9ca156c44fa8a735f0e9e7c854f-ec0fb128",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
},
{
"display_name": "Section 1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@sequential+block@92e3e9ca156c44fa8a735f0e9e7c854f",
"block_type": "sequential",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": 6,
"_formatted": {
"display_name": "Subsection 1.1",
"block_id": "92e3e9ca156c44fa8a735f0e9e7c854f",
"content": {},
"id": "block-v1sampletaxonomyorg1stc12023_1typesequentialblock92e3e9ca156c44fa8a735f0e9e7c854f-ec0fb128",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
},
{
"display_name": "Section 1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@sequential+block@92e3e9ca156c44fa8a735f0e9e7c854f",
"block_type": "sequential",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": "6"
}
},
{
"display_name": "Unit 1.1.1",
"block_id": "aaf8b8eb86b54281aeeab12499d2cb0b",
"content": {},
"id": "block-v1sampletaxonomyorg1stc12023_1typeverticalblockaaf8b8eb86b54281aeeab12499d2cb0b-afa27c6e",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
},
{
"display_name": "Section 1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04"
},
{
"display_name": "Subsection 1.1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@sequential+block@92e3e9ca156c44fa8a735f0e9e7c854f"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b",
"block_type": "vertical",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": 6,
"_formatted": {
"display_name": "Unit 1.1.1",
"block_id": "aaf8b8eb86b54281aeeab12499d2cb0b",
"content": {},
"id": "block-v1sampletaxonomyorg1stc12023_1typeverticalblockaaf8b8eb86b54281aeeab12499d2cb0b-afa27c6e",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
},
{
"display_name": "Section 1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04"
},
{
"display_name": "Subsection 1.1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@sequential+block@92e3e9ca156c44fa8a735f0e9e7c854f"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b",
"block_type": "vertical",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": "6"
}
},
{
"display_name": "Announcement",
"block_id": "0b2d1c0722f742489602b6d8645205f4",
"content": {
"html_content": "To use this template, replace the example text with your own text. When you add the component, be sure to select Settings to specify a Display Name and other values that apply. Announcement Date Short note that introduces the topic Instructor's name Heading for announcement 1 Announcement 1 text Heading for announcement 2 Announcement 2 text "
},
"id": "block-v1sampletaxonomyorg1stc12023_1typehtmlblock0b2d1c0722f742489602b6d8645205f4-2db56dce",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
},
{
"display_name": "Section 1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04"
},
{
"display_name": "Subsection 1.1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@sequential+block@92e3e9ca156c44fa8a735f0e9e7c854f"
},
{
"display_name": "Unit 1.1.1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@html+block@0b2d1c0722f742489602b6d8645205f4",
"block_type": "html",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": 6,
"_formatted": {
"display_name": "Announcement",
"block_id": "0b2d1c0722f742489602b6d8645205f4",
"content": {
"html_content": "To use this template, replace the example text with your own text. When you add the component, be sure to…"
},
"id": "block-v1sampletaxonomyorg1stc12023_1typehtmlblock0b2d1c0722f742489602b6d8645205f4-2db56dce",
"type": "course_block",
"breadcrumbs": [
{
"display_name": "Sample Taxonomy Course"
},
{
"display_name": "Section 1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@chapter+block@c7077c8cafcf420dbc0b440bf27bad04"
},
{
"display_name": "Subsection 1.1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@sequential+block@92e3e9ca156c44fa8a735f0e9e7c854f"
},
{
"display_name": "Unit 1.1.1",
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@vertical+block@aaf8b8eb86b54281aeeab12499d2cb0b"
}
],
"usage_key": "block-v1:SampleTaxonomyOrg1+STC1+2023_1+type@html+block@0b2d1c0722f742489602b6d8645205f4",
"block_type": "html",
"context_key": "course-v1:SampleTaxonomyOrg1+STC1+2023_1",
"org": "SampleTaxonomyOrg1",
"access_id": "6"
}
}
],
"query": "learn",
"processingTimeMs": 1,
"limit": 2,
"limit": 6,
"offset": 0,
"estimatedTotalHits": 2
"estimatedTotalHits": 6
},
{
"indexUid": "studio",
Expand Down

0 comments on commit 64c630e

Please sign in to comment.