-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathcrmUtilSpec.js
148 lines (127 loc) · 3.79 KB
/
crmUtilSpec.js
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
'use strict';
describe('crmUtil', function() {
beforeEach(function() {
module('crmUtil');
});
describe('crmMetadata', function() {
var crmMetadata, $q, $rootScope, crmApi;
beforeEach(inject(function(_crmMetadata_, _$rootScope_, _$q_, _crmApi_) {
crmMetadata = _crmMetadata_;
$rootScope = _$rootScope_;
$q = _$q_;
crmApi = _crmApi_;
}));
it('returns a failed promise on error', function(done) {
var apiSpy = jasmine.createSpy('crmApi');
crmApi.backend = apiSpy.and.returnValue(crmApi.val({
is_error: 1
}));
expect(apiSpy.calls.count()).toBe(0);
$q.when(crmMetadata.getFields('MyEntity')).then(
function() {
expect(false).toEqual(true);
done();
},
function() {
expect(apiSpy.calls.count()).toBe(1);
done();
}
);
$rootScope.$apply();
});
it('only calls the API once', function(done) {
var apiSpy = jasmine.createSpy('crmApi');
crmApi.backend = apiSpy.and.returnValue(crmApi.val({
is_error: 0,
values: {
id: {
name: 'id',
type: 1,
title: 'My entity ID'
}
}
}));
expect(apiSpy.calls.count()).toBe(0);
$q.when(crmMetadata.getFields('MyEntity')).then(
function(fields) {
expect(fields.id.title).toBe('My entity ID');
expect(apiSpy.calls.count()).toBe(1);
// call a second time, but now the data is cached
$q.when(crmMetadata.getFields('MyEntity')).then(
function(fields) {
expect(fields.id.title).toBe('My entity ID');
expect(apiSpy.calls.count()).toBe(1);
// call a third time using a diff interface; data is still cached!
$q.when(crmMetadata.getField('MyEntity', 'id')).then(
function(field) {
expect(field.title).toBe('My entity ID');
expect(apiSpy.calls.count()).toBe(1);
done();
}
);
}
);
}
);
$rootScope.$apply();
});
it('returns individual fields', function(done) {
var apiSpy = jasmine.createSpy('crmApi');
crmApi.backend = apiSpy.and.returnValue(crmApi.val({
is_error: 0,
values: {
id: {
name: 'id',
type: 1,
title: 'My entity ID'
}
}
}));
expect(apiSpy.calls.count()).toBe(0);
$q.when(crmMetadata.getField('MyEntity', 'id')).then(
function(field) {
expect(field.title).toBe('My entity ID');
expect(apiSpy.calls.count()).toBe(1);
done();
}
);
$rootScope.$apply();
});
});
describe('crmQueue', function() {
var crmQueue, $q, $rootScope, $timeout;
beforeEach(inject(function(_crmQueue_, _$rootScope_, _$q_, _$timeout_) {
crmQueue = _crmQueue_;
$rootScope = _$rootScope_;
$q = _$q_;
$timeout = _$timeout_;
}));
function addAfterTimeout(a, b, ms) {
var dfr = $q.defer();
$timeout(function(){
dfr.resolve(a+b);
}, ms);
return dfr.promise;
}
it('returns in order', function(done) {
var last = null;
var queuedFunc = crmQueue(addAfterTimeout);
// note: the queueing order is more important the timeout-durations (15ms vs 5ms)
queuedFunc(1, 2, 25).then(function(sum) {
expect(last).toBe(null);
expect(sum).toBe(3);
last = sum;
});
queuedFunc(3, 4, 5).then(function(sum){
expect(last).toBe(3);
expect(sum).toBe(7);
last = sum;
done();
});
for (var i = 0; i < 5; i++) {
$rootScope.$apply();
$timeout.flush(20);
}
});
});
});