|
1 | 1 | import React from 'react'
|
2 | 2 |
|
3 | 3 | import { fireEvent } from '@testing-library/dom'
|
4 |
| -import { act } from '@testing-library/react' |
| 4 | +import { |
| 5 | + act, |
| 6 | + within, |
| 7 | +} from '@testing-library/react' |
| 8 | +import userEvent from '@testing-library/user-event' |
5 | 9 |
|
6 | 10 | import { LightFoundation } from '~/src/foundation'
|
7 | 11 |
|
8 | 12 | import { render } from '~/src/utils/testUtils'
|
9 | 13 |
|
10 | 14 | import { COMMON_IME_CONTROL_KEYS } from '~/src/components/Forms/Inputs/constants/CommonImeControlKeys'
|
11 | 15 |
|
12 |
| -import TextField, { TEXT_INPUT_TEST_ID } from './TextField' |
| 16 | +import TextField, { |
| 17 | + TEXT_INPUT_CLEAR_ICON_TEST_ID, |
| 18 | + TEXT_INPUT_TEST_ID, |
| 19 | +} from './TextField' |
13 | 20 | import {
|
14 | 21 | type TextFieldProps,
|
15 | 22 | TextFieldVariant,
|
16 | 23 | } from './TextField.types'
|
17 | 24 | import { getProperTextFieldBgColor } from './TextFieldUtils'
|
18 | 25 |
|
19 | 26 | describe('TextField', () => {
|
| 27 | + const user = userEvent.setup() |
20 | 28 | let props: TextFieldProps
|
21 | 29 |
|
22 | 30 | beforeEach(() => {
|
@@ -285,4 +293,54 @@ describe('TextField', () => {
|
285 | 293 | })
|
286 | 294 | })
|
287 | 295 | })
|
| 296 | + |
| 297 | + describe('show remove button only when it is filled and focused/hovered', () => { |
| 298 | + it('disappear when empty & focused/hovered', async () => { |
| 299 | + const { getByTestId } = renderComponent({ value: '', allowClear: true }) |
| 300 | + const rendered = getByTestId(TEXT_INPUT_TEST_ID) |
| 301 | + const input = rendered.getElementsByTagName('input')[0] |
| 302 | + |
| 303 | + await act(async () => { |
| 304 | + await user.hover(input) |
| 305 | + input.focus() |
| 306 | + }) |
| 307 | + |
| 308 | + const clearButton = within(rendered).queryByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID) |
| 309 | + expect(clearButton).toBeNull() |
| 310 | + }) |
| 311 | + |
| 312 | + it('disappear when filled & not focused/hovered', () => { |
| 313 | + const { getByTestId } = renderComponent({ value: 'test', allowClear: true }) |
| 314 | + const rendered = getByTestId(TEXT_INPUT_TEST_ID) |
| 315 | + |
| 316 | + const clearButton = within(rendered).queryByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID) |
| 317 | + expect(clearButton).toBeNull() |
| 318 | + }) |
| 319 | + |
| 320 | + it('appear when filled & hovered', async () => { |
| 321 | + const { getByTestId } = renderComponent({ value: 'test', allowClear: true }) |
| 322 | + const rendered = getByTestId(TEXT_INPUT_TEST_ID) |
| 323 | + const input = rendered.getElementsByTagName('input')[0] |
| 324 | + |
| 325 | + await act(async () => { |
| 326 | + await user.hover(input) |
| 327 | + }) |
| 328 | + |
| 329 | + const clearButton = within(rendered).getByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID) |
| 330 | + expect(clearButton).toBeInTheDocument() |
| 331 | + }) |
| 332 | + |
| 333 | + it('appear when filled & focused', () => { |
| 334 | + const { getByTestId } = renderComponent({ value: 'test', allowClear: true }) |
| 335 | + const rendered = getByTestId(TEXT_INPUT_TEST_ID) |
| 336 | + const input = rendered.getElementsByTagName('input')[0] |
| 337 | + |
| 338 | + act(() => { |
| 339 | + input.focus() |
| 340 | + }) |
| 341 | + |
| 342 | + const clearButton = within(rendered).getByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID) |
| 343 | + expect(clearButton).toBeInTheDocument() |
| 344 | + }) |
| 345 | + }) |
288 | 346 | })
|
0 commit comments