import 'cypress-plugin-tab' ; import { noOp } from '../../src/utils/function' ; import { forEach } from '../../src/utils/object' ; import { AnyFunction } from '../../src/utils/types' ; import { AdapterMockedResponses } from '../../src/adapter/mocked-adapter' ; declare global { namespace Cypress { interface Chainable extends CustomCommands , CustomDualCommands { } } } import Loggable = Cypress . Loggable ; import Shadow = Cypress . Shadow ; import Timeoutable = Cypress . Timeoutable ; import Withinable = Cypress . Withinable ; interface CustomCommands { example param query returns searchQuery ( query ) : Cypress . Chainable < JQuery > ; example param queries returns searchQueries ( ... queries ) : void ; example param query returns typeQuery ( query ) : Cypress . Chainable < JQuery > ; example returns focusSearchInput ( ) : Cypress . Chainable < JQuery > ; example returns clearSearchInput ( ) : Cypress . Chainable < JQuery > ; param label returns getFilterWithLabel ( label ) : Cypress . Chainable < JQuery > ; returns getSelectedFilters ( ) : Cypress . Chainable < JQuery > ; param label param options returns clickFilterWithLabel ( label , options ? ) : Cypress . Chainable < JQuery > ; param label param status returns assertFilterIs ( label , status ) : Cypress . Chainable < JQuery > ; /**
     * Waits for the results to update by checking the existence of a loading item.
     */ waitForResultsToRender ( ) : void ; param searchResponse fakeSearchResponse ( searchResponse ) : void ; param query param toContain checkNextQueries ( query , toContain ) : void ; } interface CustomDualCommands { example param value param options returns getByDataTest ( value , options ? ) : Cypress . Chainable < JQuery > ; } type AddPreviousParam < Functions extends Record < keyof Functions , AnyFunction > > = { [ Key in keyof Functions ] : ( previous : unknown , ... args : Parameters < Functions [ Key ] > ) => ReturnType < Functions [ Key ] > ; } ; type SelectedStatus = 'selected' | 'unselected' ; interface ClickFilterOptions { filterShouldBe ? : SelectedStatus ; } export type CypressCommandOptions = Partial < Loggable & Timeoutable & Withinable & Shadow > ; const customCommands = { searchQuery : query => cy . typeQuery ( query ) . type ( '{enter}' ) , searchQueries : ( ... queries ) => { queries . forEach ( query => { cy . clearSearchInput ( ) ; cy . typeQuery ( query ) . type ( '{enter}' ) ; } ) ; } , typeQuery : query => cy . getByDataTest ( 'search-input' ) . type ( query ) , focusSearchInput : ( ) => cy . getByDataTest ( 'search-input' ) . click ( ) , clearSearchInput : ( ) => cy . getByDataTest ( 'clear-search-input' ) . click ( ) , getFilterWithLabel ( label ) { return cy . getByDataTest ( 'filter' ) . contains ( label ) ; } , clickFilterWithLabel ( label , options = { } ) { const chain = cy . getFilterWithLabel ( label ) . click ( ) ; if ( options . filterShouldBe !== undefined ) { cy . assertFilterIs ( label , options . filterShouldBe ) ; } return chain ; } , assertFilterIs ( label , selectedStatus ) { const selectedClass = 'x-filter--is-selected' ; if ( selectedStatus === 'selected' ) { return cy . getFilterWithLabel ( label ) . should ( 'have.attr' , 'aria-checked' , 'true' ) . and ( 'have.class' , selectedClass ) ; } else { return cy . getFilterWithLabel ( label ) . should ( 'have.attr' , 'aria-checked' , 'false' ) . and ( 'not.have.class' , selectedClass ) ; } } , getSelectedFilters ( ) { return cy . get ( '.x-filter--is-selected' ) ; } , waitForResultsToRender ( ) { cy . getByDataTest ( 'loading' ) . should ( 'exist' ) ; cy . getByDataTest ( 'loading' ) . should ( 'not.exist' ) . then ( noOp ) ; } , fakeSearchResponse : searchResponse => { cy . window ( ) . then ( window => { window . __mockedAdapter . responses . search = { banners : [ ] , facets : [ ] , partialResults : [ ] , promoteds : [ ] , queryTagging : { params : { } , url : '' } , redirections : [ ] , results : [ ] , spellcheck : '' , totalResults : 0 , ... searchResponse } ; } ) ; } , checkNextQueries ( query , toContain ) { cy . getByDataTest ( 'next-query' ) . should ( queries => { queries . each ( ( _ , e ) => { if ( toContain ) { expect ( e ) . to . contain ( query ) ; } else { expect ( e ) . to . not . contain ( query ) ; } } ) ; } ) ; } } ; const customDualCommands = { getByDataTest : ( previous , value , options ? ) => { const selector = `[data-test=${ value }]` ; return previous ? cy . wrap ( previous ) . find ( selector , options ) : cy . get ( selector , options ) ; } } ; forEach ( customCommands , ( name , implementation ) => { Cypress . Commands . add ( name , implementation ) ; } ) ; forEach ( customDualCommands , ( name , implementation ) => { Cypress . Commands . add ( name , { prevSubject : 'optional' } , implementation ) ; } ) ;