interface Page { tag : string ; } let currentPage ; export function waitLoadingEnd ( initialWait = 500 ) { cy . wait ( initialWait ) ; cy . get ( 'div.loading' ) . should ( 'not.exist' ) ; } function onPage < T extends Page > ( page ) { currentPage = new page ( ) ; return cy . get ( currentPage . tag ) ; } export function at < T extends Page > ( type , callback ? ) { onPage ( type ) . should ( 'exist' ) ; waitLoadingEnd ( ) ; if ( callback ) { callback ( currentPage as T ) ; } } export function back ( ) { cy . go ( 'back' ) ; } export function fillFormField ( parent , key , value ) { cy . get ( parent ) . then ( form => { const field = form . find ( `[data-testing-id="${ key }"]` ) ; expect ( field . length ) . to . equal ( 1 , `expected to find one form field "${ key }" in "${ parent }"` ) ; const tagName = field . prop ( 'tagName' ) ; expect ( tagName ) . to . match ( /^(INPUT|SELECT|TEXTAREA)$/ ) ; cy . get ( parent ) . within ( ( ) => { if ( /^(INPUT|TEXTAREA)$/ . test ( tagName ) ) { const inputField = cy . get ( `[data-testing-id="${ key }"]` ) ; inputField . clear ( ) ; if ( value ) { inputField . focus ( ) . type ( value . toString ( ) ) ; } } else if ( tagName === 'SELECT' ) { if ( typeof value === 'number' ) { cy . get ( `[data-testing-id="${ key }"]` ) . find ( 'option' ) . eq ( value as number ) . then ( option => option . attr ( 'value' ) ) . then ( val => cy . get ( `[data-testing-id="${ key }"]` ) . select ( val ) ) ; } else { cy . get ( `[data-testing-id="${ key }"]` ) . select ( value ) ; } } } ) ; } ) ; } export function performAddToCart ( button ) { waitLoadingEnd ( 1000 ) ; cy . intercept ( 'POST' , '**/baskets/*/items' ) . as ( 'basket' ) ; cy . intercept ( 'GET' , '**/baskets/*' ) . as ( 'basketCurrent' ) ; waitLoadingEnd ( 1000 ) ; button ( ) . click ( ) ; return cy . wait ( '@basket' ) . then ( result => ( result . response . statusCode >= 400 ? result : cy . wait ( '@basketCurrent' ) . then ( ( ) => result ) ) ) as any ; }