|
| 1 | +describe('pf-bootstrap-select', function () { |
| 2 | + |
| 3 | + var $scope, $compile; |
| 4 | + |
| 5 | + beforeEach(module('patternfly.select')); |
| 6 | + |
| 7 | + beforeEach(inject(function (_$rootScope_, _$compile_) { |
| 8 | + $scope = _$rootScope_; |
| 9 | + $compile = _$compile_; |
| 10 | + })); |
| 11 | + |
| 12 | + describe('Page with pf-select directive', function () { |
| 13 | + |
| 14 | + var compileSelect = function (markup, scope) { |
| 15 | + var el = $compile(markup)(scope); |
| 16 | + scope.$digest(); |
| 17 | + return el; |
| 18 | + }; |
| 19 | + |
| 20 | + it('should generate correct options from ng-options', function () { |
| 21 | + |
| 22 | + $scope.options = ['a','b','c']; |
| 23 | + $scope.modelValue = $scope.options[1]; |
| 24 | + |
| 25 | + var select = compileSelect('<select pf-bootstrap-select ng-model="modelValue" ng-options="o as o for o in options"></select>', $scope); |
| 26 | + |
| 27 | + $scope.$digest(); |
| 28 | + |
| 29 | + expect(select.text()).toBe('abc'); |
| 30 | + expect(select).toEqualSelect(['a', ['b'], 'c']); |
| 31 | + |
| 32 | + var bsSelect = angular.element(select).siblings('.dropdown-menu'); |
| 33 | + var bsSelItems = bsSelect.find('li'); |
| 34 | + expect(bsSelItems.length).toBe($scope.options.length); |
| 35 | + expect(bsSelItems.text()).toBe('abc'); |
| 36 | + |
| 37 | + var bsSelected = bsSelect.find('li.selected'); |
| 38 | + expect(bsSelected.length).toBe(1); |
| 39 | + expect(bsSelected.text()).toBe('b'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should respond to changes in ng-options', function () { |
| 43 | + |
| 44 | + $scope.options = ['a','b','c']; |
| 45 | + $scope.modelValue = $scope.options[0]; |
| 46 | + var select = compileSelect('<select pf-bootstrap-select ng-model="modelValue" ng-options="o as o for o in options"></select>', $scope); |
| 47 | + |
| 48 | + expect(select.text()).toBe('abc'); |
| 49 | + expect(select).toEqualSelect([['a'], 'b', 'c']); |
| 50 | + |
| 51 | + var bsSelect = angular.element(select).siblings('.dropdown-menu'); |
| 52 | + var bsSelItems = bsSelect.find('li'); |
| 53 | + expect(bsSelItems.length).toBe($scope.options.length); |
| 54 | + expect(bsSelItems.text()).toBe('abc'); |
| 55 | + |
| 56 | + $scope.$apply(function() { |
| 57 | + $scope.options.push('d'); |
| 58 | + }); |
| 59 | + |
| 60 | + $scope.$digest(); |
| 61 | + |
| 62 | + expect(select.text()).toBe('abcd'); |
| 63 | + expect(select).toEqualSelect([['a'], 'b', 'c', 'd']); |
| 64 | + |
| 65 | + bsSelect = angular.element(select).siblings('.dropdown-menu'); |
| 66 | + bsSelItems = bsSelect.find('li'); |
| 67 | + expect(bsSelItems.length).toBe($scope.options.length); |
| 68 | + expect(bsSelItems.text()).toBe('abcd'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should respond to ng-model changes', function () { |
| 72 | + |
| 73 | + $scope.options = ['a','b','c']; |
| 74 | + $scope.modelValue = $scope.options[0]; |
| 75 | + var select = compileSelect('<select pf-bootstrap-select ng-model="modelValue" ng-options="o as o for o in options"></select>', $scope); |
| 76 | + |
| 77 | + expect(select.text()).toBe('abc'); |
| 78 | + expect(select).toEqualSelect([['a'], 'b', 'c']); |
| 79 | + |
| 80 | + var bsSelect = angular.element(select).siblings('.dropdown-menu'); |
| 81 | + var bsSelItems = bsSelect.find('li'); |
| 82 | + expect(bsSelItems.length).toBe($scope.options.length); |
| 83 | + expect(bsSelItems.text()).toBe('abc'); |
| 84 | + |
| 85 | + var bsSelected = bsSelect.find('li.selected'); |
| 86 | + expect(bsSelected.length).toBe(1); |
| 87 | + expect(bsSelected.text()).toBe('a'); |
| 88 | + |
| 89 | + $scope.$apply(function() { |
| 90 | + $scope.modelValue = $scope.options[1]; |
| 91 | + }); |
| 92 | + |
| 93 | + $scope.$digest(); |
| 94 | + |
| 95 | + expect(select.text()).toBe('abc'); |
| 96 | + expect(select).toEqualSelect(['a', ['b'], 'c']); |
| 97 | + |
| 98 | + bsSelected = bsSelect.find('li.selected'); |
| 99 | + expect(bsSelected.length).toBe(1); |
| 100 | + expect(bsSelected.text()).toBe('b'); |
| 101 | + |
| 102 | + $scope.$apply(function() { |
| 103 | + $scope.modelValue = $scope.options[2]; |
| 104 | + }); |
| 105 | + |
| 106 | + $scope.$digest(); |
| 107 | + |
| 108 | + expect(select.text()).toBe('abc'); |
| 109 | + expect(select).toEqualSelect(['a', 'b', ['c']]); |
| 110 | + |
| 111 | + bsSelected = bsSelect.find('li.selected'); |
| 112 | + expect(bsSelected.length).toBe(1); |
| 113 | + expect(bsSelected.text()).toBe('c'); |
| 114 | + }); |
| 115 | + |
| 116 | + }); |
| 117 | +}); |
0 commit comments