commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / 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 });