@@ -69,7 +69,7 @@ describe("ouiField", () => {
6969 expect ( getLabel ( element ) ) . toBeNull ( ) ;
7070 } ) ;
7171
72- it ( "should set the for attribute on label" , ( ) => {
72+ it ( "should set the ' for' attribute on label" , ( ) => {
7373 const id = "lastname" ;
7474
7575 const element = TestUtils . compileTemplate ( `
@@ -90,6 +90,60 @@ describe("ouiField", () => {
9090 expect ( getLabel ( element ) . getAttribute ( "for" ) ) . toEqual ( id ) ;
9191 } ) ;
9292
93+ it ( "should trigger error when invalid format" , ( ) => {
94+
95+ const element = TestUtils . compileTemplate ( `
96+ <form name="form">
97+ <oui-field label="{{'username'}}">
98+ <input type="text"
99+ class="oui-input"
100+ type="text"
101+ id="username"
102+ name="username"
103+ ng-model="$ctrl.username"
104+ ng-pattern="/^[a-zA-Z]{3,8}$/">
105+ </oui-field>
106+ </form>
107+ ` ) ;
108+ const controller = getField ( element ) . controller ( "ouiField" ) ;
109+
110+ $timeout . flush ( ) ;
111+
112+ const $control = getControl ( controller , "username" ) ;
113+ $control . val ( "ch@t12" ) ;
114+ $control . triggerHandler ( "input" ) ;
115+ $control . triggerHandler ( "blur" ) ;
116+
117+ expect ( controller . getFirstError ( ) . pattern ) . toBeTruthy ( ) ;
118+ } ) ;
119+
120+ it ( "should trigger error when length too short" , ( ) => {
121+
122+ const element = TestUtils . compileTemplate ( `
123+ <form name="form">
124+ <oui-field label="{{'username'}}">
125+ <input type="text"
126+ class="oui-input"
127+ type="text"
128+ id="username"
129+ name="username"
130+ minlength="6"
131+ ng-model="$ctrl.username">
132+ </oui-field>
133+ </form>
134+ ` ) ;
135+ const controller = getField ( element ) . controller ( "ouiField" ) ;
136+
137+ $timeout . flush ( ) ;
138+
139+ const $control = getControl ( controller , "username" ) ;
140+ $control . val ( "abc" ) ;
141+ $control . triggerHandler ( "input" ) ;
142+ $control . triggerHandler ( "blur" ) ;
143+
144+ expect ( controller . getFirstError ( ) . minlength ) . toBeTruthy ( ) ;
145+ } ) ;
146+
93147 it ( "should set the name of the form field in the controller" , ( ) => {
94148 const name = "lastname" ;
95149
@@ -128,16 +182,16 @@ describe("ouiField", () => {
128182 id="age"
129183 name="age"
130184 ng-model="$ctrl.user.age"
131- min="{{$ctrl.validation.min}}"
132- max="{{$ctrl.validation.max}}">
185+ ng- min="{{$ctrl.validation.min}}"
186+ ng- max="{{$ctrl.validation.max}}">
133187 <input
134188 class="oui-input"
135189 type="text"
136190 id="name"
137191 name="name"
138192 ng-model="$ctrl.user.name"
139- minlength="{{$ctrl.validation.minlength}}"
140- maxlength="{{$ctrl.validation.maxlength}}">
193+ ng- minlength="{{$ctrl.validation.minlength}}"
194+ ng- maxlength="{{$ctrl.validation.maxlength}}">
141195 </oui-field>
142196 ` , {
143197 validation
@@ -466,6 +520,72 @@ describe("ouiField", () => {
466520 } ) ;
467521 } ) ;
468522
523+ describe ( "with validation" , ( ) => {
524+ it ( "should retrieve custom error messages" , ( ) => {
525+ const message = "Username must be a least 6 characters." ;
526+
527+ const element = TestUtils . compileTemplate ( `
528+ <form name="form">
529+ <oui-field label="{{'username'}}"
530+ error-messages="{minlength: '${ message } '}">
531+ <input type="text"
532+ class="oui-input"
533+ type="text"
534+ id="username"
535+ name="username"
536+ ng-minlength="6"
537+ ng-model="$ctrl.username">
538+ </oui-field>
539+ </form>
540+ ` ) ;
541+
542+ const controller = getField ( element ) . controller ( "ouiField" ) ;
543+
544+ $timeout . flush ( ) ;
545+
546+ const $control = getControl ( controller , "username" ) ;
547+ $control . val ( "abc" ) ;
548+ $control . triggerHandler ( "input" ) ;
549+ $control . triggerHandler ( "blur" ) ;
550+
551+ $timeout . flush ( ) ;
552+
553+ expect ( controller . getFirstError ( ) . minlength ) . toBeTruthy ( ) ;
554+ expect ( controller . getErrorMessage ( "minlength" ) ) . toBe ( message ) ;
555+ } ) ;
556+
557+ it ( "should give a message containing parameters" , ( ) => {
558+ const messageMinlength = 5 ;
559+
560+ const element = TestUtils . compileTemplate ( `
561+ <form name="form">
562+ <oui-field label="{{'username'}}">
563+ <input type="text"
564+ class="oui-input"
565+ type="text"
566+ id="username"
567+ name="username"
568+ ng-minlength="${ messageMinlength } "
569+ ng-model="$ctrl.username">
570+ </oui-field>
571+ </form>
572+ ` ) ;
573+
574+ const controller = getField ( element ) . controller ( "ouiField" ) ;
575+
576+ $timeout . flush ( ) ;
577+
578+ const $control = getControl ( controller , "username" ) ;
579+ $control . val ( "abc" ) ;
580+ $control . triggerHandler ( "input" ) ;
581+ $control . triggerHandler ( "blur" ) ;
582+
583+ $timeout . flush ( ) ;
584+
585+ expect ( controller . getFirstError ( ) . minlength ) . toBeTruthy ( ) ;
586+ expect ( controller . getErrorMessage ( "minlength" ) ) . toContain ( messageMinlength ) ;
587+ } ) ;
588+ } ) ;
469589 } ) ;
470590
471591} ) ;
0 commit comments