jdl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/sync.js ADDED
@@ -0,0 +1,210 @@
1
+ $(document).ready(function() {
2
+
3
+ var Library = Backbone.Collection.extend({
4
+ url : function() { return '/library'; }
5
+ });
6
+ var library;
7
+
8
+ var attrs = {
9
+ title : "The Tempest",
10
+ author : "Bill Shakespeare",
11
+ length : 123
12
+ };
13
+
14
+ module("Backbone.sync", {
15
+
16
+ setup : function() {
17
+ library = new Library;
18
+ library.create(attrs, {wait: false});
19
+ },
20
+
21
+ teardown: function() {
22
+ Backbone.emulateHTTP = false;
23
+ }
24
+
25
+ });
26
+
27
+ test("read", 4, function() {
28
+ library.fetch();
29
+ equal(this.ajaxSettings.url, '/library');
30
+ equal(this.ajaxSettings.type, 'GET');
31
+ equal(this.ajaxSettings.dataType, 'json');
32
+ ok(_.isEmpty(this.ajaxSettings.data));
33
+ });
34
+
35
+ test("passing data", 3, function() {
36
+ library.fetch({data: {a: 'a', one: 1}});
37
+ equal(this.ajaxSettings.url, '/library');
38
+ equal(this.ajaxSettings.data.a, 'a');
39
+ equal(this.ajaxSettings.data.one, 1);
40
+ });
41
+
42
+ test("create", 6, function() {
43
+ equal(this.ajaxSettings.url, '/library');
44
+ equal(this.ajaxSettings.type, 'POST');
45
+ equal(this.ajaxSettings.dataType, 'json');
46
+ var data = JSON.parse(this.ajaxSettings.data);
47
+ equal(data.title, 'The Tempest');
48
+ equal(data.author, 'Bill Shakespeare');
49
+ equal(data.length, 123);
50
+ });
51
+
52
+ test("update", 7, function() {
53
+ library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
54
+ equal(this.ajaxSettings.url, '/library/1-the-tempest');
55
+ equal(this.ajaxSettings.type, 'PUT');
56
+ equal(this.ajaxSettings.dataType, 'json');
57
+ var data = JSON.parse(this.ajaxSettings.data);
58
+ equal(data.id, '1-the-tempest');
59
+ equal(data.title, 'The Tempest');
60
+ equal(data.author, 'William Shakespeare');
61
+ equal(data.length, 123);
62
+ });
63
+
64
+ test("update with emulateHTTP and emulateJSON", 7, function() {
65
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
66
+ emulateHTTP: true,
67
+ emulateJSON: true
68
+ });
69
+ equal(this.ajaxSettings.url, '/library/2-the-tempest');
70
+ equal(this.ajaxSettings.type, 'POST');
71
+ equal(this.ajaxSettings.dataType, 'json');
72
+ equal(this.ajaxSettings.data._method, 'PUT');
73
+ var data = JSON.parse(this.ajaxSettings.data.model);
74
+ equal(data.id, '2-the-tempest');
75
+ equal(data.author, 'Tim Shakespeare');
76
+ equal(data.length, 123);
77
+ });
78
+
79
+ test("update with just emulateHTTP", 6, function() {
80
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
81
+ emulateHTTP: true
82
+ });
83
+ equal(this.ajaxSettings.url, '/library/2-the-tempest');
84
+ equal(this.ajaxSettings.type, 'POST');
85
+ equal(this.ajaxSettings.contentType, 'application/json');
86
+ var data = JSON.parse(this.ajaxSettings.data);
87
+ equal(data.id, '2-the-tempest');
88
+ equal(data.author, 'Tim Shakespeare');
89
+ equal(data.length, 123);
90
+ });
91
+
92
+ test("update with just emulateJSON", 6, function() {
93
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
94
+ emulateJSON: true
95
+ });
96
+ equal(this.ajaxSettings.url, '/library/2-the-tempest');
97
+ equal(this.ajaxSettings.type, 'PUT');
98
+ equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded');
99
+ var data = JSON.parse(this.ajaxSettings.data.model);
100
+ equal(data.id, '2-the-tempest');
101
+ equal(data.author, 'Tim Shakespeare');
102
+ equal(data.length, 123);
103
+ });
104
+
105
+ test("read model", 3, function() {
106
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
107
+ library.first().fetch();
108
+ equal(this.ajaxSettings.url, '/library/2-the-tempest');
109
+ equal(this.ajaxSettings.type, 'GET');
110
+ ok(_.isEmpty(this.ajaxSettings.data));
111
+ });
112
+
113
+ test("destroy", 3, function() {
114
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
115
+ library.first().destroy({wait: true});
116
+ equal(this.ajaxSettings.url, '/library/2-the-tempest');
117
+ equal(this.ajaxSettings.type, 'DELETE');
118
+ equal(this.ajaxSettings.data, null);
119
+ });
120
+
121
+ test("destroy with emulateHTTP", 3, function() {
122
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
123
+ library.first().destroy({
124
+ emulateHTTP: true,
125
+ emulateJSON: true
126
+ });
127
+ equal(this.ajaxSettings.url, '/library/2-the-tempest');
128
+ equal(this.ajaxSettings.type, 'POST');
129
+ equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}');
130
+ });
131
+
132
+ test("urlError", 2, function() {
133
+ var model = new Backbone.Model();
134
+ raises(function() {
135
+ model.fetch();
136
+ });
137
+ model.fetch({url: '/one/two'});
138
+ equal(this.ajaxSettings.url, '/one/two');
139
+ });
140
+
141
+ test("#1052 - `options` is optional.", 0, function() {
142
+ var model = new Backbone.Model();
143
+ model.url = '/test';
144
+ Backbone.sync('create', model);
145
+ });
146
+
147
+ test("Backbone.ajax", 1, function() {
148
+ Backbone.ajax = function(settings){
149
+ strictEqual(settings.url, '/test');
150
+ };
151
+ var model = new Backbone.Model();
152
+ model.url = '/test';
153
+ Backbone.sync('create', model);
154
+ });
155
+
156
+ test("Call provided error callback on error.", 1, function() {
157
+ var model = new Backbone.Model;
158
+ model.url = '/test';
159
+ Backbone.sync('read', model, {
160
+ error: function() { ok(true); }
161
+ });
162
+ this.ajaxSettings.error();
163
+ });
164
+
165
+ test('Use Backbone.emulateHTTP as default.', 2, function() {
166
+ var model = new Backbone.Model;
167
+ model.url = '/test';
168
+
169
+ Backbone.emulateHTTP = true;
170
+ model.sync('create', model);
171
+ strictEqual(this.ajaxSettings.emulateHTTP, true);
172
+
173
+ Backbone.emulateHTTP = false;
174
+ model.sync('create', model);
175
+ strictEqual(this.ajaxSettings.emulateHTTP, false);
176
+ });
177
+
178
+ test('Use Backbone.emulateJSON as default.', 2, function() {
179
+ var model = new Backbone.Model;
180
+ model.url = '/test';
181
+
182
+ Backbone.emulateJSON = true;
183
+ model.sync('create', model);
184
+ strictEqual(this.ajaxSettings.emulateJSON, true);
185
+
186
+ Backbone.emulateJSON = false;
187
+ model.sync('create', model);
188
+ strictEqual(this.ajaxSettings.emulateJSON, false);
189
+ });
190
+
191
+ test("#1756 - Call user provided beforeSend function.", 4, function() {
192
+ Backbone.emulateHTTP = true;
193
+ var model = new Backbone.Model;
194
+ model.url = '/test';
195
+ var xhr = {
196
+ setRequestHeader: function(header, value) {
197
+ strictEqual(header, 'X-HTTP-Method-Override');
198
+ strictEqual(value, 'DELETE');
199
+ }
200
+ };
201
+ model.sync('delete', model, {
202
+ beforeSend: function(_xhr) {
203
+ ok(_xhr === xhr);
204
+ return false;
205
+ }
206
+ });
207
+ strictEqual(this.ajaxSettings.beforeSend(xhr), false);
208
+ });
209
+
210
+ });