Conditions | 1 |
Paths | 1 |
Total Lines | 239 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | (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 | QUnit.module('Backbone.sync', { |
||
15 | |||
16 | beforeEach: function(assert) { |
||
17 | library = new Library; |
||
18 | library.create(attrs, {wait: false}); |
||
19 | }, |
||
20 | |||
21 | afterEach: function(assert) { |
||
22 | Backbone.emulateHTTP = false; |
||
23 | } |
||
24 | |||
25 | }); |
||
26 | |||
27 | QUnit.test('read', function(assert) { |
||
28 | assert.expect(4); |
||
29 | library.fetch(); |
||
30 | assert.equal(this.ajaxSettings.url, '/library'); |
||
31 | assert.equal(this.ajaxSettings.type, 'GET'); |
||
32 | assert.equal(this.ajaxSettings.dataType, 'json'); |
||
33 | assert.ok(_.isEmpty(this.ajaxSettings.data)); |
||
34 | }); |
||
35 | |||
36 | QUnit.test('passing data', function(assert) { |
||
37 | assert.expect(3); |
||
38 | library.fetch({data: {a: 'a', one: 1}}); |
||
39 | assert.equal(this.ajaxSettings.url, '/library'); |
||
40 | assert.equal(this.ajaxSettings.data.a, 'a'); |
||
41 | assert.equal(this.ajaxSettings.data.one, 1); |
||
42 | }); |
||
43 | |||
44 | QUnit.test('create', function(assert) { |
||
45 | assert.expect(6); |
||
46 | assert.equal(this.ajaxSettings.url, '/library'); |
||
47 | assert.equal(this.ajaxSettings.type, 'POST'); |
||
48 | assert.equal(this.ajaxSettings.dataType, 'json'); |
||
49 | var data = JSON.parse(this.ajaxSettings.data); |
||
50 | assert.equal(data.title, 'The Tempest'); |
||
51 | assert.equal(data.author, 'Bill Shakespeare'); |
||
52 | assert.equal(data.length, 123); |
||
53 | }); |
||
54 | |||
55 | QUnit.test('update', function(assert) { |
||
56 | assert.expect(7); |
||
57 | library.first().save({id: '1-the-tempest', author: 'William Shakespeare'}); |
||
58 | assert.equal(this.ajaxSettings.url, '/library/1-the-tempest'); |
||
59 | assert.equal(this.ajaxSettings.type, 'PUT'); |
||
60 | assert.equal(this.ajaxSettings.dataType, 'json'); |
||
61 | var data = JSON.parse(this.ajaxSettings.data); |
||
62 | assert.equal(data.id, '1-the-tempest'); |
||
63 | assert.equal(data.title, 'The Tempest'); |
||
64 | assert.equal(data.author, 'William Shakespeare'); |
||
65 | assert.equal(data.length, 123); |
||
66 | }); |
||
67 | |||
68 | QUnit.test('update with emulateHTTP and emulateJSON', function(assert) { |
||
69 | assert.expect(7); |
||
70 | library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, { |
||
71 | emulateHTTP: true, |
||
72 | emulateJSON: true |
||
73 | }); |
||
74 | assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); |
||
75 | assert.equal(this.ajaxSettings.type, 'POST'); |
||
76 | assert.equal(this.ajaxSettings.dataType, 'json'); |
||
77 | assert.equal(this.ajaxSettings.data._method, 'PUT'); |
||
78 | var data = JSON.parse(this.ajaxSettings.data.model); |
||
79 | assert.equal(data.id, '2-the-tempest'); |
||
80 | assert.equal(data.author, 'Tim Shakespeare'); |
||
81 | assert.equal(data.length, 123); |
||
82 | }); |
||
83 | |||
84 | QUnit.test('update with just emulateHTTP', function(assert) { |
||
85 | assert.expect(6); |
||
86 | library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, { |
||
87 | emulateHTTP: true |
||
88 | }); |
||
89 | assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); |
||
90 | assert.equal(this.ajaxSettings.type, 'POST'); |
||
91 | assert.equal(this.ajaxSettings.contentType, 'application/json'); |
||
92 | var data = JSON.parse(this.ajaxSettings.data); |
||
93 | assert.equal(data.id, '2-the-tempest'); |
||
94 | assert.equal(data.author, 'Tim Shakespeare'); |
||
95 | assert.equal(data.length, 123); |
||
96 | }); |
||
97 | |||
98 | QUnit.test('update with just emulateJSON', function(assert) { |
||
99 | assert.expect(6); |
||
100 | library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, { |
||
101 | emulateJSON: true |
||
102 | }); |
||
103 | assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); |
||
104 | assert.equal(this.ajaxSettings.type, 'PUT'); |
||
105 | assert.equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded'); |
||
106 | var data = JSON.parse(this.ajaxSettings.data.model); |
||
107 | assert.equal(data.id, '2-the-tempest'); |
||
108 | assert.equal(data.author, 'Tim Shakespeare'); |
||
109 | assert.equal(data.length, 123); |
||
110 | }); |
||
111 | |||
112 | QUnit.test('read model', function(assert) { |
||
113 | assert.expect(3); |
||
114 | library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); |
||
115 | library.first().fetch(); |
||
116 | assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); |
||
117 | assert.equal(this.ajaxSettings.type, 'GET'); |
||
118 | assert.ok(_.isEmpty(this.ajaxSettings.data)); |
||
119 | }); |
||
120 | |||
121 | QUnit.test('destroy', function(assert) { |
||
122 | assert.expect(3); |
||
123 | library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); |
||
124 | library.first().destroy({wait: true}); |
||
125 | assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); |
||
126 | assert.equal(this.ajaxSettings.type, 'DELETE'); |
||
127 | assert.equal(this.ajaxSettings.data, null); |
||
128 | }); |
||
129 | |||
130 | QUnit.test('destroy with emulateHTTP', function(assert) { |
||
131 | assert.expect(3); |
||
132 | library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); |
||
133 | library.first().destroy({ |
||
134 | emulateHTTP: true, |
||
135 | emulateJSON: true |
||
136 | }); |
||
137 | assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); |
||
138 | assert.equal(this.ajaxSettings.type, 'POST'); |
||
139 | assert.equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}'); |
||
140 | }); |
||
141 | |||
142 | QUnit.test('urlError', function(assert) { |
||
143 | assert.expect(2); |
||
144 | var model = new Backbone.Model(); |
||
145 | assert.raises(function() { |
||
146 | model.fetch(); |
||
147 | }); |
||
148 | model.fetch({url: '/one/two'}); |
||
149 | assert.equal(this.ajaxSettings.url, '/one/two'); |
||
150 | }); |
||
151 | |||
152 | QUnit.test('#1052 - `options` is optional.', function(assert) { |
||
153 | assert.expect(0); |
||
154 | var model = new Backbone.Model(); |
||
155 | model.url = '/test'; |
||
156 | Backbone.sync('create', model); |
||
157 | }); |
||
158 | |||
159 | QUnit.test('Backbone.ajax', function(assert) { |
||
160 | assert.expect(1); |
||
161 | Backbone.ajax = function(settings){ |
||
162 | assert.strictEqual(settings.url, '/test'); |
||
163 | }; |
||
164 | var model = new Backbone.Model(); |
||
165 | model.url = '/test'; |
||
166 | Backbone.sync('create', model); |
||
167 | }); |
||
168 | |||
169 | QUnit.test('Call provided error callback on error.', function(assert) { |
||
170 | assert.expect(1); |
||
171 | var model = new Backbone.Model; |
||
172 | model.url = '/test'; |
||
173 | Backbone.sync('read', model, { |
||
174 | error: function() { assert.ok(true); } |
||
175 | }); |
||
176 | this.ajaxSettings.error(); |
||
177 | }); |
||
178 | |||
179 | QUnit.test('Use Backbone.emulateHTTP as default.', function(assert) { |
||
180 | assert.expect(2); |
||
181 | var model = new Backbone.Model; |
||
182 | model.url = '/test'; |
||
183 | |||
184 | Backbone.emulateHTTP = true; |
||
185 | model.sync('create', model); |
||
186 | assert.strictEqual(this.ajaxSettings.emulateHTTP, true); |
||
187 | |||
188 | Backbone.emulateHTTP = false; |
||
189 | model.sync('create', model); |
||
190 | assert.strictEqual(this.ajaxSettings.emulateHTTP, false); |
||
191 | }); |
||
192 | |||
193 | QUnit.test('Use Backbone.emulateJSON as default.', function(assert) { |
||
194 | assert.expect(2); |
||
195 | var model = new Backbone.Model; |
||
196 | model.url = '/test'; |
||
197 | |||
198 | Backbone.emulateJSON = true; |
||
199 | model.sync('create', model); |
||
200 | assert.strictEqual(this.ajaxSettings.emulateJSON, true); |
||
201 | |||
202 | Backbone.emulateJSON = false; |
||
203 | model.sync('create', model); |
||
204 | assert.strictEqual(this.ajaxSettings.emulateJSON, false); |
||
205 | }); |
||
206 | |||
207 | QUnit.test('#1756 - Call user provided beforeSend function.', function(assert) { |
||
208 | assert.expect(4); |
||
209 | Backbone.emulateHTTP = true; |
||
210 | var model = new Backbone.Model; |
||
211 | model.url = '/test'; |
||
212 | var xhr = { |
||
213 | setRequestHeader: function(header, value) { |
||
214 | assert.strictEqual(header, 'X-HTTP-Method-Override'); |
||
215 | assert.strictEqual(value, 'DELETE'); |
||
216 | } |
||
217 | }; |
||
218 | model.sync('delete', model, { |
||
219 | beforeSend: function(_xhr) { |
||
220 | assert.ok(_xhr === xhr); |
||
221 | return false; |
||
222 | } |
||
223 | }); |
||
224 | assert.strictEqual(this.ajaxSettings.beforeSend(xhr), false); |
||
225 | }); |
||
226 | |||
227 | QUnit.test('#2928 - Pass along `textStatus` and `errorThrown`.', function(assert) { |
||
228 | assert.expect(2); |
||
229 | var model = new Backbone.Model; |
||
230 | model.url = '/test'; |
||
231 | model.on('error', function(m, xhr, options) { |
||
232 | assert.strictEqual(options.textStatus, 'textStatus'); |
||
233 | assert.strictEqual(options.errorThrown, 'errorThrown'); |
||
234 | }); |
||
235 | model.fetch(); |
||
236 | this.ajaxSettings.error({}, 'textStatus', 'errorThrown'); |
||
237 | }); |
||
238 | |||
239 | })(); |
||
240 |