@@ -288,6 +288,42 @@ describe('<md-chips>', function() {
288288 expect ( scope . items . length ) . toBe ( 4 ) ;
289289 } ) ;
290290
291+ it ( 'should update form state when a chip is added' , inject ( function ( $mdConstant ) {
292+ scope . items = [ ] ;
293+ var template =
294+ '<form name="form">' +
295+ ' <md-chips name="chips" ng-model="items"></md-chips>' +
296+ '</form>' ;
297+
298+ var element = buildChips ( template ) ;
299+ var ctrl = element . controller ( 'mdChips' ) ;
300+ var chips = getChipElements ( element ) ;
301+ var input = element . find ( 'input' ) ;
302+
303+ expect ( scope . form . $pristine ) . toBeTruthy ( ) ;
304+ expect ( scope . form . $dirty ) . toBeFalsy ( ) ;
305+
306+ // Add 'Banana'
307+ input . val ( 'Banana' ) ;
308+
309+ // IE11 does not support the `input` event to update the ngModel. An alternative for
310+ // `input` is to use the `change` event.
311+ input . triggerHandler ( 'change' ) ;
312+
313+ var enterEvent = {
314+ type : 'keydown' ,
315+ keyCode : $mdConstant . KEY_CODE . ENTER ,
316+ which : $mdConstant . KEY_CODE . ENTER
317+ } ;
318+
319+ input . triggerHandler ( enterEvent ) ;
320+ scope . $digest ( ) ;
321+
322+ expect ( scope . form . $pristine ) . toBeFalsy ( ) ;
323+ expect ( scope . form . $dirty ) . toBeTruthy ( ) ;
324+ expect ( scope . items ) . toEqual ( [ 'Banana' ] ) ;
325+ } ) ) ;
326+
291327 it ( 'should allow adding the first chip on blur when required exists' , function ( ) {
292328 scope . items = [ ] ;
293329 var template =
@@ -1591,7 +1627,30 @@ describe('<md-chips>', function() {
15911627 scope . $digest ( ) ;
15921628 chips = getChipElements ( element ) ;
15931629 expect ( chips . length ) . toBe ( 1 ) ;
1630+ } ) ;
1631+
1632+ it ( 'should update form state when a chip is removed' , function ( ) {
1633+ var template =
1634+ '<form name="form">' +
1635+ ' <md-chips name="chips" ng-model="items"></md-chips>' +
1636+ '</form>' ;
1637+
1638+ var element = buildChips ( template ) ;
1639+ var ctrl = element . controller ( 'mdChips' ) ;
1640+ var chips = getChipElements ( element ) ;
1641+
1642+ expect ( scope . form . $pristine ) . toBeTruthy ( ) ;
1643+ expect ( scope . form . $dirty ) . toBeFalsy ( ) ;
1644+
1645+ // Remove 'Banana'
1646+ var chipButton = angular . element ( chips [ 1 ] ) . find ( 'button' ) ;
1647+ chipButton [ 0 ] . click ( ) ;
1648+
1649+ scope . $digest ( ) ;
15941650
1651+ expect ( scope . form . $pristine ) . toBeFalsy ( ) ;
1652+ expect ( scope . form . $dirty ) . toBeTruthy ( ) ;
1653+ expect ( scope . items ) . toEqual ( [ 'Apple' , 'Orange' ] ) ;
15951654 } ) ;
15961655 } ) ;
15971656
0 commit comments