blob: bdcd5c55ebf97be2c49006e1402452a5fc64e194 [file] [log] [blame]
Sean Condon0c577f62018-11-18 22:40:05 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the 'License');
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an 'AS IS' BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import {ForceDirectedGraph, Options} from './force-directed-graph';
17import {Node} from './node';
18import {Link} from './link';
Sean Condon50855cf2018-12-23 15:37:42 +000019import {LogService} from 'gui2-fw-lib';
20import {TestBed} from '@angular/core/testing';
Sean Condon0c577f62018-11-18 22:40:05 +000021
22export class TestNode extends Node {
23 constructor(id: string) {
24 super(id);
25 }
26}
27
28export class TestLink extends Link {
29 constructor(source: Node, target: Node) {
30 super(source, target);
31 }
32}
33
34/**
35 * ONOS GUI -- ForceDirectedGraph - Unit Tests
36 */
37describe('ForceDirectedGraph', () => {
Sean Condon50855cf2018-12-23 15:37:42 +000038 let logServiceSpy: jasmine.SpyObj<LogService>;
Sean Condon0c577f62018-11-18 22:40:05 +000039 let fdg: ForceDirectedGraph;
40 const options: Options = {width: 1000, height: 1000};
41
42 beforeEach(() => {
Sean Condon50855cf2018-12-23 15:37:42 +000043 const logSpy = jasmine.createSpyObj('LogService', ['info', 'debug', 'warn', 'error']);
Sean Condon0c577f62018-11-18 22:40:05 +000044 const nodes: Node[] = [];
45 const links: Link[] = [];
Sean Condon50855cf2018-12-23 15:37:42 +000046 fdg = new ForceDirectedGraph(options, logSpy);
Sean Condon0c577f62018-11-18 22:40:05 +000047
48 for (let i = 0; i < 10; i++) {
49 const newNode: TestNode = new TestNode('id' + i);
50 nodes.push(newNode);
51 }
52 for (let j = 1; j < 10; j++) {
53 const newLink = new TestLink(nodes[0], nodes[j]);
54 links.push(newLink);
55 }
56 fdg.nodes = nodes;
57 fdg.links = links;
58 fdg.initSimulation(options);
59 fdg.initNodes();
Sean Condon50855cf2018-12-23 15:37:42 +000060 logServiceSpy = TestBed.get(LogService);
Sean Condon0c577f62018-11-18 22:40:05 +000061 });
62
63 afterEach(() => {
64 fdg.stopSimulation();
65 fdg.nodes = [];
66 fdg.links = [];
67 fdg.initSimulation(options);
68 });
69
70 it('should be created', () => {
71 expect(fdg).toBeTruthy();
72 });
73
74 it('should have simulation', () => {
75 expect(fdg.simulation).toBeTruthy();
76 });
77
78 it('should have 10 nodes', () => {
79 expect(fdg.nodes.length).toEqual(10);
80 });
81
82 it('should have 10 links', () => {
83 expect(fdg.links.length).toEqual(9);
84 });
85
86 // TODO fix these up to listen for tick
87 // it('nodes should not be at zero', () => {
88 // expect(nodes[0].x).toBeGreaterThan(0);
89 // });
90 // it('ticker should emit', () => {
91 // let tickMe = jasmine.createSpy("tickMe() spy");
92 // fdg.ticker.subscribe((simulation) => tickMe());
93 // expect(tickMe).toHaveBeenCalled();
94 // });
95
96 // it('init links chould be called ', () => {
97 // spyOn(fdg, 'initLinks');
98 // // expect(fdg).toBeTruthy();
99 // fdg.initSimulation(options);
100 // expect(fdg.initLinks).toHaveBeenCalled();
101 // });
102
103 it ('throws error on no options', () => {
104 expect(fdg.initSimulation).toThrowError('missing options when initializing simulation');
105 });
106
107
108
109});