Skip to content

Commit

Permalink
fix(mdChips): Run duplicate check after mdOnAppend.
Browse files Browse the repository at this point in the history
The duplicate check was firing before the user's onAppend
method had a chance to run, so the check would always
compare a string against an object and fail.

fixes angular#2748. closes angular#4113.
  • Loading branch information
topherfangio authored and kennethcachia committed Sep 23, 2015
1 parent 72897bf commit c91735f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
30 changes: 27 additions & 3 deletions src/components/chips/chips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ describe('<md-chips>', function() {
expect(scope.items[3].name).toBe('Grape');
expect(scope.items[3].uppername).toBe('GRAPE');
});

it('should disallow duplicate object chips', function() {
var element = buildChips(CHIP_APPEND_TEMPLATE);
var ctrl = element.controller('mdChips');

// Manually set the items
ctrl.items = [{name: 'Apple', uppername: 'APPLE'}];

// Make our custom appendChip function return our existing item
var chipObj = function(chip) {
return ctrl.items[0];
};

scope.appendChip = jasmine.createSpy('appendChip').and.callFake(chipObj);

element.scope().$apply(function() {
ctrl.chipBuffer = 'Apple';
simulateInputEnterKey(ctrl);
});

expect(ctrl.items.length).toBe(1);
expect(scope.appendChip).toHaveBeenCalled();
expect(scope.appendChip.calls.mostRecent().args[0]).toBe('Apple');
});
});

describe('custom inputs', function() {
Expand Down Expand Up @@ -309,9 +333,9 @@ describe('<md-chips>', function() {
});
});

// *******************************
// Internal helper methods
// *******************************
// *******************************
// Internal helper methods
// *******************************

function buildChips(str) {
var container;
Expand Down
2 changes: 1 addition & 1 deletion src/components/chips/js/chipsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ MdChipsCtrl.prototype.getAdjacentChipIndex = function(index) {
* @param newChip
*/
MdChipsCtrl.prototype.appendChip = function(newChip) {
if (this.items.indexOf(newChip) + 1) return;
if (this.useMdOnAppend && this.mdOnAppend) {
newChip = this.mdOnAppend({'$chip': newChip});
}
if (this.items.indexOf(newChip) + 1) return;
this.items.push(newChip);
};

Expand Down

0 comments on commit c91735f

Please sign in to comment.