/
test_view_controller.js
574 lines (491 loc) · 18.7 KB
/
test_view_controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
requirejs([
'jquery',
'underscore',
'model',
'view',
'abcviewcontroller',
'viewcontroller',
'slickgrid'
], function($, _, model, DecompositionView, abc, viewcontroller,
SlickGrid) {
var EmperorViewControllerABC = abc.EmperorViewControllerABC;
var EmperorViewController = viewcontroller.EmperorViewController;
var EmperorAttributeABC = viewcontroller.EmperorAttributeABC;
var DecompositionModel = model.DecompositionModel;
$(document).ready(function() {
module('EmperorViewControllerABC', {
});
/**
*
* Test that the constructor for EmperorViewControllerABC.
*
*/
test('Constructor tests', function(assert) {
var container = $('<div id="does-not-exist" style="height:11px; ' +
'width:12px"></div>');
var controller = new EmperorViewControllerABC(container, 'foo', 'bar');
equal(controller.title, 'foo', 'Check the title is correctly set');
equal(controller.description, 'bar',
'Check the description is correctly set');
equal(controller.$container.id, container.id, 'Check the id of the ' +
'parent is correct');
equal(controller.active, false, 'Check the active property');
equal(controller.identifier.slice(0, 7), 'EMPtab-',
'Check the identifier property');
parseFloat(controller.identifier.slice(7));
equal(controller.enabled, true, 'Check the enabled property');
// check all the elements were successfully created
assert.ok(controller.$canvas.length);
assert.ok(controller.$header.length);
assert.ok(controller.$body.length);
assert.ok($.contains(controller.$canvas[0], controller.$header[0]));
assert.ok($.contains(controller.$canvas[0], controller.$body[0]));
equal(controller.$body.width(), 12);
equal(controller.$body.height(), 11);
});
/**
*
* Test the enabled method
*
*/
test('Test the enabled method works', function() {
var container = $('<div id="does-not-exist"></div>');
var controller = new EmperorViewControllerABC(container, 'foo', 'bar');
equal(controller.enabled, true);
controller.setEnabled(false);
equal(controller.enabled, false);
throws(function() {
controller.setEnabled('shenanigans');
}, Error, 'setEnabled can only take a boolean');
});
/**
*
* Test the enabled method
*
*/
test('Test the setActive method works', function() {
var container = $('<div id="does-not-exist"></div>');
var controller = new EmperorViewControllerABC(container, 'foo', 'bar');
equal(controller.active, false);
controller.setActive(true);
equal(controller.active, true);
throws(function() {
controller.setActive('shenanigans');
}, Error, 'setActive can only take a boolean');
});
/**
*
* Test the resize method.
*
*/
test('Test the resize method', function() {
var container = $('<div id="does-not-exist"></div>');
var controller = new EmperorViewControllerABC(container, 'foo', 'bar');
// header of size 0
controller.resize(20, 10);
equal(controller.$header.height(), 0);
equal(controller.$header.width(), 10); // because of padding
equal(controller.$body.height(), 10);
equal(controller.$body.width(), 10); // because of padding
controller.$header.height(11);
controller.resize(30, 30);
equal(controller.$header.height(), 11);
equal(controller.$header.width(), 20); //because of padding
equal(controller.$body.height(), 19);
equal(controller.$body.width(), 20); //because of padding
});
/**
*
* Test the resize, toJSON and fromJSON methods raise the appropriate
* errors.
*
*/
test('Test resize, toJSON and fromJSON methods', function() {
var container = $('<div id="does-not-exist"></div>');
var controller = new EmperorViewControllerABC(container, 'foo', 'bar');
throws(function() {
controller.fromJSON('{foo:11}');
}, Error, 'Cannot call this abstract method');
throws(function() {
controller.toJSON();
}, Error, 'Cannot call this abstract method');
});
module('EmperorViewController', {
setup: function() {
this.sharedDecompositionViewDict = {};
// setup function
name = 'pcoa';
ids = ['PC.636', 'PC.635'];
coords = [
[-0.276542, -0.144964, 0.066647, -0.067711, 0.176070, 0.072969,
-0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061, -0.247485, -0.115211,
-0.112864, 0.064794]];
pct_var = [26.6887048633, 16.2563704022, 13.7754129161, 11.217215823,
10.024774995, 8.22835130237, 7.55971173665, 6.24945796136];
md_headers = ['SampleID', 'LinkerPrimerSequence', 'Treatment', 'DOB'];
metadata = [['PC.636', 'YATGCTGCCTCCCGTAGGAGT', 'Control', '20070314'],
['PC.635', 'YATGCTGCCTCCCGTAGGAGT', 'Fast', '20071112']];
decomp = new DecompositionModel(name, ids, coords, pct_var, md_headers,
metadata);
var dv = new DecompositionView(decomp);
this.sharedDecompositionViewDict.pcoa = dv;
name = 'biplot';
ids = ['tax_1', 'tax_2'];
coords = [
[-1, -0.144964, 0.066647, -0.067711, 0.176070, 0.072969,
-0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061, -0.247485, -0.115211,
-0.112864, 0.064794]];
pct_var = [26.6887048633, 16.2563704022, 13.7754129161, 11.217215823,
10.024774995, 8.22835130237, 7.55971173665, 6.24945796136];
md_headers = ['SampleID', 'Gram'];
metadata = [['tax_1', '1'],
['tax_2', '0']];
decomp = new DecompositionModel(name, ids, coords, pct_var, md_headers,
metadata);
dv = new DecompositionView(decomp);
this.sharedDecompositionViewDict.biplot = dv;
this.decomp = decomp;
},
teardown: function() {
this.sharedDecompositionViewDict = undefined;
this.decomp = undefined;
}
});
/**
*
* Test the constructor for EmperorViewController.
*
*/
test('Constructor tests', function(assert) {
var container = $('<div id="does-not-exist"></div>');
// verify the subclassing was set properly
assert.ok(EmperorViewController.prototype instanceof
EmperorViewControllerABC);
var attr = new EmperorViewController(container, 'foo', 'bar',
this.sharedDecompositionViewDict);
deepEqual(_.keys(attr.decompViewDict), ['pcoa', 'biplot']);
});
/**
*
* Tests to make sure the exceptions are being raised as expected
*
*/
test('Constructor test exceptions', function(assert) {
var dv = new DecompositionView(this.decomp);
throws(function() {
new EmperorViewController(container, 'foo', 'bar',
{1: 1, 2: 2}, {});
}, Error, 'The decomposition view dictionary ' +
'can only have decomposition views');
throws(function() {
new EmperorViewController(container, 'foo', 'bar',
{}, {});
}, Error, 'The decomposition view dictionary cannot be empty');
});
/**
*
* Test the active decomposition view can be correctly retrieved
*
*/
test('Test getView', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorViewController(container, 'foo', 'bar',
{'scatter': dv});
deepEqual(attr.getView(), dv);
});
module('EmperorAttributeABC', {
setup: function() {
this.sharedDecompositionViewDict = {};
var $slickid = $('<div id="fooligans"></div>');
$slickid.appendTo(document.body);
// setup function
name = 'pcoa';
ids = ['PC.636', 'PC.635'];
coords = [
[-0.276542, -0.144964, 0.066647, -0.067711, 0.176070, 0.072969,
-0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061, -0.247485, -0.115211,
-0.112864, 0.064794]];
pct_var = [26.6887048633, 16.2563704022, 13.7754129161, 11.217215823,
10.024774995, 8.22835130237, 7.55971173665, 6.24945796136];
md_headers = ['SampleID', 'LinkerPrimerSequence', 'Treatment', 'DOB'];
metadata = [['PC.636', 'YATGCTGCCTCCCGTAGGAGT', 'Control', '20070314'],
['PC.635', 'YATGCTGCCTCCCGTAGGAGT', 'Fast', '20071112']];
decomp = new DecompositionModel(name, ids, coords, pct_var, md_headers,
metadata);
var dv = new DecompositionView(decomp);
this.sharedDecompositionViewDict.pcoa = dv;
name = 'biplot';
ids = ['tax_1', 'tax_2'];
coords = [
[-1, -0.144964, 0.066647, -0.067711, 0.176070, 0.072969,
-0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061, -0.247485, -0.115211,
-0.112864, 0.064794]];
pct_var = [26.6887048633, 16.2563704022, 13.7754129161, 11.217215823,
10.024774995, 8.22835130237, 7.55971173665, 6.24945796136];
md_headers = ['SampleID', 'Gram'];
metadata = [['tax_1', '1'],
['tax_2', '0']];
decomp = new DecompositionModel(name, ids, coords, pct_var, md_headers,
metadata);
dv = new DecompositionView(decomp);
this.sharedDecompositionViewDict.biplot = dv;
// Slickgrid
var columns = [
{id: 'pc1', name: 'pc1', field: 'pc1'},
{id: 'pc2', name: 'pc2', field: 'pc2'},
{id: 'pc3', name: 'pc3', field: 'pc3'}
];
this.options = {
enableCellNavigation: true,
enableColumnReorder: false
};
var data = [];
data.push({'pc1': 1, 'pc2': 1, 'pc3': 1});
data.push({'pc1': 1, 'pc2': 1, 'pc3': 2});
grid = new Slick.Grid('#fooligans', data, columns, this.options);
this.decomp = decomp;
},
teardown: function() {
this.sharedDecompositionViewDict = undefined;
$('#fooligans').remove();
this.decomp = undefined;
}
});
/**
*
* Test the constructor for EmperorViewControllerABC.
*
*/
test('Constructor tests', function(assert) {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
// verify the subclassing was set properly
assert.ok(EmperorAttributeABC.prototype instanceof
EmperorViewController);
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
this.sharedDecompositionViewDict, {});
var met = {'biplot': ['Gram', 'SampleID'],
'pcoa': ['DOB', 'LinkerPrimerSequence', 'SampleID',
'Treatment']};
deepEqual(attr._metadata, met);
});
/**
*
* Test to see if the grid is being built correctly
*
*/
asyncTest('Constructor test buildGrid', function(assert) {
var options = {};
options.slickGridColumn = {id: 'title', name: 'spam', field: 'test',
sortable: false, maxWidth: 10, minWidth: 10};
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
this.sharedDecompositionViewDict,
options);
$(function() {
var testColumn = attr.bodyGrid.getColumns()[0];
equal(testColumn.name, 'spam');
equal(testColumn.field, 'test');
start(); // qunit
});
});
/**
*
* Test to see if the grid is being built correctly
*
*/
asyncTest('Test resize', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist" style="height:20px; ' +
'width:21px"></div>');
// verify the subclassing was set properly
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
this.sharedDecompositionViewDict, {});
$(function() {
attr.resize(20, 30);
equal(attr.$body.width(), 10); // because of padding
equal(attr.$body.height(), 30 - attr.$header.height());
equal(attr.$header.width(), 10); // because of padding
start(); // qunit
});
});
test('Test decompositionName method', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, {});
equal(attr.decompositionName(), 'scatter');
});
test('Test getView method', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, {});
deepEqual(attr.getView(), dv);
});
/**
*
* Test get metadata field
*
*/
test('Test getMetadataField', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, {});
equal(attr.getMetadataField(), 'Gram');
});
/**
*
* Test set metadata field
*
*/
test('Test setMetadataField', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, {});
attr.setMetadataField('SampleID');
equal(attr.getMetadataField(), 'SampleID');
attr.setMetadataField(null);
equal(attr.getMetadataField(), null);
});
/**
*
* Test set metadata field
*
*/
test('Test setMetadataField exceptions', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, {});
throws(function() {
attr.setMetadataField('cheese');
}, /cheese/, 'Raise error that contains the word cheese');
});
/**
*
* Test get/set slick grid dataset.
*
*/
asyncTest('Test setSlickGridDataset', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv, 'biplot': dv}, {});
$(function() {
attr.setSlickGridDataset([{'pc1': 1, 'pc2': 2, 'pc3': 3},
{'pc1': 1, 'pc2': 1, 'pc3': 2}]);
deepEqual(attr.getSlickGridDataset(), [{'pc1': 1, 'pc2': 2, 'pc3': 3},
{'pc1': 1, 'pc2': 1, 'pc3': 2}]);
start(); // qunit
});
});
/**
*
* Test refresh metadata.
*
*/
asyncTest('Test refreshMetadata', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var shared = {'scatter': this.sharedDecompositionViewDict.pcoa};
var scope = this;
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
shared, {});
$(function() {
// modify the decomposition view dictionary
shared.biplot = scope.sharedDecompositionViewDict.biplot;
deepEqual(attr._metadata, {'scatter': ['DOB', 'LinkerPrimerSequence',
'SampleID', 'Treatment']});
attr.refreshMetadata();
deepEqual(attr._metadata, {'scatter': ['DOB', 'LinkerPrimerSequence',
'SampleID', 'Treatment'],
'biplot': ['Gram', 'SampleID']});
start(); // qunit
});
});
/**
*
* Test setEnabled (false)
*
*/
asyncTest('Test setEnabled (false)', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, {});
$(function() {
// disable
attr.setEnabled(false);
equal(attr.enabled, false);
equal(attr.$select.is(':disabled'), true);
equal(attr.bodyGrid.getOptions().editable, false);
// enable
attr.setEnabled(true);
equal(attr.enabled, true);
equal(attr.$select.is(':disabled'), false);
equal(attr.bodyGrid.getOptions().editable, true);
start(); // qunit
});
});
/**
*
* Test setEnabled (true)
*
*/
asyncTest('Test setEnabled (true)', function() {
var dv = new DecompositionView(this.decomp);
var container = $('<div id="does-not-exist"></div>');
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, {});
$(function() {
// Controllers should be enabled
equal(attr.enabled, true);
equal(attr.$select.is(':disabled'), false);
equal(attr.bodyGrid.getOptions().editable, true);
// and they should remain the same after "enabling" them again
attr.setEnabled(true);
equal(attr.enabled, true);
equal(attr.$select.is(':disabled'), false);
equal(attr.bodyGrid.getOptions().editable, true);
start(); // qunit
});
});
/**
*
* Test large dataset.
*
*/
asyncTest('Test large dataset', function() {
var coords = [], metadata = [];
for (var i = 0; i < 1001; i++) {
coords.push([Math.random(), Math.random(), Math.random(),
Math.random()]);
metadata.push([i, 'b ' + Math.random(), 'c ' + Math.random()]);
}
var d = new DecompositionModel('pcoa', _.range(1001), coords,
[45, 35, 15, 5],
['SampleID', 'foo', 'bar'], metadata);
var dv = new DecompositionView(d);
var container = $('<div id="does-not-exist"></div>');
// create a dummy category selection callback
var options = {'categorySelectionCallback': function() {}};
var attr = new EmperorAttributeABC(container, 'foo', 'bar',
{'scatter': dv}, options);
$(function() {
// Controllers should be enabled
equal(attr.enabled, false);
equal(attr.$select.val(), null);
equal(attr.$select.is(':disabled'), false);
start(); // qunit
});
});
});
});