INFRA-132 - Add .jshintrc. Cleanup tests/karama/unit.
[civicrm-core.git] / tests / qunit / crm-backbone / test.js
CommitLineData
5eccc958 1/* ------------ Fixtures/constants ------------ */
231a4c0f
TO
2
3var VALID_CONTACT_ID = 3;
3c97257f 4var MALFORMED_CONTACT_ID = 'z';
231a4c0f
TO
5
6var ContactModel = Backbone.Model.extend({});
7CRM.Backbone.extendModel(ContactModel, 'Contact');
b488e159 8CRM.Backbone.trackSaved(ContactModel);
231a4c0f
TO
9
10var ContactCollection = Backbone.Collection.extend({
11 model: ContactModel
12});
13CRM.Backbone.extendCollection(ContactCollection);
14
5eccc958 15/* ------------ Assertions ------------ */
231a4c0f 16
5eccc958
TO
17/**
18 * Assert "result" contains an API error
19 * @param result
20 */
231a4c0f
TO
21function assertApiError(result) {
22 equal(1, result.is_error, 'Expected error boolean');
23 ok(result.error_message.length > 0, 'Expected error message')
24}
5eccc958
TO
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 */
231a4c0f
TO
30function 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}
5eccc958
TO
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 */
231a4c0f
TO
43function onUnexpectedSuccess(ignore) {
44 ok(false, "API succeeded - but failure was expected");
45 start();
46}
47
5eccc958 48/* ------------ Test cases ------------ */
231a4c0f
TO
49
50module('model - read');
51
52asyncTest("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
64asyncTest("fetch (error)", function() {
3c97257f 65 var c = new ContactModel({id: MALFORMED_CONTACT_ID});
231a4c0f
TO
66 c.fetch({
67 success: onUnexpectedSuccess,
68 error: function(model, error) {
69 assertApiError(error);
70 start();
71 }
72 });
73});
74
005f3f4d
TO
75module('model - create');
76
77asyncTest("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 });
b488e159 84 equal(c1.isSaved(), false, "");
005f3f4d
TO
85
86 // Create the new contact
87 c1.save({}, {
88 error: onUnexpectedError,
89 success: function() {
e8b1bc2a 90 equal(c1.get("first_name"), "George" + TOKEN, "save() should return new first name");
b488e159 91 equal(c1.isSaved(), true, "");
005f3f4d
TO
92
93 // Fetch the newly created contact
94 var c2 = new ContactModel({id: c1.get('id')});
b488e159 95 equal(c2.isSaved(), true, "");
005f3f4d
TO
96 c2.fetch({
97 error: onUnexpectedError,
98 success: function() {
99 equal(c2.get("first_name"), c1.get("first_name"), "fetch() should return first name");
b488e159 100 equal(c2.isSaved(), true, "");
005f3f4d
TO
101
102 // Destroy the newly created contact
103 c2.destroy({
104 error: onUnexpectedError,
105 success: function() {
b488e159 106 equal(c2.isSaved(), true, "");
005f3f4d
TO
107
108 // Attempt (but fail) to fetch the deleted contact
109 var c3 = new ContactModel({id: c1.get('id')});
b488e159 110 equal(c3.isSaved(), true, "");
005f3f4d
TO
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
126asyncTest("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
231a4c0f
TO
144module('model - update');
145
146asyncTest("update (ok)", function() {
147 var NICKNAME = "George" + new Date().getTime();
148 var c = new ContactModel({id: VALID_CONTACT_ID});
b488e159
TO
149 equal(c.isSaved(), true, "");
150 c.set({
231a4c0f 151 nick_name: NICKNAME
b488e159
TO
152 });
153 equal(c.isSaved(), false, "");
154 c.save({}, {
231a4c0f
TO
155 error: onUnexpectedError,
156 success: function() {
157 equal(c.get("nick_name"), NICKNAME, "save() should return new nickname");
b488e159
TO
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 });
231a4c0f
TO
170 });
171 }
172 });
173});
174
175asyncTest("update (error)", function() {
176 var NICKNAME = "George" + new Date().getTime();
177 var c = new ContactModel({id: VALID_CONTACT_ID});
b488e159
TO
178 equal(c.isSaved(), true, "");
179 c.set({
231a4c0f 180 contact_type: 'Not-a.va+lidConta(ype'
b488e159
TO
181 });
182 equal(c.isSaved(), false, "");
183 c.save({}, {
231a4c0f
TO
184 success: onUnexpectedSuccess,
185 error: function(model, error) {
186 assertApiError(error);
b488e159
TO
187 _.defer(function(){
188 equal(c.isSaved(), false, "");
189 start();
190 });
231a4c0f
TO
191 }
192 });
193});
194
195
196module('collection - read');
197
c639fc21 198asyncTest("fetch by contact_type (passive criteria, 1+ results)", function() {
231a4c0f
TO
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
c639fc21
TO
217asyncTest("fetch by contact_type (active criteria, 1+ results)", function() {
218 var criteria = new Backbone.Model({
219 contact_type: 'Organization'
220 });
221 var c = new ContactCollection([], {
222 crmCriteriaModel: criteria
223 });
224 c.fetch({
225 error: onUnexpectedError,
226 success: function() {
227 ok(c.models.length > 0, "Expected at least one contact");
228 c.each(function(model) {
229 equal(model.get('contact_type'), 'Organization', 'Expected contact with type organization');
230 ok(model.get('display_name') != '', 'Expected contact with valid name');
231 });
232 start();
233 }
234 });
235});
236
237asyncTest("fetch by contact_type (active criteria revision, 1+ results)", function() {
238 var criteria = new Backbone.Model({
239 contact_type: 'Household'
240 });
241 var c = new ContactCollection([], {
242 crmCriteriaModel: criteria
243 });
244 c.fetch({
245 error: onUnexpectedError,
246 success: function() {
247 ok(c.models.length > 0, "Expected at least one contact");
248 c.each(function(model) {
249 equal(model.get('contact_type'), 'Household', 'Expected contact with type household');
250 ok(model.get('display_name') != '', 'Expected contact with valid name');
251 });
252
253 criteria.set('contact_type', 'Organization');
254 _.delay(function() {
255 ok(c.models.length > 0, "Expected at least one contact");
256 c.each(function(model) {
257 equal(model.get('contact_type'), 'Organization', 'Expected contact with type organization');
258 ok(model.get('display_name') != '', 'Expected contact with valid name');
259 });
260 start();
261 }, 1000);
262 }
263 });
264});
265
231a4c0f
TO
266asyncTest("fetch by crazy name (0 results)", function() {
267 var c = new ContactCollection([], {
268 crmCriteria: {
269 display_name: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
270 }
271 });
272 c.fetch({
273 error: onUnexpectedError,
274 success: function() {
275 equal(c.models.length, 0, "Expected no contacts");
276 start();
277 }
278 });
279});
280
281asyncTest("fetch by malformed ID (error)", function() {
282 var c = new ContactCollection([], {
283 crmCriteria: {
3c97257f 284 id: MALFORMED_CONTACT_ID
231a4c0f
TO
285 }
286 });
287 c.fetch({
288 success: onUnexpectedSuccess,
289 error: function(collection, error) {
290 assertApiError(error);
291 start();
292 }
293 });
294});
295
4bbb9761 296module('findCreate');
3c97257f 297
4bbb9761
TO
298asyncTest("findCreate by ID (1 result)", function() {
299 CRM.Backbone.findCreate({
300 CollectionClass: ContactCollection,
3c97257f
TO
301 crmCriteria: {
302 id: VALID_CONTACT_ID
4bbb9761 303 },
3c97257f
TO
304 error: onUnexpectedError,
305 success: function(model) {
306 equal(model.get('id'), VALID_CONTACT_ID);
307 ok(model.get('contact_type') != '', 'Expected contact with valid type')
308 ok(model.get('id'), 'Expected contact with valid ID')
309 start();
310 }
311 });
312});
313
4bbb9761
TO
314asyncTest("findCreate by crazy name (0 results) - autocreate", function() {
315 CRM.Backbone.findCreate({
316 CollectionClass: ContactCollection,
3c97257f
TO
317 crmCriteria: {
318 organization_name: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
4bbb9761 319 },
3c97257f
TO
320 defaults: {
321 contact_type: 'Organization'
322 },
323 error: onUnexpectedError,
324 success: function(model) {
325 equal(model.get('organization_name'), 'asdf23vmlk2309lk2lkasdk-23ASDF32f', 'Expected default values from crmCriteria');
326 equal(model.get('contact_type'), 'Organization', 'Expected default values from parameters');
327 ok(!model.get('id'), 'Expected contact without valid ID')
328 start();
329 }
330 });
331});
332
4bbb9761
TO
333asyncTest("findCreate by malformed ID (error)", function() {
334 CRM.Backbone.findCreate({
335 CollectionClass: ContactCollection,
3c97257f
TO
336 crmCriteria: {
337 id: MALFORMED_CONTACT_ID
4bbb9761 338 },
3c97257f
TO
339 success: onUnexpectedSuccess,
340 error: function(collection, error) {
341 assertApiError(error);
342 start();
343 }
344 });
345});