import { PlaywrightFluent } from '../../../fluent-api' ; import * as path from 'path' ; describe ( 'Selector API - next sibling' , ( ) : void => { let p ; beforeEach ( ( ) : void => { p = new PlaywrightFluent ( ) ; } ) ; afterEach ( async ( ) : Promise < void > => { await p . close ( ) ; } ) ; test ( 'should get no handle on wrong selector' , async ( ) : Promise < void > => { const url = `file:${ path . join ( __dirname , 'next-sibling.test.html' ) }` ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : true } ) . withCursor ( ) . navigateTo ( url ) ; const selector = p . selector ( '[role="row"]' ) . withText ( 'foo and bar and baz' ) . nextSibling ( ) ; const handles = await selector . getAllHandles ( ) ; expect ( Array . isArray ( handles ) ) . toBe ( true ) ; expect ( handles . length ) . toBe ( 0 ) ; } ) ; test ( 'should get next sibling handle' , async ( ) : Promise < void > => { const url = `file:${ path . join ( __dirname , 'next-sibling.test.html' ) }` ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : true } ) . withCursor ( ) . navigateTo ( url ) ; const selector = p . selector ( '[role="row"]' ) . find ( 'select[data-test-id="my-select2"]' ) . find ( 'option' ) . nextSibling ( ) ; const handles = await selector . getAllHandles ( ) ; expect ( Array . isArray ( handles ) ) . toBe ( true ) ; expect ( handles . length ) . toBe ( 2 ) ; expect ( await handles [ 0 ] . evaluate ( ( node ) => ( node as HTMLSelectElement ) . tagName ) ) . toBe ( 'OPTION' ) ; expect ( await handles [ 0 ] . evaluate ( ( node ) => ( node as HTMLElement ) . innerText ) ) . toBe ( 'Select 2 - label 2' ) ; expect ( await handles [ 1 ] . evaluate ( ( node ) => ( node as HTMLSelectElement ) . tagName ) ) . toBe ( 'OPTION' ) ; expect ( await handles [ 1 ] . evaluate ( ( node ) => ( node as HTMLElement ) . innerText ) ) . toBe ( 'Select 2 - label 3' ) ; expect ( selector . toString ( ) ) . toBe ( `selector([role="row"])
  .find(select[data-test-id="my-select2"])
  .find(option)
  .nextSibling()` ) ; } ) ; test ( 'should get handles, even when selector is created before page is instanciated' , async ( ) : Promise < void > => { const selector = p . selector ( '[role="row"]' ) . find ( 'select[data-test-id="my-select2"]' ) . find ( 'option' ) . nextSibling ( ) ; const url = `file:${ path . join ( __dirname , 'next-sibling.test.html' ) }` ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : true } ) . withCursor ( ) . navigateTo ( url ) ; const handles = await selector . getAllHandles ( ) ; expect ( Array . isArray ( handles ) ) . toBe ( true ) ; expect ( handles . length ) . toBe ( 2 ) ; expect ( await handles [ 0 ] . evaluate ( ( node ) => ( node as HTMLSelectElement ) . tagName ) ) . toBe ( 'OPTION' ) ; expect ( await handles [ 0 ] . evaluate ( ( node ) => ( node as HTMLElement ) . innerText ) ) . toBe ( 'Select 2 - label 2' ) ; expect ( await handles [ 1 ] . evaluate ( ( node ) => ( node as HTMLSelectElement ) . tagName ) ) . toBe ( 'OPTION' ) ; expect ( await handles [ 1 ] . evaluate ( ( node ) => ( node as HTMLElement ) . innerText ) ) . toBe ( 'Select 2 - label 3' ) ; expect ( selector . toString ( ) ) . toBe ( `selector([role="row"])
  .find(select[data-test-id="my-select2"])
  .find(option)
  .nextSibling()` ) ; } ) ; test ( 'should select sibling option' , async ( ) : Promise < void > => { const url = `file:${ path . join ( __dirname , 'next-sibling.test.html' ) }` ; const select = 'select[data-test-id="my-select1"]' ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : true } ) . withCursor ( ) . navigateTo ( url ) . hover ( select ) ; const firstOption = p . selector ( '[role="row"]' ) . find ( select ) . find ( 'option' ) ; const nextOption = ( await firstOption . nextSibling ( ) . innerText ( ) ) || 'unknown' ; await p . select ( nextOption ) . in ( select ) ; const selectedOption = await p . getSelectedOptionOf ( select ) ; expect ( selectedOption ! . label ) . toBe ( 'Select 1 - label 2' ) ; } ) ; } ) ;