blob: bdde37f86c843f84200edf1f192fad95931df3ab [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'
70 }
71 ].concat(mockRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080072 }));
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 Scandoloa6487ce2017-02-06 16:42:21 -080085 expect($log.warn).not.toHaveBeenCalled();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080086 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 Scandoloa4a47112016-12-16 10:06:13 -080097 });
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 Scandoloa6487ce2017-02-06 16:42:21 -0800109
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 Chu6c64fdc2017-09-07 17:50:58 -0700115 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 -0800116 expect(service.query()).toEqual(defaultRoutes.concat([testRoute]));
117 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800118});