Total Complexity | 132 |
Complexity/F | 1 |
Lines of Code | 1626 |
Function Count | 132 |
Duplicated Lines | 295 |
Ratio | 18.14 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like test/cell.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
8 | describe("A CellEditor", function () { |
||
9 | |||
10 | it("calls postRender when model triggers 'backgrid:editing'", function () { |
||
11 | var postRenderCalled = 0; |
||
12 | var editor = new(Backgrid.CellEditor.extend({ |
||
13 | postRender: function () { |
||
14 | postRenderCalled++; |
||
15 | } |
||
16 | }))({ |
||
17 | formatter: { |
||
18 | fromRaw: function () {}, |
||
19 | toRaw: function () {} |
||
20 | }, |
||
21 | column: { |
||
22 | name: "name", |
||
23 | cell: "string" |
||
24 | }, |
||
25 | model: new Backbone.Model({ |
||
26 | name: "alice" |
||
27 | }) |
||
28 | }); |
||
29 | editor.render(); |
||
30 | |||
31 | editor.model.trigger("backgrid:editing"); |
||
32 | expect(postRenderCalled).toBe(1); |
||
33 | }); |
||
34 | |||
35 | }); |
||
36 | |||
37 | describe("An InputCellEditor", function () { |
||
38 | |||
39 | var book; |
||
40 | var editor; |
||
41 | var backgridEditedTriggerCount; |
||
42 | var backgridEditedTriggerArgs; |
||
43 | var backgridErrorTriggerCount; |
||
44 | var backgridErrorTriggerArgs; |
||
45 | |||
46 | beforeEach(function () { |
||
47 | |||
48 | book = new Backbone.Model({ |
||
49 | title: "title" |
||
50 | }); |
||
51 | |||
52 | editor = new(Backgrid.InputCellEditor.extend({ |
||
53 | remove: jasmine.createSpy("remove") |
||
54 | }))({ |
||
55 | model: book, |
||
56 | column: new Backgrid.Column({ |
||
57 | name: "title", |
||
58 | cell: Backgrid.StringCell |
||
59 | }), |
||
60 | formatter: new Backgrid.StringCell.prototype.formatter(), |
||
61 | placeholder: "put your text here" |
||
62 | }); |
||
63 | |||
64 | backgridEditedTriggerCount = 0; |
||
65 | book.on("backgrid:edited", function () { |
||
66 | backgridEditedTriggerCount++; |
||
67 | backgridEditedTriggerArgs = [].slice.call(arguments); |
||
68 | }); |
||
69 | |||
70 | backgridErrorTriggerCount = 0; |
||
71 | book.on("backgrid:error", function () { |
||
72 | backgridErrorTriggerCount++; |
||
73 | backgridErrorTriggerArgs = [].slice.call(arguments); |
||
74 | }); |
||
75 | |||
76 | jasmine.addMatchers({ |
||
77 | toBeAnInstanceOf: function (util) { |
||
78 | return { |
||
79 | compare: function (actual, expected) { |
||
80 | return { |
||
81 | pass: actual instanceof expected |
||
82 | }; |
||
83 | }, |
||
84 | negativeCompare: function (actual, expected) { |
||
85 | return !this.compare(actual, expected) |
||
86 | } |
||
87 | }; |
||
88 | } |
||
89 | }); |
||
90 | }); |
||
91 | |||
92 | it("renders a text input box with a placeholder and the model value formatted for display", function () { |
||
93 | editor.render(); |
||
94 | expect(editor.el).toBeAnInstanceOf(HTMLInputElement); |
||
95 | expect(editor.$el.attr("placeholder")).toBe("put your text here"); |
||
96 | expect(editor.$el.val()).toBe("title"); |
||
97 | }); |
||
98 | |||
99 | it("saves a formatted value in the input box to the model and triggers 'backgrid:edited' from the model when tab is pressed", function () { |
||
100 | editor.render(); |
||
101 | editor.$el.val("another title"); |
||
102 | var tab = $.Event("keydown", { |
||
103 | keyCode: 9 |
||
104 | }); |
||
105 | editor.$el.trigger(tab); |
||
106 | expect(editor.model.get(editor.column.get("name"))).toBe("another title"); |
||
107 | expect(backgridEditedTriggerCount).toBe(1); |
||
108 | expect(backgridEditedTriggerArgs[0]).toEqual(editor.model); |
||
109 | expect(backgridEditedTriggerArgs[1]).toEqual(editor.column); |
||
110 | expect(backgridEditedTriggerArgs[2].moveRight()).toBe(true); |
||
111 | }); |
||
112 | |||
113 | it("saves a formatted value in the input box to the model and triggers 'backgrid:edited' from the model when enter is pressed", function () { |
||
114 | editor.render(); |
||
115 | editor.$el.val("another title"); |
||
116 | var enter = $.Event("keydown", { |
||
117 | keyCode: 13 |
||
118 | }); |
||
119 | editor.$el.trigger(enter); |
||
120 | expect(editor.model.get(editor.column.get("name"))).toBe("another title"); |
||
121 | expect(backgridEditedTriggerCount).toBe(1); |
||
122 | expect(backgridEditedTriggerArgs[0]).toEqual(editor.model); |
||
123 | expect(backgridEditedTriggerArgs[1]).toEqual(editor.column); |
||
124 | expect(backgridEditedTriggerArgs[2].save()).toBe(true); |
||
125 | }); |
||
126 | |||
127 | it("triggers 'backgrid:error' from the model when trying to save an invalid value", function () { |
||
128 | editor.formatter = { |
||
129 | fromRaw: jasmine.createSpy("fromRaw").and.callFake(function (d) { |
||
130 | return d; |
||
131 | }), |
||
132 | toRaw: jasmine.createSpy("toRaw").and.returnValue(undefined) |
||
133 | }; |
||
134 | editor.render(); |
||
135 | editor.$el.val("invalid value"); |
||
136 | var enter = $.Event("keydown", { |
||
137 | keyCode: 13 |
||
138 | }); |
||
139 | editor.$el.trigger(enter); |
||
140 | expect(editor.formatter.toRaw.calls.count()).toBe(1); |
||
141 | expect(editor.formatter.toRaw).toHaveBeenCalledWith("invalid value", editor.model); |
||
142 | expect(backgridErrorTriggerCount).toBe(1); |
||
143 | expect(backgridErrorTriggerArgs[0]).toEqual(editor.model); |
||
144 | expect(backgridErrorTriggerArgs[1]).toEqual(editor.column); |
||
145 | expect(backgridErrorTriggerArgs[2]).toEqual("invalid value"); |
||
146 | |||
147 | editor.formatter.toRaw.calls.reset(); |
||
148 | editor.$el.blur(); |
||
149 | expect(backgridErrorTriggerCount).toBe(2); |
||
150 | expect(backgridErrorTriggerArgs[0]).toEqual(editor.model); |
||
151 | expect(backgridErrorTriggerArgs[1]).toEqual(editor.column); |
||
152 | expect(backgridErrorTriggerArgs[2]).toEqual("invalid value"); |
||
153 | }); |
||
154 | |||
155 | it("discards changes and triggers 'backgrid:edited' from the model when esc is pressed'", function () { |
||
156 | editor.render(); |
||
157 | editor.$el.val("new value"); |
||
158 | var esc = $.Event("keydown", { |
||
159 | keyCode: 27 |
||
160 | }); |
||
161 | editor.$el.trigger(esc); |
||
162 | expect(backgridEditedTriggerCount).toBe(1); |
||
163 | expect(backgridEditedTriggerArgs[0]).toEqual(editor.model); |
||
164 | expect(backgridEditedTriggerArgs[1]).toEqual(editor.column); |
||
165 | expect(backgridEditedTriggerArgs[2].cancel()).toBe(true); |
||
166 | expect(editor.model.get(editor.column.get("name"))).toBe("title"); |
||
167 | }); |
||
168 | |||
169 | it("triggers 'backgrid:edited' from the model when value hasn't changed and focus is lost", function () { |
||
170 | editor.render(); |
||
171 | editor.$el.blur(); |
||
172 | expect(backgridEditedTriggerCount).toBe(1); |
||
173 | expect(backgridEditedTriggerArgs[0]).toEqual(editor.model); |
||
174 | expect(backgridEditedTriggerArgs[1]).toEqual(editor.column); |
||
175 | expect(backgridEditedTriggerArgs[2].passThru()).toBe(true); |
||
176 | expect(editor.model.get(editor.column.get("name"))).toBe("title"); |
||
177 | }); |
||
178 | |||
179 | it("saves the value if the value is valid when going out of focus", function () { |
||
180 | editor.render(); |
||
181 | editor.$el.val("another title"); |
||
182 | editor.$el.blur(); |
||
183 | expect(backgridEditedTriggerCount).toBe(1); |
||
184 | expect(backgridEditedTriggerArgs[0]).toEqual(editor.model); |
||
185 | expect(backgridEditedTriggerArgs[1]).toEqual(editor.column); |
||
186 | expect(backgridEditedTriggerArgs[2].passThru()).toBe(true); |
||
187 | expect(editor.model.get(editor.column.get("name"))).toBe("another title"); |
||
188 | }); |
||
189 | |||
190 | }); |
||
191 | |||
192 | describe("A Cell", function () { |
||
193 | |||
194 | var book; |
||
195 | var column; |
||
196 | var cell; |
||
197 | |||
198 | beforeEach(function () { |
||
199 | book = new Backbone.Model({ |
||
200 | title: "title" |
||
201 | }); |
||
202 | |||
203 | column = { |
||
204 | name: "title", |
||
205 | cell: "string" |
||
206 | }; |
||
207 | |||
208 | cell = new Backgrid.Cell({ |
||
209 | model: book, |
||
210 | column: column |
||
211 | }); |
||
212 | }); |
||
213 | |||
214 | it("uses the formatter from the column if one is given", function () { |
||
215 | |||
216 | var formatter = { |
||
217 | fromRaw: function (rawValue) { |
||
218 | return rawValue; |
||
219 | }, |
||
220 | toRaw: function (formattedValue) { |
||
221 | return formattedValue; |
||
222 | } |
||
223 | }; |
||
224 | |||
225 | column = { |
||
226 | name: "title", |
||
227 | cell: "string", |
||
228 | formatter: formatter |
||
229 | }; |
||
230 | |||
231 | cell = new Backgrid.Cell({ |
||
232 | model: book, |
||
233 | column: column |
||
234 | }); |
||
235 | |||
236 | expect(cell.formatter).toBe(formatter); |
||
237 | }); |
||
238 | |||
239 | it("adds an editable, sortable and a renderable class to the cell if these column attributes are true", function () { |
||
240 | column = { |
||
241 | name: "title", |
||
242 | cell: "string" |
||
243 | }; |
||
244 | |||
245 | cell = new Backgrid.Cell({ |
||
246 | model: book, |
||
247 | column: column |
||
248 | }); |
||
249 | |||
250 | expect(cell.$el.hasClass("editable")).toBe(true); |
||
251 | expect(cell.$el.hasClass("sortable")).toBe(true); |
||
252 | expect(cell.$el.hasClass("renderable")).toBe(true); |
||
253 | |||
254 | cell.column.set("editable", false); |
||
255 | expect(cell.$el.hasClass("editable")).toBe(false); |
||
256 | |||
257 | cell.column.set("sortable", false); |
||
258 | expect(cell.$el.hasClass("sortable")).toBe(false); |
||
259 | |||
260 | cell.column.set("renderable", false); |
||
261 | expect(cell.$el.hasClass("renderable")).toBe(false); |
||
262 | |||
263 | var TrueCol = Backgrid.Column.extend({ |
||
264 | mySortable: function () { |
||
265 | return true; |
||
266 | }, |
||
267 | myRenderable: function () { |
||
268 | return true; |
||
269 | }, |
||
270 | myEditable: function () { |
||
271 | return true; |
||
272 | } |
||
273 | }); |
||
274 | |||
275 | var FalseCol = Backgrid.Column.extend({ |
||
276 | mySortable: function () { |
||
277 | return false; |
||
278 | }, |
||
279 | myRenderable: function () { |
||
280 | return false; |
||
281 | }, |
||
282 | myEditable: function () { |
||
283 | return false; |
||
284 | } |
||
285 | }); |
||
286 | |||
287 | column = new TrueCol({ |
||
288 | name: "title", |
||
289 | cell: "string", |
||
290 | sortable: "mySortable", |
||
291 | renderable: "myRenderable", |
||
292 | editable: "myEditable" |
||
293 | }); |
||
294 | |||
295 | cell = new Backgrid.Cell({ |
||
296 | model: book, |
||
297 | column: column |
||
298 | }); |
||
299 | |||
300 | expect(cell.$el.hasClass("editable")).toBe(true); |
||
301 | expect(cell.$el.hasClass("sortable")).toBe(true); |
||
302 | expect(cell.$el.hasClass("renderable")).toBe(true); |
||
303 | |||
304 | column = new FalseCol({ |
||
305 | name: "title", |
||
306 | cell: "string", |
||
307 | sortable: "mySortable", |
||
308 | renderable: "myRenderable", |
||
309 | editable: "myEditable" |
||
310 | }); |
||
311 | |||
312 | cell = new Backgrid.Cell({ |
||
313 | model: book, |
||
314 | column: column |
||
315 | }); |
||
316 | |||
317 | expect(cell.$el.hasClass("editable")).toBe(false); |
||
318 | expect(cell.$el.hasClass("sortable")).toBe(false); |
||
319 | expect(cell.$el.hasClass("renderable")).toBe(false); |
||
320 | |||
321 | column = new Backgrid.Column({ |
||
322 | name: "title", |
||
323 | cell: "string", |
||
324 | sortable: function () { |
||
325 | return true; |
||
326 | }, |
||
327 | editable: function () { |
||
328 | return true; |
||
329 | }, |
||
330 | renderable: function () { |
||
331 | return true; |
||
332 | } |
||
333 | }); |
||
334 | |||
335 | cell = new Backgrid.Cell({ |
||
336 | model: book, |
||
337 | column: column |
||
338 | }); |
||
339 | |||
340 | expect(cell.$el.hasClass("editable")).toBe(true); |
||
341 | expect(cell.$el.hasClass("sortable")).toBe(true); |
||
342 | expect(cell.$el.hasClass("renderable")).toBe(true); |
||
343 | }); |
||
344 | |||
345 | it("renders a td with the model value formatted for display", function () { |
||
346 | cell.render(); |
||
347 | expect(cell.$el.text()).toBe("title"); |
||
348 | }); |
||
349 | |||
350 | it("#472 updates editable, sortable, and renderable when rerendering", function () { |
||
351 | |||
352 | function starable(model) { |
||
353 | return model.get("title") == "Alice in Wonderland"; |
||
354 | } |
||
355 | |||
356 | var TestCol = Backgrid.Column.extend({ |
||
357 | mySortable: starable, |
||
358 | myRenderable: starable, |
||
359 | mySortable: starable |
||
360 | }); |
||
361 | |||
362 | column = new TestCol({ |
||
363 | name: "title", |
||
364 | cell: "string", |
||
365 | editable: "myEditable", |
||
366 | renderable: "myRenderable", |
||
367 | sortable: "mySortable" |
||
368 | }); |
||
369 | |||
370 | cell = new Backgrid.Cell({ |
||
371 | model: book, |
||
372 | column: column |
||
373 | }); |
||
374 | |||
375 | cell.render(); |
||
376 | |||
377 | book.set("title", "Alice in Wonderland"); |
||
378 | expect(cell.$el.hasClass("editable")).toBe(true); |
||
379 | expect(cell.$el.hasClass("sortable")).toBe(true); |
||
380 | expect(cell.$el.hasClass("renderable")).toBe(true); |
||
381 | |||
382 | book.set("title", "Oliver Twist"); |
||
383 | expect(cell.$el.hasClass("editable")).toBe(false); |
||
384 | expect(cell.$el.hasClass("sortable")).toBe(false); |
||
385 | expect(cell.$el.hasClass("renderable")).toBe(false); |
||
386 | }); |
||
387 | |||
388 | it("goes into edit mode on click", function () { |
||
389 | cell.render(); |
||
390 | cell.$el.click(); |
||
391 | expect(cell.$el.hasClass("editor")).toBe(true); |
||
392 | }); |
||
393 | |||
394 | it("goes into edit mode when `enterEditMode` is called", function () { |
||
395 | cell.render(); |
||
396 | cell.enterEditMode(); |
||
397 | expect(cell.$el.hasClass("editor")).toBe(true); |
||
398 | }); |
||
399 | |||
400 | it("goes back into display mode when `exitEditMode` is called", function () { |
||
401 | cell.render(); |
||
402 | |||
403 | cell.$el.click(); |
||
404 | cell.exitEditMode(); |
||
405 | expect(cell.$el.hasClass("editor")).toBe(false); |
||
406 | expect(cell.$el.text()).toBe("title"); |
||
407 | }); |
||
408 | |||
409 | it("renders error when the editor triggers 'backgrid:error'", function () { |
||
410 | |||
411 | cell.formatter = { |
||
412 | fromRaw: function () {}, |
||
413 | toRaw: function () {} |
||
414 | }; |
||
415 | |||
416 | cell.render(); |
||
417 | cell.$el.click(); |
||
418 | |||
419 | var editor = cell.currentEditor; |
||
420 | editor.$el.val(undefined); |
||
421 | |||
422 | var enter = $.Event("keydown", { |
||
423 | keyCode: 13 |
||
424 | }); |
||
425 | editor.$el.trigger(enter); |
||
426 | |||
427 | expect(cell.$el.hasClass("error")).toBe(true); |
||
428 | expect(cell.$el.hasClass("editor")).toBe(true); |
||
429 | }); |
||
430 | |||
431 | it("removes the editor correctly when removing the cell", function () { |
||
432 | cell.render(); |
||
433 | cell.$el.click(); |
||
434 | |||
435 | var editor = cell.currentEditor; |
||
436 | |||
437 | spyOn(editor, "remove"); |
||
438 | |||
439 | cell.remove("argument1", "argument2"); |
||
440 | |||
441 | expect(editor.remove).toHaveBeenCalledWith("argument1", "argument2"); |
||
442 | }); |
||
443 | |||
444 | describe("when the model value has changed", function () { |
||
445 | it("refreshes during display mode", function () { |
||
446 | cell.render(); |
||
447 | book.set("title", "another title"); |
||
448 | expect(cell.$el.text()).toBe("another title"); |
||
449 | }); |
||
450 | |||
451 | it("does not refresh during display mode if the change was silenced", function () { |
||
452 | cell.render(); |
||
453 | book.set("title", "another title", { |
||
454 | silent: true |
||
455 | }); |
||
456 | expect(cell.$el.text()).toBe("title"); |
||
457 | }); |
||
458 | |||
459 | it("does not refresh during edit mode", function () { |
||
460 | cell.render(); |
||
461 | cell.$el.click(); |
||
462 | book.set("title", "another title"); |
||
463 | expect(cell.$el.find("input[type=text]").val(), "title"); |
||
464 | }); |
||
465 | }); |
||
466 | |||
467 | }); |
||
468 | |||
469 | describe("A StringCell", function () { |
||
470 | |||
471 | it("applies a string-cell class to the cell", function () { |
||
472 | var book = new Backbone.Model({ |
||
473 | title: "<title>" |
||
474 | }); |
||
475 | |||
476 | var column = { |
||
477 | name: "title", |
||
478 | cell: "string" |
||
479 | }; |
||
480 | |||
481 | var cell = new Backgrid.StringCell({ |
||
482 | model: book, |
||
483 | column: column |
||
484 | }); |
||
485 | |||
486 | cell.render(); |
||
487 | expect(cell.$el.hasClass("string-cell")).toBe(true); |
||
488 | }); |
||
489 | |||
490 | }); |
||
491 | |||
492 | describe("A UriCell", function () { |
||
493 | |||
494 | var model; |
||
495 | var column; |
||
496 | var cell; |
||
497 | |||
498 | beforeEach(function () { |
||
499 | model = new Backbone.Model({ |
||
500 | url: "http://www.example.com" |
||
501 | }); |
||
502 | |||
503 | column = { |
||
504 | name: "url", |
||
505 | cell: "uri" |
||
506 | }; |
||
507 | |||
508 | cell = new Backgrid.UriCell({ |
||
509 | model: model, |
||
510 | column: column |
||
511 | }); |
||
512 | }); |
||
513 | |||
514 | it("applies a uri-cell class to the cell", function () { |
||
515 | cell.render(); |
||
516 | expect(cell.$el.hasClass("uri-cell")).toBe(true); |
||
517 | }); |
||
518 | |||
519 | it("renders the model value in an anchor", function () { |
||
520 | cell.render(); |
||
521 | expect(cell.$el.find("a").attr("href")).toBe("http://www.example.com"); |
||
522 | expect(cell.$el.find("a").text()).toBe("http://www.example.com"); |
||
523 | }); |
||
524 | |||
525 | it("uses the supplied target or _blank", function () { |
||
526 | cell.render(); |
||
527 | expect(cell.$el.find("a").attr("target")).toBe("_blank"); |
||
528 | |||
529 | cell = new Backgrid.UriCell({ |
||
530 | model: model, |
||
531 | column: column, |
||
532 | target: "_self" |
||
533 | }); |
||
534 | cell.render(); |
||
535 | expect(cell.$el.find("a").attr("target")).toBe("_self"); |
||
536 | }); |
||
537 | |||
538 | it("uses the supplied title or the raw value", function () { |
||
539 | cell.render(); |
||
540 | expect(cell.$el.find("a").attr("title")).toBe("http://www.example.com"); |
||
541 | |||
542 | cell = new Backgrid.UriCell({ |
||
543 | model: model, |
||
544 | column: column, |
||
545 | title: "http://backgridjs.com" |
||
546 | }); |
||
547 | cell.render(); |
||
548 | expect(cell.$el.find("a").attr("title")).toBe("http://backgridjs.com"); |
||
549 | }); |
||
550 | |||
551 | }); |
||
552 | |||
553 | describe("An EmailCell", function () { |
||
554 | |||
555 | var model; |
||
556 | var column; |
||
557 | var cell; |
||
558 | |||
559 | beforeEach(function () { |
||
560 | model = new Backbone.Model({ |
||
561 | email: "email@host" |
||
562 | }); |
||
563 | |||
564 | column = { |
||
565 | name: "email", |
||
566 | cell: "email" |
||
567 | }; |
||
568 | |||
569 | cell = new Backgrid.EmailCell({ |
||
570 | model: model, |
||
571 | column: column |
||
572 | }); |
||
573 | }); |
||
574 | |||
575 | it("applies a email-cell class to the cell", function () { |
||
576 | expect(cell.render().$el.hasClass("email-cell")).toBe(true); |
||
577 | }); |
||
578 | |||
579 | it("renders the model value in a mailto: anchor", function () { |
||
580 | cell.render(); |
||
581 | expect(cell.$el.find("a").attr("href")).toBe("mailto:email@host"); |
||
582 | expect(cell.$el.find("a").text()).toBe("email@host"); |
||
583 | }); |
||
584 | |||
585 | }); |
||
586 | |||
587 | describe("A NumberCell", function () { |
||
588 | |||
589 | it("accepts a formatter instance", function () { |
||
590 | |||
591 | var formatter = { |
||
592 | fromRaw: function (rawValue) { |
||
593 | return rawValue; |
||
594 | }, |
||
595 | toRaw: function (formattedValue) { |
||
596 | return +formattedValue; |
||
597 | } |
||
598 | }; |
||
599 | |||
600 | var cell = new Backgrid.NumberCell({ |
||
601 | model: new Backbone.Model({ |
||
602 | age: 1.1 |
||
603 | }), |
||
604 | column: { |
||
605 | name: "age", |
||
606 | cell: "number", |
||
607 | formatter: formatter |
||
608 | }, |
||
609 | }); |
||
610 | |||
611 | expect(cell.formatter).toBe(formatter); |
||
612 | }); |
||
613 | |||
614 | it("applies a number-cell class to the cell", function () { |
||
615 | var cell = new Backgrid.NumberCell({ |
||
616 | model: new Backbone.Model({ |
||
617 | age: 1.1 |
||
618 | }), |
||
619 | column: { |
||
620 | name: "age", |
||
621 | cell: "number" |
||
622 | } |
||
623 | }); |
||
624 | cell.render(); |
||
625 | expect(cell.$el.hasClass("number-cell")).toBe(true); |
||
626 | }); |
||
627 | |||
628 | }); |
||
629 | |||
630 | describe("An IntegerCell", function () { |
||
631 | |||
632 | var cell; |
||
633 | |||
634 | beforeEach(function () { |
||
635 | cell = new Backgrid.IntegerCell({ |
||
636 | model: new Backbone.Model({ |
||
637 | age: 1 |
||
638 | }), |
||
639 | column: { |
||
640 | name: "age", |
||
641 | cell: "integer" |
||
642 | } |
||
643 | }); |
||
644 | }); |
||
645 | |||
646 | it("applies an integer-cell class to the cell", function () { |
||
647 | cell.render(); |
||
648 | expect(cell.$el.hasClass("integer-cell")).toBe(true); |
||
649 | }); |
||
650 | |||
651 | it("will render a number with no trailing decimals numbers", function () { |
||
652 | cell.model.set("age", 1.1); |
||
653 | cell.render(); |
||
654 | expect(cell.$el.text()).toBe("1"); |
||
655 | }); |
||
656 | |||
657 | it("can be extended and it's defaults overidden", function () { |
||
658 | |||
659 | var PlainIntegerCell = Backgrid.IntegerCell.extend({ |
||
660 | orderSeparator: '' |
||
661 | }); |
||
662 | |||
663 | var cell = new PlainIntegerCell({ |
||
664 | model: new Backbone.Model({ |
||
665 | age: 1000 |
||
666 | }), |
||
667 | column: { |
||
668 | name: "age", |
||
669 | cell: PlainIntegerCell |
||
670 | } |
||
671 | }); |
||
672 | |||
673 | cell.render(); |
||
674 | expect(cell.$el.text()).toBe("1000"); |
||
675 | }); |
||
676 | |||
677 | }); |
||
678 | |||
679 | describe("A PercentCell", function () { |
||
680 | |||
681 | var cell; |
||
682 | |||
683 | beforeEach(function () { |
||
684 | cell = new Backgrid.PercentCell({ |
||
685 | model: new Backbone.Model({ |
||
686 | rate: 99.8 |
||
687 | }), |
||
688 | column: { |
||
689 | name: "rate", |
||
690 | cell: "percent" |
||
691 | } |
||
692 | }); |
||
693 | }); |
||
694 | |||
695 | it("applies an percent-cell class to the cell", function () { |
||
696 | cell.render(); |
||
697 | expect(cell.$el.hasClass("percent-cell")).toBe(true); |
||
698 | }); |
||
699 | |||
700 | it("will render a percentage string", function () { |
||
701 | cell.render(); |
||
702 | expect(cell.$el.text()).toBe("99.80%"); |
||
703 | }); |
||
704 | |||
705 | it("can be extended and it's defaults overidden", function () { |
||
706 | |||
707 | var MyPercentCell = Backgrid.PercentCell.extend({ |
||
708 | orderSeparator: '', |
||
709 | symbol: "pct", |
||
710 | multiplier: 100 |
||
711 | }); |
||
712 | |||
713 | var cell = new MyPercentCell({ |
||
714 | model: new Backbone.Model({ |
||
715 | rate: 10.99 |
||
716 | }), |
||
717 | column: { |
||
718 | name: "rate", |
||
719 | cell: MyPercentCell |
||
720 | } |
||
721 | }); |
||
722 | |||
723 | cell.render(); |
||
724 | expect(cell.$el.text()).toBe("1099.00pct"); |
||
725 | }); |
||
726 | |||
727 | }); |
||
728 | |||
729 | describe("A DatetimeCell", function () { |
||
730 | |||
731 | var model; |
||
732 | var column; |
||
733 | var cell; |
||
734 | |||
735 | beforeEach(function () { |
||
736 | |||
737 | model = new Backbone.Model({ |
||
738 | datetime: "2000-01-01T00:00:00.000Z" |
||
739 | }); |
||
740 | |||
741 | column = { |
||
742 | name: "datetime", |
||
743 | cell: "datetime" |
||
744 | }; |
||
745 | |||
746 | cell = new Backgrid.DatetimeCell({ |
||
747 | model: model, |
||
748 | column: column |
||
749 | }); |
||
750 | }); |
||
751 | |||
752 | it("accepts a formatter instance", function () { |
||
753 | |||
754 | var formatter = { |
||
755 | fromRaw: function (rawValue) { |
||
756 | return rawValue; |
||
757 | }, |
||
758 | toRaw: function (formattedValue) { |
||
759 | return formattedValue; |
||
760 | } |
||
761 | }; |
||
762 | |||
763 | cell = new Backgrid.DatetimeCell({ |
||
764 | model: model, |
||
765 | column: _.extend({}, column, { |
||
766 | formatter: formatter |
||
767 | }) |
||
768 | }); |
||
769 | |||
770 | expect(cell.formatter).toBe(formatter); |
||
771 | }); |
||
772 | |||
773 | it("applies a datetime-cell class to the cell", function () { |
||
774 | cell.render(); |
||
775 | expect(cell.$el.hasClass("datetime-cell")).toBe(true); |
||
776 | }); |
||
777 | |||
778 | it("renders a placeholder for different datetime formats for the editor according to configuration", function () { |
||
779 | cell = new Backgrid.DatetimeCell({ |
||
780 | model: model, |
||
781 | column: column |
||
782 | }); |
||
783 | expect(cell.editor.prototype.attributes.placeholder).toBe("YYYY-MM-DDTHH:mm:ss"); |
||
784 | |||
785 | cell = new(Backgrid.DatetimeCell.extend({ |
||
786 | includeTime: false |
||
787 | }))({ |
||
788 | model: model, |
||
789 | column: column |
||
790 | }); |
||
791 | expect(cell.editor.prototype.attributes.placeholder).toBe("YYYY-MM-DD"); |
||
792 | |||
793 | cell = new(Backgrid.DatetimeCell.extend({ |
||
794 | includeDate: false |
||
795 | }))({ |
||
796 | model: model, |
||
797 | column: column |
||
798 | }); |
||
799 | expect(cell.editor.prototype.attributes.placeholder).toBe("HH:mm:ss"); |
||
800 | |||
801 | cell = new(Backgrid.DatetimeCell.extend({ |
||
802 | includeDate: false, |
||
803 | includeMilli: true |
||
804 | }))({ |
||
805 | model: model, |
||
806 | column: column |
||
807 | }); |
||
808 | expect(cell.editor.prototype.attributes.placeholder).toBe("HH:mm:ss.SSS"); |
||
809 | |||
810 | cell = new(Backgrid.DatetimeCell.extend({ |
||
811 | includeMilli: true |
||
812 | }))({ |
||
813 | model: model, |
||
814 | column: column |
||
815 | }); |
||
816 | expect(cell.editor.prototype.attributes.placeholder).toBe("YYYY-MM-DDTHH:mm:ss.SSS"); |
||
817 | }); |
||
818 | |||
819 | it("is blank when the date value is null", function () { |
||
820 | cell = new(Backgrid.DatetimeCell.extend({ |
||
821 | includeMilli: true |
||
822 | }))({ |
||
823 | model: new Backbone.Model({ |
||
824 | datetime: null |
||
825 | }), |
||
826 | column: column |
||
827 | }); |
||
828 | expect(cell.$el.html()).toBe(''); |
||
829 | }); |
||
830 | |||
831 | }); |
||
832 | |||
833 | describe("A DateCell", function () { |
||
834 | |||
835 | var cell; |
||
836 | |||
837 | beforeEach(function () { |
||
838 | cell = new Backgrid.DateCell({ |
||
839 | model: new Backbone.Model({ |
||
840 | date: "2000-01-01" |
||
841 | }), |
||
842 | column: { |
||
843 | name: "date", |
||
844 | cell: "date" |
||
845 | } |
||
846 | }); |
||
847 | }); |
||
848 | |||
849 | it("applies a date-cell class to the cell", function () { |
||
850 | cell.render(); |
||
851 | expect(cell.$el.hasClass("date-cell")).toBe(true); |
||
852 | }); |
||
853 | |||
854 | it("will render a date with no time", function () { |
||
855 | cell.model.set("date", "2000-01-01T00:00:00.000Z"); |
||
856 | cell.render(); |
||
857 | expect(cell.$el.text()).toBe("2000-01-01"); |
||
858 | }); |
||
859 | |||
860 | }); |
||
861 | |||
862 | describe("A TimeCell", function () { |
||
863 | |||
864 | var cell; |
||
865 | |||
866 | beforeEach(function () { |
||
867 | cell = new Backgrid.TimeCell({ |
||
868 | model: new Backbone.Model({ |
||
869 | time: "00:00:00" |
||
870 | }), |
||
871 | column: { |
||
872 | name: "time", |
||
873 | cell: "time" |
||
874 | } |
||
875 | }); |
||
876 | }); |
||
877 | |||
878 | it("applies a time-cell class to the cell", function () { |
||
879 | cell.render(); |
||
880 | expect(cell.$el.hasClass("time-cell")).toBe(true); |
||
881 | }); |
||
882 | |||
883 | it("will render a time with no date", function () { |
||
884 | cell.model.set("date", "2000-01-01T00:00:00.000Z"); |
||
885 | cell.render(); |
||
886 | expect(cell.$el.text()).toBe("00:00:00"); |
||
887 | }); |
||
888 | |||
889 | }); |
||
890 | |||
891 | describe("A BooleanCell", function () { |
||
892 | |||
893 | var model; |
||
894 | var column; |
||
895 | var cell; |
||
896 | |||
897 | beforeEach(function () { |
||
898 | model = new Backbone.Model({ |
||
899 | ate: true |
||
900 | }); |
||
901 | column = { |
||
902 | name: "ate", |
||
903 | cell: "boolean" |
||
904 | }; |
||
905 | cell = new Backgrid.BooleanCell({ |
||
906 | model: model, |
||
907 | column: column |
||
908 | }); |
||
909 | }); |
||
910 | |||
911 | it("applies a boolean-cell class to the cell", function () { |
||
912 | expect(cell.render().$el.hasClass("boolean-cell")).toBe(true); |
||
913 | }); |
||
914 | |||
915 | it("has a display mode that renders a checkbox with the checkbox checked if the model value is true, not checked otherwise", function () { |
||
916 | cell.render(); |
||
917 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(true); |
||
918 | model.set("ate", false); |
||
919 | cell.render(); |
||
920 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(false); |
||
921 | }); |
||
922 | |||
923 | it("renders a disabled checkbox if not editable", function () { |
||
924 | cell.column.set("editable", false); |
||
925 | cell.render(); |
||
926 | expect(cell.$el.find(":checkbox").prop("disabled")).toBe(true); |
||
927 | |||
928 | cell.column.set("editable", true); |
||
929 | cell.render(); |
||
930 | expect(cell.$el.find(":checkbox").prop("disabled")).toBe(false); |
||
931 | }); |
||
932 | |||
933 | it("goes into edit mode after clicking the cell with the checkbox intact", function () { |
||
934 | cell.render(); |
||
935 | cell.$el.click(); |
||
936 | expect(cell.$el.hasClass("editor")).toBe(true); |
||
937 | expect(cell.$el.find(":checkbox").length).toBe(1); |
||
938 | }); |
||
939 | |||
940 | it("goes into edit mode after calling `enterEditMode` with the checkbox intact", function () { |
||
941 | cell.render(); |
||
942 | cell.enterEditMode(); |
||
943 | expect(cell.$el.hasClass("editor")).toBe(true); |
||
944 | expect(cell.$el.find(":checkbox").length).toBe(1); |
||
945 | }); |
||
946 | |||
947 | it("goes back to display mode after calling `exitEditMode`", function () { |
||
948 | cell.render(); |
||
949 | cell.enterEditMode(); |
||
950 | cell.exitEditMode(); |
||
951 | expect(cell.$el.hasClass("editor")).toBe(false); |
||
952 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(true); |
||
953 | }); |
||
954 | |||
955 | it("triggers `backgrid:edited` when the checkbox goes out of focus", function () { |
||
956 | var backgridEditedTriggerCount = 0; |
||
957 | var backgridEditedTriggerArgs; |
||
958 | cell.model.on("backgrid:edited", function () { |
||
959 | backgridEditedTriggerCount++; |
||
960 | backgridEditedTriggerArgs = [].slice.call(arguments); |
||
961 | }); |
||
962 | |||
963 | cell.render(); |
||
964 | cell.$el.click(); |
||
965 | cell.$el.find(":checkbox").blur(); |
||
966 | expect(backgridEditedTriggerCount).toBe(1); |
||
967 | expect(backgridEditedTriggerArgs[0]).toBe(cell.model); |
||
968 | expect(backgridEditedTriggerArgs[1]).toBe(cell.column); |
||
969 | expect(backgridEditedTriggerArgs[2].passThru()).toBe(true); |
||
970 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(true); |
||
971 | |||
972 | cell.render(); |
||
973 | cell.$el.click(); |
||
974 | cell.currentEditor.$el.mousedown(); |
||
975 | cell.$el.find(":checkbox").blur(); |
||
976 | expect(backgridEditedTriggerCount).toBe(1); |
||
977 | }); |
||
978 | |||
979 | it("saves a boolean value to the model when the checkbox is toggled", function () { |
||
980 | cell.render(); |
||
981 | cell.enterEditMode(); |
||
982 | cell.$el.find(":checkbox").prop("checked", false).change(); |
||
983 | expect(cell.model.get(cell.column.get("name"))).toBe(false); |
||
984 | }); |
||
985 | |||
986 | describe("when the model value has changed", function () { |
||
987 | it("refreshes during display mode", function () { |
||
988 | cell.render(); |
||
989 | model.set("ate", false); |
||
990 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(false); |
||
991 | model.set("ate", true); |
||
992 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(true); |
||
993 | }); |
||
994 | |||
995 | it("does not refresh during display mode if the change was silenced", function () { |
||
996 | cell.render(); |
||
997 | model.set("ate", false, { |
||
998 | silent: true |
||
999 | }); |
||
1000 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(true); |
||
1001 | model.set("ate", false); |
||
1002 | model.set("ate", true, { |
||
1003 | silent: true |
||
1004 | }); |
||
1005 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(true); |
||
1006 | }); |
||
1007 | |||
1008 | it("does not refresh during edit mode", function () { |
||
1009 | cell.render(); |
||
1010 | cell.$el.click(); |
||
1011 | model.set("ate", false); |
||
1012 | expect(cell.$el.find(":checkbox").prop("checked")).toBe(true); |
||
1013 | }); |
||
1014 | }); |
||
1015 | |||
1016 | }); |
||
1017 | |||
1018 | describe("A SelectCellEditor", function () { |
||
1019 | |||
1020 | var optionValues; |
||
1021 | var optionGroupValues; |
||
1022 | |||
1023 | beforeEach(function () { |
||
1024 | optionValues = [ |
||
1025 | ["Boy", 1], |
||
1026 | ["Girl", 2] |
||
1027 | ]; |
||
1028 | |||
1029 | optionGroupValues = [{ |
||
1030 | "name": "\" ><script></script>Fruit", |
||
1031 | "values": [ |
||
1032 | ["Apple<script></script>", "\" ><script></script>a"], |
||
1033 | ["Banana", "b"], |
||
1034 | ["Cantaloupe", "c"] |
||
1035 | ] |
||
1036 | }, { |
||
1037 | "name": "Cereal", |
||
1038 | "values": [ |
||
1039 | ["Wheat", "w"], |
||
1040 | ["Rice", "r"], |
||
1041 | ["Maize", "m"] |
||
1042 | ] |
||
1043 | }]; |
||
1044 | }); |
||
1045 | |||
1046 | it("renders a select box using a list if nvps", function () { |
||
1047 | |||
1048 | // single selection |
||
1049 | var editor = new Backgrid.SelectCellEditor({ |
||
1050 | formatter: new Backgrid.SelectFormatter(), |
||
1051 | column: { |
||
1052 | name: "gender", |
||
1053 | cell: "select" |
||
1054 | }, |
||
1055 | model: new Backbone.Model({ |
||
1056 | gender: 2 |
||
1057 | }) |
||
1058 | }); |
||
1059 | |||
1060 | editor.setOptionValues(optionValues); |
||
1061 | editor.render(); |
||
1062 | expect(editor.el.tagName).toBe("SELECT"); |
||
1063 | var $options = editor.$el.children(); |
||
1064 | expect($options.length).toBe(2); |
||
1065 | expect($options.eq(0).val()).toBe("1"); |
||
1066 | expect($options.eq(0).prop("selected")).toBe(false); |
||
1067 | expect($options.eq(0).text()).toBe("Boy"); |
||
1068 | expect($options.eq(1).val()).toBe("2"); |
||
1069 | expect($options.eq(1).prop("selected")).toBe(true); |
||
1070 | expect($options.eq(1).text()).toBe("Girl"); |
||
1071 | |||
1072 | // multiple selection |
||
1073 | var editor = new Backgrid.SelectCellEditor({ |
||
1074 | formatter: new Backgrid.SelectFormatter(), |
||
1075 | column: { |
||
1076 | name: "gender", |
||
1077 | cell: "select" |
||
1078 | }, |
||
1079 | model: new Backbone.Model({ |
||
1080 | gender: [1, 2] |
||
1081 | }) |
||
1082 | }); |
||
1083 | |||
1084 | editor.setMultiple(true); |
||
1085 | editor.setOptionValues(optionValues); |
||
1086 | editor.render(); |
||
1087 | expect(editor.el.tagName).toBe("SELECT"); |
||
1088 | expect(editor.el.multiple).toBe(true); |
||
1089 | var $options = editor.$el.children(); |
||
1090 | expect($options.length).toBe(2); |
||
1091 | expect($options.eq(0).val()).toBe("1"); |
||
1092 | expect($options.eq(0).prop("selected")).toBe(true); |
||
1093 | expect($options.eq(0).text()).toBe("Boy"); |
||
1094 | expect($options.eq(1).val()).toBe("2"); |
||
1095 | expect($options.eq(1).prop("selected")).toBe(true); |
||
1096 | expect($options.eq(1).text()).toBe("Girl"); |
||
1097 | }); |
||
1098 | |||
1099 | it("renders a select box using a parameter-less function that returns a list if nvps", function () { |
||
1100 | |||
1101 | // single selection |
||
1102 | var editor = new Backgrid.SelectCellEditor({ |
||
1103 | formatter: new Backgrid.SelectFormatter(), |
||
1104 | column: { |
||
1105 | name: "gender", |
||
1106 | cell: "select" |
||
1107 | }, |
||
1108 | model: new Backbone.Model({ |
||
1109 | gender: 2 |
||
1110 | }) |
||
1111 | }); |
||
1112 | |||
1113 | editor.setOptionValues(function () { |
||
1114 | return optionValues; |
||
1115 | }); |
||
1116 | editor.render(); |
||
1117 | expect(editor.el.tagName).toBe("SELECT"); |
||
1118 | var $options = editor.$el.children(); |
||
1119 | expect($options.length).toBe(2); |
||
1120 | expect($options.eq(0).val()).toBe("1"); |
||
1121 | expect($options.eq(0).prop("selected")).toBe(false); |
||
1122 | expect($options.eq(0).text()).toBe("Boy"); |
||
1123 | expect($options.eq(1).val()).toBe("2"); |
||
1124 | expect($options.eq(1).prop("selected")).toBe(true); |
||
1125 | expect($options.eq(1).text()).toBe("Girl"); |
||
1126 | |||
1127 | // multiple selection |
||
1128 | var editor = new Backgrid.SelectCellEditor({ |
||
1129 | formatter: new Backgrid.SelectFormatter(), |
||
1130 | column: { |
||
1131 | name: "gender", |
||
1132 | cell: "select" |
||
1133 | }, |
||
1134 | model: new Backbone.Model({ |
||
1135 | gender: [1, 2] |
||
1136 | }) |
||
1137 | }); |
||
1138 | |||
1139 | editor.setMultiple(true); |
||
1140 | editor.setOptionValues(function () { |
||
1141 | return optionValues; |
||
1142 | }); |
||
1143 | editor.render(); |
||
1144 | expect(editor.el.tagName).toBe("SELECT"); |
||
1145 | expect(editor.el.multiple).toBe(true); |
||
1146 | var $options = editor.$el.children(); |
||
1147 | expect($options.length).toBe(2); |
||
1148 | expect($options.eq(0).val()).toBe("1"); |
||
1149 | expect($options.eq(0).prop("selected")).toBe(true); |
||
1150 | expect($options.eq(0).text()).toBe("Boy"); |
||
1151 | expect($options.eq(1).val()).toBe("2"); |
||
1152 | expect($options.eq(1).prop("selected")).toBe(true); |
||
1153 | expect($options.eq(1).text()).toBe("Girl"); |
||
1154 | }); |
||
1155 | |||
1156 | it("renders a select box using a list of object literals denoting option groups", function () { |
||
1157 | |||
1158 | // single selection |
||
1159 | var editor = new Backgrid.SelectCellEditor({ |
||
1160 | formatter: new Backgrid.SelectFormatter(), |
||
1161 | column: { |
||
1162 | name: "food", |
||
1163 | cell: "select" |
||
1164 | }, |
||
1165 | model: new Backbone.Model({ |
||
1166 | food: "b" |
||
1167 | }) |
||
1168 | }); |
||
1169 | |||
1170 | editor.setOptionValues(optionGroupValues); |
||
1171 | editor.render(); |
||
1172 | var $optionGroups = editor.$el.children(); |
||
1173 | expect($optionGroups.length).toBe(2); |
||
1174 | |||
1175 | var $group1 = $optionGroups.eq(0); |
||
1176 | var $group2 = $optionGroups.eq(1); |
||
1177 | |||
1178 | expect($group1.attr("label")).toBe("\" ><script></script>Fruit"); |
||
1179 | expect($group2.attr("label")).toBe("Cereal"); |
||
1180 | |||
1181 | var $group1Options = $group1.children(); |
||
1182 | expect($group1Options.eq(0).val()).toBe("\" ><script></script>a"); |
||
1183 | expect($group1Options.eq(0).prop("selected")).toBe(false); |
||
1184 | expect($group1Options.eq(0).html()).toBe("Apple<script></script>"); |
||
1185 | expect($group1Options.eq(1).val()).toBe("b"); |
||
1186 | expect($group1Options.eq(1).prop("selected")).toBe(true); |
||
1187 | expect($group1Options.eq(1).text()).toBe("Banana"); |
||
1188 | expect($group1Options.eq(2).val()).toBe("c"); |
||
1189 | expect($group1Options.eq(2).prop("selected")).toBe(false); |
||
1190 | expect($group1Options.eq(2).text()).toBe("Cantaloupe"); |
||
1191 | |||
1192 | var $group2Options = $group2.children(); |
||
1193 | expect($group2Options.eq(0).val()).toBe("w"); |
||
1194 | expect($group2Options.eq(0).prop("selected")).toBe(false); |
||
1195 | expect($group2Options.eq(0).text()).toBe("Wheat"); |
||
1196 | expect($group2Options.eq(1).val()).toBe("r"); |
||
1197 | expect($group2Options.eq(1).prop("selected")).toBe(false); |
||
1198 | expect($group2Options.eq(1).text()).toBe("Rice"); |
||
1199 | expect($group2Options.eq(2).val()).toBe("m"); |
||
1200 | expect($group2Options.eq(2).prop("selected")).toBe(false); |
||
1201 | expect($group2Options.eq(2).text()).toBe("Maize"); |
||
1202 | |||
1203 | // multiple selection |
||
1204 | var editor = new Backgrid.SelectCellEditor({ |
||
1205 | formatter: new Backgrid.SelectFormatter(), |
||
1206 | column: { |
||
1207 | name: "food", |
||
1208 | cell: "select" |
||
1209 | }, |
||
1210 | model: new Backbone.Model({ |
||
1211 | food: ["b", "c"] |
||
1212 | }) |
||
1213 | }); |
||
1214 | |||
1215 | editor.setMultiple(true); |
||
1216 | editor.setOptionValues(optionGroupValues); |
||
1217 | editor.render(); |
||
1218 | var $optionGroups = editor.$el.children(); |
||
1219 | expect($optionGroups.length).toBe(2); |
||
1220 | |||
1221 | var $group1 = $optionGroups.eq(0); |
||
1222 | var $group2 = $optionGroups.eq(1); |
||
1223 | |||
1224 | expect($group1.attr("label")).toBe("\" ><script></script>Fruit"); |
||
1225 | expect($group2.attr("label")).toBe("Cereal"); |
||
1226 | |||
1227 | var $group1Options = $group1.children(); |
||
1228 | expect($group1Options.eq(0).val()).toBe("\" ><script></script>a"); |
||
1229 | expect($group1Options.eq(0).prop("selected")).toBe(false); |
||
1230 | expect($group1Options.eq(0).html()).toBe("Apple<script></script>"); |
||
1231 | expect($group1Options.eq(1).val()).toBe("b"); |
||
1232 | expect($group1Options.eq(1).prop("selected")).toBe(true); |
||
1233 | expect($group1Options.eq(1).text()).toBe("Banana"); |
||
1234 | expect($group1Options.eq(2).val()).toBe("c"); |
||
1235 | expect($group1Options.eq(2).prop("selected")).toBe(true); |
||
1236 | expect($group1Options.eq(2).text()).toBe("Cantaloupe"); |
||
1237 | |||
1238 | var $group2Options = $group2.children(); |
||
1239 | expect($group2Options.eq(0).val()).toBe("w"); |
||
1240 | expect($group2Options.eq(0).prop("selected")).toBe(false); |
||
1241 | expect($group2Options.eq(0).text()).toBe("Wheat"); |
||
1242 | expect($group2Options.eq(1).val()).toBe("r"); |
||
1243 | expect($group2Options.eq(1).prop("selected")).toBe(false); |
||
1244 | expect($group2Options.eq(1).text()).toBe("Rice"); |
||
1245 | expect($group2Options.eq(2).val()).toBe("m"); |
||
1246 | expect($group2Options.eq(2).prop("selected")).toBe(false); |
||
1247 | expect($group2Options.eq(2).text()).toBe("Maize"); |
||
1248 | }); |
||
1249 | |||
1250 | it("renders a select box using a parameter-less function that returns a list of object literals denoting option groups", function () { |
||
1251 | |||
1252 | // single selection |
||
1253 | var editor = new Backgrid.SelectCellEditor({ |
||
1254 | formatter: new Backgrid.SelectFormatter(), |
||
1255 | column: { |
||
1256 | name: "food", |
||
1257 | cell: "select" |
||
1258 | }, |
||
1259 | model: new Backbone.Model({ |
||
1260 | food: "b" |
||
1261 | }) |
||
1262 | }); |
||
1263 | |||
1264 | editor.setOptionValues(function () { |
||
1265 | return optionGroupValues; |
||
1266 | }); |
||
1267 | editor.render(); |
||
1268 | var $optionGroups = editor.$el.children(); |
||
1269 | expect($optionGroups.length).toBe(2); |
||
1270 | |||
1271 | var $group1 = $optionGroups.eq(0); |
||
1272 | var $group2 = $optionGroups.eq(1); |
||
1273 | |||
1274 | expect($group1.attr("label")).toBe("\" ><script></script>Fruit"); |
||
1275 | expect($group2.attr("label")).toBe("Cereal"); |
||
1276 | |||
1277 | var $group1Options = $group1.children(); |
||
1278 | expect($group1Options.eq(0).val()).toBe("\" ><script></script>a"); |
||
1279 | expect($group1Options.eq(0).prop("selected")).toBe(false); |
||
1280 | expect($group1Options.eq(0).html()).toBe("Apple<script></script>"); |
||
1281 | expect($group1Options.eq(1).val()).toBe("b"); |
||
1282 | expect($group1Options.eq(1).prop("selected")).toBe(true); |
||
1283 | expect($group1Options.eq(1).text()).toBe("Banana"); |
||
1284 | expect($group1Options.eq(2).val()).toBe("c"); |
||
1285 | expect($group1Options.eq(2).prop("selected")).toBe(false); |
||
1286 | expect($group1Options.eq(2).text()).toBe("Cantaloupe"); |
||
1287 | |||
1288 | var $group2Options = $group2.children(); |
||
1289 | expect($group2Options.eq(0).val()).toBe("w"); |
||
1290 | expect($group2Options.eq(0).prop("selected")).toBe(false); |
||
1291 | expect($group2Options.eq(0).text()).toBe("Wheat"); |
||
1292 | expect($group2Options.eq(1).val()).toBe("r"); |
||
1293 | expect($group2Options.eq(1).prop("selected")).toBe(false); |
||
1294 | expect($group2Options.eq(1).text()).toBe("Rice"); |
||
1295 | expect($group2Options.eq(2).val()).toBe("m"); |
||
1296 | expect($group2Options.eq(2).prop("selected")).toBe(false); |
||
1297 | expect($group2Options.eq(2).text()).toBe("Maize"); |
||
1298 | |||
1299 | // multiple selection |
||
1300 | var editor = new Backgrid.SelectCellEditor({ |
||
1301 | formatter: new Backgrid.SelectFormatter(), |
||
1302 | column: { |
||
1303 | name: "food", |
||
1304 | cell: "select" |
||
1305 | }, |
||
1306 | model: new Backbone.Model({ |
||
1307 | food: ["b", "c"] |
||
1308 | }) |
||
1309 | }); |
||
1310 | |||
1311 | editor.setMultiple(true); |
||
1312 | editor.setOptionValues(function () { |
||
1313 | return optionGroupValues; |
||
1314 | }); |
||
1315 | editor.render(); |
||
1316 | var $optionGroups = editor.$el.children(); |
||
1317 | expect($optionGroups.length).toBe(2); |
||
1318 | |||
1319 | var $group1 = $optionGroups.eq(0); |
||
1320 | var $group2 = $optionGroups.eq(1); |
||
1321 | |||
1322 | expect($group1.attr("label")).toBe("\" ><script></script>Fruit"); |
||
1323 | expect($group2.attr("label")).toBe("Cereal"); |
||
1324 | |||
1325 | var $group1Options = $group1.children(); |
||
1326 | expect($group1Options.eq(0).val()).toBe("\" ><script></script>a"); |
||
1327 | expect($group1Options.eq(0).prop("selected")).toBe(false); |
||
1328 | expect($group1Options.eq(0).html()).toBe("Apple<script></script>"); |
||
1329 | expect($group1Options.eq(1).val()).toBe("b"); |
||
1330 | expect($group1Options.eq(1).prop("selected")).toBe(true); |
||
1331 | expect($group1Options.eq(1).text()).toBe("Banana"); |
||
1332 | expect($group1Options.eq(2).val()).toBe("c"); |
||
1333 | expect($group1Options.eq(2).prop("selected")).toBe(true); |
||
1334 | expect($group1Options.eq(2).text()).toBe("Cantaloupe"); |
||
1335 | |||
1336 | var $group2Options = $group2.children(); |
||
1337 | expect($group2Options.eq(0).val()).toBe("w"); |
||
1338 | expect($group2Options.eq(0).prop("selected")).toBe(false); |
||
1339 | expect($group2Options.eq(0).text()).toBe("Wheat"); |
||
1340 | expect($group2Options.eq(1).val()).toBe("r"); |
||
1341 | expect($group2Options.eq(1).prop("selected")).toBe(false); |
||
1342 | expect($group2Options.eq(1).text()).toBe("Rice"); |
||
1343 | expect($group2Options.eq(2).val()).toBe("m"); |
||
1344 | expect($group2Options.eq(2).prop("selected")).toBe(false); |
||
1345 | expect($group2Options.eq(2).text()).toBe("Maize"); |
||
1346 | }); |
||
1347 | |||
1348 | it("saves the value to the model on change", function () { |
||
1349 | |||
1350 | // single selection |
||
1351 | var editor = new Backgrid.SelectCellEditor({ |
||
1352 | formatter: new Backgrid.SelectFormatter(), |
||
1353 | column: { |
||
1354 | name: "gender", |
||
1355 | cell: "select" |
||
1356 | }, |
||
1357 | model: new Backbone.Model({ |
||
1358 | gender: 2 |
||
1359 | }) |
||
1360 | }); |
||
1361 | |||
1362 | editor.setOptionValues(optionValues); |
||
1363 | editor.render(); |
||
1364 | |||
1365 | spyOn(editor.formatter, "toRaw").and.callThrough(); |
||
1366 | |||
1367 | editor.$el.val(1).change(); |
||
1368 | expect(editor.formatter.toRaw).toHaveBeenCalledWith("1", editor.model); |
||
1369 | expect(editor.formatter.toRaw.calls.count()).toBe(1); |
||
1370 | expect(editor.model.get(editor.column.get("name"))).toBe("1"); |
||
1371 | |||
1372 | // multiple selection |
||
1373 | var editor = new Backgrid.SelectCellEditor({ |
||
1374 | formatter: new Backgrid.SelectFormatter(), |
||
1375 | column: { |
||
1376 | name: "gender", |
||
1377 | cell: "select" |
||
1378 | }, |
||
1379 | model: new Backbone.Model({ |
||
1380 | gender: [1, 2] |
||
1381 | }) |
||
1382 | }); |
||
1383 | |||
1384 | editor.setMultiple(true); |
||
1385 | editor.setOptionValues(optionValues); |
||
1386 | editor.render(); |
||
1387 | |||
1388 | spyOn(editor.formatter, "toRaw").and.callThrough(); |
||
1389 | |||
1390 | editor.$el.val([1, 2]).change(); |
||
1391 | expect(editor.formatter.toRaw).toHaveBeenCalledWith(["1", "2"], editor.model); |
||
1392 | expect(editor.formatter.toRaw.calls.count()).toBe(1); |
||
1393 | expect(editor.model.get(editor.column.get("name"))).toEqual(["1", "2"]); |
||
1394 | |||
1395 | editor.$el.val(null).change(); |
||
1396 | //expect(editor.formatter.toRaw).toHaveBeenCalledWith(null, editor.model); |
||
1397 | expect(editor.formatter.toRaw.calls.count()).toBe(2); |
||
1398 | |||
1399 | var editor_column_name = editor.column.get("name"), |
||
1400 | get_gender = editor.model.get(editor_column_name); |
||
1401 | |||
1402 | //console.log('editor_column_name', editor_column_name); |
||
1403 | var edcm_get = editor.model.get(editor_column_name); |
||
1404 | //console.log('editor_column_name', editor_column_name, edcm_get); |
||
1405 | |||
1406 | expect(edcm_get).toEqual([]); |
||
1407 | |||
1408 | }); |
||
1409 | |||
1410 | it("saves the value to the model on blur if there's only one option", function () { |
||
1411 | var editor = new Backgrid.SelectCellEditor({ |
||
1412 | formatter: new Backgrid.SelectFormatter(), |
||
1413 | column: { |
||
1414 | name: "gender", |
||
1415 | cell: "select" |
||
1416 | }, |
||
1417 | model: new Backbone.Model() |
||
1418 | }); |
||
1419 | editor.setOptionValues([ |
||
1420 | ["Boy", "1"] |
||
1421 | ]); |
||
1422 | editor.render(); |
||
1423 | |||
1424 | editor.$el.blur(); |
||
1425 | expect(editor.model.get(editor.column.get("name"))).toBe("1"); |
||
1426 | }); |
||
1427 | |||
1428 | it("can accept optionValues that is a function", function () { |
||
1429 | var editor = new Backgrid.SelectCellEditor({ |
||
1430 | formatter: new Backgrid.SelectFormatter(), |
||
1431 | column: { |
||
1432 | name: "gender", |
||
1433 | cell: "select" |
||
1434 | }, |
||
1435 | model: new Backbone.Model() |
||
1436 | }); |
||
1437 | editor.setOptionValues(function () { |
||
1438 | return [ |
||
1439 | ["Male", "M"], |
||
1440 | ["Female", "F"] |
||
1441 | ]; |
||
1442 | }); |
||
1443 | editor.render(); |
||
1444 | expect(editor.$el.find("option").eq(0).val()).toBe("M"); |
||
1445 | expect(editor.$el.find("option").eq(0).text()).toBe("Male"); |
||
1446 | expect(editor.$el.find("option").eq(1).val()).toBe("F"); |
||
1447 | expect(editor.$el.find("option").eq(1).text()).toBe("Female"); |
||
1448 | expect(editor.el.tagName).toBe("SELECT"); |
||
1449 | }); |
||
1450 | |||
1451 | }); |
||
1452 | |||
1453 | describe("A SelectCell", function () { |
||
1454 | var optionValues; |
||
1455 | var optionGroupValues; |
||
1456 | |||
1457 | beforeEach(function () { |
||
1458 | optionValues = [ |
||
1459 | ["Boy", 1], |
||
1460 | ["Girl", 2] |
||
1461 | ]; |
||
1462 | |||
1463 | optionGroupValues = [{ |
||
1464 | "name": "Fruit", |
||
1465 | "values": [ |
||
1466 | ["Apple", "a"], |
||
1467 | ["Banana", "b"], |
||
1468 | ["Cantaloupe", "c"] |
||
1469 | ] |
||
1470 | }, { |
||
1471 | "name": "Cereal", |
||
1472 | "values": [ |
||
1473 | ["Wheat", "w"], |
||
1474 | ["Rice", "r"], |
||
1475 | ["Maize", "m"] |
||
1476 | ] |
||
1477 | }]; |
||
1478 | }); |
||
1479 | |||
1480 | it("applies a select-cell class to the cell", function () { |
||
1481 | var cell = new(Backgrid.SelectCell.extend({ |
||
1482 | optionValues: optionValues |
||
1483 | }))({ |
||
1484 | column: { |
||
1485 | name: "gender", |
||
1486 | cell: "select" |
||
1487 | }, |
||
1488 | model: new Backbone.Model({ |
||
1489 | gender: 2 |
||
1490 | }) |
||
1491 | }); |
||
1492 | |||
1493 | cell.render(); |
||
1494 | |||
1495 | expect(cell.$el.hasClass("select-cell")).toBe(true); |
||
1496 | }); |
||
1497 | |||
1498 | it("renders the label of the selected option in display mode", function () { |
||
1499 | |||
1500 | // single selection |
||
1501 | var cell = new(Backgrid.SelectCell.extend({ |
||
1502 | optionValues: optionValues |
||
1503 | }))({ |
||
1504 | column: { |
||
1505 | name: "gender", |
||
1506 | cell: "select" |
||
1507 | }, |
||
1508 | model: new Backbone.Model({ |
||
1509 | gender: 2 |
||
1510 | }) |
||
1511 | }); |
||
1512 | |||
1513 | cell.render(); |
||
1514 | expect(cell.$el.text()).toBe("Girl"); |
||
1515 | |||
1516 | var cell = new(Backgrid.SelectCell.extend({ |
||
1517 | optionValues: optionGroupValues |
||
1518 | }))({ |
||
1519 | column: { |
||
1520 | name: "food", |
||
1521 | cell: "select" |
||
1522 | }, |
||
1523 | model: new Backbone.Model({ |
||
1524 | food: "b" |
||
1525 | }) |
||
1526 | }); |
||
1527 | |||
1528 | cell.render(); |
||
1529 | expect(cell.$el.text()).toBe("Banana"); |
||
1530 | |||
1531 | // multiple selection |
||
1532 | var cell = new(Backgrid.SelectCell.extend({ |
||
1533 | optionValues: optionValues |
||
1534 | }))({ |
||
1535 | column: { |
||
1536 | name: "gender", |
||
1537 | cell: "select" |
||
1538 | }, |
||
1539 | model: new Backbone.Model({ |
||
1540 | gender: [1, 2] |
||
1541 | }) |
||
1542 | }); |
||
1543 | |||
1544 | cell.render(); |
||
1545 | expect(cell.$el.text()).toBe("Boy, Girl"); |
||
1546 | |||
1547 | var cell = new(Backgrid.SelectCell.extend({ |
||
1548 | optionValues: optionGroupValues |
||
1549 | }))({ |
||
1550 | column: { |
||
1551 | name: "food", |
||
1552 | cell: "select" |
||
1553 | }, |
||
1554 | model: new Backbone.Model({ |
||
1555 | food: ["a", "b"] |
||
1556 | }) |
||
1557 | }); |
||
1558 | |||
1559 | cell.render(); |
||
1560 | expect(cell.$el.text()).toBe("Apple, Banana"); |
||
1561 | }); |
||
1562 | |||
1563 | it("can accept optionValues that is a function", function () { |
||
1564 | var cell = new(Backgrid.SelectCell.extend({ |
||
1565 | optionValues: function () { |
||
1566 | return [ |
||
1567 | ["Male", "m"], |
||
1568 | ["Female", "f"] |
||
1569 | ]; |
||
1570 | } |
||
1571 | }))({ |
||
1572 | column: { |
||
1573 | name: "gender", |
||
1574 | cell: "select" |
||
1575 | }, |
||
1576 | model: new Backbone.Model({ |
||
1577 | "gender": "m" |
||
1578 | }) |
||
1579 | }); |
||
1580 | cell.render(); |
||
1581 | expect(cell.$el.text()).toBe("Male"); |
||
1582 | }); |
||
1583 | |||
1584 | describe("when the model value has changed", function () { |
||
1585 | |||
1586 | var cell; |
||
1587 | var model; |
||
1588 | |||
1589 | var optionValues = [ |
||
1590 | ["Boy", 1], |
||
1591 | ["Girl", 2] |
||
1592 | ]; |
||
1593 | |||
1594 | beforeEach(function () { |
||
1595 | model = new Backbone.Model({ |
||
1596 | gender: 2 |
||
1597 | }); |
||
1598 | |||
1599 | cell = new(Backgrid.SelectCell.extend({ |
||
1600 | optionValues: optionValues |
||
1601 | }))({ |
||
1602 | column: { |
||
1603 | name: "gender", |
||
1604 | cell: "select" |
||
1605 | }, |
||
1606 | model: model |
||
1607 | }); |
||
1608 | }); |
||
1609 | |||
1610 | it("refreshes during display mode", function () { |
||
1611 | cell.render(); |
||
1612 | model.set("gender", 1); |
||
1613 | expect(cell.$el.text()).toBe("Boy"); |
||
1614 | }); |
||
1615 | |||
1616 | it("does not refresh during display mode if the change was silenced", function () { |
||
1617 | cell.render(); |
||
1618 | model.set("gender", 1, { |
||
1619 | silent: true |
||
1620 | }); |
||
1621 | expect(cell.$el.text()).toBe("Girl"); |
||
1622 | }); |
||
1623 | |||
1624 | it("does not refresh during edit mode", function () { |
||
1625 | cell.render(); |
||
1626 | cell.$el.click(); |
||
1627 | model.set("gender", 1); |
||
1628 | expect(cell.$el.find("option[selected]").val()).toBe("2"); |
||
1629 | expect(cell.$el.find("option[selected]").text()).toBe("Girl"); |
||
1630 | }); |
||
1631 | }); |
||
1632 | |||
1633 | }); |
||
1634 |