forked from patternfly/angular-patternfly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbar.spec.js
More file actions
579 lines (457 loc) · 17 KB
/
Copy pathtoolbar.spec.js
File metadata and controls
579 lines (457 loc) · 17 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
describe('Directive: pfToolbar', function () {
var $scope;
var $compile;
var element;
var $pfViewUtils;
var performedAction;
// load the controller's module
beforeEach(function () {
module('patternfly.toolbars', 'patternfly.views', 'patternfly.filters', 'toolbars/toolbar.html',
'filters/simple-filter/filter.html', 'filters/simple-filter/filter-fields.html', 'filters/simple-filter/filter-results.html',
'sort/sort.html');
});
beforeEach(inject(function (_$compile_, _$rootScope_, pfViewUtils) {
$compile = _$compile_;
$scope = _$rootScope_;
$pfViewUtils = pfViewUtils;
}));
var compileHTML = function (markup, scope) {
element = angular.element(markup);
$compile(element)(scope);
scope.$digest();
};
beforeEach(function () {
performedAction = undefined;
var performAction = function (action) {
performedAction = action;
};
$scope.config = {
viewsConfig: {
views: [$pfViewUtils.getDashboardView(), $pfViewUtils.getListView(), $pfViewUtils.getCardView(), $pfViewUtils.getTableView(), $pfViewUtils.getTopologyView()]
},
sortConfig: {
fields: [
{
id: 'name',
title: 'Name',
sortType: 'alpha'
},
{
id: 'age',
title: 'Age',
sortType: 'numeric'
},
{
id: 'address',
title: 'Address',
sortType: 'alpha'
}
]
},
filterConfig: {
fields: [
{
id: 'name',
title: 'Name',
placeholder: 'Filter by Name',
filterType: 'text'
},
{
id: 'address',
title: 'Address',
placeholder: 'Filter by Address',
filterType: 'text'
},
{
id: 'birthMonth',
title: 'Birth Month',
placeholder: 'Filter by Birth Month',
filterType: 'select',
filterValues: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
}
],
resultsCount: 5,
appliedFilters: []
},
actionsConfig: {
primaryActions: [
{
name: 'Action 1',
title: 'Do the first thing',
actionFn: performAction
},
{
name: 'Action 2',
title: 'Do something else',
actionFn: performAction
}
],
moreActions: [
{
name: 'Action',
title: 'Perform an action',
actionFn: performAction
},
{
name: 'Another Action',
title: 'Do something else',
actionFn: performAction
},
{
name: 'Disabled Action',
title: 'Unavailable action',
actionFn: performAction,
isDisabled: true
},
{
name: 'Something Else',
title: '',
actionFn: performAction
},
{
isSeparator: true
},
{
name: 'Grouped Action 1',
title: 'Do something',
actionFn: performAction
},
{
name: 'Grouped Action 2',
title: 'Do something similar',
actionFn: performAction
}
]
}
};
var htmlTmp = '<pf-toolbar config="config"></pf-toolbar>';
compileHTML(htmlTmp, $scope);
});
it('should have correct number of filter fields', function () {
var fields = element.find('.filter-field');
expect(fields.length).toBe(3);
});
it('should have correct number of results', function () {
var results = element.find('h5');
expect(results.length).toBe(1);
expect(results.html()).toBe("5 Results");
$scope.config.filterConfig.resultsCount = 10;
$scope.$digest();
results = element.find('h5');
expect(results.length).toBe(1);
expect(results.html()).toBe("10 Results");
});
it('should show active filters and clear filters button when there are filters', function () {
var activeFilters = element.find('.active-filter');
expect(activeFilters.length).toBe(0);
expect(element.find('.clear-filters').length).toBe(0);
$scope.config.filterConfig.appliedFilters = [
{
id: 'address',
title: 'Address',
value: 'New York'
}
];
$scope.$digest();
activeFilters = element.find('.active-filter');
expect(activeFilters.length).toBe(1);
expect(element.find('.clear-filters').length).toBe(1);
});
it ('should add a dropdown select when a select type is chosen', function() {
var filterSelect = element.find('.filter-select');
var fields = element.find('.filter-field');
expect(filterSelect.length).toBe(0);
expect(fields.length).toBe(3);
eventFire(fields[2], 'click');
$scope.$digest();
filterSelect = element.find('.filter-select');
expect(filterSelect.length).toBe(1);
var items = filterSelect.find('li');
expect(items.length).toBe($scope.config.filterConfig.fields[2].filterValues.length + 1); // +1 for the null value
});
it ('should clear a filter when the close button is clicked', function () {
var closeButtons;
closeButtons = element.find('.pficon-close');
expect(closeButtons.length).toBe(0);
$scope.config.filterConfig.appliedFilters = [
{
id: 'address',
title: 'Address',
value: 'New York'
}
];
$scope.$digest();
closeButtons = element.find('.pficon-close');
expect(closeButtons.length).toBe(1);
eventFire(closeButtons[0], 'click');
$scope.$digest();
expect(element.find('.pficon-close').length).toBe(0);
});
it ('should clear all filters when the clear all filters button is clicked', function () {
var clearButtons = element.find('.clear-filters');
var activeFilters = element.find('.active-filter');
expect(activeFilters.length).toBe(0);
expect(clearButtons.length).toBe(0);
$scope.config.filterConfig.appliedFilters = [
{
id: 'address',
title: 'Address',
value: 'New York'
}
];
$scope.$digest();
activeFilters = element.find('.active-filter');
clearButtons = element.find('.clear-filters');
expect(activeFilters.length).toBe(1);
expect(clearButtons.length).toBe(1);
eventFire(clearButtons[0], 'click');
$scope.$digest();
activeFilters = element.find('.active-filter');
clearButtons = element.find('.clear-filters');
expect(activeFilters.length).toBe(0);
expect(clearButtons.length).toBe(0);
});
it ('should not show filters when a filter config is not supplied', function () {
var filter = element.find('.filter-pf');
expect(filter.length).toBe(2);
$scope.config = {
viewsConfig: {
views: [$pfViewUtils.getListView(), $pfViewUtils.getCardView()]
}
};
var htmlTmp = '<pf-toolbar config="config"></pf-toolbar>';
compileHTML(htmlTmp, $scope);
filter = element.find('.filter-pf');
expect(filter.length).toBe(0);
});
it ('should show the correct view selection buttons', function () {
var selectors = element.find('.toolbar-pf-view-selector .btn-link');
expect(selectors.length).toBe(5);
expect(element.find('.fa-dashboard').length).toBe(1);
expect(element.find('.fa-th').length).toBe(1);
expect(element.find('.fa-th-list').length).toBe(1);
expect(element.find('.fa-table').length).toBe(1);
expect(element.find('.fa-sitemap').length).toBe(1);
});
it ('should show the currently selected view', function () {
var viewSelector = element.find('.toolbar-pf-view-selector');
var active = element.find('.active');
expect(viewSelector.length).toBe(1);
expect(active.length).toBe(1);
});
it ('should update the currently selected view when a view selector clicked', function () {
var viewSelector = element.find('.toolbar-pf-view-selector');
var active = element.find('.active');
var listSelector = element.find('.toolbar-pf-view-selector .btn-link');
expect(viewSelector.length).toBe(1);
expect(active.length).toBe(1);
expect(listSelector.length).toBe(5);
eventFire(listSelector[0], 'click');
$scope.$apply();
active = element.find('.active');
expect(active.length).toBe(1);
});
it ('should call the callback function when a view selector clicked', function () {
var listSelector = element.find('.toolbar-pf-view-selector .btn-link');
var functionCalled = false;
var onViewSelect = function () {
functionCalled = true;
};
$scope.config.viewsConfig.onViewSelect = onViewSelect;
expect(functionCalled).toBeFalsy();
expect(listSelector.length).toBe(5);
eventFire(listSelector[0], 'click');
$scope.$apply();
expect(functionCalled).toBeTruthy();
});
it ('should not show view selectors when no viewsConfig is supplied', function () {
var viewSelector = element.find('.toolbar-pf-view-selector');
expect(viewSelector.length).toBe(1);
$scope.config.viewsConfig = undefined;
$scope.$digest();
viewSelector = element.find('.toolbar-pf-view-selector');
expect(viewSelector.length).toBe(0);
});
it('should have correct number of sort fields', function () {
var fields = element.find('.sort-pf .sort-field');
expect(fields.length).toBe(3);
});
it('should have default to the first sort field', function () {
var results = element.find('.sort-pf .dropdown-toggle');
expect(results.length).toBe(1);
expect(results.html().trim().slice(0,'Name'.length)).toBe("Name");
});
it('should default to ascending sort', function () {
var sortIcon = element.find('.sort-pf .fa-sort-alpha-asc');
expect(sortIcon.length).toBe(1);
});
it('should update the current sort when one is selected', function () {
var results = element.find('.sort-pf .dropdown-toggle');
var fields = element.find('.sort-pf .sort-field');
expect(results.length).toBe(1);
expect(results.html().trim().slice(0,'Name'.length)).toBe("Name");
expect(fields.length).toBe(3);
eventFire(fields[2], 'click');
$scope.$digest();
results = element.find('.sort-pf .dropdown-toggle');
expect(results.length).toBe(1);
expect(results.html().trim().slice(0,'Address'.length)).toBe("Address");
});
it('should update the direction icon when the sort type changes', function () {
var results = element.find('.sort-pf .dropdown-toggle');
var fields = element.find('.sort-pf .sort-field');
var sortIcon = element.find('.sort-pf .fa-sort-alpha-asc');
expect(results.length).toBe(1);
expect(results.html().trim().slice(0,'Name'.length)).toBe("Name");
expect(fields.length).toBe(3);
expect(sortIcon.length).toBe(1);
eventFire(fields[1], 'click');
$scope.$digest();
results = element.find('.sort-pf .dropdown-toggle');
sortIcon = element.find('.sort-pf .fa-sort-numeric-asc');
expect(results.length).toBe(1);
expect(results.html().trim().slice(0,'Age'.length)).toBe("Age");
expect(sortIcon.length).toBe(1);
});
it('should reverse the sort direction when the direction button is clicked', function () {
var sortIcon = element.find('.sort-pf .fa-sort-alpha-asc');
var sortButton = element.find('.sort-pf .btn.btn-link');
expect(sortIcon.length).toBe(1);
expect(sortButton.length).toBe(1);
eventFire(sortButton[0], 'click');
$scope.$digest();
sortIcon = element.find('.sort-pf .fa-sort-alpha-desc');
expect(sortIcon.length).toBe(1);
});
it ('should notify when a new sort field is chosen', function() {
var notified = false;
var chosenField = '';
var chosenDir = '';
var fields = element.find('.sort-pf .sort-field');
var watchForNotify = function (sortField, isAscending) {
notified = true;
chosenField = sortField;
chosenDir = isAscending;
};
$scope.config.sortConfig.onSortChange = watchForNotify;
expect(fields.length).toBe(3);
eventFire(fields[2], 'click');
$scope.$digest();
expect(notified).toBeTruthy();
expect(chosenField).toBe($scope.config.sortConfig.fields[2]);
expect(chosenDir).toBeTruthy();
});
it ('should notify when the sort direction changes', function() {
var notified = false;
var chosenField = '';
var chosenDir = '';
var sortButton = element.find('.sort-pf .btn.btn-link');
var watchForNotify = function (sortField, isAscending) {
notified = true;
chosenField = sortField;
chosenDir = isAscending;
};
$scope.config.sortConfig.onSortChange = watchForNotify;
expect(sortButton.length).toBe(1);
eventFire(sortButton[0], 'click');
$scope.$digest();
expect(notified).toBeTruthy();
expect(chosenField).toBe($scope.config.sortConfig.fields[0]);
expect(chosenDir).toBeFalsy();
});
it ('should not show sort components when a sort config is not supplied', function () {
var filter = element.find('.sort-pf');
expect(filter.length).toBe(1);
$scope.config = {
viewsConfig: {
views: [$pfViewUtils.getListView(), $pfViewUtils.getCardView()]
}
};
var htmlTmp = '<pf-toolbar config="config"></pf-toolbar>';
compileHTML(htmlTmp, $scope);
filter = element.find('.sort-pf');
expect(filter.length).toBe(0);
});
it('should have correct number of primary actions', function () {
var fields = element.find('.toolbar-pf-actions .primary-action');
expect(fields.length).toBe(2);
});
it('should have correct number of secondary actions', function () {
var fields = element.find('.toolbar-pf-actions .secondary-action');
expect(fields.length).toBe(6);
});
it('should have correct number of separators', function () {
var fields = element.find('.toolbar-pf-actions .divider');
expect(fields.length).toBe(1);
});
it('should correctly disable actions', function () {
var fields = element.find('.toolbar-pf-actions .disabled');
expect(fields.length).toBe(1);
});
it('should not show more actions menu when there are no more actions', function () {
var menus = element.find('.fa-ellipsis-v');
expect(menus.length).toBe(1);
$scope.config.actionsConfig.moreActions = undefined;
$scope.$digest();
menus = element.find('.toolbar-pf-actions .fa-ellipsis-v');
expect(menus.length).toBe(0);
});
it('should call the action function with the appropriate action when an action is clicked', function () {
var primaryActions = element.find('.toolbar-pf-actions .primary-action');
var moreActions = element.find('.toolbar-pf-actions .secondary-action');
expect(primaryActions.length).toBe(2);
expect(moreActions.length).toBe(6);
eventFire(primaryActions[0], 'click');
$scope.$digest();
expect(performedAction.name).toBe('Action 1');
eventFire(moreActions[3], 'click');
$scope.$digest();
expect(performedAction.name).toBe('Something Else');
});
it('should not call the action function when a disabled action is clicked', function () {
var primaryActions = element.find('.toolbar-pf-actions .primary-action');
var moreActions = element.find('.toolbar-pf-actions .secondary-action');
expect(primaryActions.length).toBe(2);
expect(moreActions.length).toBe(6);
eventFire(moreActions[2], 'click');
$scope.$digest();
expect(performedAction).toBe(undefined);
eventFire(primaryActions[1], 'click');
$scope.$digest();
expect(performedAction.name).toBe('Action 2');
performedAction = undefined;
$scope.config.actionsConfig.primaryActions[1].isDisabled = true;
$scope.$digest();
eventFire(primaryActions[1], 'click');
$scope.$digest();
expect(performedAction).toBe(undefined);
});
it ('should not show action components when an action config is not supplied', function () {
var filter = element.find('.toolbar-actions');
expect(filter.length).toBe(1);
$scope.config = {
viewsConfig: {
views: [$pfViewUtils.getListView(), $pfViewUtils.getCardView()]
}
};
var htmlTmp = '<pf-toolbar config="config"></pf-toolbar>';
compileHTML(htmlTmp, $scope);
filter = element.find('.toolbar-actions');
expect(filter.length).toBe(0);
});
it ('should add custom actions in the correct location', function () {
var actionBar = element.find('.toolbar-actions');
expect(actionBar.length).toBe(1);
var includeActions = actionBar.find('.toolbar-pf-include-actions');
expect(includeActions.length).toBe(0);
$scope.config.actionsConfig.actionsInclude = true;
var includeHtml = '<pf-toolbar config="config"><actions><button class="btn btn-default add-action" type="button">Add Action</button></actions></pf-toolbar>';
compileHTML(includeHtml, $scope);
actionBar = element.find('.toolbar-actions');
expect(actionBar.length).toBe(1);
includeActions = actionBar.find('.toolbar-pf-include-actions');
expect(includeActions.length).toBe(1);
var addAction = includeActions.find('.add-action');
expect(addAction.length).toBe(1);
});
});