Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 0dd688c

Browse files
Splaktarjelbourn
authored andcommitted
fix(chips): set dirty when a chip is removed (#11363)
add tests for $dirty/$pristine when adding and removing chips Fixes #11356
1 parent 607dd02 commit 0dd688c

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/components/chips/chips.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/components/chips/js/chipsController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ MdChipsCtrl.prototype.removeChip = function(index, event) {
623623
var removed = this.items.splice(index, 1);
624624

625625
this.updateNgModel();
626+
this.ngModelCtrl.$setDirty();
626627

627628
if (removed && removed.length && this.useOnRemove && this.onRemove) {
628629
this.onRemove({ '$chip': removed[0], '$index': index, '$event': event });

0 commit comments

Comments
 (0)