Merge pull request #2279 from colemanw/optionForms
[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 (passive criteria, 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 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
237 asyncTest("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
266 asyncTest("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
281 asyncTest("fetch by malformed ID (error)", function() {
282 var c = new ContactCollection([], {
283 crmCriteria: {
284 id: MALFORMED_CONTACT_ID
285 }
286 });
287 c.fetch({
288 success: onUnexpectedSuccess,
289 error: function(collection, error) {
290 assertApiError(error);
291 start();
292 }
293 });
294 });
295
296 module('findCreate');
297
298 asyncTest("findCreate by ID (1 result)", function() {
299 CRM.Backbone.findCreate({
300 CollectionClass: ContactCollection,
301 crmCriteria: {
302 id: VALID_CONTACT_ID
303 },
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
314 asyncTest("findCreate by crazy name (0 results) - autocreate", function() {
315 CRM.Backbone.findCreate({
316 CollectionClass: ContactCollection,
317 crmCriteria: {
318 organization_name: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
319 },
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
333 asyncTest("findCreate by malformed ID (error)", function() {
334 CRM.Backbone.findCreate({
335 CollectionClass: ContactCollection,
336 crmCriteria: {
337 id: MALFORMED_CONTACT_ID
338 },
339 success: onUnexpectedSuccess,
340 error: function(collection, error) {
341 assertApiError(error);
342 start();
343 }
344 });
345 });