|
| 1 | +import { act, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { h } from 'react-hyperscript-helpers'; |
| 4 | +import { renderWithAppContexts as render } from 'src/testing/test-utils'; |
| 5 | + |
| 6 | +import { RenameTableModal } from './RenameTableModal'; |
| 7 | + |
| 8 | +jest.mock('src/libs/ajax/Metrics'); |
| 9 | +jest.mock('src/libs/ajax/workspaces/Workspaces'); |
| 10 | + |
| 11 | +describe('RenameTableModal', () => { |
| 12 | + const validTableNames = ['aliquot', 'Some-Name_With_123-789-Chars']; |
| 13 | + const invalidCharacters = [ |
| 14 | + 'unknownprefix:sample', |
| 15 | + 'disallowed/characters', |
| 16 | + 'space character', |
| 17 | + 'or 1=1; drop table users; --', |
| 18 | + ]; |
| 19 | + validTableNames.forEach((newName) => { |
| 20 | + it(`passes validation for "${newName}"`, async () => { |
| 21 | + // Act |
| 22 | + await act(async () => { |
| 23 | + render( |
| 24 | + h(RenameTableModal, { |
| 25 | + onDismiss: jest.fn(), |
| 26 | + onUpdateSuccess: jest.fn(), |
| 27 | + getAllSavedColumnSettings: jest.fn(), |
| 28 | + updateAllSavedColumnSettings: jest.fn(), |
| 29 | + setTableNames: [], |
| 30 | + namespace: 'namespace', |
| 31 | + name: 'name', |
| 32 | + selectedDataType: 'selectedDataType', |
| 33 | + }) |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + const input = screen.getByLabelText(/New Name/); |
| 38 | + await userEvent.type(input, newName); |
| 39 | + |
| 40 | + const submitButton = screen.getByRole('button', { name: /Rename/ }); |
| 41 | + |
| 42 | + // Assert |
| 43 | + expect(submitButton).not.toHaveAttribute('disabled'); |
| 44 | + expect(screen.queryByText(/Table name may only/)).toBeNull(); |
| 45 | + expect(screen.queryByText(/Table name is required/)).toBeNull(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + invalidCharacters.forEach((newName) => { |
| 50 | + it(`fails validation for "${newName}"`, async () => { |
| 51 | + // Act |
| 52 | + await act(async () => { |
| 53 | + render( |
| 54 | + h(RenameTableModal, { |
| 55 | + onDismiss: jest.fn(), |
| 56 | + onUpdateSuccess: jest.fn(), |
| 57 | + getAllSavedColumnSettings: jest.fn(), |
| 58 | + updateAllSavedColumnSettings: jest.fn(), |
| 59 | + setTableNames: [], |
| 60 | + namespace: 'namespace', |
| 61 | + name: 'name', |
| 62 | + selectedDataType: 'selectedDataType', |
| 63 | + }) |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + const input = screen.getByLabelText(/New Name/); |
| 68 | + await userEvent.type(input, newName); |
| 69 | + |
| 70 | + const submitButton = screen.getByRole('button', { name: /Rename/ }); |
| 71 | + |
| 72 | + // Assert |
| 73 | + expect(submitButton).toHaveAttribute('disabled'); |
| 74 | + expect(screen.queryByText(/Table name may only/)).not.toBeNull(); |
| 75 | + expect(screen.queryByText(/Table name is required/)).toBeNull(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('requires table name', async () => { |
| 80 | + // Act |
| 81 | + await act(async () => { |
| 82 | + render( |
| 83 | + h(RenameTableModal, { |
| 84 | + onDismiss: jest.fn(), |
| 85 | + onUpdateSuccess: jest.fn(), |
| 86 | + getAllSavedColumnSettings: jest.fn(), |
| 87 | + updateAllSavedColumnSettings: jest.fn(), |
| 88 | + setTableNames: [], |
| 89 | + namespace: 'namespace', |
| 90 | + name: 'name', |
| 91 | + selectedDataType: 'selectedDataType', |
| 92 | + }) |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + const input = screen.getByLabelText(/New Name/); |
| 97 | + await userEvent.type(input, 'anewname'); |
| 98 | + |
| 99 | + const submitButton = screen.getByRole('button', { name: /Rename/ }); |
| 100 | + |
| 101 | + // Assert |
| 102 | + expect(submitButton).not.toHaveAttribute('disabled'); |
| 103 | + expect(screen.queryByText(/Table name may only/)).toBeNull(); |
| 104 | + expect(screen.queryByText(/Table name is required/)).toBeNull(); |
| 105 | + |
| 106 | + await userEvent.clear(input); |
| 107 | + expect(submitButton).toHaveAttribute('disabled'); |
| 108 | + expect(screen.queryByText(/Table name may only/)).toBeNull(); |
| 109 | + expect(screen.queryByText(/Table name is required/)).not.toBeNull(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments