|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { EventShimCustomEvent } from '@deephaven/utils'; |
| 5 | +import CustomColumnBuilder, { |
| 6 | + CustomColumnBuilderProps, |
| 7 | +} from './CustomColumnBuilder'; |
| 8 | +import IrisGridTestUtils from '../IrisGridTestUtils'; |
| 9 | +import IrisGridModel from '../IrisGridModel'; |
| 10 | + |
| 11 | +function Builder({ |
| 12 | + model = IrisGridTestUtils.makeModel(), |
| 13 | + customColumns = [], |
| 14 | + onSave = jest.fn(), |
| 15 | + onCancel = jest.fn(), |
| 16 | +}: Partial<CustomColumnBuilderProps>) { |
| 17 | + return ( |
| 18 | + <CustomColumnBuilder |
| 19 | + model={model} |
| 20 | + customColumns={customColumns} |
| 21 | + onSave={onSave} |
| 22 | + onCancel={onCancel} |
| 23 | + /> |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +test('Renders the default state', async () => { |
| 28 | + render(<Builder />); |
| 29 | + expect(screen.getByPlaceholderText('Column Name')).toBeInTheDocument(); |
| 30 | + expect(screen.getByText('Column Formula')).toBeInTheDocument(); |
| 31 | +}); |
| 32 | + |
| 33 | +test('Calls on save', async () => { |
| 34 | + const user = userEvent.setup(); |
| 35 | + const customColumns = ['abc=def', 'foo=bar']; |
| 36 | + const mockSave = jest.fn(); |
| 37 | + render(<Builder onSave={mockSave} customColumns={customColumns} />); |
| 38 | + |
| 39 | + await user.type(screen.getByDisplayValue('abc'), 'cba'); |
| 40 | + await user.click(screen.getByText(/Save/)); |
| 41 | + expect(mockSave).toHaveBeenLastCalledWith(['abccba=def', 'foo=bar']); |
| 42 | +}); |
| 43 | + |
| 44 | +test('Switches to loader button while saving', async () => { |
| 45 | + jest.useFakeTimers(); |
| 46 | + const user = userEvent.setup({ delay: null }); |
| 47 | + const model = IrisGridTestUtils.makeModel(); |
| 48 | + const mockSave = jest.fn(() => |
| 49 | + setTimeout(() => { |
| 50 | + model.dispatchEvent( |
| 51 | + new EventShimCustomEvent(IrisGridModel.EVENT.COLUMNS_CHANGED) |
| 52 | + ); |
| 53 | + }, 50) |
| 54 | + ); |
| 55 | + |
| 56 | + render( |
| 57 | + <Builder model={model} onSave={mockSave} customColumns={['foo=bar']} /> |
| 58 | + ); |
| 59 | + |
| 60 | + await user.click(screen.getByText(/Save/)); |
| 61 | + expect(screen.getByText('Applying')).toBeInTheDocument(); |
| 62 | + jest.advanceTimersByTime(50); |
| 63 | + expect(screen.getByText('Success')).toBeInTheDocument(); |
| 64 | + jest.advanceTimersByTime(CustomColumnBuilder.SUCCESS_SHOW_DURATION); |
| 65 | + expect(screen.getByText(/Save/)).toBeInTheDocument(); |
| 66 | + |
| 67 | + // Component should ignore this event and not change the save button |
| 68 | + model.dispatchEvent( |
| 69 | + new EventShimCustomEvent(IrisGridModel.EVENT.COLUMNS_CHANGED) |
| 70 | + ); |
| 71 | + expect(screen.getByText(/Save/)).toBeInTheDocument(); |
| 72 | + jest.useRealTimers(); |
| 73 | +}); |
| 74 | + |
| 75 | +test('Adds a column', async () => { |
| 76 | + const user = userEvent.setup(); |
| 77 | + render(<Builder />); |
| 78 | + |
| 79 | + await user.click(screen.getByText('Add Another Column')); |
| 80 | + expect(screen.getAllByPlaceholderText('Column Name').length).toBe(2); |
| 81 | + expect(screen.getAllByText('Column Formula').length).toBe(2); |
| 82 | +}); |
| 83 | + |
| 84 | +test('Ignores empty names or formulas on save', async () => { |
| 85 | + const user = userEvent.setup(); |
| 86 | + const customColumns = ['foo=bar']; |
| 87 | + const mockSave = jest.fn(); |
| 88 | + render(<Builder customColumns={customColumns} onSave={mockSave} />); |
| 89 | + |
| 90 | + await user.click(screen.getByText('Add Another Column')); |
| 91 | + await user.type(screen.getAllByPlaceholderText('Column Name')[1], 'test'); |
| 92 | + await user.click(screen.getByText(/Save/)); |
| 93 | + expect(mockSave).toBeCalledWith(customColumns); |
| 94 | +}); |
| 95 | + |
| 96 | +test('Deletes columns', async () => { |
| 97 | + const user = userEvent.setup(); |
| 98 | + const customColumns = ['abc=def', 'foo=bar']; |
| 99 | + render(<Builder customColumns={customColumns} />); |
| 100 | + |
| 101 | + await user.click(screen.getAllByLabelText(/Delete/)[0]); |
| 102 | + expect(screen.queryByDisplayValue('abc')).toBeNull(); |
| 103 | + expect(screen.queryByDisplayValue('def')).toBeNull(); |
| 104 | + expect(screen.getByDisplayValue('foo')).toBeInTheDocument(); |
| 105 | + expect(screen.getByDisplayValue('bar')).toBeInTheDocument(); |
| 106 | + |
| 107 | + await user.click(screen.getByLabelText(/Delete/)); |
| 108 | + expect(screen.queryByDisplayValue('foo')).toBeNull(); |
| 109 | + expect(screen.queryByDisplayValue('bar')).toBeNull(); |
| 110 | + expect(screen.getByPlaceholderText('Column Name')).toBeInTheDocument(); |
| 111 | + expect(screen.getByText('Column Formula')).toBeInTheDocument(); |
| 112 | +}); |
| 113 | + |
| 114 | +test('Displays request failure message', async () => { |
| 115 | + const user = userEvent.setup(); |
| 116 | + const model = IrisGridTestUtils.makeModel(); |
| 117 | + render(<Builder model={model} customColumns={['foo=bar']} />); |
| 118 | + |
| 119 | + // Should ignore this since not in saving state |
| 120 | + model.dispatchEvent( |
| 121 | + new EventShimCustomEvent(IrisGridModel.EVENT.REQUEST_FAILED, { |
| 122 | + detail: { errorMessage: 'Error message' }, |
| 123 | + }) |
| 124 | + ); |
| 125 | + expect(screen.queryByText(/Error message/)).toBeNull(); |
| 126 | + |
| 127 | + await user.click(screen.getByText(/Save/)); |
| 128 | + model.dispatchEvent( |
| 129 | + new EventShimCustomEvent(IrisGridModel.EVENT.REQUEST_FAILED, { |
| 130 | + detail: { errorMessage: 'Error message' }, |
| 131 | + }) |
| 132 | + ); |
| 133 | + |
| 134 | + expect(screen.getByText(/Error message/)).toBeInTheDocument(); |
| 135 | + |
| 136 | + const input = screen.getByDisplayValue('foo'); |
| 137 | + await user.click(input); |
| 138 | + expect(input).not.toHaveClass('is-invalid'); |
| 139 | +}); |
| 140 | + |
| 141 | +test('Handles focus changes via keyboard', async () => { |
| 142 | + const user = userEvent.setup(); |
| 143 | + const { container } = render( |
| 144 | + <Builder customColumns={['abc=bar', 'foo=bar']} /> |
| 145 | + ); |
| 146 | + |
| 147 | + const nameInputs = screen.getAllByPlaceholderText('Column Name'); |
| 148 | + const formulaInputs = container.querySelectorAll( |
| 149 | + '.input-editor-wrapper textarea' |
| 150 | + ); |
| 151 | + const deleteButtons = screen.getAllByLabelText(/Delete/); |
| 152 | + const dragHandles = screen.getAllByLabelText(/Drag/); |
| 153 | + await user.click(nameInputs[0]); |
| 154 | + |
| 155 | + await user.keyboard('{Tab}'); |
| 156 | + expect(deleteButtons[0]).toHaveFocus(); |
| 157 | + await user.keyboard('{Tab}'); |
| 158 | + expect(dragHandles[0]).toHaveFocus(); |
| 159 | + await user.keyboard('{Tab}'); |
| 160 | + expect(formulaInputs[0]).toHaveFocus(); |
| 161 | + |
| 162 | + await user.keyboard('{Tab}'); |
| 163 | + expect(nameInputs[1]).toHaveFocus(); |
| 164 | + await user.keyboard('{Tab}'); |
| 165 | + expect(deleteButtons[1]).toHaveFocus(); |
| 166 | + await user.keyboard('{Tab}'); |
| 167 | + expect(dragHandles[1]).toHaveFocus(); |
| 168 | + await user.keyboard('{Tab}'); |
| 169 | + expect(formulaInputs[1]).toHaveFocus(); |
| 170 | + |
| 171 | + await user.keyboard('{Tab}'); |
| 172 | + expect(screen.getByText('Add Another Column')).toHaveFocus(); |
| 173 | + |
| 174 | + await user.keyboard('{Shift>}{Tab}{/Shift}'); |
| 175 | + expect(formulaInputs[1]).toHaveFocus(); |
| 176 | + await user.keyboard('{Shift>}{Tab}{/Shift}'); |
| 177 | + expect(dragHandles[1]).toHaveFocus(); |
| 178 | +}); |
0 commit comments