Commit | Line | Data |
---|---|---|
81eab931 TO |
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 | ||
3cf58cc3 | 58 | // call a second time, but now the data is cached |
81eab931 TO |
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 | ||
3cf58cc3 | 64 | // call a third time using a diff interface; data is still cached! |
81eab931 TO |
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 | }); |