3 describe('crmUtil', function() {
5 beforeEach(function() {
9 describe('crmMetadata', function() {
10 var crmMetadata
, $q
, $rootScope
, crmApi
;
12 beforeEach(inject(function(_crmMetadata_
, _
$rootScope_
, _
$q_
, _crmApi_
) {
13 crmMetadata
= _crmMetadata_
;
14 $rootScope
= _
$rootScope_
;
19 it('returns a failed promise on error', function(done
) {
20 var apiSpy
= jasmine
.createSpy('crmApi');
21 crmApi
.backend
= apiSpy
.and
.returnValue(crmApi
.val({
24 expect(apiSpy
.calls
.count()).toBe(0);
25 $q
.when(crmMetadata
.getFields('MyEntity')).then(
27 expect(false).toEqual(true);
31 expect(apiSpy
.calls
.count()).toBe(1);
39 it('only calls the API once', function(done
) {
40 var apiSpy
= jasmine
.createSpy('crmApi');
41 crmApi
.backend
= apiSpy
.and
.returnValue(crmApi
.val({
52 expect(apiSpy
.calls
.count()).toBe(0);
53 $q
.when(crmMetadata
.getFields('MyEntity')).then(
55 expect(fields
.id
.title
).toBe('My entity ID');
56 expect(apiSpy
.calls
.count()).toBe(1);
58 // call a second time, but now the data is cached
59 $q
.when(crmMetadata
.getFields('MyEntity')).then(
61 expect(fields
.id
.title
).toBe('My entity ID');
62 expect(apiSpy
.calls
.count()).toBe(1);
64 // call a third time using a diff interface; data is still cached!
65 $q
.when(crmMetadata
.getField('MyEntity', 'id')).then(
67 expect(field
.title
).toBe('My entity ID');
68 expect(apiSpy
.calls
.count()).toBe(1);
80 it('returns individual fields', function(done
) {
81 var apiSpy
= jasmine
.createSpy('crmApi');
82 crmApi
.backend
= apiSpy
.and
.returnValue(crmApi
.val({
93 expect(apiSpy
.calls
.count()).toBe(0);
94 $q
.when(crmMetadata
.getField('MyEntity', 'id')).then(
96 expect(field
.title
).toBe('My entity ID');
97 expect(apiSpy
.calls
.count()).toBe(1);
107 describe('crmQueue', function() {
108 var crmQueue
, $q
, $rootScope
, $timeout
;
110 beforeEach(inject(function(_crmQueue_
, _
$rootScope_
, _
$q_
, _
$timeout_
) {
111 crmQueue
= _crmQueue_
;
112 $rootScope
= _
$rootScope_
;
114 $timeout
= _
$timeout_
;
117 function addAfterTimeout(a
, b
, ms
) {
118 var dfr
= $q
.defer();
125 it('returns in order', function(done
) {
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);
134 queuedFunc(3, 4, 5).then(function(sum
){
135 expect(last
).toBe(3);
141 for (var i
= 0; i
< 5; i
++) {