11import EmberObject from '@ember/object' ;
22import { module , test } from 'qunit' ;
33import { setupRenderingTest } from 'ember-qunit' ;
4- import { render , find , triggerEvent } from '@ember/test-helpers' ;
4+ import { render , find , triggerEvent , fillIn , focus , blur } from '@ember/test-helpers' ;
5+
56import hbs from 'htmlbars-inline-precompile' ;
67
78import {
89 validatePresence ,
9- validateLength
10+ validateLength ,
11+ validateConfirmation ,
12+ validateFormat ,
1013} from 'ember-changeset-validations/validators' ;
1114
1215module ( 'Integration | Component | bs form element' , function ( hooks ) {
@@ -24,6 +27,18 @@ module('Integration | Component | bs form element', function(hooks) {
2427 ]
2528 } ;
2629
30+ const extendedValidation = {
31+ name : [
32+ validatePresence ( true ) ,
33+ validateLength ( { min : 4 } )
34+ ] ,
35+ email : validateFormat ( { type : 'email' , allowBlank : true } ) ,
36+ password : [
37+ validateLength ( { min : 6 } )
38+ ] ,
39+ passwordConfirmation : validateConfirmation ( { on : 'password' } )
40+ }
41+
2742 test ( 'valid validation is supported as expected' , async function ( assert ) {
2843 let model = EmberObject . create ( {
2944 name : '1234'
@@ -79,4 +94,53 @@ module('Integration | Component | bs form element', function(hooks) {
7994 await triggerEvent ( 'form' , 'submit' ) ;
8095 assert . ok ( find ( '.form-group' ) . classList . contains ( 'has-error' ) , 'form element group has error class' ) ;
8196 } ) ;
97+
98+
99+ test ( 'more complicated validations' , async function ( assert ) {
100+ let model = EmberObject . create ( {
101+ name : '' ,
102+ password : null ,
103+ passwordConfirmation : null ,
104+ email : ''
105+ } ) ;
106+
107+ this . set ( 'model' , model ) ;
108+ this . set ( 'validation' , extendedValidation ) ;
109+ this . actions . submitAction = function ( ) {
110+ assert . ok ( false , 'submit action must not been called.' ) ;
111+ } ;
112+ this . actions . invalidAction = function ( ) {
113+ assert . ok ( true , 'Invalid action has been called.' ) ;
114+ } ;
115+
116+ this . actions . validate = function ( ) {
117+ return 'invalid' ;
118+ } ;
119+
120+ await render ( hbs `
121+ {{#bs-form model=(changeset model validation) onSubmit=(action "submitAction") onInvalid=(action "invalidAction") as |form|}}
122+ {{form.element id="name" label="Name" property="name"}}
123+ {{form.element id="email" label="Email" property="email"}}
124+ {{form.element id="password" label="Password" property="password"}}
125+ {{form.element id="password-confirmation" label="Password confirmation" property="passwordConfirmation"}}
126+ {{/bs-form}}
127+ ` ) ;
128+
129+ assert . expect ( 6 ) ;
130+
131+ await fillIn ( '#password-field' , 'bad' ) ;
132+ assert . notOk ( find ( '#password' ) . classList . contains ( 'has-error' ) , 'password does not have error while typing.' ) ;
133+ await blur ( '#password-field' ) ;
134+ assert . ok ( find ( '#password' ) . classList . contains ( 'has-error' ) , 'password does have error when focus out.' ) ;
135+
136+ await fillIn ( '#password-confirmation-field' , 'betterpass' ) ;
137+ assert . notOk ( find ( '#password-confirmation' ) . classList . contains ( 'has-error' ) , 'password confirmation does not have error while typing.' ) ;
138+ await focus ( '#password-field' ) ;
139+ assert . ok ( find ( '#password-confirmation' ) . classList . contains ( 'has-error' ) , 'password confirmation does have error when focus out.' ) ;
140+
141+
142+ await triggerEvent ( 'form' , 'submit' ) ;
143+ assert . ok ( find ( '.form-group' ) . classList . contains ( 'has-error' ) , 'form element group has error class' ) ;
144+ } ) ;
145+
82146} ) ;
0 commit comments