Merge pull request #1260 from aadityawalawalkar/CRM-12994
[civicrm-core.git] / tests / qunit / crm-backbone / test.js
1 /* ------------ Fixtures/constants ------------ */
2
3 var VALID_CONTACT_ID = 3;
4 var MALFORMED_CONTACT_ID = 'z';
5
6 var ContactModel = Backbone.Model.extend({});
7 CRM.Backbone.extendModel(ContactModel, 'Contact');
8 CRM.Backbone.trackSaved(ContactModel);
9
10 var ContactCollection = Backbone.Collection.extend({
11 model: ContactModel
12 });
13 CRM.Backbone.extendCollection(ContactCollection);
14
15 /* ------------ Assertions ------------ */
16
17 /**
18 * Assert "result" contains an API error
19 * @param result
20 */
21 function assertApiError(result) {
22 equal(1, result.is_error, 'Expected error boolean');
23 ok(result.error_message.length > 0, 'Expected error message')
24 }
25
26 /**
27 * When calling an AJAX operation which should return successfully,
28 * make sure that there's no error by setting a callback (error: onUnexpectedError)
29 */
30 function onUnexpectedError(ignore, result) {
31 if (result && result.error_message) {
32 ok(false, "API returned an unexpected error: " + result.error_message);
33 } else {
34 ok(false, "API returned an unexpected error: (missing message)");
35 }
36 start();
37 }
38
39 /**
40 * When calling an AJAX operation which should return an error,
41 * make sure that there's no success by setting a callback (success: onUnexpectedSuccess)
42 */
43 function onUnexpectedSuccess(ignore) {
44 ok(false, "API succeeded - but failure was expected");
45 start();
46 }
47
48 /* ------------ Test cases ------------ */
49
50 module('model - read');
51
52 asyncTest("fetch (ok)", function() {
53 var c = new ContactModel({id: VALID_CONTACT_ID});
54 c.fetch({
55 error: onUnexpectedError,
56 success: function() {
57 notEqual(-1, _.indexOf(['Individual', 'Household', 'Organization'], c.get('contact_type')), 'Loaded contact with valid contact_type');
58 ok(c.get('display_name') != '', 'Loaded contact with valid name');
59 start();
60 }
61 });
62 });
63
64 asyncTest("fetch (error)", function() {
65 var c = new ContactModel({id: MALFORMED_CONTACT_ID});
66 c.fetch({
67 success: onUnexpectedSuccess,
68 error: function(model, error) {
69 assertApiError(error);
70 start();
71 }
72 });
73 });
74
75 module('model - create');
76
77 asyncTest("create/read/delete/read (ok)", function() {
78 var TOKEN = new Date().getTime();
79 var c1 = new ContactModel({
80 contact_type: "Individual",
81 first_name: "George" + TOKEN,
82 last_name: "Anon" + TOKEN
83 });
84 equal(c1.isSaved(), false, "");
85
86 // Create the new contact
87 c1.save({}, {
88 error: onUnexpectedError,
89 success: function() {
90 equal(c1.get("first_name"), "George" + TOKEN, "save() should return new first name");
91 equal(c1.isSaved(), true, "");
92
93 // Fetch the newly created contact
94 var c2 = new ContactModel({id: c1.get('id')});
95 equal(c2.isSaved(), true, "");
96 c2.fetch({
97 error: onUnexpectedError,
98 success: function() {
99 equal(c2.get("first_name"), c1.get("first_name"), "fetch() should return first name");
100 equal(c2.isSaved(), true, "");
101
102 // Destroy the newly created contact
103 c2.destroy({
104 error: onUnexpectedError,
105 success: function() {
106 equal(c2.isSaved(), true, "");
107
108 // Attempt (but fail) to fetch the deleted contact
109 var c3 = new ContactModel({id: c1.get('id')});
110 equal(c3.isSaved(), true, "");
111 c3.fetch({
112 success: onUnexpectedSuccess,
113 error: function(model, error) {
114 assertApiError(error);
115 start();
116 }
117 }); // fetch
118 }
119 }); // destroy
120 }
121 }); // fetch
122 }
123 }); // save
124 });
125
126 asyncTest("create (error)", function() {
127 var TOKEN = new Date().getTime();
128 var c1 = new ContactModel({
129 // MISSING: contact_type: "Individual",
130 first_name: "George" + TOKEN,
131 last_name: "Anon" + TOKEN
132 });
133
134 // Create the new contact
135 c1.save({}, {
136 success: onUnexpectedSuccess,
137 error: function(model, error) {
138 assertApiError(error);
139 start();
140 }
141 });
142 });
143
144 module('model - update');
145
146 asyncTest("update (ok)", function() {
147 var NICKNAME = "George" + new Date().getTime();
148 var c = new ContactModel({id: VALID_CONTACT_ID});
149 equal(c.isSaved(), true, "");
150 c.set({
151 nick_name: NICKNAME
152 });
153 equal(c.isSaved(), false, "");
154 c.save({}, {
155 error: onUnexpectedError,
156 success: function() {
157 equal(c.get("nick_name"), NICKNAME, "save() should return new nickname");
158 _.defer(function(){
159 equal(c.isSaved(), true, "");
160
161 // read back - make sure the save worked
162 var c2 = new ContactModel({id: VALID_CONTACT_ID});
163 c2.fetch({
164 error: onUnexpectedError,
165 success: function() {
166 equal(c2.get("nick_name"), NICKNAME, "fetch() should return new nickname");
167 start();
168 }
169 });
170 });
171 }
172 });
173 });
174
175 asyncTest("update (error)", function() {
176 var NICKNAME = "George" + new Date().getTime();
177 var c = new ContactModel({id: VALID_CONTACT_ID});
178 equal(c.isSaved(), true, "");
179 c.set({
180 contact_type: 'Not-a.va+lidConta(ype'
181 });
182 equal(c.isSaved(), false, "");
183 c.save({}, {
184 success: onUnexpectedSuccess,
185 error: function(model, error) {
186 assertApiError(error);
187 _.defer(function(){
188 equal(c.isSaved(), false, "");
189 start();
190 });
191 }
192 });
193 });
194
195
196 module('collection - read');
197
198 asyncTest("fetch by contact_type (1+ results)", function() {
199 var c = new ContactCollection([], {
200 crmCriteria: {
201 contact_type: 'Organization'
202 }
203 });
204 c.fetch({
205 error: onUnexpectedError,
206 success: function() {
207 ok(c.models.length > 0, "Expected at least one contact");
208 c.each(function(model) {
209 equal(model.get('contact_type'), 'Organization', 'Expected contact with type organization');
210 ok(model.get('display_name') != '', 'Expected contact with valid name');
211 });
212 start();
213 }
214 });
215 });
216
217 asyncTest("fetch by crazy name (0 results)", function() {
218 var c = new ContactCollection([], {
219 crmCriteria: {
220 display_name: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
221 }
222 });
223 c.fetch({
224 error: onUnexpectedError,
225 success: function() {
226 equal(c.models.length, 0, "Expected no contacts");
227 start();
228 }
229 });
230 });
231
232 asyncTest("fetch by malformed ID (error)", function() {
233 var c = new ContactCollection([], {
234 crmCriteria: {
235 id: MALFORMED_CONTACT_ID
236 }
237 });
238 c.fetch({
239 success: onUnexpectedSuccess,
240 error: function(collection, error) {
241 assertApiError(error);
242 start();
243 }
244 });
245 });
246
247 module('findCreate');
248
249 asyncTest("findCreate by ID (1 result)", function() {
250 CRM.Backbone.findCreate({
251 CollectionClass: ContactCollection,
252 crmCriteria: {
253 id: VALID_CONTACT_ID
254 },
255 error: onUnexpectedError,
256 success: function(model) {
257 equal(model.get('id'), VALID_CONTACT_ID);
258 ok(model.get('contact_type') != '', 'Expected contact with valid type')
259 ok(model.get('id'), 'Expected contact with valid ID')
260 start();
261 }
262 });
263 });
264
265 asyncTest("findCreate by crazy name (0 results) - autocreate", function() {
266 CRM.Backbone.findCreate({
267 CollectionClass: ContactCollection,
268 crmCriteria: {
269 organization_name: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
270 },
271 defaults: {
272 contact_type: 'Organization'
273 },
274 error: onUnexpectedError,
275 success: function(model) {
276 equal(model.get('organization_name'), 'asdf23vmlk2309lk2lkasdk-23ASDF32f', 'Expected default values from crmCriteria');
277 equal(model.get('contact_type'), 'Organization', 'Expected default values from parameters');
278 ok(!model.get('id'), 'Expected contact without valid ID')
279 start();
280 }
281 });
282 });
283
284 asyncTest("findCreate by malformed ID (error)", function() {
285 CRM.Backbone.findCreate({
286 CollectionClass: ContactCollection,
287 crmCriteria: {
288 id: MALFORMED_CONTACT_ID
289 },
290 success: onUnexpectedSuccess,
291 error: function(collection, error) {
292 assertApiError(error);
293 start();
294 }
295 });
296 });