Merge pull request #18706 from civicrm/5.30
[civicrm-core.git] / tests / karma / unit / crmUtilSpec.js
1 'use strict';
2
3 describe('crmUtil', function() {
4
5 beforeEach(function() {
6 module('crmUtil');
7 });
8
9 describe('crmMetadata', function() {
10 var crmMetadata, $q, $rootScope, crmApi;
11
12 beforeEach(inject(function(_crmMetadata_, _$rootScope_, _$q_, _crmApi_) {
13 crmMetadata = _crmMetadata_;
14 $rootScope = _$rootScope_;
15 $q = _$q_;
16 crmApi = _crmApi_;
17 }));
18
19 it('returns a failed promise on error', function(done) {
20 var apiSpy = jasmine.createSpy('crmApi');
21 crmApi.backend = apiSpy.and.returnValue(crmApi.val({
22 is_error: 1
23 }));
24 expect(apiSpy.calls.count()).toBe(0);
25 $q.when(crmMetadata.getFields('MyEntity')).then(
26 function() {
27 expect(false).toEqual(true);
28 done();
29 },
30 function() {
31 expect(apiSpy.calls.count()).toBe(1);
32 done();
33 }
34 );
35
36 $rootScope.$apply();
37 });
38
39 it('only calls the API once', function(done) {
40 var apiSpy = jasmine.createSpy('crmApi');
41 crmApi.backend = apiSpy.and.returnValue(crmApi.val({
42 is_error: 0,
43 values: {
44 id: {
45 name: 'id',
46 type: 1,
47 title: 'My entity ID'
48 }
49 }
50 }));
51
52 expect(apiSpy.calls.count()).toBe(0);
53 $q.when(crmMetadata.getFields('MyEntity')).then(
54 function(fields) {
55 expect(fields.id.title).toBe('My entity ID');
56 expect(apiSpy.calls.count()).toBe(1);
57
58 // call a second time, but now the data is cached
59 $q.when(crmMetadata.getFields('MyEntity')).then(
60 function(fields) {
61 expect(fields.id.title).toBe('My entity ID');
62 expect(apiSpy.calls.count()).toBe(1);
63
64 // call a third time using a diff interface; data is still cached!
65 $q.when(crmMetadata.getField('MyEntity', 'id')).then(
66 function(field) {
67 expect(field.title).toBe('My entity ID');
68 expect(apiSpy.calls.count()).toBe(1);
69 done();
70 }
71 );
72 }
73 );
74 }
75 );
76
77 $rootScope.$apply();
78 });
79
80 it('returns individual fields', function(done) {
81 var apiSpy = jasmine.createSpy('crmApi');
82 crmApi.backend = apiSpy.and.returnValue(crmApi.val({
83 is_error: 0,
84 values: {
85 id: {
86 name: 'id',
87 type: 1,
88 title: 'My entity ID'
89 }
90 }
91 }));
92
93 expect(apiSpy.calls.count()).toBe(0);
94 $q.when(crmMetadata.getField('MyEntity', 'id')).then(
95 function(field) {
96 expect(field.title).toBe('My entity ID');
97 expect(apiSpy.calls.count()).toBe(1);
98 done();
99 }
100 );
101
102 $rootScope.$apply();
103 });
104
105 });
106
107 describe('crmQueue', function() {
108 var crmQueue, $q, $rootScope, $timeout;
109
110 beforeEach(inject(function(_crmQueue_, _$rootScope_, _$q_, _$timeout_) {
111 crmQueue = _crmQueue_;
112 $rootScope = _$rootScope_;
113 $q = _$q_;
114 $timeout = _$timeout_;
115 }));
116
117 function addAfterTimeout(a, b, ms) {
118 var dfr = $q.defer();
119 $timeout(function(){
120 dfr.resolve(a+b);
121 }, ms);
122 return dfr.promise;
123 }
124
125 it('returns in order', function(done) {
126 var last = null;
127 var queuedFunc = crmQueue(addAfterTimeout);
128 // note: the queueing order is more important the timeout-durations (15ms vs 5ms)
129 queuedFunc(1, 2, 25).then(function(sum) {
130 expect(last).toBe(null);
131 expect(sum).toBe(3);
132 last = sum;
133 });
134 queuedFunc(3, 4, 5).then(function(sum){
135 expect(last).toBe(3);
136 expect(sum).toBe(7);
137 last = sum;
138 done();
139 });
140
141 for (var i = 0; i < 5; i++) {
142 $rootScope.$apply();
143 $timeout.flush(20);
144 }
145 });
146 });
147
148 describe('crmThrottle', function() {
149 var crmThrottle, $q, $timeout, i;
150
151 beforeEach(inject(function(_crmThrottle_, _$q_, _$timeout_) {
152 crmThrottle = _crmThrottle_;
153 $q = _$q_;
154 $timeout = _$timeout_;
155 }));
156
157 function resolveAfterTimeout() {
158 var dfr = $q.defer();
159 $timeout(function(){
160 dfr.resolve(i++);
161 }, 80);
162 return dfr.promise;
163 }
164
165 it('executes the function once', function() {
166 i = 0;
167 crmThrottle(resolveAfterTimeout);
168 expect(i).toBe(0);
169 $timeout.flush(100);
170 expect(i).toBe(1);
171 $timeout.verifyNoPendingTasks();
172 });
173
174 it('executes the function again', function() {
175 i = 0;
176 crmThrottle(resolveAfterTimeout);
177 $timeout.flush(100);
178 expect(i).toBe(1);
179 crmThrottle(resolveAfterTimeout);
180 $timeout.flush(20);
181 expect(i).toBe(1);
182 $timeout.flush(100);
183 expect(i).toBe(2);
184 $timeout.verifyNoPendingTasks();
185 });
186
187 it('executes the first and last function', function() {
188 i = 0;
189 crmThrottle(resolveAfterTimeout);
190 $timeout.flush(10);
191 crmThrottle(resolveAfterTimeout);
192 crmThrottle(resolveAfterTimeout);
193 crmThrottle(resolveAfterTimeout);
194 crmThrottle(resolveAfterTimeout);
195 expect(i).toBe(0);
196 $timeout.flush(100);
197 expect(i).toBe(1);
198 $timeout.flush(100);
199 $timeout.flush(100);
200 $timeout.flush(100);
201 $timeout.flush(100);
202 expect(i).toBe(2);
203 $timeout.verifyNoPendingTasks();
204 });
205
206 });
207
208 });