11import { module , test } from 'qunit' ;
22import { setupRenderingTest } from 'ember-qunit' ;
3- import { render , triggerEvent , fillIn , blur } from '@ember/test-helpers' ;
4-
3+ import { render , triggerEvent , fillIn , focus , blur } from '@ember/test-helpers' ;
54import hbs from 'htmlbars-inline-precompile' ;
6-
75import {
86 validatePresence ,
97 validateLength ,
10- validateConfirmation ,
11- validateFormat ,
128} from 'ember-changeset-validations/validators' ;
139
1410module ( 'Integration | Component | bs form element' , function ( hooks ) {
@@ -21,19 +17,7 @@ module('Integration | Component | bs form element', function(hooks) {
2117 ]
2218 } ;
2319
24- const extendedValidation = {
25- name : [
26- validatePresence ( true ) ,
27- validateLength ( { min : 4 } )
28- ] ,
29- email : validateFormat ( { type : 'email' , allowBlank : true } ) ,
30- password : [
31- validateLength ( { min : 6 } )
32- ] ,
33- passwordConfirmation : validateConfirmation ( { on : 'password' } )
34- }
35-
36- test ( 'valid validation is supported as expected' , async function ( assert ) {
20+ test ( 'form is submitted if valid and validation success shown' , async function ( assert ) {
3721 let model = {
3822 name : '1234' ,
3923 } ;
@@ -57,7 +41,7 @@ module('Integration | Component | bs form element', function(hooks) {
5741 assert . verifySteps ( [ 'submit action has been called.' ] ) ;
5842 } ) ;
5943
60- test ( 'invalid validation is supported as expected ' , async function ( assert ) {
44+ test ( 'validation errors are shown on submit ' , async function ( assert ) {
6145 let model = {
6246 name : '' ,
6347 } ;
@@ -82,50 +66,121 @@ module('Integration | Component | bs form element', function(hooks) {
8266 assert . verifySteps ( [ 'Invalid action has been called.' ] ) ;
8367 } ) ;
8468
69+ test ( 'validation errors are shown after blur' , async function ( assert ) {
70+ this . set ( 'model' , { name : '' } ) ;
71+ this . set ( 'validation' , validation ) ;
8572
86- test ( 'more complicated validations' , async function ( assert ) {
87- let model = {
88- name : '' ,
89- password : null ,
90- passwordConfirmation : null ,
91- email : '' ,
92- } ;
73+ await render ( hbs `
74+ <BsForm @model={{changeset this.model this.validation}} as |form|>
75+ <form.element @label="Name" @property="name" />
76+ </BsForm>
77+ ` ) ;
78+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
9379
94- this . set ( 'model' , model ) ;
95- this . set ( 'validation' , extendedValidation ) ;
96- this . submitAction = function ( ) {
97- assert . ok ( false , 'submit action must not been called.' ) ;
98- } ;
99- this . invalidAction = function ( ) {
100- assert . step ( 'Invalid action has been called.' ) ;
101- } ;
80+ await focus ( 'input' ) ;
81+ await blur ( 'input' ) ;
82+ assert . dom ( 'input' ) . hasClass ( 'is-invalid' ) ;
83+ } ) ;
84+
85+ test ( 'validation success is shown after blur' , async function ( assert ) {
86+ this . set ( 'model' , { name : 'Clara' } ) ;
87+ this . set ( 'validation' , validation ) ;
10288
10389 await render ( hbs `
104- <BsForm @model={{changeset this.model this.validation}} @onSubmit={{this.submitAction}} @onInvalid={{this.invalidAction}} as |form|>
105- <form.element id="name" @label="Name" @property="name" />
106- <form.element id="email" @label="Email" @property="email" />
107- <form.element id="password" @label="Password" @property="password" />
108- <form.element id="password-confirmation" @label="Password confirmation" @property="passwordConfirmation" />
90+ <BsForm @model={{changeset this.model this.validation}} as |form|>
91+ <form.element @label="Name" @property="name" />
10992 </BsForm>
11093 ` ) ;
94+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' ) ;
11195
112- await fillIn ( '#password input' , 'bad' ) ;
113- assert . dom ( '#password input' ) . doesNotHaveClass ( 'is-invalid' , 'password does not have error while typing.' ) ;
114- assert . dom ( '#password input' ) . doesNotHaveClass ( 'is-valid' , 'password does not have success while typing.' ) ;
96+ await focus ( 'input' ) ;
97+ await blur ( 'input' ) ;
98+ assert . dom ( 'input' ) . hasClass ( 'is-valid' ) ;
99+ } ) ;
115100
116- await blur ( '#password input' ) ;
117- assert . dom ( '#password input' ) . hasClass ( 'is-invalid' , 'password does have error when focus out.' ) ;
101+ test ( 'validation errors are shown after user input' , async function ( assert ) {
102+ this . set ( 'model' , { name : '' } ) ;
103+ this . set ( 'validation' , validation ) ;
118104
119- await fillIn ( '#password-confirmation input' , 'betterpass' ) ;
120- assert . dom ( '#password-confirmation input' ) . doesNotHaveClass ( 'is-invalid' , 'password confirmation does not have error while typing.' ) ;
105+ await render ( hbs `
106+ <BsForm @model={{changeset this.model this.validation}} as |form|>
107+ <form.element @label="Name" @property="name" />
108+ </BsForm>
109+ ` ) ;
110+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
111+
112+ await fillIn ( 'input' , 'R' ) ;
113+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' , 'validation is not shown while user is typing' ) ;
114+
115+ await blur ( 'input' ) ;
116+ assert . dom ( 'input' ) . hasClass ( 'is-invalid' , 'validation error is shown after focus out' ) ;
117+ } ) ;
121118
122- await blur ( '#password-confirmation input' ) ;
123- assert . dom ( '#password-confirmation input' ) . hasClass ( 'is-invalid' , 'password confirmation does have error when focus out.' ) ;
119+ test ( 'validation success is shown after user input' , async function ( assert ) {
120+ this . set ( 'model' , { name : '' } ) ;
121+ this . set ( 'validation' , validation ) ;
122+
123+ await render ( hbs `
124+ <BsForm @model={{changeset this.model this.validation}} as |form|>
125+ <form.element @label="Name" @property="name" />
126+ </BsForm>
127+ ` ) ;
128+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' ) ;
129+
130+ await fillIn ( 'input' , 'Rosa' ) ;
131+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' , 'validation is not shown while user is typing' ) ;
132+
133+ await blur ( 'input' ) ;
134+ assert . dom ( 'input' ) . hasClass ( 'is-valid' , 'validation error is shown after focus out' ) ;
135+ } ) ;
136+
137+ test ( 'does not break forms which are not using a changeset as model' , async function ( assert ) {
138+ this . set ( 'model' , { name : '' } ) ;
139+ this . set ( 'submitAction' , ( ) => {
140+ assert . step ( 'submit action has been called' ) ;
141+ } ) ;
142+
143+ await render ( hbs `
144+ <BsForm @model={{this.model}} @onSubmit={{this.submitAction}} as |form|>
145+ <form.element @label="Name" @property="name" />
146+ </BsForm>
147+ ` ) ;
148+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' ) ;
149+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
150+
151+ await fillIn ( 'input' , 'Rosa' ) ;
152+ await blur ( 'input' ) ;
153+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' ) ;
154+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
124155
125156 await triggerEvent ( 'form' , 'submit' ) ;
126- assert . dom ( '#password input' ) . hasClass ( 'is-invalid' , 'password still has error after submit. ') ;
127- assert . dom ( '#password-confirmation input' ) . hasClass ( 'is-invalid' , 'password confirmation still has error after submit. ') ;
128- assert . verifySteps ( [ 'Invalid action has been called. ' ] ) ;
157+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid ' ) ;
158+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
159+ assert . verifySteps ( [ 'submit action has been called' ] ) ;
129160 } ) ;
130161
162+ test ( 'does not break for forms which are not having a model at all' , async function ( assert ) {
163+ this . set ( 'submitAction' , ( ) => {
164+ assert . step ( 'submit action has been called' ) ;
165+ } ) ;
166+ this . set ( 'noop' , ( ) => { } ) ;
167+
168+ await render ( hbs `
169+ <BsForm @onSubmit={{this.submitAction}} as |form|>
170+ <form.element @label="Name" @property="name" @onChange={{this.noop}} />
171+ </BsForm>
172+ ` ) ;
173+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' ) ;
174+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
175+
176+ await fillIn ( 'input' , 'Rosa' ) ;
177+ await blur ( 'input' ) ;
178+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' ) ;
179+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
180+
181+ await triggerEvent ( 'form' , 'submit' ) ;
182+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-valid' ) ;
183+ assert . dom ( 'input' ) . doesNotHaveClass ( 'is-invalid' ) ;
184+ assert . verifySteps ( [ 'submit action has been called' ] ) ;
185+ } ) ;
131186} ) ;
0 commit comments