Skip to content

Commit e19af46

Browse files
test(content-picker): navigate into subfolder before selection checks
Co-Authored-By: [email protected] <[email protected]>
1 parent e8adc20 commit e19af46

File tree

1 file changed

+110
-65
lines changed

1 file changed

+110
-65
lines changed

src/elements/content-picker/stories/tests/ContentPicker-visual.stories.js

Lines changed: 110 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { http, HttpResponse } from 'msw';
44

55
import ContentPicker from '../../ContentPicker';
66
import { mockRootFolder, mockEmptyRootFolder } from '../../../content-explorer/stories/__mocks__/mockRootFolder';
7+
import mockSubFolder from '../../../content-explorer/stories/__mocks__/mockSubFolder';
78
import { DEFAULT_HOSTNAME_API } from '../../../../constants';
89

910
export const basic = {
@@ -116,7 +117,7 @@ export const hitSelectionLimit = {
116117

117118
// Wait for folder contents to load
118119
await waitFor(() => {
119-
expect(canvas.getByRole('link', { name: /All Files/i })).toBeInTheDocument();
120+
expect(canvas.getByText('Preview Test Folder')).toBeInTheDocument();
120121
const items = canvas.getAllByRole('row');
121122
expect(items.length).toBeGreaterThan(1);
122123
});
@@ -178,14 +179,8 @@ export const cancelUnselectsItems = {
178179
play: async ({ canvasElement }) => {
179180
const canvas = within(canvasElement);
180181

181-
// Wait for initial loading state
182-
await waitFor(() => {
183-
expect(canvas.getByRole('progressbar')).toBeInTheDocument();
184-
});
185-
186182
// Wait for items to load
187183
await waitFor(() => {
188-
expect(canvas.queryByRole('progressbar')).not.toBeInTheDocument();
189184
const items = canvas.getAllByRole('row');
190185
expect(items.length).toBeGreaterThan(1); // Header row + at least one item
191186
});
@@ -196,7 +191,7 @@ export const cancelUnselectsItems = {
196191

197192
// Wait for folder contents to load
198193
await waitFor(() => {
199-
expect(canvas.getByRole('link', { name: /All Files/i })).toBeInTheDocument();
194+
expect(canvas.getByText('Preview Test Folder')).toBeInTheDocument();
200195
const items = canvas.getAllByRole('row');
201196
expect(items.length).toBeGreaterThan(1);
202197
});
@@ -267,7 +262,7 @@ export const singleSelectWithItems = {
267262

268263
// Wait for folder contents to load
269264
await waitFor(() => {
270-
expect(canvas.getByRole('link', { name: /All Files/i })).toBeInTheDocument();
265+
expect(canvas.getByText('Preview Test Folder')).toBeInTheDocument();
271266
const items = canvas.getAllByRole('row');
272267
expect(items.length).toBeGreaterThan(1);
273268
});
@@ -336,14 +331,8 @@ export const keyboardShortcuts = {
336331
play: async ({ canvasElement }) => {
337332
const canvas = within(canvasElement);
338333

339-
// Wait for initial loading state
340-
await waitFor(() => {
341-
expect(canvas.getByRole('progressbar')).toBeInTheDocument();
342-
});
343-
344334
// Wait for items to load
345335
await waitFor(() => {
346-
expect(canvas.queryByRole('progressbar')).not.toBeInTheDocument();
347336
const items = canvas.getAllByRole('row');
348337
expect(items.length).toBeGreaterThan(1);
349338
});
@@ -354,7 +343,7 @@ export const keyboardShortcuts = {
354343

355344
// Wait for folder contents to load
356345
await waitFor(() => {
357-
expect(canvas.getByRole('link', { name: /All Files/i })).toBeInTheDocument();
346+
expect(canvas.getByText('Preview Test Folder')).toBeInTheDocument();
358347
const items = canvas.getAllByRole('row');
359348
expect(items.length).toBeGreaterThan(1);
360349
});
@@ -504,10 +493,9 @@ export const multiSelectWithKeyboard = {
504493
play: async ({ canvasElement }) => {
505494
const canvas = within(canvasElement);
506495

507-
// Wait for items to load and verify initial state
496+
// Wait for initial load and verify state
508497
await waitFor(() => {
509-
const items = canvas.getAllByRole('row');
510-
expect(items.length).toBeGreaterThan(1);
498+
expect(canvas.getByText('An Ordered Folder')).toBeInTheDocument();
511499
expect(canvas.getByRole('button', { name: /0 Selected/i })).toBeInTheDocument();
512500
expect(canvas.getByLabelText('Choose')).toBeDisabled();
513501
});
@@ -516,66 +504,120 @@ export const multiSelectWithKeyboard = {
516504
const orderedFolder = await canvas.findByText('An Ordered Folder');
517505
await userEvent.dblClick(orderedFolder);
518506

507+
// Wait for folder contents to load
508+
await waitFor(
509+
() => {
510+
const audioRow = canvas.getByRole('row', { name: /Audio\.mp3/ });
511+
expect(audioRow).toBeInTheDocument();
512+
},
513+
{
514+
timeout: 3000,
515+
interval: 500,
516+
},
517+
);
518+
519519
// Wait for folder contents to load
520520
await waitFor(() => {
521-
expect(canvas.getByRole('link', { name: /All Files/i })).toBeInTheDocument();
522-
const items = canvas.getAllByRole('row');
523-
expect(items.length).toBeGreaterThan(1);
521+
expect(canvas.getByRole('button', { name: /Preview Test Folder/i })).toBeInTheDocument();
522+
const rows = canvas.getAllByRole('row');
523+
expect(rows.length).toBeGreaterThan(1);
524+
expect(rows[1]).toBeVisible();
524525
});
525526

526-
// Select first item
527-
const firstItems = canvas.getAllByRole('row');
528-
await userEvent.click(firstItems[1]);
527+
// Get the Audio.mp3 row and click its checkbox
528+
const audioRow = await canvas.findByRole('row', { name: /Audio\.mp3/ });
529+
const firstCheckbox = within(audioRow).getByRole('checkbox', { name: 'Audio.mp3' });
530+
await userEvent.click(firstCheckbox);
529531

530532
// Verify first selection state
531-
await waitFor(() => {
532-
const selectedItems = canvas.getAllByRole('row');
533-
const row = selectedItems[1];
534-
expect(row).toHaveClass('bcp-item-row-selected');
535-
const selectedButton = canvas.getByRole('button', { name: /1 Selected/i });
536-
expect(selectedButton).toBeInTheDocument();
537-
const chooseButton = canvas.getByLabelText('Choose');
538-
expect(chooseButton).toBeEnabled();
539-
});
533+
await waitFor(
534+
() => {
535+
expect(audioRow).toHaveClass('bcp-item-row-selected');
536+
expect(canvas.getByRole('button', { name: /1 Selected/i })).toBeInTheDocument();
537+
expect(canvas.getByLabelText('Choose')).toBeEnabled();
538+
},
539+
{
540+
timeout: 3000,
541+
interval: 500,
542+
},
543+
);
544+
545+
// Find and click the second item's checkbox directly
546+
const rows = await canvas.findAllByRole('row');
547+
const secondRow = rows[1];
548+
549+
// Wait for the second row to be visible and get its checkbox
550+
await waitFor(
551+
() => {
552+
expect(secondRow).toBeVisible();
553+
},
554+
{
555+
timeout: 3000,
556+
interval: 500,
557+
},
558+
);
559+
560+
const secondCheckbox = within(secondRow).getByRole('checkbox');
561+
await userEvent.click(secondCheckbox);
540562

541-
// Select second item with keyboard
542-
await userEvent.keyboard('{ArrowDown}');
543-
await new Promise(resolve => setTimeout(resolve, 100)); // Small delay for selection update
544-
await userEvent.keyboard(' ');
563+
// Verify second selection state
564+
await waitFor(
565+
() => {
566+
expect(secondRow).toHaveClass('bcp-item-row-selected');
567+
expect(canvas.getByRole('button', { name: /2 Selected/i })).toBeInTheDocument();
568+
expect(canvas.getByLabelText('Choose')).toBeEnabled();
569+
},
570+
{
571+
timeout: 3000,
572+
interval: 500,
573+
},
574+
);
545575

546576
// Verify second selection state
547-
await waitFor(() => {
548-
const selectedItems = canvas.getAllByRole('row');
549-
const row = selectedItems[2];
550-
expect(row).toHaveClass('bcp-item-row-selected');
551-
const selectedButton = canvas.getByRole('button', { name: /2 Selected/i });
552-
expect(selectedButton).toBeInTheDocument();
553-
const chooseButton = canvas.getByLabelText('Choose');
554-
expect(chooseButton).toBeEnabled();
555-
});
577+
await waitFor(
578+
() => {
579+
const selectedButton = canvas.getByRole('button', { name: /Selected/i });
580+
expect(selectedButton).toHaveTextContent('2 Selected');
581+
expect(canvas.getByLabelText('Choose')).toBeEnabled();
582+
},
583+
{
584+
timeout: 3000,
585+
interval: 500,
586+
},
587+
);
556588

557589
// Select third item to hit max
558590
await userEvent.keyboard('{ArrowDown}');
559-
await new Promise(resolve => setTimeout(resolve, 100)); // Small delay for selection update
591+
await new Promise(resolve => setTimeout(resolve, 1000)); // Longer delay for keyboard navigation
560592
await userEvent.keyboard(' ');
593+
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for selection to update
561594

562595
// Verify max selection state
563-
await waitFor(() => {
564-
const selectedItems = canvas.getAllByRole('row');
565-
const row = selectedItems[3];
566-
expect(row).toHaveClass('bcp-item-row-selected');
567-
const selectedButton = canvas.getByRole('button', { name: /3 Selected/i });
568-
expect(selectedButton).toBeInTheDocument();
569-
expect(canvas.getByText('(max)')).toBeInTheDocument();
570-
const chooseButton = canvas.getByLabelText('Choose');
571-
expect(chooseButton).toBeEnabled();
572-
});
573-
574-
// Verify cannot select beyond max
575-
if (items.length > 4) {
596+
await waitFor(
597+
() => {
598+
// First verify selection count and max indicator
599+
const selectedButton = canvas.getByRole('button', { name: /Selected/i });
600+
expect(selectedButton).toHaveTextContent('3 Selected');
601+
expect(canvas.getByText('(max)')).toBeInTheDocument();
602+
expect(canvas.getByLabelText('Choose')).toBeEnabled();
603+
},
604+
{
605+
timeout: 3000,
606+
interval: 500,
607+
},
608+
);
609+
610+
// Get all rows and verify we can't select beyond max
611+
const allRows = canvas.getAllByRole('row');
612+
if (allRows.length > 4) {
576613
await userEvent.keyboard('{ArrowDown}');
614+
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for navigation
577615
await userEvent.keyboard(' ');
578-
expect(items[4]).not.toHaveClass('bcp-item-row-selected');
616+
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for attempted selection
617+
618+
// Verify max selection is maintained
619+
const selectedButton = canvas.getByRole('button', { name: /Selected/i });
620+
expect(selectedButton).toHaveTextContent('3 Selected');
579621
expect(canvas.getByText('(max)')).toBeInTheDocument();
580622
}
581623
},
@@ -603,15 +645,15 @@ export const folderNavigationAndSelection = {
603645
await userEvent.dblClick(items[2]);
604646
await waitFor(() => {
605647
expect(canvas.getByText('Subfolder')).toBeInTheDocument();
606-
expect(canvas.getByRole('link', { name: /All Files/i })).toBeInTheDocument();
648+
expect(canvas.getByText('Preview Test Folder')).toBeInTheDocument();
607649
});
608650

609651
// Verify selection persists in subfolder
610652
expect(canvas.getByRole('button', { name: /1 Selected/i })).toBeInTheDocument();
611653
expect(canvas.getByLabelText('Choose')).toBeEnabled();
612654

613655
// Navigate back to root
614-
await userEvent.click(canvas.getByText('All Files'));
656+
await userEvent.click(canvas.getByText('Preview Test Folder'));
615657
await waitFor(() => {
616658
expect(canvas.queryByRole('link', { name: /Subfolder/i })).not.toBeInTheDocument();
617659
});
@@ -639,7 +681,7 @@ export const searchFunctionality = {
639681

640682
// Wait for folder contents to load
641683
await waitFor(() => {
642-
expect(canvas.getByRole('link', { name: /All Files/i })).toBeInTheDocument();
684+
expect(canvas.getByText('Preview Test Folder')).toBeInTheDocument();
643685
const items = canvas.getAllByRole('row');
644686
expect(items.length).toBeGreaterThan(1);
645687
});
@@ -698,6 +740,9 @@ export default {
698740
http.get(`${DEFAULT_HOSTNAME_API}/2.0/folders/69083462919`, () => {
699741
return HttpResponse.json(mockRootFolder);
700742
}),
743+
http.get(`${DEFAULT_HOSTNAME_API}/2.0/folders/73426618530`, () => {
744+
return HttpResponse.json(mockSubFolder);
745+
}),
701746
],
702747
},
703748
},

0 commit comments

Comments
 (0)