|
| 1 | +import * as React from 'react'; |
| 2 | +import { render, screen } from '../../../../test-utils/testing-library'; |
| 3 | +import SubHeaderLeftV2 from '../SubHeaderLeftV2'; |
| 4 | +import type { Collection } from '../../../../common/types/core'; |
| 5 | +import type { SubHeaderLeftV2Props } from '../SubHeaderLeftV2'; |
| 6 | + |
| 7 | +const mockCollection: Collection = { |
| 8 | + items: [ |
| 9 | + { id: '1', name: 'file1.txt' }, |
| 10 | + { id: '2', name: 'file2.txt' }, |
| 11 | + { id: '3', name: 'file3.txt' }, |
| 12 | + ], |
| 13 | +}; |
| 14 | + |
| 15 | +const defaultProps: SubHeaderLeftV2Props = { |
| 16 | + currentCollection: mockCollection, |
| 17 | + selectedItemIds: new Set(), |
| 18 | +}; |
| 19 | + |
| 20 | +const renderComponent = (props: Partial<SubHeaderLeftV2Props> = {}) => |
| 21 | + render(<SubHeaderLeftV2 {...defaultProps} {...props} />); |
| 22 | + |
| 23 | +describe('elements/common/sub-header/SubHeaderLeftV2', () => { |
| 24 | + describe('when no items are selected', () => { |
| 25 | + test('should render title if provided', () => { |
| 26 | + renderComponent({ |
| 27 | + rootName: 'Custom Folder', |
| 28 | + title: 'Custom Title', |
| 29 | + selectedItemIds: new Set(), |
| 30 | + }); |
| 31 | + |
| 32 | + expect(screen.getByText('Custom Title')).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + test('should render root name if no title is provided', () => { |
| 36 | + renderComponent({ |
| 37 | + rootName: 'Custom Folder', |
| 38 | + title: undefined, |
| 39 | + selectedItemIds: new Set(), |
| 40 | + }); |
| 41 | + |
| 42 | + expect(screen.getByText('Custom Folder')).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('when items are selected', () => { |
| 47 | + test('should render single selected item name', () => { |
| 48 | + renderComponent({ |
| 49 | + selectedItemIds: new Set(['1']), |
| 50 | + }); |
| 51 | + |
| 52 | + expect(screen.getByText('file1.txt')).toBeInTheDocument(); |
| 53 | + expect(screen.getByRole('button')).toBeInTheDocument(); // Close button |
| 54 | + }); |
| 55 | + |
| 56 | + test('should render multiple selected items count', () => { |
| 57 | + renderComponent({ |
| 58 | + selectedItemIds: new Set(['1', '2']), |
| 59 | + }); |
| 60 | + |
| 61 | + expect(screen.getByText('2 files selected')).toBeInTheDocument(); |
| 62 | + expect(screen.getByRole('button')).toBeInTheDocument(); // Close button |
| 63 | + }); |
| 64 | + |
| 65 | + test('should render all items selected count', () => { |
| 66 | + renderComponent({ |
| 67 | + selectedItemIds: 'all', |
| 68 | + }); |
| 69 | + |
| 70 | + expect(screen.getByText('3 files selected')).toBeInTheDocument(); |
| 71 | + expect(screen.getByRole('button')).toBeInTheDocument(); // Close button |
| 72 | + }); |
| 73 | + |
| 74 | + test('should call onClearSelectedItemIds when close button is clicked', () => { |
| 75 | + const mockOnClearSelectedItemIds = jest.fn(); |
| 76 | + |
| 77 | + renderComponent({ |
| 78 | + selectedItemIds: new Set(['1']), |
| 79 | + onClearSelectedItemIds: mockOnClearSelectedItemIds, |
| 80 | + }); |
| 81 | + |
| 82 | + const closeButton = screen.getByRole('button'); |
| 83 | + closeButton.click(); |
| 84 | + |
| 85 | + expect(mockOnClearSelectedItemIds).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should handle selected item not found in collection', () => { |
| 89 | + renderComponent({ |
| 90 | + selectedItemIds: new Set(['999']), // Non-existent ID |
| 91 | + }); |
| 92 | + |
| 93 | + // Should not crash and should not render any selected item text |
| 94 | + expect(screen.queryByText('file1.txt')).not.toBeInTheDocument(); |
| 95 | + expect(screen.queryByText('file2.txt')).not.toBeInTheDocument(); |
| 96 | + expect(screen.queryByText('file3.txt')).not.toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + test('should handle empty collection with selected items', () => { |
| 100 | + renderComponent({ |
| 101 | + currentCollection: { items: [] }, |
| 102 | + selectedItemIds: new Set(['1']), |
| 103 | + }); |
| 104 | + |
| 105 | + // Should not crash and should not render any selected item text |
| 106 | + expect(screen.queryByText('file1.txt')).not.toBeInTheDocument(); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments