Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -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 | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-ui-router'; |
| 22 | import {xosCore} from '../index'; |
| 23 | import {IXosNavigationService, IXosNavigationRoute} from './navigation'; |
| 24 | |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 25 | let service: IXosNavigationService, $log: ng.ILogService; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 26 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 27 | let defaultRoutes: IXosNavigationRoute[]; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 28 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 29 | const mockRoutes = [ |
| 30 | { |
| 31 | label: 'Slices', |
| 32 | state: 'xos.core.slices' |
| 33 | }, |
| 34 | { |
| 35 | label: 'Instances', |
| 36 | state: 'xos.core.instances' |
| 37 | }, |
| 38 | { |
| 39 | label: 'Nodes', |
| 40 | state: 'xos.core.nodes' |
| 41 | } |
| 42 | ]; |
| 43 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 44 | describe('The Navigation service', () => { |
| 45 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 46 | beforeEach(() => { |
| 47 | angular.module(xosCore) |
| 48 | .value('StyleConfig', { |
| 49 | routes: mockRoutes |
| 50 | }); |
| 51 | |
| 52 | angular.mock.module(xosCore); |
| 53 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 54 | |
| 55 | beforeEach(angular.mock.inject(( |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 56 | XosNavigationService: IXosNavigationService, |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 57 | _$log_: ng.ILogService |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 58 | ) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 59 | service = XosNavigationService; |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 60 | $log = _$log_; |
| 61 | spyOn($log, 'warn'); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 62 | defaultRoutes = [ |
| 63 | { |
| 64 | label: 'Home', |
| 65 | state: 'xos.dashboard' |
| 66 | }, |
| 67 | { |
| 68 | label: 'Core', |
| 69 | state: 'xos.core' |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 70 | }, |
| 71 | { |
| 72 | label: 'Service Graph', |
| 73 | state: 'xos.fine-grained-graph' |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 74 | } |
| 75 | ].concat(mockRoutes); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 76 | })); |
| 77 | |
| 78 | it('should return navigation routes', () => { |
| 79 | expect(service.query()).toEqual(defaultRoutes); |
| 80 | }); |
| 81 | |
| 82 | it('should add a route', () => { |
| 83 | const testRoutes: IXosNavigationRoute[] = [ |
| 84 | {label: 'TestState', state: 'xos.test'}, |
| 85 | {label: 'TestUrl', url: 'test'} |
| 86 | ]; |
| 87 | service.add(testRoutes[0]); |
| 88 | service.add(testRoutes[1]); |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 89 | expect($log.warn).not.toHaveBeenCalled(); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 90 | const serviceRoutes = service.query(); |
| 91 | expect(serviceRoutes).toEqual(defaultRoutes.concat(testRoutes)); |
| 92 | }); |
| 93 | |
| 94 | it('should add a child route', () => { |
| 95 | const testRoute: IXosNavigationRoute = { |
| 96 | label: 'TestState', state: 'xos.test', parent: 'xos.core' |
| 97 | }; |
| 98 | service.add(testRoute); |
| 99 | defaultRoutes[1].children = [testRoute]; |
| 100 | expect(service.query()).toEqual(defaultRoutes); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 101 | }); |
| 102 | |
| 103 | it('should not add route that have both url and state', () => { |
| 104 | function wrapper() { |
| 105 | service.add({ |
| 106 | label: 'Fail', |
| 107 | url: 'f', |
| 108 | state: 'f' |
| 109 | }); |
| 110 | } |
| 111 | expect(wrapper).toThrowError('[XosNavigation] You can\'t provide both state and url'); |
| 112 | }); |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 113 | |
| 114 | it('should not add route that already exist', () => { |
| 115 | const testRoute: IXosNavigationRoute = {label: 'TestState', state: 'xos.test'}; |
| 116 | service.add(testRoute); |
| 117 | service.add(testRoute); |
| 118 | expect($log.warn).toHaveBeenCalled(); |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 119 | expect($log.warn).toHaveBeenCalledWith(`[XosNavigation] Route with label: ${testRoute.label}, state: ${testRoute.state} and parent: ${testRoute.parent} already exists`); |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 120 | expect(service.query()).toEqual(defaultRoutes.concat([testRoute])); |
| 121 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 122 | }); |