|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { |
| 5 | + Type as FilterType, |
| 6 | + TypeValue as FilterTypeValue, |
| 7 | +} from '@deephaven/filters'; |
| 8 | +import { TableUtils } from '@deephaven/jsapi-utils'; |
| 9 | +import GotoRow from './GotoRow'; |
| 10 | +import IrisGridTestUtils from './IrisGridTestUtils'; |
| 11 | + |
| 12 | +function makeGotoRow({ |
| 13 | + gotoRow = '', |
| 14 | + gotoRowError = '', |
| 15 | + gotoValueError = '', |
| 16 | + onGotoRowSubmit = jest.fn(), |
| 17 | + model = IrisGridTestUtils.makeModel(), |
| 18 | + onGotoRowNumberChanged = jest.fn(), |
| 19 | + onClose = jest.fn(), |
| 20 | + isShown = true, |
| 21 | + onEntering = jest.fn(), |
| 22 | + onEntered = jest.fn(), |
| 23 | + onExiting = jest.fn(), |
| 24 | + onExited = jest.fn(), |
| 25 | + gotoValueSelectedColumnName = '', |
| 26 | + gotoValue = '', |
| 27 | + gotoValueFilter = FilterType.eq, |
| 28 | + onGotoValueSelectedColumnNameChanged = jest.fn(), |
| 29 | + onGotoValueSelectedFilterChanged = jest.fn(), |
| 30 | + onGotoValueChanged = jest.fn(), |
| 31 | + onGotoValueSubmit = jest.fn(), |
| 32 | +} = {}) { |
| 33 | + return render( |
| 34 | + <GotoRow |
| 35 | + gotoRow={gotoRow} |
| 36 | + gotoRowError={gotoRowError} |
| 37 | + gotoValueError={gotoValueError} |
| 38 | + onGotoRowSubmit={onGotoRowSubmit} |
| 39 | + model={model} |
| 40 | + onGotoRowNumberChanged={onGotoRowNumberChanged} |
| 41 | + onClose={onClose} |
| 42 | + isShown={isShown} |
| 43 | + onEntering={onEntering} |
| 44 | + onEntered={onEntered} |
| 45 | + onExiting={onExiting} |
| 46 | + onExited={onExited} |
| 47 | + gotoValueSelectedColumnName={gotoValueSelectedColumnName} |
| 48 | + gotoValue={gotoValue} |
| 49 | + gotoValueFilter={gotoValueFilter as FilterTypeValue} |
| 50 | + onGotoValueSelectedColumnNameChanged={ |
| 51 | + onGotoValueSelectedColumnNameChanged |
| 52 | + } |
| 53 | + onGotoValueSelectedFilterChanged={onGotoValueSelectedFilterChanged} |
| 54 | + onGotoValueChanged={onGotoValueChanged} |
| 55 | + onGotoValueSubmit={onGotoValueSubmit} |
| 56 | + /> |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +beforeEach(() => { |
| 61 | + jest.useFakeTimers(); |
| 62 | +}); |
| 63 | + |
| 64 | +afterEach(() => { |
| 65 | + jest.useRealTimers(); |
| 66 | +}); |
| 67 | + |
| 68 | +it('mounts and unmounts properly', () => { |
| 69 | + makeGotoRow(); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('Go to row', () => { |
| 73 | + it('calls onGotoRowSubmit on Enter key press', async () => { |
| 74 | + const user = userEvent.setup({ delay: null }); |
| 75 | + const onGotoRowSubmitMock = jest.fn(); |
| 76 | + const component = makeGotoRow({ onGotoRowSubmit: onGotoRowSubmitMock }); |
| 77 | + |
| 78 | + const inputElement = screen.getByRole('spinbutton'); |
| 79 | + await user.type(inputElement, '{Enter}'); |
| 80 | + |
| 81 | + expect(onGotoRowSubmitMock).toHaveBeenCalledTimes(1); |
| 82 | + |
| 83 | + component.unmount(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('does not call onGotoRowSubmit on non-Enter key press', async () => { |
| 87 | + const user = userEvent.setup({ delay: null }); |
| 88 | + const onGotoRowSubmitMock = jest.fn(); |
| 89 | + const component = makeGotoRow({ onGotoRowSubmit: onGotoRowSubmitMock }); |
| 90 | + |
| 91 | + const inputElement = screen.getByRole('spinbutton'); |
| 92 | + await user.type(inputElement, 'a1`'); |
| 93 | + |
| 94 | + expect(onGotoRowSubmitMock).not.toHaveBeenCalled(); |
| 95 | + |
| 96 | + component.unmount(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('calls onGotoRowNumberChanged on number key press', async () => { |
| 100 | + const user = userEvent.setup({ delay: null }); |
| 101 | + const onGotoRowNumberChangedMock = jest.fn(); |
| 102 | + const component = makeGotoRow({ |
| 103 | + onGotoRowNumberChanged: onGotoRowNumberChangedMock, |
| 104 | + }); |
| 105 | + |
| 106 | + const inputElement = screen.getByRole('spinbutton'); |
| 107 | + await user.type(inputElement, '1'); |
| 108 | + |
| 109 | + expect(onGotoRowNumberChangedMock).toHaveBeenCalledTimes(1); |
| 110 | + |
| 111 | + component.unmount(); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('Go to value', () => { |
| 116 | + it('calls onGotoValueInputChanged when input value changes', async () => { |
| 117 | + const user = userEvent.setup({ delay: null }); |
| 118 | + const onGotoValueInputChangedMock = jest.fn(); |
| 119 | + const component = makeGotoRow({ |
| 120 | + onGotoValueChanged: onGotoValueInputChangedMock, |
| 121 | + }); |
| 122 | + |
| 123 | + const inputElement = screen.getByPlaceholderText('value'); |
| 124 | + await user.type(inputElement, 'a'); |
| 125 | + |
| 126 | + expect(onGotoValueInputChangedMock).toHaveBeenCalledTimes(1); |
| 127 | + |
| 128 | + component.unmount(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('calls onGotoValueSelectedFilterChanged when select value changes', async () => { |
| 132 | + const user = userEvent.setup({ delay: null }); |
| 133 | + const onGotoValueSelectedFilterChangedMock = jest.fn(); |
| 134 | + |
| 135 | + jest |
| 136 | + .spyOn(TableUtils, 'getNormalizedType') |
| 137 | + .mockImplementation(() => TableUtils.dataType.STRING); |
| 138 | + |
| 139 | + const component = makeGotoRow({ |
| 140 | + onGotoValueSelectedFilterChanged: onGotoValueSelectedFilterChangedMock, |
| 141 | + }); |
| 142 | + |
| 143 | + const inputElement = screen.getByTestId('filter-type-select'); |
| 144 | + await user.selectOptions(inputElement, [FilterType.contains]); |
| 145 | + |
| 146 | + expect(onGotoValueSelectedFilterChangedMock).toHaveBeenCalledTimes(1); |
| 147 | + |
| 148 | + component.unmount(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('calls onGotoValueSelectedColumnNameChanged when select value changes', async () => { |
| 152 | + const user = userEvent.setup({ delay: null }); |
| 153 | + const onGotoValueSelectedColumnNameChangedMock = jest.fn(); |
| 154 | + const component = makeGotoRow({ |
| 155 | + onGotoValueSelectedColumnNameChanged: onGotoValueSelectedColumnNameChangedMock, |
| 156 | + }); |
| 157 | + |
| 158 | + const inputElement = screen.getByTestId('column-name-select'); |
| 159 | + await user.selectOptions(inputElement, ['1']); |
| 160 | + |
| 161 | + expect(onGotoValueSelectedColumnNameChangedMock).toHaveBeenCalledTimes(1); |
| 162 | + |
| 163 | + component.unmount(); |
| 164 | + }); |
| 165 | +}); |
0 commit comments