Skip to content

Commit c1d2d1a

Browse files
committed
test: search and migration filter
1 parent 73998ab commit c1d2d1a

File tree

3 files changed

+85
-38
lines changed

3 files changed

+85
-38
lines changed

src/studio-home/factories/mockApiResponses.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ export const generateGetStudioHomeLibrariesApiResponse = () => ({
103103
migratedToCollectionKey: 'imported-content',
104104
migratedToCollectionTitle: 'Imported content',
105105
},
106+
{
107+
displayName: 'MBA 1',
108+
libraryKey: 'library-v1:MBA+1234',
109+
url: '/library/library-v1:MBA+1234',
110+
org: 'Cambridge',
111+
number: '1234',
112+
canEdit: true,
113+
isMigrated: false,
114+
},
106115
],
107116
});
108117

src/studio-home/tabs-section/TabsSection.test.tsx

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
fireEvent,
1313
screen,
1414
act,
15+
within,
1516
} from '@src/testUtils';
1617
import messages from '../messages';
1718
import tabMessages from './messages';
@@ -269,7 +270,7 @@ describe('<TabsSection />', () => {
269270
beforeEach(async () => {
270271
await axiosMock.onGet(courseApiLinkV2).reply(200, generateGetStudioCoursesApiResponseV2());
271272
});
272-
it('should switch to Legacy Libraries tab and render specific v1 library details', async () => {
273+
it('should switch to Legacy Libraries tab and render - search and filter should work as expected', async () => {
273274
await axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
274275
await axiosMock.onGet(libraryApiLink).reply(200, generateGetStudioHomeLibrariesApiResponse());
275276
render();
@@ -280,12 +281,57 @@ describe('<TabsSection />', () => {
280281
await user.click(librariesTab);
281282

282283
expect(librariesTab).toHaveClass('active');
284+
const panel = await screen.findByRole('tabpanel', { hidden: false });
283285

284286
expect(await screen.findByText(studioHomeMock.libraries[0].displayName)).toBeVisible();
285287

286288
expect(
287289
await screen.findByText(`${studioHomeMock.libraries[0].org} / ${studioHomeMock.libraries[0].number}`),
288290
).toBeVisible();
291+
292+
// Migration info should be displayed
293+
const migratedContent = generateGetStudioHomeLibrariesApiResponse().libraries[1];
294+
expect(await screen.findByText(migratedContent.displayName)).toBeVisible();
295+
const newTitleElement = await screen.findAllByText(migratedContent.migratedToTitle!);
296+
expect(newTitleElement[0]).toBeVisible();
297+
expect(newTitleElement[0]).toHaveAttribute('href', `/library/${migratedContent.migratedToKey}`);
298+
expect(newTitleElement[1]).toHaveAttribute(
299+
'href',
300+
`/library/${migratedContent.migratedToKey}/collection/${migratedContent.migratedToCollectionKey}`,
301+
);
302+
303+
// Check total count display
304+
expect(await within(panel).findByText('Showing 3 of 3')).toBeInTheDocument();
305+
306+
// Test search functionality
307+
const searchField = await within(panel).findByPlaceholderText('Search');
308+
309+
fireEvent.change(searchField, { target: { value: 'Legacy' } });
310+
// Should only show 1 result i.e. migratedContent.displayName
311+
expect(await within(panel).findByText('Showing 1 of 3')).toBeInTheDocument();
312+
expect(await within(panel).findByText(migratedContent.displayName)).toBeVisible();
313+
// Should not show other items.
314+
expect(within(panel).queryByText(
315+
generateGetStudioHomeLibrariesApiResponse().libraries[0].displayName,
316+
)).not.toBeInTheDocument();
317+
// reset search
318+
fireEvent.change(searchField, { target: { value: '' } });
319+
320+
// Test migration filter
321+
const filter = await within(panel).findByRole('button', { name: 'Any Migration Status' });
322+
await user.click(filter);
323+
const migratedOption = await within(panel).findByRole('checkbox', { name: 'Migrated' });
324+
// This should uncheck Migrated option as all options are selected by default
325+
await user.click(migratedOption);
326+
// Should only show 2 result i.e. unmigrated libraries
327+
expect(await within(panel).findByText('Showing 2 of 3')).toBeInTheDocument();
328+
const unmigratedOption = await within(panel).findByRole('checkbox', { name: 'Unmigrated' });
329+
// Un-checking both options should reset the state to both checked.
330+
await user.click(unmigratedOption);
331+
expect(migratedOption).toBeChecked();
332+
expect(unmigratedOption).toBeChecked();
333+
// Should only show all 3 results
334+
expect(await within(panel).findByText('Showing 3 of 3')).toBeInTheDocument();
289335
});
290336

291337
it('should switch to Libraries tab and render specific v2 library details', async () => {
@@ -331,17 +377,6 @@ describe('<TabsSection />', () => {
331377
expect(
332378
await screen.findByText(`${studioHomeMock.libraries[0].org} / ${studioHomeMock.libraries[0].number}`),
333379
).toBeVisible();
334-
335-
// Migration info should be displayed
336-
const migratedContent = generateGetStudioHomeLibrariesApiResponse().libraries[1];
337-
expect(await screen.findByText(migratedContent.displayName)).toBeVisible();
338-
const newTitleElement = await screen.findAllByText(migratedContent.migratedToTitle!);
339-
expect(newTitleElement[0]).toBeVisible();
340-
expect(newTitleElement[0]).toHaveAttribute('href', `/library/${migratedContent.migratedToKey}`);
341-
expect(newTitleElement[1]).toHaveAttribute(
342-
'href',
343-
`/library/${migratedContent.migratedToKey}/collection/${migratedContent.migratedToCollectionKey}`,
344-
);
345380
});
346381

347382
it('should switch to Libraries tab and render specific v2 library details ("v2 only" mode)', async () => {
@@ -402,10 +437,11 @@ describe('<TabsSection />', () => {
402437
await axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
403438
await axiosMock.onGet(libraryApiLink).reply(404);
404439
render();
440+
const user = userEvent.setup();
405441
await executeThunk(fetchStudioHomeData(), store.dispatch);
406442

407443
const librariesTab = await screen.findByText(tabMessages.legacyLibrariesTabTitle.defaultMessage);
408-
fireEvent.click(librariesTab);
444+
await user.click(librariesTab);
409445

410446
expect(librariesTab).toHaveClass('active');
411447

src/studio-home/tabs-section/libraries-tab/index.tsx

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SearchFilterWidget from '../../../search-manager/SearchFilterWidget';
1515

1616
function findInValues<T extends {}>(arr: T[] | undefined, value: string) {
1717
return arr?.filter(o => Object.entries(o).some(entry => String(entry[1]).toLowerCase().includes(
18-
String(value).toLowerCase().trim()
18+
String(value).toLowerCase().trim(),
1919
)));
2020
}
2121

@@ -76,7 +76,7 @@ const MigrationFilter = ({ filters, setFilters }: MigrationFilterProps) => {
7676
<SearchFilterWidget
7777
appliedFilters={appliedFilters}
7878
label={label}
79-
clearFilter={() => setFilters(BaseFilterState)} // On clear select both migrated and unmigrated options.
79+
clearFilter={() => setFilters(BaseFilterState)} // On clear select both migrated and unmigrated options.
8080
icon={FilterList}
8181
skipLabelUpdate
8282
>
@@ -119,15 +119,17 @@ const LibrariesTab = () => {
119119
}
120120

121121
if (isError) {
122-
<AlertMessage
123-
variant="danger"
124-
description={(
125-
<Row className="m-0 align-items-center">
126-
<Icon src={Error} className="text-danger-500 mr-1" />
127-
<span>{intl.formatMessage(messages.librariesTabErrorMessage)}</span>
128-
</Row>
129-
)}
130-
/>
122+
return (
123+
<AlertMessage
124+
variant="danger"
125+
description={(
126+
<Row className="m-0 align-items-center">
127+
<Icon src={Error} className="text-danger-500 mr-1" />
128+
<span>{intl.formatMessage(messages.librariesTabErrorMessage)}</span>
129+
</Row>
130+
)}
131+
/>
132+
);
131133
}
132134

133135
return (
@@ -157,19 +159,19 @@ const LibrariesTab = () => {
157159
{currentPageData?.map(({
158160
displayName, org, number, url, isMigrated, migratedToKey, migratedToTitle, migratedToCollectionKey,
159161
}) => (
160-
<CardItem
161-
key={`${org}+${number}`}
162-
isLibraries
163-
displayName={displayName}
164-
org={org}
165-
number={number}
166-
url={url}
167-
isMigrated={isMigrated}
168-
migratedToKey={migratedToKey}
169-
migratedToTitle={migratedToTitle}
170-
migratedToCollectionKey={migratedToCollectionKey}
171-
/>
172-
))}
162+
<CardItem
163+
key={`${org}+${number}`}
164+
isLibraries
165+
displayName={displayName}
166+
org={org}
167+
number={number}
168+
url={url}
169+
isMigrated={isMigrated}
170+
migratedToKey={migratedToKey}
171+
migratedToTitle={migratedToTitle}
172+
migratedToCollectionKey={migratedToCollectionKey}
173+
/>
174+
))}
173175
{
174176
totalPages > 1
175177
&& (
@@ -184,7 +186,7 @@ const LibrariesTab = () => {
184186
}
185187
</div>
186188
</>
187-
)
189+
);
188190
};
189191

190192
export default LibrariesTab;

0 commit comments

Comments
 (0)