|
| 1 | +describe('Component: pfSelect', function () { |
| 2 | + var $scope; |
| 3 | + var $compile; |
| 4 | + var element; |
| 5 | + |
| 6 | + // load the controller's module |
| 7 | + beforeEach(function () { |
| 8 | + module('patternfly.select', 'select/select.html'); |
| 9 | + }); |
| 10 | + |
| 11 | + beforeEach(inject(function (_$compile_, _$rootScope_) { |
| 12 | + $compile = _$compile_; |
| 13 | + $scope = _$rootScope_; |
| 14 | + })); |
| 15 | + |
| 16 | + var compileHTML = function (markup, scope) { |
| 17 | + element = angular.element(markup); |
| 18 | + $compile(element)(scope); |
| 19 | + |
| 20 | + scope.$digest(); |
| 21 | + }; |
| 22 | + |
| 23 | + beforeEach(function () { |
| 24 | + $scope.items = [ |
| 25 | + { |
| 26 | + id: 1, |
| 27 | + title: 'A' |
| 28 | + }, |
| 29 | + { |
| 30 | + id: 2, |
| 31 | + title: 'B' |
| 32 | + }, |
| 33 | + { |
| 34 | + id: 3, |
| 35 | + title: 'C' |
| 36 | + }, |
| 37 | + { |
| 38 | + id: 4, |
| 39 | + title: 'D' |
| 40 | + } |
| 41 | + ]; |
| 42 | + |
| 43 | + $scope.selected = $scope.items[1]; |
| 44 | + |
| 45 | + var htmlTmp = '<pf-select selected="selected" options="items" display-field="title"></pf-select>'; |
| 46 | + |
| 47 | + compileHTML(htmlTmp, $scope); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should have correct number of options', function () { |
| 51 | + var menuItems = element.find('.dropdown-menu li'); |
| 52 | + expect(menuItems.length).toBe(4); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should have the correct item selected', function () { |
| 56 | + var results = element.find('.dropdown-toggle'); |
| 57 | + expect(results.length).toBe(1); |
| 58 | + expect(results.html().trim().slice(0, $scope.items[1].title.length)).toBe($scope.items[1].title); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should update the selected item when one is clicked', function () { |
| 62 | + var menuItems = element.find('.dropdown-menu li > a'); |
| 63 | + expect(menuItems.length).toBe(4); |
| 64 | + |
| 65 | + eventFire(menuItems[2], 'click'); |
| 66 | + $scope.$digest(); |
| 67 | + |
| 68 | + expect($scope.selected.id).toBe(3); |
| 69 | + var results = element.find('.dropdown-toggle'); |
| 70 | + expect(results.length).toBe(1); |
| 71 | + expect(results.html().trim().slice(0, $scope.items[1].title.length)).toBe($scope.items[2].title); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should add a default item when empty value is given', function () { |
| 75 | + var htmlTmp = '<pf-select selected="selected" options="items" display-field="title" empty-value="nothing"></pf-select>'; |
| 76 | + |
| 77 | + compileHTML(htmlTmp, $scope); |
| 78 | + |
| 79 | + var menuItems = element.find('.dropdown-menu li'); |
| 80 | + expect(menuItems.length).toBe(5); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should show the default when no selection is given', function () { |
| 84 | + |
| 85 | + $scope.selected = null; |
| 86 | + var htmlTmp = '<pf-select selected="selected" options="items" display-field="title" empty-value="nothing"></pf-select>'; |
| 87 | + |
| 88 | + compileHTML(htmlTmp, $scope); |
| 89 | + |
| 90 | + var menuItems = element.find('.dropdown-menu li'); |
| 91 | + expect(menuItems.length).toBe(5); |
| 92 | + |
| 93 | + var results = element.find('.dropdown-toggle'); |
| 94 | + expect(results.length).toBe(1); |
| 95 | + expect(results.html().trim().slice(0, "nothing".length)).toBe("nothing"); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should call the onSelect function on selection', function () { |
| 99 | + |
| 100 | + var onSelectCalled = false; |
| 101 | + var selectedItem = null; |
| 102 | + $scope.onSelect = function(item) { |
| 103 | + onSelectCalled = true; |
| 104 | + selectedItem = item; |
| 105 | + }; |
| 106 | + |
| 107 | + $scope.selected = null; |
| 108 | + var htmlTmp = '<pf-select selected="selected" options="items" display-field="title" empty-value="nothing" on-select="onSelect"></pf-select>'; |
| 109 | + |
| 110 | + compileHTML(htmlTmp, $scope); |
| 111 | + |
| 112 | + var menuItems = element.find('.dropdown-menu li > a'); |
| 113 | + expect(menuItems.length).toBe(5); |
| 114 | + |
| 115 | + expect(onSelectCalled).toBe(false); |
| 116 | + expect(selectedItem).toBe(null); |
| 117 | + |
| 118 | + eventFire(menuItems[2], 'click'); |
| 119 | + $scope.$digest(); |
| 120 | + |
| 121 | + expect(onSelectCalled).toBe(true); |
| 122 | + expect(selectedItem.id).toBe(2); |
| 123 | + }); |
| 124 | + |
| 125 | +}); |
0 commit comments