|
| 1 | +import { act, render, screen } from '@testing-library/react'; |
| 2 | +import { FunctionComponent, PropsWithChildren } from 'react'; |
| 3 | + |
| 4 | +import { DocumentDefinitionType, DocumentInitializationModel, DocumentType } from '../../../../models/datamapper'; |
| 5 | +import { DataMapperProvider } from '../../../../providers/datamapper.provider'; |
| 6 | +import { DataMapperSettingsButton } from './DataMapperSettingsButton'; |
| 7 | + |
| 8 | +describe('DataMapperSettingsButton', () => { |
| 9 | + const createWrapper = (): FunctionComponent<PropsWithChildren> => { |
| 10 | + return ({ children }) => ( |
| 11 | + <DataMapperProvider |
| 12 | + documentInitializationModel={ |
| 13 | + new DocumentInitializationModel( |
| 14 | + {}, |
| 15 | + { |
| 16 | + documentType: DocumentType.SOURCE_BODY, |
| 17 | + definitionType: DocumentDefinitionType.XML_SCHEMA, |
| 18 | + name: 'source', |
| 19 | + }, |
| 20 | + { |
| 21 | + documentType: DocumentType.TARGET_BODY, |
| 22 | + definitionType: DocumentDefinitionType.XML_SCHEMA, |
| 23 | + name: 'target', |
| 24 | + }, |
| 25 | + ) |
| 26 | + } |
| 27 | + > |
| 28 | + {children} |
| 29 | + </DataMapperProvider> |
| 30 | + ); |
| 31 | + }; |
| 32 | + |
| 33 | + describe('Button Rendering', () => { |
| 34 | + it('should render settings button', () => { |
| 35 | + const wrapper = createWrapper(); |
| 36 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 37 | + |
| 38 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 39 | + expect(button).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should have correct aria-label', () => { |
| 43 | + const wrapper = createWrapper(); |
| 44 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 45 | + |
| 46 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 47 | + expect(button).toHaveAttribute('aria-label', 'Settings'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should have correct title', () => { |
| 51 | + const wrapper = createWrapper(); |
| 52 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 53 | + |
| 54 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 55 | + expect(button).toHaveAttribute('title', 'DataMapper Settings'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should render with plain variant', () => { |
| 59 | + const wrapper = createWrapper(); |
| 60 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 61 | + |
| 62 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 63 | + expect(button).toHaveClass('pf-m-plain'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should render cog icon', () => { |
| 67 | + const wrapper = createWrapper(); |
| 68 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 69 | + |
| 70 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 71 | + const icon = button.querySelector('svg'); |
| 72 | + expect(icon).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('Modal Interaction', () => { |
| 77 | + it('should not render modal initially', () => { |
| 78 | + const wrapper = createWrapper(); |
| 79 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 80 | + |
| 81 | + expect(screen.queryByTestId('datamapper-settings-modal')).not.toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should open modal when button is clicked', async () => { |
| 85 | + const wrapper = createWrapper(); |
| 86 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 87 | + |
| 88 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 89 | + |
| 90 | + act(() => { |
| 91 | + button.click(); |
| 92 | + }); |
| 93 | + |
| 94 | + await screen.findByTestId('datamapper-settings-modal'); |
| 95 | + expect(screen.getByText('DataMapper Settings')).toBeInTheDocument(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should close modal when cancel button is clicked', async () => { |
| 99 | + const wrapper = createWrapper(); |
| 100 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 101 | + |
| 102 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 103 | + |
| 104 | + // Open modal |
| 105 | + act(() => { |
| 106 | + button.click(); |
| 107 | + }); |
| 108 | + |
| 109 | + await screen.findByTestId('datamapper-settings-modal'); |
| 110 | + |
| 111 | + // Close modal |
| 112 | + const cancelButton = screen.getByTestId('datamapper-settings-cancel-btn'); |
| 113 | + act(() => { |
| 114 | + cancelButton.click(); |
| 115 | + }); |
| 116 | + |
| 117 | + expect(screen.queryByTestId('datamapper-settings-modal')).not.toBeInTheDocument(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should close modal when save button is clicked', async () => { |
| 121 | + const wrapper = createWrapper(); |
| 122 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 123 | + |
| 124 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 125 | + |
| 126 | + // Open modal |
| 127 | + act(() => { |
| 128 | + button.click(); |
| 129 | + }); |
| 130 | + |
| 131 | + await screen.findByTestId('datamapper-settings-modal'); |
| 132 | + |
| 133 | + // Close modal |
| 134 | + const saveButton = screen.getByTestId('datamapper-settings-save-btn'); |
| 135 | + act(() => { |
| 136 | + saveButton.click(); |
| 137 | + }); |
| 138 | + |
| 139 | + expect(screen.queryByTestId('datamapper-settings-modal')).not.toBeInTheDocument(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should close modal when close button is clicked', async () => { |
| 143 | + const wrapper = createWrapper(); |
| 144 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 145 | + |
| 146 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 147 | + |
| 148 | + // Open modal |
| 149 | + act(() => { |
| 150 | + button.click(); |
| 151 | + }); |
| 152 | + |
| 153 | + await screen.findByTestId('datamapper-settings-modal'); |
| 154 | + |
| 155 | + // Close modal |
| 156 | + const closeButton = screen.getByLabelText('Close'); |
| 157 | + act(() => { |
| 158 | + closeButton.click(); |
| 159 | + }); |
| 160 | + |
| 161 | + expect(screen.queryByTestId('datamapper-settings-modal')).not.toBeInTheDocument(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should be able to reopen modal after closing', async () => { |
| 165 | + const wrapper = createWrapper(); |
| 166 | + render(<DataMapperSettingsButton />, { wrapper }); |
| 167 | + |
| 168 | + const button = screen.getByTestId('datamapper-settings-button'); |
| 169 | + |
| 170 | + // Open modal |
| 171 | + act(() => { |
| 172 | + button.click(); |
| 173 | + }); |
| 174 | + await screen.findByTestId('datamapper-settings-modal'); |
| 175 | + |
| 176 | + // Close modal |
| 177 | + const cancelButton = screen.getByTestId('datamapper-settings-cancel-btn'); |
| 178 | + act(() => { |
| 179 | + cancelButton.click(); |
| 180 | + }); |
| 181 | + expect(screen.queryByTestId('datamapper-settings-modal')).not.toBeInTheDocument(); |
| 182 | + |
| 183 | + // Reopen modal |
| 184 | + act(() => { |
| 185 | + button.click(); |
| 186 | + }); |
| 187 | + await screen.findByTestId('datamapper-settings-modal'); |
| 188 | + expect(screen.getByText('DataMapper Settings')).toBeInTheDocument(); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
0 commit comments