2019-03-13 22:40:27 +03:00
import React from 'react' ;
2019-04-22 23:34:33 +03:00
import { mountWithContexts } from '../enzymeHelpers' ;
2019-03-13 22:40:27 +03:00
import Search from '../../src/components/Search' ;
describe ( '<Search />' , ( ) => {
let search ;
afterEach ( ( ) => {
if ( search ) {
search = null ;
}
} ) ;
test ( 'it triggers the expected callbacks' , ( ) => {
const columns = [ { name : 'Name' , key : 'name' , isSortable : true } ] ;
const searchBtn = 'button[aria-label="Search"]' ;
const searchTextInput = 'input[aria-label="Search text input"]' ;
const onSearch = jest . fn ( ) ;
2019-04-22 23:34:33 +03:00
search = mountWithContexts (
< Search
sortedColumnKey = "name"
columns = { columns }
onSearch = { onSearch }
/ >
2019-03-13 22:40:27 +03:00
) ;
search . find ( searchTextInput ) . instance ( ) . value = 'test-321' ;
search . find ( searchTextInput ) . simulate ( 'change' ) ;
search . find ( searchBtn ) . simulate ( 'click' ) ;
expect ( onSearch ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( onSearch ) . toBeCalledWith ( 'test-321' ) ;
} ) ;
2019-03-14 20:36:50 +03:00
test ( 'handleDropdownToggle properly updates state' , async ( ) => {
2019-03-13 22:40:27 +03:00
const columns = [ { name : 'Name' , key : 'name' , isSortable : true } ] ;
const onSearch = jest . fn ( ) ;
2019-04-22 23:34:33 +03:00
const wrapper = mountWithContexts (
< Search
sortedColumnKey = "name"
columns = { columns }
onSearch = { onSearch }
/ >
2019-03-13 22:40:27 +03:00
) . find ( 'Search' ) ;
expect ( wrapper . state ( 'isSearchDropdownOpen' ) ) . toEqual ( false ) ;
2019-03-14 20:36:50 +03:00
wrapper . instance ( ) . handleDropdownToggle ( true ) ;
2019-03-13 22:40:27 +03:00
expect ( wrapper . state ( 'isSearchDropdownOpen' ) ) . toEqual ( true ) ;
} ) ;
2019-03-14 20:36:50 +03:00
test ( 'handleDropdownSelect properly updates state' , async ( ) => {
2019-03-13 22:40:27 +03:00
const columns = [
{ name : 'Name' , key : 'name' , isSortable : true } ,
{ name : 'Description' , key : 'description' , isSortable : true }
] ;
const onSearch = jest . fn ( ) ;
2019-04-22 23:34:33 +03:00
const wrapper = mountWithContexts (
< Search
sortedColumnKey = "name"
columns = { columns }
onSearch = { onSearch }
/ >
2019-03-13 22:40:27 +03:00
) . find ( 'Search' ) ;
expect ( wrapper . state ( 'searchKey' ) ) . toEqual ( 'name' ) ;
2019-03-14 20:36:50 +03:00
wrapper . instance ( ) . handleDropdownSelect ( { target : { innerText : 'Description' } } ) ;
2019-03-13 22:40:27 +03:00
expect ( wrapper . state ( 'searchKey' ) ) . toEqual ( 'description' ) ;
} ) ;
} ) ;