import { StageComponent , ComponentTester } from 'aurelia-testing' ; import { bootstrap } from 'aurelia-bootstrapper' ; import { configure , blur } from './shared' ; import { ValidationControllerFactory } from '../src/aurelia-validation' ; import { Container } from 'aurelia-dependency-injection' ; import { Aurelia } from 'aurelia-framework' ; describe ( 'ValidationErrorsCustomAttribute' , ( ) => { let component ; let viewModel ; const parentViewModel = { form : '' , controller : ( ) : any => null , theController : null } ; let container ; const stageTest = ( validationErrors , supplyControllerToViewModel ? ) => { const form = `
      <template>
        <form novalidate autocomplete='off' ${ validationErrors }>
          <input ref='standardInput' type='text' value.bind='standardProp & validateOnBlur'>
        </form>
      </template>` ; parentViewModel . form = form ; component = StageComponent . withResources ( ) . inView ( `<compose containerless view-model="./dist/test/test/resources/validation-errors-form-one" model.bind="{ form: form, controller: controller }"></compose>` ) . boundTo ( parentViewModel ) ; const myConfigure = ( aurelia ) => { const config = configure ( aurelia ) ; container = aurelia . container ; return config ; } ; component . bootstrap ( myConfigure ) ; if ( supplyControllerToViewModel ) { parentViewModel . controller = ( ) => { const factory = container . get ( ValidationControllerFactory ) ; const controller = factory . createForCurrentScope ( ) ; parentViewModel . theController = controller as any ; return controller ; } ; } return component . create ( bootstrap as any ) . then ( ( ) => { viewModel = component . viewModel . currentViewModel ; } ) ; } ; it ( 'sets errors given as default property' , ( done ) => { stageTest ( `validation-errors.bind='myErrors'` ) . then ( ( ) => expect ( viewModel ) . not . toBeNull ( ) ) . then ( ( ) => expect ( viewModel . myErrors instanceof Array ) . toBe ( true ) ) . then ( ( ) => expect ( viewModel . myErrors . length ) . toBe ( 0 ) ) . then ( ( ) => { blur ( viewModel . standardInput ) ; return new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( ) ; } , 0 ) ; } ) ; } ) . then ( ( ) => expect ( viewModel . myErrors . length ) . toBe ( 1 ) ) . then ( done ) . catch ( e => { console . log ( e . toString ( ) ) ; done ( ) ; } ) ; } ) ; it ( 'sets errors given as named property' , ( done ) => { stageTest ( `validation-errors='errors.bind:myErrors'` ) . then ( ( ) => expect ( viewModel ) . not . toBeNull ( ) ) . then ( ( ) => expect ( viewModel . myErrors instanceof Array ) . toBe ( true ) ) . then ( ( ) => expect ( viewModel . myErrors . length ) . toBe ( 0 ) ) . then ( ( ) => { blur ( viewModel . standardInput ) ; return new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( ) ; } , 0 ) ; } ) ; } ) . then ( ( ) => expect ( viewModel . myErrors . length ) . toBe ( 1 ) ) . then ( done ) . catch ( e => { console . log ( e . toString ( ) ) ; done ( ) ; } ) ; } ) ; it ( 'uses given controller' , ( done ) => { stageTest ( `validation-errors='errors.bind:myErrors;controller.bind:controller'` , true ) . then ( ( ) => expect ( viewModel ) . not . toBeNull ( ) ) . then ( ( ) => expect ( parentViewModel . controller ) . not . toBeNull ( ) ) . then ( ( ) => expect ( viewModel . myErrors instanceof Array ) . toBe ( true ) ) . then ( ( ) => expect ( viewModel . myErrors . length ) . toBe ( 0 ) ) . then ( ( ) => { blur ( viewModel . standardInput ) ; return new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( ) ; } , 0 ) ; } ) ; } ) . then ( ( ) => expect ( viewModel . myErrors . length ) . toBe ( 1 ) ) . then ( ( ) => expect ( viewModel . myErrors [ 0 ] . error ) . toEqual ( ( parentViewModel as any ) . theController . errors [ 0 ] ) ) . then ( done ) . catch ( e => { console . log ( e . toString ( ) ) ; done ( ) ; } ) ; } ) ; it ( 'does nothing when given only a controller' , ( done ) => { stageTest ( `validation-errors='controller.bind:controller'` , true ) . then ( ( ) => expect ( viewModel ) . not . toBeNull ( ) ) . then ( ( ) => expect ( parentViewModel . controller ) . not . toBeNull ( ) ) . then ( ( ) => expect ( viewModel . myErrors ) . toBeUndefined ( ) ) . then ( ( ) => { blur ( viewModel . standardInput ) ; return new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( ) ; } , 0 ) ; } ) ; } ) . then ( ( ) => expect ( viewModel . myErrors ) . toBeUndefined ( ) ) . then ( done ) . catch ( e => { console . log ( e . toString ( ) ) ; done ( ) ; } ) ; } ) ; it ( 'does nothing when given nothing' , ( done ) => { stageTest ( `validation-errors=''` , true ) . then ( ( ) => expect ( viewModel ) . not . toBeNull ( ) ) . then ( ( ) => expect ( parentViewModel . controller ) . not . toBeNull ( ) ) . then ( ( ) => expect ( viewModel . myErrors ) . toBeUndefined ( ) ) . then ( ( ) => { blur ( viewModel . standardInput ) ; return new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( ) ; } , 0 ) ; } ) ; } ) . then ( ( ) => expect ( viewModel . myErrors ) . toBeUndefined ( ) ) . then ( done ) . catch ( e => { console . log ( e . toString ( ) ) ; done ( ) ; } ) ; } ) ; afterEach ( ( ) => { if ( component ) { component . dispose ( ) ; } } ) ; } ) ;