blob: 82a235eaa6310dbecf806247d1630acd95d1dd38 [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'
52 }
53 ].concat(mockRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080054 }));
55
56 it('should return navigation routes', () => {
57 expect(service.query()).toEqual(defaultRoutes);
58 });
59
60 it('should add a route', () => {
61 const testRoutes: IXosNavigationRoute[] = [
62 {label: 'TestState', state: 'xos.test'},
63 {label: 'TestUrl', url: 'test'}
64 ];
65 service.add(testRoutes[0]);
66 service.add(testRoutes[1]);
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080067 expect($log.warn).not.toHaveBeenCalled();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080068 const serviceRoutes = service.query();
69 expect(serviceRoutes).toEqual(defaultRoutes.concat(testRoutes));
70 });
71
72 it('should add a child route', () => {
73 const testRoute: IXosNavigationRoute = {
74 label: 'TestState', state: 'xos.test', parent: 'xos.core'
75 };
76 service.add(testRoute);
77 defaultRoutes[1].children = [testRoute];
78 expect(service.query()).toEqual(defaultRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080079 });
80
81 it('should not add route that have both url and state', () => {
82 function wrapper() {
83 service.add({
84 label: 'Fail',
85 url: 'f',
86 state: 'f'
87 });
88 }
89 expect(wrapper).toThrowError('[XosNavigation] You can\'t provide both state and url');
90 });
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080091
92 it('should not add route that already exist', () => {
93 const testRoute: IXosNavigationRoute = {label: 'TestState', state: 'xos.test'};
94 service.add(testRoute);
95 service.add(testRoute);
96 expect($log.warn).toHaveBeenCalled();
97 expect($log.warn).toHaveBeenCalledWith(`[XosNavigation] Route with label: ${testRoute.label}, state: ${testRoute.state} and parent: ${testRoute.parent} already exist`);
98 expect(service.query()).toEqual(defaultRoutes.concat([testRoute]));
99 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800100});