blob: 62e8d3c1d250a224109e777eecb2fc23f82759ee [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
7let service: IXosNavigationService;
8
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((
38 NavigationService: IXosNavigationService,
39 ) => {
40 service = NavigationService;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080041 defaultRoutes = [
42 {
43 label: 'Home',
44 state: 'xos.dashboard'
45 },
46 {
47 label: 'Core',
48 state: 'xos.core'
49 }
50 ].concat(mockRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080051 }));
52
53 it('should return navigation routes', () => {
54 expect(service.query()).toEqual(defaultRoutes);
55 });
56
57 it('should add a route', () => {
58 const testRoutes: IXosNavigationRoute[] = [
59 {label: 'TestState', state: 'xos.test'},
60 {label: 'TestUrl', url: 'test'}
61 ];
62 service.add(testRoutes[0]);
63 service.add(testRoutes[1]);
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080064 const serviceRoutes = service.query();
65 expect(serviceRoutes).toEqual(defaultRoutes.concat(testRoutes));
66 });
67
68 it('should add a child route', () => {
69 const testRoute: IXosNavigationRoute = {
70 label: 'TestState', state: 'xos.test', parent: 'xos.core'
71 };
72 service.add(testRoute);
73 defaultRoutes[1].children = [testRoute];
74 expect(service.query()).toEqual(defaultRoutes);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080075 });
76
77 it('should not add route that have both url and state', () => {
78 function wrapper() {
79 service.add({
80 label: 'Fail',
81 url: 'f',
82 state: 'f'
83 });
84 }
85 expect(wrapper).toThrowError('[XosNavigation] You can\'t provide both state and url');
86 });
87});