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 (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 crazy name (0 results)", function() {
218 var c
= new ContactCollection([], {
220 display_name
: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
224 error
: onUnexpectedError
,
225 success: function() {
226 equal(c
.models
.length
, 0, "Expected no contacts");
232 asyncTest("fetch by malformed ID (error)", function() {
233 var c
= new ContactCollection([], {
235 id
: MALFORMED_CONTACT_ID
239 success
: onUnexpectedSuccess
,
240 error: function(collection
, error
) {
241 assertApiError(error
);
247 module('findCreate');
249 asyncTest("findCreate by ID (1 result)", function() {
250 CRM
.Backbone
.findCreate({
251 CollectionClass
: ContactCollection
,
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')
265 asyncTest("findCreate by crazy name (0 results) - autocreate", function() {
266 CRM
.Backbone
.findCreate({
267 CollectionClass
: ContactCollection
,
269 organization_name
: 'asdf23vmlk2309lk2lkasdk-23ASDF32f'
272 contact_type
: 'Organization'
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')
284 asyncTest("findCreate by malformed ID (error)", function() {
285 CRM
.Backbone
.findCreate({
286 CollectionClass
: ContactCollection
,
288 id
: MALFORMED_CONTACT_ID
290 success
: onUnexpectedSuccess
,
291 error: function(collection
, error
) {
292 assertApiError(error
);