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' |
| 70 | } |
| 71 | ].concat(mockRoutes); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 72 | })); |
| 73 | |
| 74 | it('should return navigation routes', () => { |
| 75 | expect(service.query()).toEqual(defaultRoutes); |
| 76 | }); |
| 77 | |
| 78 | it('should add a route', () => { |
| 79 | const testRoutes: IXosNavigationRoute[] = [ |
| 80 | {label: 'TestState', state: 'xos.test'}, |
| 81 | {label: 'TestUrl', url: 'test'} |
| 82 | ]; |
| 83 | service.add(testRoutes[0]); |
| 84 | service.add(testRoutes[1]); |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 85 | expect($log.warn).not.toHaveBeenCalled(); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 86 | const serviceRoutes = service.query(); |
| 87 | expect(serviceRoutes).toEqual(defaultRoutes.concat(testRoutes)); |
| 88 | }); |
| 89 | |
| 90 | it('should add a child route', () => { |
| 91 | const testRoute: IXosNavigationRoute = { |
| 92 | label: 'TestState', state: 'xos.test', parent: 'xos.core' |
| 93 | }; |
| 94 | service.add(testRoute); |
| 95 | defaultRoutes[1].children = [testRoute]; |
| 96 | expect(service.query()).toEqual(defaultRoutes); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 97 | }); |
| 98 | |
| 99 | it('should not add route that have both url and state', () => { |
| 100 | function wrapper() { |
| 101 | service.add({ |
| 102 | label: 'Fail', |
| 103 | url: 'f', |
| 104 | state: 'f' |
| 105 | }); |
| 106 | } |
| 107 | expect(wrapper).toThrowError('[XosNavigation] You can\'t provide both state and url'); |
| 108 | }); |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 109 | |
| 110 | it('should not add route that already exist', () => { |
| 111 | const testRoute: IXosNavigationRoute = {label: 'TestState', state: 'xos.test'}; |
| 112 | service.add(testRoute); |
| 113 | service.add(testRoute); |
| 114 | expect($log.warn).toHaveBeenCalled(); |
Max Chu | 6c64fdc | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 115 | 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] | 116 | expect(service.query()).toEqual(defaultRoutes.concat([testRoute])); |
| 117 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 118 | }); |