blob: b9cb84cf97560534acd124b086241e8f912b1bac [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandoloa4a47112016-12-16 10:06:13 -080019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-ui-router';
22import {xosCore} from '../index';
23import {IXosNavigationService, IXosNavigationRoute} from './navigation';
24
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080025let service: IXosNavigationService, $log: ng.ILogService;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080026
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080027let defaultRoutes: IXosNavigationRoute[];
Matteo Scandoloa4a47112016-12-16 10:06:13 -080028
Matteo Scandolo828d1e82017-01-17 14:49:38 -080029const 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 Scandoloa4a47112016-12-16 10:06:13 -080044describe('The Navigation service', () => {
45
Matteo Scandolo828d1e82017-01-17 14:49:38 -080046 beforeEach(() => {
47 angular.module(xosCore)
48 .value('StyleConfig', {
49 routes: mockRoutes
50 });
51
52 angular.mock.module(xosCore);
53 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -080054
55 beforeEach(angular.mock.inject((
Matteo Scandolo1aee1982017-02-17 08:33:23 -080056 XosNavigationService: IXosNavigationService,
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080057 _$log_: ng.ILogService
Matteo Scandoloa4a47112016-12-16 10:06:13 -080058 ) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080059 service = XosNavigationService;
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080060 $log = _$log_;
61 spyOn($log, 'warn');
Matteo Scandolo828d1e82017-01-17 14:49:38 -080062 defaultRoutes = [
63 {
64 label: 'Home',
65 state: 'xos.dashboard'
66 },
67 {
68 label: 'Core',
69 state: 'xos.core'
Matteo Scandolo75171782017-03-08 14:17:01 -080070 },
71 {
72 label: 'Service Graph',
73 state: 'xos.fine-grained-graph'
Matteo Scandolo828d1e82017-01-17 14:49:38 -080074 }
75 ].concat(mockRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080076 }));
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 Scandoloa6487ce2017-02-06 16:42:21 -080089 expect($log.warn).not.toHaveBeenCalled();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080090 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 Scandoloa4a47112016-12-16 10:06:13 -0800101 });
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 Scandoloa6487ce2017-02-06 16:42:21 -0800113
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 Chu71b185a2017-09-07 17:50:58 -0700119 expect($log.warn).toHaveBeenCalledWith(`[XosNavigation] Route with label: ${testRoute.label}, state: ${testRoute.state} and parent: ${testRoute.parent} already exists`);
Matteo Scandoloa6487ce2017-02-06 16:42:21 -0800120 expect(service.query()).toEqual(defaultRoutes.concat([testRoute]));
121 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800122});