|
1 | 1 | import * as React from 'react'; |
2 | | -import { shallow } from 'enzyme/build'; |
3 | | -import IconClockPast from '../../../../icons/general/IconClockPast'; |
4 | | -import IconDownload from '../../../../icons/general/IconDownload'; |
5 | | -import IconEllipsis from '../../../../icons/general/IconEllipsis'; |
6 | | -import IconOpenWith from '../../../../icons/general/IconOpenWith'; |
7 | | -import IconTrash from '../../../../icons/general/IconTrash'; |
8 | | -import IconUpload from '../../../../icons/general/IconUpload'; |
9 | | -import Tooltip from '../../../../components/tooltip/Tooltip'; |
| 2 | +import { userEvent } from '@testing-library/user-event'; |
| 3 | +import { screen, render } from '../../../../test-utils/testing-library'; |
10 | 4 | import VersionsItemActions from '../VersionsItemActions'; |
11 | 5 |
|
12 | 6 | describe('elements/content-sidebar/versions/VersionsItemActions', () => { |
13 | | - const getWrapper = (props = {}) => shallow(<VersionsItemActions isDownloadable isPreviewable {...props} />); |
| 7 | + const defaultProps = { |
| 8 | + fileId: '12345', |
| 9 | + }; |
| 10 | + |
| 11 | + const renderComponent = (props = {}, features = {}) => |
| 12 | + render(<VersionsItemActions {...defaultProps} {...props} />, { |
| 13 | + wrapperProps: { features }, |
| 14 | + }); |
14 | 15 |
|
15 | 16 | describe('render', () => { |
16 | | - test.each([true, false])('should return the correct menu items based on options', option => { |
17 | | - const wrapper = getWrapper({ |
18 | | - showDelete: option, |
19 | | - showDownload: option, |
20 | | - showPreview: option, |
21 | | - showPromote: option, |
22 | | - showRestore: option, |
| 17 | + test('should return null when no actions are shown', () => { |
| 18 | + const { container } = renderComponent({ |
| 19 | + showDelete: false, |
| 20 | + showDownload: false, |
| 21 | + showPreview: false, |
| 22 | + showPromote: false, |
| 23 | + showRestore: false, |
| 24 | + }); |
| 25 | + |
| 26 | + expect(container).toBeEmptyDOMElement(); |
| 27 | + }); |
| 28 | + |
| 29 | + test('should render actions toggle button when at least one action is shown', () => { |
| 30 | + renderComponent({ showDownload: true }); |
| 31 | + |
| 32 | + expect(screen.getByRole('button', { name: 'Toggle Actions Menu' })).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + test.each([ |
| 36 | + { actionProp: 'showPreview', label: 'Preview' }, |
| 37 | + { actionProp: 'showDownload', label: 'Download' }, |
| 38 | + { actionProp: 'showPromote', label: 'Make Current' }, |
| 39 | + { actionProp: 'showRestore', label: 'Restore' }, |
| 40 | + { actionProp: 'showDelete', label: 'Delete' }, |
| 41 | + ])('should render $label action when $actionProp is true', async ({ actionProp, label }) => { |
| 42 | + renderComponent({ [actionProp]: true }); |
| 43 | + |
| 44 | + const toggleButton = screen.getByRole('button', { name: 'Toggle Actions Menu' }); |
| 45 | + await userEvent.click(toggleButton); |
| 46 | + |
| 47 | + expect(screen.getByRole('menuitem', { name: new RegExp(label) })).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should render all actions when all show props are true', async () => { |
| 51 | + renderComponent({ |
| 52 | + showDelete: true, |
| 53 | + showDownload: true, |
| 54 | + showPreview: true, |
| 55 | + showPromote: true, |
| 56 | + showRestore: true, |
23 | 57 | }); |
24 | 58 |
|
25 | | - expect(wrapper.find(IconEllipsis).exists()).toBe(option); // Versions show actions if any permission is true |
26 | | - expect(wrapper.find(IconClockPast).exists()).toBe(option); |
27 | | - expect(wrapper.find(IconDownload).exists()).toBe(option); |
28 | | - expect(wrapper.find(IconOpenWith).exists()).toBe(option); |
29 | | - expect(wrapper.find(IconTrash).exists()).toBe(option); |
30 | | - expect(wrapper.find(IconUpload).exists()).toBe(option); |
31 | | - expect(wrapper).toMatchSnapshot(); |
| 59 | + const toggleButton = screen.getByRole('button', { name: 'Toggle Actions Menu' }); |
| 60 | + await userEvent.click(toggleButton); |
| 61 | + |
| 62 | + expect(screen.getByRole('menuitem', { name: /Preview/ })).toBeInTheDocument(); |
| 63 | + expect(screen.getByRole('menuitem', { name: /Download/ })).toBeInTheDocument(); |
| 64 | + expect(screen.getByRole('menuitem', { name: /Make Current/ })).toBeInTheDocument(); |
| 65 | + expect(screen.getByRole('menuitem', { name: /Restore/ })).toBeInTheDocument(); |
| 66 | + expect(screen.getByRole('menuitem', { name: /Delete/ })).toBeInTheDocument(); |
32 | 67 | }); |
33 | 68 |
|
34 | | - test.each([true, false])('should enable/disable actions and tooltips if isRetained is %s', option => { |
35 | | - const wrapper = getWrapper({ |
36 | | - isRetained: option, |
| 69 | + test('should disable delete action when isRetained is true', async () => { |
| 70 | + renderComponent({ |
| 71 | + isRetained: true, |
37 | 72 | showDelete: true, |
38 | 73 | }); |
39 | 74 |
|
40 | | - expect(wrapper.find(Tooltip).prop('isDisabled')).toBe(!option); |
41 | | - expect(wrapper).toMatchSnapshot(); |
| 75 | + const toggleButton = screen.getByRole('button', { name: 'Toggle Actions Menu' }); |
| 76 | + await userEvent.click(toggleButton); |
| 77 | + |
| 78 | + const deleteButton = screen.getByRole('menuitem', { name: /Delete/ }); |
| 79 | + expect(deleteButton).toHaveAttribute('aria-disabled', 'true'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('should not disable delete action when isRetained is false', async () => { |
| 83 | + renderComponent({ |
| 84 | + isRetained: false, |
| 85 | + showDelete: true, |
| 86 | + }); |
| 87 | + |
| 88 | + const toggleButton = screen.getByRole('button', { name: 'Toggle Actions Menu' }); |
| 89 | + await userEvent.click(toggleButton); |
| 90 | + |
| 91 | + const deleteButton = screen.getByRole('menuitem', { name: /Delete/ }); |
| 92 | + expect(deleteButton).not.toHaveAttribute('aria-disabled', 'true'); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('with previewModernization feature enabled', () => { |
| 96 | + const previewModernizationFeatures = { |
| 97 | + previewModernization: { enabled: true }, |
| 98 | + }; |
| 99 | + |
| 100 | + test('should render modernized toggle button', () => { |
| 101 | + renderComponent({ showDownload: true }, previewModernizationFeatures); |
| 102 | + |
| 103 | + const toggleButton = screen.getByRole('button', { name: 'Toggle Actions Menu' }); |
| 104 | + expect(toggleButton).toHaveClass('bcs-VersionsItemActions-toggle--modernized'); |
| 105 | + }); |
| 106 | + |
| 107 | + test('should render actions in dropdown menu', async () => { |
| 108 | + renderComponent( |
| 109 | + { |
| 110 | + showDownload: true, |
| 111 | + showPreview: true, |
| 112 | + }, |
| 113 | + previewModernizationFeatures, |
| 114 | + ); |
| 115 | + |
| 116 | + const toggleButton = screen.getByRole('button', { name: 'Toggle Actions Menu' }); |
| 117 | + await userEvent.click(toggleButton); |
| 118 | + |
| 119 | + expect(screen.getByRole('menuitem', { name: /Download/ })).toBeInTheDocument(); |
| 120 | + expect(screen.getByRole('menuitem', { name: /Preview/ })).toBeInTheDocument(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('with previewModernization feature disabled', () => { |
| 125 | + test('should render legacy toggle button', () => { |
| 126 | + renderComponent({ showDownload: true }); |
| 127 | + |
| 128 | + const toggleButton = screen.getByRole('button', { name: 'Toggle Actions Menu' }); |
| 129 | + expect(toggleButton).toHaveClass('bcs-VersionsItemActions-toggle'); |
| 130 | + expect(toggleButton).not.toHaveClass('bcs-VersionsItemActions-toggle--modernized'); |
| 131 | + }); |
42 | 132 | }); |
43 | 133 | }); |
44 | 134 | }); |
0 commit comments