|
| 1 | +describe('<md-chips>', function() { |
| 2 | + |
| 3 | + beforeEach(module('material.components.chips')); |
| 4 | + |
| 5 | + function compile (str, scope) { |
| 6 | + var container; |
| 7 | + inject(function ($compile) { |
| 8 | + container = $compile(str)(scope); |
| 9 | + scope.$apply(); |
| 10 | + }); |
| 11 | + return container; |
| 12 | + } |
| 13 | + |
| 14 | + function createScope () { |
| 15 | + var scope; |
| 16 | + var items = ['Apple', 'Banana', 'Orange']; |
| 17 | + inject(function ($rootScope) { |
| 18 | + scope = $rootScope.$new(); |
| 19 | + scope.items = items; |
| 20 | + }); |
| 21 | + return scope; |
| 22 | + } |
| 23 | + |
| 24 | + function getChipElements(root) { |
| 25 | + return angular.element(root[0].querySelectorAll('div.md-chip')); |
| 26 | + } |
| 27 | + |
| 28 | + describe('basic functionality', function () { |
| 29 | + it('should render a default input element', function() { |
| 30 | + var scope = createScope(); |
| 31 | + var template = '<md-chips ng-model="items"></md-chips>'; |
| 32 | + var element = compile(template, scope); |
| 33 | + var ctrl = element.controller('mdChips'); |
| 34 | + |
| 35 | + element.scope().$apply(); |
| 36 | + var input = element.find('input'); |
| 37 | + expect(input.length).toBe(1); |
| 38 | + expect(input).toHaveClass('md-chip-input'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should render a list of chips', function() { |
| 42 | + var scope = createScope(); |
| 43 | + var template = '<md-chips ng-model="items"></md-chips>'; |
| 44 | + var element = compile(template, scope); |
| 45 | + var ctrl = element.controller('mdChips'); |
| 46 | + |
| 47 | + element.scope().$apply(); |
| 48 | + var chips = getChipElements(element); |
| 49 | + expect(chips.length).toBe(3); |
| 50 | + expect(chips[0].innerHTML).toContain('Apple'); |
| 51 | + expect(chips[1].innerHTML).toContain('Banana'); |
| 52 | + expect(chips[2].innerHTML).toContain('Orange'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should render a user-provided chip template', function() { |
| 56 | + var scope = createScope(); |
| 57 | + var template = |
| 58 | + '<md-chips ng-model="items">' + |
| 59 | + ' <md-chip class="mychiptemplate"></md-chip>' + |
| 60 | + '</md-chips>'; |
| 61 | + var element = compile(template, scope); |
| 62 | + var ctrl = element.controller('mdChips'); |
| 63 | + |
| 64 | + element.scope().$apply(); |
| 65 | + var chip = element.find('md-chip'); |
| 66 | + expect(chip).toHaveClass('mychiptemplate'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should add a chip', function() { |
| 70 | + var scope = createScope(); |
| 71 | + var template = '<md-chips ng-model="items"></md-chips>'; |
| 72 | + var element = compile(template, scope); |
| 73 | + var ctrl = element.controller('mdChips'); |
| 74 | + |
| 75 | + element.scope().$apply(); |
| 76 | + ctrl.chipBuffer = 'Grape'; |
| 77 | + element.scope().$apply(); |
| 78 | + |
| 79 | + ctrl.appendChipBuffer(); |
| 80 | + element.scope().$apply(); |
| 81 | + |
| 82 | + var chips = getChipElements(element); |
| 83 | + expect(chips.length).toBe(4); |
| 84 | + expect(chips[0].innerHTML).toContain('Apple'); |
| 85 | + expect(chips[1].innerHTML).toContain('Banana'); |
| 86 | + expect(chips[2].innerHTML).toContain('Orange'); |
| 87 | + expect(chips[3].innerHTML).toContain('Grape'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should remove a chip', function() { |
| 91 | + var scope = createScope(); |
| 92 | + var template = '<md-chips ng-model="items"></md-chips>'; |
| 93 | + var element = compile(template, scope); |
| 94 | + var ctrl = element.controller('mdChips'); |
| 95 | + |
| 96 | + element.scope().$apply(); |
| 97 | + |
| 98 | + // Remove "Banana" |
| 99 | + ctrl.removeChip(1); |
| 100 | + element.scope().$apply(); |
| 101 | + |
| 102 | + var chips = getChipElements(element); |
| 103 | + expect(chips.length).toBe(2); |
| 104 | + expect(chips[0].innerHTML).toContain('Apple'); |
| 105 | + expect(chips[1].innerHTML).toContain('Orange'); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('<md-chip-remove>', function() { |
| 110 | + it('should remove a chip', function() { |
| 111 | + var scope = createScope(); |
| 112 | + var template = '<md-chips ng-model="items"></md-chips>'; |
| 113 | + var element = compile(template, scope); |
| 114 | + var ctrl = element.controller('mdChips'); |
| 115 | + element.scope().$apply(); |
| 116 | + |
| 117 | + var chips = getChipElements(element); |
| 118 | + expect(chips.length).toBe(3); |
| 119 | + // Remove 'Banana' |
| 120 | + var db = angular.element(chips[1]).find('md-button'); |
| 121 | + db[0].click(); |
| 122 | + element.scope().$apply(); |
| 123 | + |
| 124 | + chips = getChipElements(element); |
| 125 | + expect(chips.length).toBe(2); |
| 126 | + |
| 127 | + // Remove 'Orange' |
| 128 | + db = angular.element(chips[1]).find('md-button'); |
| 129 | + db[0].click(); |
| 130 | + element.scope().$apply(); |
| 131 | + |
| 132 | + chips = getChipElements(element); |
| 133 | + expect(chips.length).toBe(1); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | +}); |
0 commit comments