import 'reflect-metadata' ; import { AxeResults } from 'axe-core' ; import { AxePuppeteerFactory , AxeScanResults , Page } from 'scanner-global-library' ; import { IMock , It , Mock , Times } from 'typemoq' ; import { AIScanner } from './ai-scanner' ; describe ( 'AIScanner' , ( ) => { let pageMock ; let scanner ; let axeBrowserFactoryMock ; beforeEach ( ( ) => { axeBrowserFactoryMock = Mock . ofType ( ) ; pageMock = Mock . ofType2 < Page > ( Page , [ axeBrowserFactoryMock . object ] ) ; scanner = new AIScanner ( pageMock . object ) ; } ) ; it ( 'should create instance' , ( ) => { expect ( scanner ) . not . toBeNull ( ) ; } ) ; it ( 'should launch browser page with given url and scan the page with axe-core' , async ( ) => { const url = 'some url' ; const axeResultsStub = 'axe results' as any as AxeResults ; setupNewPageCall ( ) ; setupPageNavigateCall ( url ) ; setupPageScanCall ( url , axeResultsStub ) ; await scanner . scan ( url ) ; verifyMocks ( ) ; } ) ; it ( 'should close browser if exception occurs' , async ( ) => { const url = 'some url' ; const errorMessage = `An error occurred while scanning website page ${ url }.` ; setupNewPageCall ( ) ; setupPageNavigateCall ( url ) ; setupPageErrorScanCall ( url , errorMessage ) ; setupPageCloseCall ( ) ; const scanResult = await scanner . scan ( url ) ; expect ( scanResult . error ) . not . toBeNull ( ) ; } ) ; function setupNewPageCall ( ) { pageMock . setup ( async ( p ) => p . create ( It . isObjectWith ( { browserExecutablePath : undefined } ) ) ) . verifiable ( Times . once ( ) ) ; } function setupPageCloseCall ( ) { pageMock . setup ( async ( b ) => b . close ( ) ) . verifiable ( ) ; } function setupPageNavigateCall ( url ) { pageMock . setup ( ( p ) => p . navigateToUrl ( url ) ) . verifiable ( Times . once ( ) ) ; } function setupPageScanCall ( url , axeResults ) { pageMock . setup ( async ( p ) => p . scanForA11yIssues ( undefined ) ) . returns ( async ( ) => Promise . resolve ( { results : axeResults } ) ) . verifiable ( Times . once ( ) ) ; } function setupPageErrorScanCall ( url , errorMessage ) { pageMock . setup ( async ( p ) => p . scanForA11yIssues ( undefined ) ) . returns ( async ( ) => Promise . resolve ( { error : errorMessage } ) ) . verifiable ( Times . once ( ) ) ; } function verifyMocks ( ) { pageMock . verifyAll ( ) ; } } ) ;