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