Merge pull request #20185 from civicrm/5.37
[civicrm-core.git] / tests / karma / unit / crmUiOrderSpec.js
1 'use strict';
2
3 describe('crmUiOrder', function() {
4
5 beforeEach(function() {
6 module('crmResource');
7 module('crmUtil');
8 module('crmUi');
9 });
10
11 describe('crmUiOrder', function() {
12 var $compile, $q, $rootScope, rows, element;
13
14 var html = '<div>' +
15 ' <span crm-ui-order="{var: \'myOrder\', defaults: [\'-num\']}"></span>' +
16 ' <table>' +
17 ' <thead>' +
18 ' <tr>' +
19 ' <th><a crm-ui-order-by="[myOrder,\'name\']" id="th-name">Name</a></th>' +
20 ' <th><a crm-ui-order-by="[myOrder,\'num\']" id="th-num">Num</a></th>' +
21 ' </tr>' +
22 ' </thead>' +
23 ' <tbody>' +
24 ' <tr ng-repeat="r in rows|orderBy:myOrder.get()">' +
25 ' <td class="row-value">{{r.name}}</td>' +
26 ' </tr>' +
27 ' </tbody>' +
28 ' </table>' +
29 '</div>';
30
31 beforeEach(inject(function(_$compile_, _$rootScope_, _$q_) {
32 $compile = _$compile_;
33 $rootScope = _$rootScope_;
34 $q = _$q_;
35
36 $rootScope.rows = rows = [
37 {name: 'a', num: 200},
38 {name: 'c', num: 300},
39 {name: 'b', num: 100},
40 {name: 'd', num: 0}
41 ];
42
43 }));
44
45 it('changes primary ordering on click', function() {
46 element = $compile(html)($rootScope);
47 $rootScope.$digest();
48 expect($rootScope.myOrder).toEqual(jasmine.any(Object));
49 expect(element.find('.row-value').text()).toBe('cabd');
50
51 element.find('#th-name').click();
52 $rootScope.$digest();
53 expect(element.find('.row-value').text()).toBe('abcd');
54 });
55
56 it('cycles through ascending/descending orderings on multiple clicks', function() {
57 // default: -num
58 element = $compile(html)($rootScope);
59 $rootScope.$digest();
60 expect($rootScope.myOrder.get()).toEqual(['-num']);
61 expect($rootScope.myOrder.getDir('num')).toEqual('-');
62 expect(element.find('.row-value').text()).toBe('cabd');
63
64 // toggle: "-num" => ""
65 element.find('#th-num').click();
66 $rootScope.$digest();
67 expect($rootScope.myOrder.get()).toEqual([]);
68 expect($rootScope.myOrder.getDir('num')).toEqual('');
69 expect(element.find('.row-value').text()).toBe('acbd');
70
71 // toggle: "" => "+num"
72 element.find('#th-num').click();
73 $rootScope.$digest();
74 expect($rootScope.myOrder.get()).toEqual(['+num']);
75 expect($rootScope.myOrder.getDir('num')).toEqual('+');
76 expect(element.find('.row-value').text()).toBe('dbac');
77
78 // toggle: "+num" => "-num"
79 element.find('#th-num').click();
80 $rootScope.$digest();
81 expect($rootScope.myOrder.get()).toEqual(['-num']);
82 expect($rootScope.myOrder.getDir('num')).toEqual('-');
83 expect(element.find('.row-value').text()).toBe('cabd');
84 });
85
86 });
87 });