ToolchainEditor.spec.jsx 28.5 KB
Newer Older
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
1
2
// @flow
import React from 'react';
3
import chai, { expect } from 'chai';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
4
5
6
import { mount } from 'enzyme';
import sinon from 'sinon';
import { spies } from '@test';
7
8
// sometimes we dont care about order of items in arrays when comparing objects deeply
import deepEqualInAnyOrder from 'deep-equal-in-any-order';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
9

10
import { getValidToolchainObj as getValidObj, getValidDatabaseObj, getValidAlgorithmObj } from '@helpers/beat';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
11
import { ToolchainEditor as C } from '.';
12
13
import * as Selectors from '@store/selectors';
import reducer from '@store/reducers';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
14
15

import testTcs from '@test/test_tcs.json';
16
17
import testDbs from '@test/test_dbs.json';
import testAlgs from '@test/test_algs.json';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
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
const getIrisTc = (color1: string = '#008000', color2: string = '#FF0000') => {
	return {
		'name': 'test/iris/1',
		'contents': {
			'description': '',
			'datasets': [
				{
					'name': 'training_data',
					'outputs': [
						'measurements',
						'species'
					]
				},
				{
					'name': 'testing_data',
					'outputs': [
						'measurements',
						'species'
					]
				}
			],
			'blocks': [
				{
					'inputs': [
						'measurements',
						'species'
					],
					'name': 'training_alg',
					'outputs': [
						'lda_machine'
					],
					'synchronized_channel': 'training_data'
				},
				{
					'inputs': [
						'lda_machine',
						'measurements'
					],
					'name': 'testing_alg',
					'outputs': [
						'scores'
					],
					'synchronized_channel': 'testing_data'
				}
			],
			'analyzers': [
				{
					'inputs': [
						'scores',
						'species'
					],
					'name': 'analyzer',
					'synchronized_channel': 'testing_data'
				}
			],
			'connections': [
				{
					'channel': 'testing_data',
					'from': 'testing_alg.scores',
					'to': 'analyzer.scores'
				},
				{
					'channel': 'training_data',
					'from': 'training_alg.lda_machine',
					'to': 'testing_alg.lda_machine'
				},
				{
					'channel': 'testing_data',
					'from': 'testing_data.measurements',
					'to': 'testing_alg.measurements'
				},
				{
					'channel': 'training_data',
					'from': 'training_data.measurements',
					'to': 'training_alg.measurements'
				},
				{
					'channel': 'training_data',
					'from': 'training_data.species',
					'to': 'training_alg.species'
				},
				{
					'channel': 'testing_data',
					'from': 'testing_data.species',
					'to': 'analyzer.species'
				}
			],
			'representation': {
				'blocks': {
					'analyzer': {
						'col': 46,
						'height': 3,
						'row': 4,
						'width': 10
					},
					'testing_alg': {
						'col': 32,
						'height': 3,
						'row': 3,
						'width': 10
					},
					'testing_data': {
						'col': 6,
						'height': 3,
						'row': 5,
						'width': 10
					},
					'training_alg': {
						'col': 19,
						'height': 3,
						'row': 0,
						'width': 10
					},
					'training_data': {
						'col': 6,
						'height': 3,
						'row': 0,
						'width': 10
					}
				},
				'channel_colors': {
					'testing_data': color2,
					'training_data': color1
				},
				'connections': {
					'testing_alg.scores/analyzer.scores': [],
					'testing_data.measurements/testing_alg.measurements': [],
					'testing_data.species/analyzer.species': [],
					'training_alg.lda_machine/testing_alg.lda_machine': [],
					'training_data.measurements/training_alg.measurements': [],
					'training_data.species/training_alg.species': []
				}
			}
		},
		'extraContents': {
			'groups': []
		}
	};
};

159
160
chai.use(deepEqualInAnyOrder);

