1 /* ------------ Fixtures/constants ------------ */
3 var VALID_CONTACT_ID = 3;
4 var MALFORMED_CONTACT_ID = 'z';
6 var ContactModel = Backbone.Model.extend({});
7 CRM.Backbone.extendModel(ContactModel, 'Contact');
8 CRM.Backbone.trackSaved(ContactModel);
10 var ContactCollection = Backbone.Collection.extend({
13 CRM.Backbone.extendCollection(ContactCollection);
15 /* ------------ Assertions ------------ */
18 * Assert "result" contains an API error
21 function assertApiError(result
) {
22 equal(1, result
.is_error
, 'Expected error boolean');
23 ok(result
.error_message
.length
> 0, 'Expected error message')
27 * When calling an AJAX operation which should return successfully,
28 * make sure that there's no error by setting a callback (error: onUnexpectedError)
30 function onUnexpectedError(ignore
, result
) {
31 if (result
&& result
.error_message
) {
32 ok(false, "API returned an unexpected error: " + result
.error_message
);
34 ok(false, "API returned an unexpected error: (missing message)");
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)
43 function onUnexpectedSuccess(ignore
) {
44 ok(false, "API succeeded - but failure was expected");
48 /* ------------ Test cases ------------ */
50 module('model - read');
52 asyncTest("fetch (ok)", function() {
53 var c
= new ContactModel({id
: VALID_CONTACT_ID
});
55 error
: onUnexpectedError
,
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');
64 asyncTest("fetch (error)", function() {
65 var c
= new ContactModel({id
: MALFORMED_CONTACT_ID
});
67 success
: onUnexpectedSuccess
,
68 error: function(model
, error
) {
69 assertApiError(error
);
75 module('model - create');
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
84 equal(c1
.isSaved(), false, "");
86 // Create the new contact
88 error
: onUnexpectedError
,
90 equal(c1
.get("first_name"), "George" + TOKEN
, "save() should return new first name");
91 equal(c1
.isSaved(), true, "");
93 // Fetch the newly created contact
94 var c2
= new ContactModel({id
: c1
.get('id')});
95 equal(c2
.isSaved(), true, "");
97 error
: onUnexpectedError
,
99 equal(c2
.get("first_name"), c1
.get("first_name"), "fetch() should return first name");
100 equal(c2
.isSaved(), true, "");
102 // Destroy the newly created contact
104 error
: onUnexpectedError
,
105 success: function() {
106 equal(c2
.isSaved(), true, "");
108 // Attempt (but fail) to fetch the deleted contact
109 var c3
= new ContactModel({id
: c1
.get('id')});
110 equal(c3
.isSaved(), true, "");
112 success
: onUnexpectedSuccess
,
113 error: function(model
, error
) {
114 assertApiError(error
);
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
134 // Create the new contact
136 success
: onUnexpectedSuccess
,
137 error: function(model
, error
) {
138 assertApiError(error
);
144 module('model - update');
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, "");
153 equal(c
.isSaved(), false, "");
155 error
: onUnexpectedError
,
156 success: function() {
157 equal(c
.get("nick_name"), NICKNAME
, "save() should return new nickname");
159 equal(c
.isSaved(), true, "");
161 // read back - make sure the save worked
162 var c2
= new ContactModel({id
: VALID_CONTACT_ID
});
164 error
: onUnexpectedError
,
165 success: function() {
166 equal(c2
.get("nick_name"), NICKNAME
, "fetch() should return new nickname");
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, "");
180 contact_type
: 'Not-a.va+lidConta(ype'
182 equal(c
.isSaved(), false, "");
184 success
: onUnexpectedSuccess
,
185 error: function(model
, error
) {
186 assertApiError(error
);
188 equal(c
.isSaved(), false, "");
196 module('collection - read');
198 asyncTest("fetch by contact_type (passive criteria, 1+ results)", function() {
199 var c
= new ContactCollection([], {
201 contact_type
: 'Organization'
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');
217 asyncTest("fetch by contact_type (active criteria, 1+ results)", function() {
218 var criteria
= new Backbone
.Model({
219 contact_type
: 'Organization'
221 var c
= new ContactCollection([], {
222 crmCriteriaModel
: criteria
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');
237 asyncTest("fetch by contact_type (active criteria revision, 1+ results)", function() {
238 var criteria
= new Backbone
.Model({
239 contact_type
: 'Household'
241 var c
= new ContactCollection([], {
242 crmCriteriaModel
: criteria
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');
253 criteria
.set('contact_type', 'Organization');
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');
266 asyncTest("fetch by crazy name (0 results)", function() {
267 var c
= new ContactCollection([], {
269 display_name
: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
273 error
: onUnexpectedError
,
274 success: function() {
275 equal(c
.models
.length
, 0, "Expected no contacts");
281 asyncTest("fetch by malformed ID (error)", function() {
282 var c
= new ContactCollection([], {
284 id
: MALFORMED_CONTACT_ID
288 success
: onUnexpectedSuccess
,
289 error: function(collection
, error
) {
290 assertApiError(error
);
296 module('findCreate');
298 asyncTest("findCreate by ID (1 result)", function() {
299 CRM
.Backbone
.findCreate({
300 CollectionClass
: ContactCollection
,
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')
314 asyncTest("findCreate by crazy name (0 results) - autocreate", function() {
315 CRM
.Backbone
.findCreate({
316 CollectionClass
: ContactCollection
,
318 organization_name
: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
321 contact_type
: 'Organization'
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')
333 asyncTest("findCreate by malformed ID (error)", function() {
334 CRM
.Backbone
.findCreate({
335 CollectionClass
: ContactCollection
,
337 id
: MALFORMED_CONTACT_ID
339 success
: onUnexpectedSuccess
,
340 error: function(collection
, error
) {
341 assertApiError(error
);