blob: 50af9d966c099988aa3cc040be9dd9609018d8e4 [file] [log] [blame]
Matteo Scandoloa4a47112016-12-16 10:06:13 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-ui-router';
4import {xosCore} from '../index';
5import {IXosNavigationService, IXosNavigationRoute} from './navigation';
6
Matteo Scandoloa6487ce2017-02-06 16:42:21 -08007let service: IXosNavigationService, $log: ng.ILogService;
Matteo Scandoloa4a47112016-12-16 10:06:13 -08008
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08009let defaultRoutes: IXosNavigationRoute[];
Matteo Scandoloa4a47112016-12-16 10:06:13 -080010
Matteo Scandolo828d1e82017-01-17 14:49:38 -080011const mockRoutes = [
12 {
13 label: 'Slices',
14 state: 'xos.core.slices'
15 },
16 {
17 label: 'Instances',
18 state: 'xos.core.instances'
19 },
20 {
21 label: 'Nodes',
22 state: 'xos.core.nodes'
23 }
24];
25
Matteo Scandoloa4a47112016-12-16 10:06:13 -080026describe('The Navigation service', () => {
27
Matteo Scandolo828d1e82017-01-17 14:49:38 -080028 beforeEach(() => {
29 angular.module(xosCore)
30 .value('StyleConfig', {
31 routes: mockRoutes
32 });
33
34 angular.mock.module(xosCore);
35 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -080036
37 beforeEach(angular.mock.inject((
Matteo Scandolo1aee1982017-02-17 08:33:23 -080038 XosNavigationService: IXosNavigationService,
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080039 _$log_: ng.ILogService
Matteo Scandoloa4a47112016-12-16 10:06:13 -080040 ) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080041 service = XosNavigationService;
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080042 $log = _$log_;
43 spyOn($log, 'warn');
Matteo Scandolo828d1e82017-01-17 14:49:38 -080044 defaultRoutes = [
45 {
46 label: 'Home',
47 state: 'xos.dashboard'
48 },
49 {
50 label: 'Core',
51 state: 'xos.core'
Matteo Scandolo75171782017-03-08 14:17:01 -080052 },
53 {
54 label: 'Service Graph',
55 state: 'xos.fine-grained-graph'
Matteo Scandolo828d1e82017-01-17 14:49:38 -080056 }
57 ].concat(mockRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080058 }));
59
60 it('should return navigation routes', () => {
61 expect(service.query()).toEqual(defaultRoutes);
62 });
63
64 it('should add a route', () => {
65 const testRoutes: IXosNavigationRoute[] = [
66 {label: 'TestState', state: 'xos.test'},
67 {label: 'TestUrl', url: 'test'}
68 ];
69 service.add(testRoutes[0]);
70 service.add(testRoutes[1]);
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080071 expect($log.warn).not.toHaveBeenCalled();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080072 const serviceRoutes = service.query();
73 expect(serviceRoutes).toEqual(defaultRoutes.concat(testRoutes));
74 });
75
76 it('should add a child route', () => {
77 const testRoute: IXosNavigationRoute = {
78 label: 'TestState', state: 'xos.test', parent: 'xos.core'
79 };
80 service.add(testRoute);
81 defaultRoutes[1].children = [testRoute];
82 expect(service.query()).toEqual(defaultRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080083 });
84
85 it('should not add route that have both url and state', () => {
86 function wrapper() {
87 service.add({
88 label: 'Fail',
89 url: 'f',
90 state: 'f'
91 });
92 }
93 expect(wrapper).toThrowError('[XosNavigation] You can\'t provide both state and url');
94 });
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080095
96 it('should not add route that already exist', () => {
97 const testRoute: IXosNavigationRoute = {label: 'TestState', state: 'xos.test'};
98 service.add(testRoute);
99 service.add(testRoute);
100 expect($log.warn).toHaveBeenCalled();
101 expect($log.warn).toHaveBeenCalledWith(`[XosNavigation] Route with label: ${testRoute.label}, state: ${testRoute.state} and parent: ${testRoute.parent} already exist`);
102 expect(service.query()).toEqual(defaultRoutes.concat([testRoute]));
103 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800104});