2 /* ------------ Fixtures/constants ------------ */
4 var VALID_CONTACT_ID = 3;
5 var MALFORMED_CONTACT_ID = 'z';
7 var ContactModel = Backbone.Model.extend({});
8 CRM.Backbone.extendModel(ContactModel, 'Contact');
9 CRM.Backbone.trackSaved(ContactModel);
11 var ContactCollection = Backbone.Collection.extend({
14 CRM.Backbone.extendCollection(ContactCollection);
16 /* ------------ Assertions ------------ */
19 * Assert "result" contains an API error.
22 function assertApiError(result
) {
23 equal(1, result
.is_error
, 'Expected error boolean');
24 ok(result
.error_message
.length
> 0, 'Expected error message');
28 * When calling an AJAX operation which should return successfully,
29 * make sure that there's no error by setting a callback (error: onUnexpectedError)
31 function onUnexpectedError(ignore
, result
) {
32 if (result
&& result
.error_message
) {
33 ok(false, "API returned an unexpected error: " + result
.error_message
);
35 ok(false, "API returned an unexpected error: (missing message)");
41 * When calling an AJAX operation which should return an error,
42 * make sure that there's no success by setting a callback (success: onUnexpectedSuccess)
44 function onUnexpectedSuccess(ignore
) {
45 ok(false, "API succeeded - but failure was expected");
49 /* ------------ Test cases ------------ */
51 module('model - read');
53 asyncTest("fetch (ok)", function() {
54 var c
= new ContactModel({id
: VALID_CONTACT_ID
});
56 error
: onUnexpectedError
,
58 notEqual(-1, _
.indexOf(['Individual', 'Household', 'Organization'], c
.get('contact_type')), 'Loaded contact with valid contact_type');
59 ok(c
.get('display_name') !== '', 'Loaded contact with valid name');
65 asyncTest("fetch (error)", function() {
66 var c
= new ContactModel({id
: MALFORMED_CONTACT_ID
});
68 success
: onUnexpectedSuccess
,
69 error: function(model
, error
) {
70 assertApiError(error
);
76 module('model - create');
78 asyncTest("create/read/delete/read (ok)", function() {
79 var TOKEN
= new Date().getTime();
80 var c1
= new ContactModel({
81 contact_type
: "Individual",
82 first_name
: "George" + TOKEN
,
83 last_name
: "Anon" + TOKEN
85 equal(c1
.isSaved(), false, "");
87 // Create the new contact
89 error
: onUnexpectedError
,
91 equal(c1
.get("first_name"), "George" + TOKEN
, "save() should return new first name");
92 equal(c1
.isSaved(), true, "");
94 // Fetch the newly created contact
95 var c2
= new ContactModel({id
: c1
.get('id')});
96 equal(c2
.isSaved(), true, "");
98 error
: onUnexpectedError
,
100 equal(c2
.get("first_name"), c1
.get("first_name"), "fetch() should return first name");
101 equal(c2
.isSaved(), true, "");
103 // Destroy the newly created contact
105 error
: onUnexpectedError
,
106 success: function() {
107 equal(c2
.isSaved(), true, "");
109 // Attempt (but fail) to fetch the deleted contact
110 var c3
= new ContactModel({id
: c1
.get('id')});
111 equal(c3
.isSaved(), true, "");
113 success
: onUnexpectedSuccess
,
114 error: function(model
, error
) {
115 assertApiError(error
);
127 asyncTest("create (error)", function() {
128 var TOKEN
= new Date().getTime();
129 var c1
= new ContactModel({
130 // MISSING: contact_type: "Individual",
131 first_name
: "George" + TOKEN
,
132 last_name
: "Anon" + TOKEN
135 // Create the new contact
137 success
: onUnexpectedSuccess
,
138 error: function(model
, error
) {
139 assertApiError(error
);
145 module('model - update');
147 asyncTest("update (ok)", function() {
148 var NICKNAME
= "George" + new Date().getTime();
149 var c
= new ContactModel({id
: VALID_CONTACT_ID
});
150 equal(c
.isSaved(), true, "");
154 equal(c
.isSaved(), false, "");
156 error
: onUnexpectedError
,
157 success: function() {
158 equal(c
.get("nick_name"), NICKNAME
, "save() should return new nickname");
160 equal(c
.isSaved(), true, "");
162 // read back - make sure the save worked
163 var c2
= new ContactModel({id
: VALID_CONTACT_ID
});
165 error
: onUnexpectedError
,
166 success: function() {
167 equal(c2
.get("nick_name"), NICKNAME
, "fetch() should return new nickname");
176 asyncTest("update (error)", function() {
177 var NICKNAME
= "George" + new Date().getTime();
178 var c
= new ContactModel({id
: VALID_CONTACT_ID
});
179 equal(c
.isSaved(), true, "");
181 contact_type
: 'Not-a.va+lidConta(ype'
183 equal(c
.isSaved(), false, "");
185 success
: onUnexpectedSuccess
,
186 error: function(model
, error
) {
187 assertApiError(error
);
189 equal(c
.isSaved(), false, "");
197 module('collection - read');
199 asyncTest("fetch by contact_type (passive criteria, 1+ results)", function() {
200 var c
= new ContactCollection([], {
202 contact_type
: 'Organization'
206 error
: onUnexpectedError
,
207 success: function() {
208 ok(c
.models
.length
> 0, "Expected at least one contact");
209 c
.each(function(model
) {
210 equal(model
.get('contact_type'), 'Organization', 'Expected contact with type organization');
211 ok(model
.get('display_name') !== '', 'Expected contact with valid name');
218 asyncTest("fetch by contact_type (active criteria, 1+ results)", function() {
219 var criteria
= new Backbone
.Model({
220 contact_type
: 'Organization'
222 var c
= new ContactCollection([], {
223 crmCriteriaModel
: criteria
226 error
: onUnexpectedError
,
227 success: function() {
228 ok(c
.models
.length
> 0, "Expected at least one contact");
229 c
.each(function(model
) {
230 equal(model
.get('contact_type'), 'Organization', 'Expected contact with type organization');
231 ok(model
.get('display_name') !== '', 'Expected contact with valid name');
238 asyncTest("fetch by contact_type (active criteria revision, 1+ results)", function() {
239 var criteria
= new Backbone
.Model({
240 contact_type
: 'Household'
242 var c
= new ContactCollection([], {
243 crmCriteriaModel
: criteria
246 error
: onUnexpectedError
,
247 success: function() {
248 ok(c
.models
.length
> 0, "Expected at least one contact");
249 c
.each(function(model
) {
250 equal(model
.get('contact_type'), 'Household', 'Expected contact with type household');
251 ok(model
.get('display_name') !== '', 'Expected contact with valid name');
254 criteria
.set('contact_type', 'Organization');
256 ok(c
.models
.length
> 0, "Expected at least one contact");
257 c
.each(function(model
) {
258 equal(model
.get('contact_type'), 'Organization', 'Expected contact with type organization');
259 ok(model
.get('display_name') !== '', 'Expected contact with valid name');
267 asyncTest("fetch by crazy name (0 results)", function() {
268 var c
= new ContactCollection([], {
270 display_name
: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
274 error
: onUnexpectedError
,
275 success: function() {
276 equal(c
.models
.length
, 0, "Expected no contacts");
282 asyncTest("fetch by malformed ID (error)", function() {
283 var c
= new ContactCollection([], {
285 id
: MALFORMED_CONTACT_ID
289 success
: onUnexpectedSuccess
,
290 error: function(collection
, error
) {
291 assertApiError(error
);
297 module('findCreate');
299 asyncTest("findCreate by ID (1 result)", function() {
300 CRM
.Backbone
.findCreate({
301 CollectionClass
: ContactCollection
,
305 error
: onUnexpectedError
,
306 success: function(model
) {
307 equal(model
.get('id'), VALID_CONTACT_ID
);
308 ok(model
.get('contact_type') !== '', 'Expected contact with valid type');
309 ok(model
.get('id'), 'Expected contact with valid ID');
315 asyncTest("findCreate by crazy name (0 results) - autocreate", function() {
316 CRM
.Backbone
.findCreate({
317 CollectionClass
: ContactCollection
,
319 organization_name
: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
322 contact_type
: 'Organization'
324 error
: onUnexpectedError
,
325 success: function(model
) {
326 equal(model
.get('organization_name'), 'asdf23vmlk2309lk2lkasdk-23ASDF32f', 'Expected default values from crmCriteria');
327 equal(model
.get('contact_type'), 'Organization', 'Expected default values from parameters');
328 ok(!model
.get('id'), 'Expected contact without valid ID');
334 asyncTest("findCreate by malformed ID (error)", function() {
335 CRM
.Backbone
.findCreate({
336 CollectionClass
: ContactCollection
,
338 id
: MALFORMED_CONTACT_ID
340 success
: onUnexpectedSuccess
,
341 error: function(collection
, error
) {
342 assertApiError(error
);