Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
| 22 | describe('The Logic Topology Helper Service', () => { |
| 23 | |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 24 | var Service, Config; |
| 25 | |
| 26 | var x0, x1, x2, x3, x4; |
| 27 | |
| 28 | var svgWidth = 900; |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 29 | |
Matteo Scandolo | 930e4fd | 2016-03-07 16:41:25 -0800 | [diff] [blame] | 30 | beforeEach(module('xos.diagnostic')); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 31 | |
| 32 | // inject the rackHelper service |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 33 | beforeEach(inject(function (_LogicTopologyHelper_, _serviceTopologyConfig_) { |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 34 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 35 | Service = _LogicTopologyHelper_; |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 36 | Config = _serviceTopologyConfig_; |
| 37 | |
| 38 | // result |
| 39 | let totalElWidth = Config.elWidths.reduce((el, val) => val + el, 0); |
| 40 | let remainingSpace = svgWidth - totalElWidth - (Config.widthMargin * 2); |
| 41 | let step = remainingSpace / (Config.elWidths.length - 1); |
| 42 | x0 = Config.widthMargin; |
| 43 | x1 = x0 + Config.elWidths[0] + step; |
| 44 | x2 = x1 + Config.elWidths[1] + step; |
| 45 | x3 = x2 + Config.elWidths[2] + step; |
| 46 | x4 = x3 + Config.elWidths[3] + step; |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 47 | })); |
| 48 | |
| 49 | var customMatchers = { |
| 50 | toBeSimilar: () => { |
| 51 | |
| 52 | const tolerance = 0.1; |
| 53 | |
| 54 | return { |
| 55 | compare: (actual, expected) => { |
| 56 | return { |
| 57 | pass: (Math.abs(actual - expected) < tolerance), |
| 58 | message: `Expected ${actual} to be ${expected}` |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | beforeEach(function() { |
| 66 | jasmine.addMatchers(customMatchers); |
| 67 | }); |
| 68 | |
| 69 | it('should calculate horizontal position for each element', () => { |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 70 | let [el0x, el1x, el2x, el3x, el4x] = Service.computeElementPosition(svgWidth); |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 71 | expect(el0x).toBeSimilar(svgWidth - (x0 + (Config.elWidths[0] / 2))); |
| 72 | expect(el1x).toBeSimilar(svgWidth - (x1 + (Config.elWidths[1] / 2))); |
| 73 | expect(el2x).toBeSimilar(svgWidth - (x2 + (Config.elWidths[2] / 2))); |
| 74 | expect(el3x).toBeSimilar(svgWidth - (x3 + (Config.elWidths[3] / 2))); |
| 75 | expect(el4x).toBeSimilar(svgWidth - (x4 + (Config.elWidths[4] / 2))); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 76 | }); |
| 77 | }); |
| 78 | |
| 79 | })(); |