blob: 14fb9c4e6b3f40d8424837c3504e905d8277a855 [file] [log] [blame]
Matteo Scandolo8cf33a32017-11-14 15:52:29 -08001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-ui-router';
22import {IXosNodePositioner, XosNodePositioner} from './node-positioner.service';
23
24let service: IXosNodePositioner;
25
26let scope: ng.IRootScopeService;
27
28let constraints: string = '';
29
30let mockResource = {
31 query: () => {
32 return;
33 }
34};
35
36const mockModelRest = {
37 getResource: jasmine.createSpy('ModelRest.getResource')
38 .and.returnValue(mockResource)
39};
40
41describe('The XosNodePositioner service', () => {
42
43 beforeEach(() => {
44 angular.module('XosNodePositioner', [])
45 .service('XosNodePositioner', XosNodePositioner)
46 .value('ModelRest', mockModelRest);
47
48 angular.mock.module('XosNodePositioner');
49 });
50
51 beforeEach(angular.mock.inject((
52 XosNodePositioner: IXosNodePositioner,
53 $rootScope: ng.IRootScopeService,
54 _$q_: ng.IQService) => {
55
56 service = XosNodePositioner;
57 scope = $rootScope;
58
59 spyOn(mockResource, 'query').and.callFake(() => {
60 const d = _$q_.defer();
61 d.resolve([{constraints}]);
62 return {$promise: d.promise};
63 });
64 }));
65
66 it('should position the nodes on the svg', (done) => {
67 const svg = {width: 300, height: 100};
68 const nodes = [
69 {data: {name: 'a'}},
70 {data: {name: 'b'}}
71 ];
72 constraints = '["a", "b"]';
73 service.positionNodes(svg, nodes)
74 .then((positioned) => {
75 expect(positioned[0].x).toBe(100);
76 expect(positioned[0].y).toBe(50);
77 expect(positioned[1].x).toBe(200);
78 expect(positioned[1].y).toBe(50);
79 done();
80 });
81
82 scope.$apply();
83 });
84
85 it('should position the nodes on the svg in vertical bundles', (done) => {
86 const svg = {width: 300, height: 90};
87 const nodes = [
88 {data: {name: 'a'}},
89 {data: {name: 'b'}},
90 {data: {name: 'c'}}
91 ];
92 constraints = '["a", ["b", "c"]]';
93 service.positionNodes(svg, nodes)
94 .then((positioned) => {
95 expect(positioned[0].x).toBe(100);
96 expect(positioned[0].y).toBe(45);
97 expect(positioned[1].x).toBe(200);
98 expect(positioned[1].y).toBe(30);
99 expect(positioned[2].x).toBe(200);
100 expect(positioned[2].y).toBe(60);
101 done();
102 });
103
104 scope.$apply();
105 });
106
107 it('should accept null as constraint to leave an empty space', (done) => {
108 const svg = {width: 300, height: 90};
109 const nodes = [
110 {data: {name: 'a'}},
111 {data: {name: 'b'}},
112 {data: {name: 'c'}}
113 ];
114 constraints = '[[null, "a"], ["b", "c"]]';
115 service.positionNodes(svg, nodes)
116 .then((positioned) => {
117 expect(positioned[0].x).toBe(100);
118 expect(positioned[0].y).toBe(60);
119 expect(positioned[1].x).toBe(200);
120 expect(positioned[1].y).toBe(30);
121 expect(positioned[2].x).toBe(200);
122 expect(positioned[2].y).toBe(60);
123 done();
124 });
125
126 scope.$apply();
127 });
128});