@@ -2,7 +2,7 @@ import Search from 'components/common/Search/Search';
22import React from 'react' ;
33import { render } from 'lib/testHelpers' ;
44import userEvent from '@testing-library/user-event' ;
5- import { screen } from '@testing-library/react' ;
5+ import { screen , fireEvent } from '@testing-library/react' ;
66import { useSearchParams } from 'react-router-dom' ;
77
88jest . mock ( 'use-debounce' , ( ) => ( {
@@ -42,23 +42,34 @@ describe('Search', () => {
4242 expect ( screen . queryByPlaceholderText ( 'Search' ) ) . toBeInTheDocument ( ) ;
4343 } ) ;
4444
45- it ( 'Clear button is visible' , ( ) => {
45+ it ( 'Clear button is not visible by default' , async ( ) => {
4646 render ( < Search placeholder = { placeholder } /> ) ;
4747
48- const clearButton = screen . getByRole ( 'button' ) ;
48+ const clearButton = screen . queryByTestId ( 'search-clear-button' ) ;
49+ expect ( clearButton ) . not . toBeInTheDocument ( ) ;
50+ } ) ;
51+
52+ it ( 'Clear button is visible if value passed' , async ( ) => {
53+ render ( < Search placeholder = { placeholder } value = "text" /> ) ;
54+
55+ const clearButton = screen . queryByTestId ( 'search-clear-button' ) ;
4956 expect ( clearButton ) . toBeInTheDocument ( ) ;
5057 } ) ;
5158
5259 it ( 'Clear button should clear text from input' , async ( ) => {
5360 render ( < Search placeholder = { placeholder } /> ) ;
5461
5562 const searchField = screen . getAllByRole ( 'textbox' ) [ 0 ] ;
56- await userEvent . type ( searchField , 'some text' ) ;
57- expect ( searchField ) . toHaveValue ( 'some text ' ) ;
63+ fireEvent . change ( searchField , { target : { value : 'hello' } } ) ;
64+ expect ( searchField ) . toHaveValue ( 'hello ' ) ;
5865
59- const clearButton = screen . getByRole ( 'button' ) ;
60- await userEvent . click ( clearButton ) ;
66+ let clearButton = screen . queryByTestId ( 'search-clear-button' ) ;
67+ expect ( clearButton ) . toBeInTheDocument ( ) ;
68+ await userEvent . click ( clearButton ! ) ;
6169
6270 expect ( searchField ) . toHaveValue ( '' ) ;
71+
72+ clearButton = screen . queryByTestId ( 'search-clear-button' ) ;
73+ expect ( clearButton ) . not . toBeInTheDocument ( ) ;
6374 } ) ;
6475} ) ;
0 commit comments