161
describe.only('<ToolchainEditor />', function() {
162
163
164
	// these tests might take a long time, comparatively
	this.timeout(10000);

Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
165
166
167
168
169
170
171
172
	let wrapper;

	afterEach(() => {
		if(wrapper && wrapper.unmount)
			wrapper.unmount();
	});

	describe('accepts', () => {
173
174
175
176
177
178
179
180
181
182
183
184
		const tcs = testTcs.map(tc => getValidObj(tc));
		const dbs = testDbs.map(db => getValidDatabaseObj(db));
		const algs = testAlgs.map(alg => getValidAlgorithmObj(alg));

		const state = {
			...reducer({}, { type: '', payload: {}}),
			toolchain: tcs,
			database: dbs,
			algorithm: algs,
		};

		const sets = Selectors.flattenedDatabases(state);
185
		const protocols = Selectors.databaseProtocols(state);
186
187
		const normalAlgorithms = Selectors.normalBlocks(state);
		const analyzerAlgorithms = Selectors.analyzerBlocks(state);
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
188
189
190

		tcs.forEach(function(tc){
			const saveFunc = () => {};
191
			const updateFunc = () => {};
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
192
193
			it(`${ tc.name }`, () => {
				wrapper = mount(
194
195
196
197
198
199
200
201
202
203
204
					<C
						data={tc}
						sets={sets}
						protocols={protocols}
						toolchains={state.toolchain}
						databases={state.database}
						normalAlgorithms={normalAlgorithms}
						analyzerAlgorithms={analyzerAlgorithms}
						saveFunc={saveFunc}
						updateFunc={updateFunc}
					/>
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
205
206
				);

207
208
				expect(wrapper).to.have.props(
					['data', 'sets', 'protocols', 'toolchains', 'databases', 'normalAlgorithms', 'analyzerAlgorithms', 'saveFunc', 'updateFunc']
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
209
				).deep.equal(
210
					[tc, sets, protocols, tcs, state.database, normalAlgorithms, analyzerAlgorithms, saveFunc, updateFunc]
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
211
212
213
214
215
				);
			});
		});
	});

216
	describe('creates', () => {
217
218
219
220
221
222
223
224
225
226
227
228
229
		const tcs = [];
		const dbs = testDbs.map(db => getValidDatabaseObj(db));
		const algs = testAlgs.map(alg => getValidAlgorithmObj(alg));

		const state = {
			...reducer({}, { type: '', payload: {}}),
			toolchain: tcs,
			database: dbs,
			algorithm: algs,
		};

		const sets = Selectors.flattenedDatabases(state);
		const normalAlgorithms = Selectors.normalBlocks(state);
230
		const protocols = Selectors.databaseProtocols(state);
231
232
233
234
235
236
237
238
239
240
241
		const analyzerAlgorithms = Selectors.analyzerBlocks(state);

		it(`test/iris/1`, () => {
			const saveFunc = sinon.spy();
			const _updateFunc = (obj) => {
				wrapper.setProps && wrapper.setProps({ data: obj });
			};
			const updateFunc = sinon.spy(_updateFunc);
			const tcName = 'test/iris/1';
			const tc = getValidObj({name: tcName, contents: {}});
			wrapper = mount(
242
243
244
245
246
247
248
249
250
251
252
				<C
					data={tc}
					sets={sets}
					protocols={protocols}
					toolchains={state.toolchain}
					databases={state.database}
					normalAlgorithms={normalAlgorithms}
					analyzerAlgorithms={analyzerAlgorithms}
					saveFunc={saveFunc}
					updateFunc={updateFunc}
				/>
253
			);
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
254

255
256
			expect(wrapper).to.have.props(
				['data', 'sets', 'protocols', 'toolchains', 'databases', 'normalAlgorithms', 'analyzerAlgorithms', 'saveFunc', 'updateFunc']
257
258
			);

259
			expect(wrapper.props().data).to.have.property('name', tcName);
260
261
262
263
264

			// add lots of blocks
			const _selectBlocks = (bNames: string[]) => { return; };
			const selectBlocks = sinon.spy(_selectBlocks);

265
266
267

			/* add blocks via contextmenu handler */

268
			// pretend to right click at a spot by calling the event handler
269
270
			// dataset 1 training_data @ 6,0
			wrapper.instance().handleSvgContextMenu({}, { clicked: 'addDataset', x: 6, y: 0, selectBlocks });
271
272
			wrapper.update();
			expect(updateFunc.callCount).to.equal(1);
273
			expect(wrapper.props().data.contents.datasets.length).to.equal(1);
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641

			// dataset 2 @ 6,5
			wrapper.instance().handleSvgContextMenu({}, { clicked: 'addDataset', x: 6, y: 5, selectBlocks });
			wrapper.update();
			expect(updateFunc.callCount).to.equal(2);
			expect(wrapper.props().data.contents.datasets.length).to.equal(2);

			// block 1 (training) @ 19,0
			wrapper.instance().handleSvgContextMenu({}, { clicked: 'addBlock', x: 19, y: 0, selectBlocks });
			wrapper.update();
			expect(updateFunc.callCount).to.equal(3);
			expect(wrapper.props().data.contents.blocks.length).to.equal(1);

			// block 2 (testing) @ 32,3
			wrapper.instance().handleSvgContextMenu({}, { clicked: 'addBlock', x: 32, y: 3, selectBlocks });
			wrapper.update();
			expect(updateFunc.callCount).to.equal(4);
			expect(wrapper.props().data.contents.blocks.length).to.equal(2);

			// block 3 (analyzer) @ 46,4
			wrapper.instance().handleSvgContextMenu({}, { clicked: 'addAnalyzer', x: 46, y: 4, selectBlocks });
			wrapper.update();
			expect(updateFunc.callCount).to.equal(5);
			expect(wrapper.props().data.contents.analyzers.length).to.equal(1);

			/* open edit modal for each & edit blocks via input onChange stuff */

			// training_data
			wrapper.find('rect#block_dataset').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			expect(wrapper.find('.modal').find('CacheInput').props().value).to.equal('dataset');
			wrapper.find('.modal').find('CacheInput').prop('onChange')( { target: { value: 'training_data' }});
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').simulate('click');
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').simulate('click');
			wrapper.update();
			wrapper.find('.modal CacheInput[value="output"]').prop('onChange')( { target: { value: 'measurements' }});
			wrapper.update();
			wrapper.find('.modal CacheInput[value="output0"]').prop('onChange')( { target: { value: 'species' }});
			wrapper.update();
			wrapper.find('button.close').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(false);

			expect(wrapper.props().data.contents.datasets[0]).to.deep.equal({
				'name': 'training_data',
				'outputs': [
					'measurements',
					'species'
				]
			});

			// testing_data
			wrapper.find('rect#block_dataset0').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			expect(wrapper.find('.modal').find('CacheInput').props().value).to.equal('dataset0');
			wrapper.find('.modal').find('CacheInput').prop('onChange')( { target: { value: 'testing_data' }});
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').simulate('click');
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').simulate('click');
			wrapper.update();
			wrapper.find('.modal CacheInput[value="output"]').prop('onChange')( { target: { value: 'measurements' }});
			wrapper.update();
			wrapper.find('.modal CacheInput[value="output0"]').prop('onChange')( { target: { value: 'species' }});
			wrapper.update();
			wrapper.find('button.close').simulate('click');
			wrapper.update();

			expect(wrapper.props().data.contents.datasets[1]).to.deep.equal({
				'name': 'testing_data',
				'outputs': [
					'measurements',
					'species'
				]
			});

			// training_alg
			wrapper.find('rect#block_block').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			expect(wrapper.find('.modal').find('CacheInput').props().value).to.equal('block');
			wrapper.find('.modal').find('CacheInput').prop('onChange')( { target: { value: 'training_alg' }});
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').at(0).simulate('click');
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').at(0).simulate('click');
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').at(1).simulate('click');
			wrapper.update();
			wrapper.find('.modal CacheInput[value="input"]').prop('onChange')( { target: { value: 'measurements' }});
			wrapper.update();
			wrapper.find('.modal CacheInput[value="input0"]').prop('onChange')( { target: { value: 'species' }});
			wrapper.update();
			wrapper.find('.modal CacheInput[value="output"]').prop('onChange')( { target: { value: 'lda_machine' }});
			wrapper.update();
			wrapper.find('button.close').simulate('click');
			wrapper.update();

			expect(wrapper.props().data.contents.blocks[0]).to.deep.equal({
				'name': 'training_alg',
				'inputs': [
					'measurements',
					'species'
				],
				'outputs': [
					'lda_machine'
				],
				'synchronized_channel': 'training_data',
			});

			// testing_alg
			wrapper.find('rect#block_block0').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			expect(wrapper.find('.modal').find('CacheInput').props().value).to.equal('block0');
			wrapper.find('.modal').find('CacheInput').prop('onChange')( { target: { value: 'testing_alg' }});
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').at(0).simulate('click');
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').at(0).simulate('click');
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').at(1).simulate('click');
			wrapper.update();
			wrapper.find('.modal CacheInput[value="input"]').prop('onChange')( { target: { value: 'measurements' }});
			wrapper.update();
			wrapper.find('.modal CacheInput[value="input0"]').prop('onChange')( { target: { value: 'lda_machine' }});
			wrapper.update();
			wrapper.find('.modal CacheInput[value="output"]').prop('onChange')( { target: { value: 'scores' }});
			wrapper.update();
			wrapper.find('button.close').simulate('click');
			wrapper.update();

			expect(wrapper.props().data.contents.blocks[1]).to.deep.equal({
				'name': 'testing_alg',
				'inputs': [
					'measurements',
					'lda_machine'
				],
				'outputs': [
					'scores'
				],
				'synchronized_channel': 'training_data',
			});

			// analyzer
			wrapper.find('rect#block_analyzer').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			expect(wrapper.find('.modal').find('CacheInput').props().value).to.equal('analyzer');
			wrapper.find('.modal button.btn-secondary').simulate('click');
			wrapper.update();
			wrapper.find('.modal button.btn-secondary').simulate('click');
			wrapper.update();
			wrapper.find('.modal CacheInput[value="input"]').prop('onChange')( { target: { value: 'scores' }});
			wrapper.update();
			wrapper.find('.modal CacheInput[value="input0"]').prop('onChange')( { target: { value: 'species' }});
			wrapper.update();
			wrapper.find('button.close').simulate('click');
			wrapper.update();

			expect(wrapper.props().data.contents.analyzers[0]).to.deep.equal({
				'name': 'analyzer',
				'inputs': [
					'scores',
					'species'
				],
				'synchronized_channel': 'training_data',
			});

			/* connect stuff via createConnections() */
			// channel: training_data
			wrapper.instance().createConnections([{ from: 'training_data.measurements', to: 'training_alg.measurements', channel: 'training_data' }]);
			// channel: training_data
			wrapper.instance().createConnections([{ from: 'training_data.species', to: 'training_alg.species', channel: 'training_data' }]);
			// channel: training_data
			wrapper.instance().createConnections([{ from: 'training_alg.lda_machine', to: 'testing_alg.lda_machine', channel: 'training_data' }]);
			// channel: testing_data
			wrapper.instance().createConnections([{ from: 'testing_data.measurements', to: 'testing_alg.measurements', channel: 'testing_data' }]);
			// channel: training_data
			wrapper.instance().createConnections([{ from: 'testing_alg.scores', to: 'analyzer.scores', channel: 'training_data' }]);
			// channel: testing_data
			wrapper.instance().createConnections([{ from: 'testing_data.species', to: 'analyzer.species', channel: 'testing_data' }]);

			/* fix channels */
			// testing_alg
			wrapper.find('rect#block_testing_alg').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			wrapper.find('.modal select').prop('onChange')( { target: { value: 'testing_data' }});
			wrapper.update();
			wrapper.find('button.close').simulate('click');
			wrapper.update();

			expect(wrapper.props().data.contents.blocks[1]).to.deep.equal({
				'name': 'testing_alg',
				'inputs': [
					'measurements',
					'lda_machine'
				],
				'outputs': [
					'scores'
				],
				'synchronized_channel': 'testing_data',
			});

			// analyzer
			wrapper.find('rect#block_analyzer').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			wrapper.find('.modal select').prop('onChange')( { target: { value: 'testing_data' }});
			wrapper.update();
			wrapper.find('button.close').simulate('click');
			wrapper.update();

			expect(wrapper.props().data.contents.analyzers[0]).to.deep.equal({
				'name': 'analyzer',
				'inputs': [
					'scores',
					'species'
				],
				'synchronized_channel': 'testing_data',
			});

			/* theres alot of expect statements here for a reason:
			 * each statement checks a sub-part of the toolchain
			 * its redundant when the test passes but helpful when somethings off,
			 * you can narrow the part of the tc that isn't right alot quicker
			 */
			const data = wrapper.props().data;
			const ch1 = data.contents.representation.channel_colors['training_data'];
			const ch2 = data.contents.representation.channel_colors['testing_data'];
			expect(data.contents.datasets).to.deep.equalInAnyOrder( [
				{
					'name': 'training_data',
					'outputs': [
						'measurements',
						'species'
					]
				},
				{
					'name': 'testing_data',
					'outputs': [
						'measurements',
						'species'
					]
				}
			]);

			expect(data.contents.blocks).to.deep.equalInAnyOrder([
				{
					'inputs': [
						'measurements',
						'species'
					],
					'name': 'training_alg',
					'outputs': [
						'lda_machine'
					],
					'synchronized_channel': 'training_data'
				},
				{
					'inputs': [
						'lda_machine',
						'measurements'
					],
					'name': 'testing_alg',
					'outputs': [
						'scores'
					],
					'synchronized_channel': 'testing_data'
				}
			]);

			expect(data.contents.analyzers).to.deep.equalInAnyOrder([
				{
					'inputs': [
						'scores',
						'species'
					],
					'name': 'analyzer',
					'synchronized_channel': 'testing_data'
				}
			]);

			expect(data.contents.connections).to.deep.equalInAnyOrder([
				{
					'channel': 'testing_data',
					'from': 'testing_alg.scores',
					'to': 'analyzer.scores'
				},
				{
					'channel': 'training_data',
					'from': 'training_alg.lda_machine',
					'to': 'testing_alg.lda_machine'
				},
				{
					'channel': 'testing_data',
					'from': 'testing_data.measurements',
					'to': 'testing_alg.measurements'
				},
				{
					'channel': 'training_data',
					'from': 'training_data.measurements',
					'to': 'training_alg.measurements'
				},
				{
					'channel': 'training_data',
					'from': 'training_data.species',
					'to': 'training_alg.species'
				},
				{
					'channel': 'testing_data',
					'from': 'testing_data.species',
					'to': 'analyzer.species'
				}
			]);

			expect(data.contents.representation).to.deep.equalInAnyOrder({
				'blocks': {
					'analyzer': {
						'col': 46,
						'height': 3,
						'row': 4,
						'width': 10
					},
					'testing_alg': {
						'col': 32,
						'height': 3,
						'row': 3,
						'width': 10
					},
					'testing_data': {
						'col': 6,
						'height': 3,
						'row': 5,
						'width': 10
					},
					'training_alg': {
						'col': 19,
						'height': 3,
						'row': 0,
						'width': 10
					},
					'training_data': {
						'col': 6,
						'height': 3,
						'row': 0,
						'width': 10
					}
				},
				'channel_colors': {
					'testing_data': ch2,
					'training_data': ch1
				},
				'connections': {
					'testing_alg.scores/analyzer.scores': [],
					'testing_data.measurements/testing_alg.measurements': [],
					'testing_data.species/analyzer.species': [],
					'training_alg.lda_machine/testing_alg.lda_machine': [],
					'training_data.measurements/training_alg.measurements': [],
					'training_data.species/training_alg.species': []
				}
			});

642
			expect(data).to.deep.equalInAnyOrder(getIrisTc(ch1, ch2));
643
		});
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
644
	});
645

646
	describe('Insert Object Modal', () => {
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
		const timeout = (ms) => {
			return new Promise(resolve => setTimeout(resolve, ms));
		}

		const tcs = testTcs.map(tc => getValidObj(tc));
		const dbs = testDbs.map(db => getValidDatabaseObj(db));
		const algs = testAlgs.map(alg => getValidAlgorithmObj(alg));

		const state = {
			...reducer({}, { type: '', payload: {}}),
			toolchain: tcs,
			database: dbs,
			algorithm: algs,
		};

		const sets = Selectors.flattenedDatabases(state);
		const normalAlgorithms = Selectors.normalBlocks(state);
		const protocols = Selectors.databaseProtocols(state);
		const analyzerAlgorithms = Selectors.analyzerBlocks(state);

		it(`inserts test/iris_advanced/1`, async () => {
			const saveFunc = sinon.spy();
			const _updateFunc = (obj) => {
				wrapper.setProps && wrapper.setProps({ data: obj });
			};
			const updateFunc = sinon.spy(_updateFunc);
			const tcName = 'test/iom/1';
			const tc = getValidObj({name: tcName, contents: {}});
			wrapper = mount(
				<C
					data={tc}
					sets={sets}
					protocols={protocols}
					toolchains={state.toolchain}
					databases={state.database}
					normalAlgorithms={normalAlgorithms}
					analyzerAlgorithms={analyzerAlgorithms}
					saveFunc={saveFunc}
					updateFunc={updateFunc}
				/>
			);

			expect(wrapper).to.have.props(
				['data', 'sets', 'protocols', 'toolchains', 'databases', 'normalAlgorithms', 'analyzerAlgorithms', 'saveFunc', 'updateFunc']
			);

			expect(wrapper.props().data).to.have.property('name', tcName);

			const _selectBlocks = (bNames: string[]) => { return; };
			const selectBlocks = sinon.spy(_selectBlocks);

			// context-menu insert obj modal
			// insert at x:6 because thats where the original toolchain has its first block on the x-axis
			wrapper.instance().handleSvgContextMenu({}, { clicked: 'addObject', x: 6, y: 0, selectBlocks });
			wrapper.update();
			expect(wrapper.find('InsertObjectModal').props().active).to.equal(true);
			expect(updateFunc.callCount).to.equal(0);

			// search for "test/iris" in toolchain list
			wrapper.find('InsertObjectModal Input[placeholder="Search..."]').prop('onChange')( { target: { value: 'test/iris' }});
			wrapper.update();

			// wait for fuse process to search & return result list
			await timeout(1000);
			wrapper.update();

			// click on the iris_advanced one, which should be first
			const res = wrapper.find('ListGroup.searchResults ListGroupItem');
			expect(res).to.have.lengthOf(4);
			expect(res.at(0).html()).to.include('test/iris_advanced/1');
			res.at(0).simulate('click');
			wrapper.update();

			const data = wrapper.props().data;
			const ch1 = data.contents.representation.channel_colors['training_data'];
			const ch2 = data.contents.representation.channel_colors['testing_data'];

			// make sure the toolchain was inserted
			// basically make sure the toolchain is the same as test/iris_advanced/1 but w different name
			expect(data.contents).to.deep.equalInAnyOrder({
				'analyzers': [
					{
						'inputs': [
							'scores',
							'species'
						],
						'name': 'analyzer',
						'synchronized_channel': 'testing_data'
					}
				],
				'blocks': [
					{
						'inputs': [
							'measurements',
							'species'
						],
						'name': 'training_alg',
						'outputs': [
							'lda_machine'
						],
						'synchronized_channel': 'training_data'
					},
					{
						'inputs': [
							'lda_machine',
							'measurements'
						],
						'name': 'testing_alg',
						'outputs': [
							'scores'
						],
						'synchronized_channel': 'testing_data'
					},
					{
						'inputs': [
							'measurements'
						],
						'name': 'pre_training',
						'outputs': [
							'measurements'
						],
						'synchronized_channel': 'training_data'
					},
					{
						'inputs': [
							'measurements'
						],
						'name': 'pre_testing',
						'outputs': [
							'measurements'
						],
						'synchronized_channel': 'testing_data'
					}
				],
				'connections': [
					{
						'channel': 'testing_data',
						'from': 'testing_alg.scores',
						'to': 'analyzer.scores'
					},
					{
						'channel': 'training_data',
						'from': 'training_alg.lda_machine',
						'to': 'testing_alg.lda_machine'
					},
					{
						'channel': 'testing_data',
						'from': 'testing_data.species',
						'to': 'analyzer.species'
					},
					{
						'channel': 'training_data',
						'from': 'training_data.species',
						'to': 'training_alg.species'
					},
					{
						'channel': 'training_data',
						'from': 'training_data.measurements',
						'to': 'pre_training.measurements'
					},
					{
						'channel': 'training_data',
						'from': 'pre_training.measurements',
						'to': 'training_alg.measurements'
					},
					{
						'channel': 'testing_data',
						'from': 'testing_data.measurements',
						'to': 'pre_testing.measurements'
					},
					{
						'channel': 'testing_data',
						'from': 'pre_testing.measurements',
						'to': 'testing_alg.measurements'
					}
				],
				'datasets': [
					{
						'name': 'training_data',
						'outputs': [
							'measurements',
							'species'
						]
					},
					{
						'name': 'testing_data',
						'outputs': [
							'measurements',
							'species'
						]
					}
				],
				'description': '',
				'representation': {
					'blocks': {
						'analyzer': {
							'col': 59,
							'height': 3,
							'row': 6,
							'width': 10
						},
						'pre_testing': {
							'col': 19,
							'height': 3,
							'row': 5,
							'width': 10
						},
						'pre_training': {
							'col': 19,
							'height': 3,
							'row': 0,
							'width': 10
						},
						'testing_alg': {
							'col': 46,
							'height': 3,
							'row': 5,
							'width': 10
						},
						'testing_data': {
							'col': 6,
							'height': 3,
							'row': 6,
							'width': 10
						},
						'training_alg': {
							'col': 32,
							'height': 3,
							'row': 1,
							'width': 10
						},
						'training_data': {
							'col': 6,
							'height': 3,
							'row': 1,
							'width': 10
						}
					},
					'channel_colors': {
						'testing_data': ch2,
						'training_data': ch1
					},
					'connections': {
						'pre_testing.measurements/testing_alg.measurements': [],
						'pre_training.measurements/training_alg.measurements': [],
						'testing_alg.scores/analyzer.scores': [],
						'testing_data.measurements/pre_testing.measurements': [],
						'testing_data.species/analyzer.species': [],
						'training_alg.lda_machine/testing_alg.lda_machine': [],
						'training_data.measurements/pre_training.measurements': [],
						'training_data.species/training_alg.species': []
					}
				}
			});
		});
	});
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989

	describe('Regression Tests', () => {
		const timeout = (ms) => {
			return new Promise(resolve => setTimeout(resolve, ms));
		};

		const tcs = testTcs.map(tc => getValidObj(tc));
		const dbs = testDbs.map(db => getValidDatabaseObj(db));
		const algs = testAlgs.map(alg => getValidAlgorithmObj(alg));

		const state = {
			...reducer({}, { type: '', payload: {}}),
			toolchain: tcs,
			database: dbs,
			algorithm: algs,
		};

		const sets = Selectors.flattenedDatabases(state);
		const normalAlgorithms = Selectors.normalBlocks(state);
		const protocols = Selectors.databaseProtocols(state);
		const analyzerAlgorithms = Selectors.analyzerBlocks(state);

		const saveFunc = sinon.spy();
		const _updateFunc = (obj) => {
			wrapper.setProps && wrapper.setProps({ data: obj });
		};
		const updateFunc = sinon.spy(_updateFunc);
		const tcName = 'test/iris/1';

		it('Properly changes connection names when renaming an input', () => {
			const tc = getValidObj(getIrisTc());
			wrapper = mount(
				<C
					data={tc}
					sets={sets}
					protocols={protocols}
					toolchains={state.toolchain}
					databases={state.database}
					normalAlgorithms={normalAlgorithms}
					analyzerAlgorithms={analyzerAlgorithms}
					saveFunc={saveFunc}
					updateFunc={updateFunc}
				/>
			);

			// change testing_alg.lda_machine to testing_alg.lda
			wrapper.find('rect#block_testing_alg').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			wrapper.find('.modal CacheInput[value="lda_machine"]').prop('onChange')( { target: { value: 'lda' }});
			wrapper.update();
			expect(wrapper.find('.modal CacheInput[value="lda"]').props().value).to.equal('lda');

			const data = wrapper.props().data;
			expect(data.contents.representation.connections).to.have.property('training_alg.lda_machine/testing_alg.lda');
			expect(data.contents.representation.connections).to.not.have.property('training_alg.lda_machine/testing_alg.lda_machine');
		});

		it('Properly changes connection names when renaming an output', () => {
			const tc = getValidObj(getIrisTc());
			wrapper = mount(
				<C
					data={tc}
					sets={sets}
					protocols={protocols}
					toolchains={state.toolchain}
					databases={state.database}
					normalAlgorithms={normalAlgorithms}
					analyzerAlgorithms={analyzerAlgorithms}
					saveFunc={saveFunc}
					updateFunc={updateFunc}
				/>
			);

			// change testing_alg.lda_machine to testing_alg.lda
			wrapper.find('rect#block_training_alg').simulate('click');
			wrapper.update();
			expect(wrapper.find('ToolchainModal').props().active).to.equal(true);
			wrapper.find('.modal CacheInput[value="lda_machine"]').prop('onChange')( { target: { value: 'lda' }});
			wrapper.update();
			expect(wrapper.find('.modal CacheInput[value="lda"]').props().value).to.equal('lda');

			const data = wrapper.props().data;
			expect(data.contents.representation.connections).to.have.property('training_alg.lda/testing_alg.lda_machine');
			expect(data.contents.representation.connections).to.not.have.property('training_alg.lda_machine/testing_alg.lda_machine');
		});
	});
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
990
});