|
| 1 | +import React from 'react'; |
| 2 | +import { screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { DataTableContext } from '@openedx/paragon'; |
| 4 | +import { initializeMocks, render } from '../../../testUtils'; |
| 5 | +import TableActions from './TableActions'; |
| 6 | +import messages from '../messages'; |
| 7 | + |
| 8 | +const defaultProps = { |
| 9 | + selectedFlatRows: [], |
| 10 | + fileInputControl: { click: jest.fn() }, |
| 11 | + handleOpenDeleteConfirmation: jest.fn(), |
| 12 | + handleBulkDownload: jest.fn(), |
| 13 | + encodingsDownloadUrl: null, |
| 14 | + handleSort: jest.fn(), |
| 15 | + fileType: 'video', |
| 16 | + setInitialState: jest.fn(), |
| 17 | + intl: { |
| 18 | + formatMessage: (msg, values) => msg.defaultMessage.replace('{fileType}', values?.fileType ?? ''), |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +const mockColumns = [ |
| 23 | + { |
| 24 | + id: 'wrapperType', |
| 25 | + Header: 'Type', |
| 26 | + accessor: 'wrapperType', |
| 27 | + filter: 'includes', |
| 28 | + }, |
| 29 | +]; |
| 30 | + |
| 31 | +const renderWithContext = (props = {}, contextOverrides = {}) => { |
| 32 | + const contextValue = { |
| 33 | + state: { |
| 34 | + selectedRowIds: {}, |
| 35 | + filters: [], |
| 36 | + ...contextOverrides.state, |
| 37 | + }, |
| 38 | + clearSelection: jest.fn(), |
| 39 | + gotoPage: jest.fn(), |
| 40 | + setAllFilters: jest.fn(), |
| 41 | + columns: mockColumns, |
| 42 | + ...contextOverrides, |
| 43 | + }; |
| 44 | + |
| 45 | + return render( |
| 46 | + <DataTableContext.Provider value={contextValue}> |
| 47 | + <TableActions {...defaultProps} {...props} /> |
| 48 | + </DataTableContext.Provider>, |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +describe('TableActions', () => { |
| 53 | + beforeEach(() => { |
| 54 | + initializeMocks(); |
| 55 | + jest.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('renders buttons and dropdown', () => { |
| 59 | + renderWithContext(); |
| 60 | + |
| 61 | + expect(screen.getByRole('button', { name: messages.sortButtonLabel.defaultMessage })).toBeInTheDocument(); |
| 62 | + expect(screen.getByRole('button', { name: messages.actionsButtonLabel.defaultMessage })).toBeInTheDocument(); |
| 63 | + expect(screen.getByRole('button', { name: messages.addFilesButtonLabel.defaultMessage.replace('{fileType}', 'video') })).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + test('disables bulk and delete actions if no rows selected', () => { |
| 67 | + renderWithContext(); |
| 68 | + |
| 69 | + fireEvent.click(screen.getByRole('button', { name: messages.actionsButtonLabel.defaultMessage })); |
| 70 | + |
| 71 | + const downloadOption = screen.getByText(messages.downloadTitle.defaultMessage); |
| 72 | + const deleteButton = screen.getByTestId('open-delete-confirmation-button'); |
| 73 | + |
| 74 | + expect(downloadOption).toHaveAttribute('aria-disabled', 'true'); |
| 75 | + expect(downloadOption).toHaveClass('disabled'); |
| 76 | + |
| 77 | + expect(deleteButton).toHaveAttribute('aria-disabled', 'true'); |
| 78 | + expect(deleteButton).toHaveClass('disabled'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('enables bulk and delete actions when rows are selected', () => { |
| 82 | + renderWithContext({ |
| 83 | + selectedFlatRows: [{ original: { id: '1', displayName: 'Video 1', wrapperType: 'video' } }], |
| 84 | + }); |
| 85 | + |
| 86 | + fireEvent.click(screen.getByRole('button', { name: messages.actionsButtonLabel.defaultMessage })); |
| 87 | + expect(screen.getByText(messages.downloadTitle.defaultMessage)).not.toBeDisabled(); |
| 88 | + expect(screen.getByTestId('open-delete-confirmation-button')).not.toBeDisabled(); |
| 89 | + }); |
| 90 | + |
| 91 | + test('calls file input click and clears selection when add button clicked', () => { |
| 92 | + const mockClick = jest.fn(); |
| 93 | + const mockClear = jest.fn(); |
| 94 | + |
| 95 | + renderWithContext({ fileInputControl: { click: mockClick } }, {}, mockClear); |
| 96 | + fireEvent.click(screen.getByRole('button', { name: messages.addFilesButtonLabel.defaultMessage.replace('{fileType}', 'video') })); |
| 97 | + expect(mockClick).toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + test('opens sort modal when sort button clicked', () => { |
| 101 | + renderWithContext(); |
| 102 | + fireEvent.click(screen.getByRole('button', { name: messages.sortButtonLabel.defaultMessage })); |
| 103 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 104 | + }); |
| 105 | + |
| 106 | + test('calls handleBulkDownload when selected and clicked', () => { |
| 107 | + const handleBulkDownload = jest.fn(); |
| 108 | + renderWithContext({ |
| 109 | + selectedFlatRows: [{ original: { id: '1', displayName: 'Video 1', wrapperType: 'video' } }], |
| 110 | + handleBulkDownload, |
| 111 | + }); |
| 112 | + |
| 113 | + fireEvent.click(screen.getByRole('button', { name: messages.actionsButtonLabel.defaultMessage })); |
| 114 | + fireEvent.click(screen.getByText(messages.downloadTitle.defaultMessage)); |
| 115 | + expect(handleBulkDownload).toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + test('calls handleOpenDeleteConfirmation when clicked', () => { |
| 119 | + const handleOpenDeleteConfirmation = jest.fn(); |
| 120 | + const selectedFlatRows = [{ original: { id: '1', displayName: 'Video 1', wrapperType: 'video' } }]; |
| 121 | + renderWithContext({ |
| 122 | + selectedFlatRows, |
| 123 | + handleOpenDeleteConfirmation, |
| 124 | + }); |
| 125 | + |
| 126 | + fireEvent.click(screen.getByRole('button', { name: messages.actionsButtonLabel.defaultMessage })); |
| 127 | + fireEvent.click(screen.getByTestId('open-delete-confirmation-button')); |
| 128 | + expect(handleOpenDeleteConfirmation).toHaveBeenCalledWith(selectedFlatRows); |
| 129 | + }); |
| 130 | + |
| 131 | + test('shows encoding download link when provided', () => { |
| 132 | + const encodingsDownloadUrl = '/some/path/to/encoding.zip'; |
| 133 | + renderWithContext({ encodingsDownloadUrl }); |
| 134 | + |
| 135 | + fireEvent.click(screen.getByRole('button', { name: messages.actionsButtonLabel.defaultMessage })); |
| 136 | + expect(screen.getByRole('link', { name: messages.downloadEncodingsTitle.defaultMessage })).toHaveAttribute('href', expect.stringContaining(encodingsDownloadUrl)); |
| 137 | + }); |
| 138 | +}); |
0 commit comments