import { And , Given , Then , When } from 'cypress-cucumber-preprocessor/steps' ; import { InstallXOptions } from '../../../src/x-installer/x-installer/types' ; let historyQueriesList ; Given ( 'following config: hide if equals query {boolean}, debounce {int}, requested items {int}, rendered {int}, instant search {boolean}' , ( hideIfEqualsQuery , debounceInMs , maxItemsToStore , maxItemsToRender , instant ) => { const config = { historyQueries : { config : { hideIfEqualsQuery , debounceInMs , maxItemsToStore } } } ; cy . visit ( '/?useMockedAdapter=true' , { qs : { xModules : JSON . stringify ( config ) } } ) . then ( ( ) => { cy . getByDataTest ( 'history-queries-max-to-render' ) . clear ( ) . type ( maxItemsToRender . toString ( ) ) ; if ( ! instant ) { cy . getByDataTest ( 'search-input-instant' ) . uncheck ( ) ; } else { cy . getByDataTest ( 'search-input-instant' ) . check ( ) ; } } ) ; } ) ; And ( 'a {string} of queries already searched' , ( list ) => { historyQueriesList = list . split ( ', ' ) ; cy . searchQueries ( ... historyQueriesList ) ; cy . clearSearchInput ( ) ; } ) ; When ( 'history query number {int} is clicked' , ( historyQueryItem ) => { cy . getByDataTest ( 'history-query' ) . eq ( historyQueryItem ) . click ( ) . invoke ( 'text' ) . as ( 'searchedQuery' ) ; } ) ; And ( 'the searched query is not displayed in history queries if {boolean} is true' , function ( this , hideIfEqualsQuery ) { if ( ! hideIfEqualsQuery ) { cy . getByDataTest ( 'history-query' ) . eq ( 0 ) . should ( 'have.text' , this . searchedQuery ) ; } else { cy . getByDataTest ( 'history-query' ) . eq ( 0 ) . should ( 'not.have.text' , this . searchedQuery ) ; } } ) ; When ( 'the delete button of {int} is clicked' , ( historyQueryItem ) => { cy . getByDataTest ( 'history-query' ) . eq ( historyQueryItem ) . invoke ( 'text' ) . as ( 'deletedHistoryQuery' ) . then ( ( ) => { cy . getByDataTest ( 'remove-history-query' ) . eq ( historyQueryItem ) . click ( ) ; } ) ; } ) ; Then ( 'the deleted history query is removed from history queries' , function ( this ) { cy . getByDataTest ( 'history-query' ) . should ( historicalQueries => { historicalQueries . each ( ( _ , e ) => { expect ( e ) . to . not . have . text ( this . deletedHistoryQuery ) ; } ) ; } ) ; } ) ; And ( 'the number of rendered history queries is {int} - 1 if {int} < {int}' , ( maxItemsToRender , maxItemsToStore ) => { if ( maxItemsToRender >= maxItemsToStore ) { cy . getByDataTest ( 'history-query' ) . should ( 'have.length' , maxItemsToStore - 1 ) ; } else { cy . getByDataTest ( 'history-query' ) . should ( 'have.length' , maxItemsToRender ) ; } } ) ; Then ( 'no history queries are displayed' , ( ) => { cy . getByDataTest ( 'history-queries' ) . should ( 'not.exist' ) ; } ) ; And ( 'clear history queries button is disabled' , ( ) => { cy . getByDataTest ( 'clear-history-queries' ) . should ( 'be.disabled' ) ; } ) ; Then ( '{string} is deleted from history queries, whereas {string} remains' , ( query , followingQuery ) => { cy . getByDataTest ( 'history-query' ) . should ( historicalQueries => { historicalQueries . each ( ( _ , e ) => { expect ( e ) . to . contain ( query + followingQuery ) ; expect ( e ) . not . to . equal ( query ) ; } ) ; } ) ; } ) ; Then ( 'no history queries are displayed after {int} ms if {boolean} is false' , ( debounceInMs , instant ) => { cy . wait ( debounceInMs ) ; cy . clearSearchInput ( ) . then ( ( ) => { cy . getByDataTest ( 'search-input' ) . click ( ) ; } ) ; if ( ! instant ) { cy . getByDataTest ( 'history-query' ) . should ( 'not.exist' ) ; } else { cy . getByDataTest ( 'history-query' ) . should ( 'exist' ) ; } } ) ; And ( 'intro is pressed' , ( ) => { cy . getByDataTest ( 'search-input' ) . type ( '{enter}' ) ; } ) ; Then ( 'displayed history queries are min of number of queries already searched, max requested items {int}, max rendered items {int}' , ( maxItemsToStore , maxItemsToRender ) => { const queriesToDisplay = Math . min ( historyQueriesList . length , maxItemsToStore , maxItemsToRender ) ; cy . getByDataTest ( 'history-query' ) . should ( 'have.length' , queriesToDisplay ) ; } ) ; Then ( 'the searched query is removed from {int} position in history queries' , function ( this , historyQueryItem ) { cy . getByDataTest ( 'history-query' ) . eq ( historyQueryItem ) . should ( 'not.have.text' , this . searchedQuery ) ; } ) ; And ( 'the searched query is the first item in history queries' , function ( this ) { cy . getByDataTest ( 'history-query' ) . first ( ) . should ( 'have.text' , this . searchedQuery ) ; } ) ;