Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-ui-router'; |
| 4 | import {xosCore} from '../index'; |
| 5 | import {IXosNavigationService, IXosNavigationRoute} from './navigation'; |
| 6 | |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 7 | let service: IXosNavigationService, $log: ng.ILogService; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 8 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 9 | let defaultRoutes: IXosNavigationRoute[]; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 10 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 11 | const 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 Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 26 | describe('The Navigation service', () => { |
| 27 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 28 | beforeEach(() => { |
| 29 | angular.module(xosCore) |
| 30 | .value('StyleConfig', { |
| 31 | routes: mockRoutes |
| 32 | }); |
| 33 | |
| 34 | angular.mock.module(xosCore); |
| 35 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 36 | |
| 37 | beforeEach(angular.mock.inject(( |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 38 | XosNavigationService: IXosNavigationService, |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 39 | _$log_: ng.ILogService |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 40 | ) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 41 | service = XosNavigationService; |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 42 | $log = _$log_; |
| 43 | spyOn($log, 'warn'); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 44 | defaultRoutes = [ |
| 45 | { |
| 46 | label: 'Home', |
| 47 | state: 'xos.dashboard' |
| 48 | }, |
| 49 | { |
| 50 | label: 'Core', |
| 51 | state: 'xos.core' |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 52 | }, |
| 53 | { |
| 54 | label: 'Service Graph', |
| 55 | state: 'xos.fine-grained-graph' |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 56 | } |
| 57 | ].concat(mockRoutes); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 58 | })); |
| 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 Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 71 | expect($log.warn).not.toHaveBeenCalled(); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 72 | 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 Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 83 | }); |
| 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 Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 95 | |
| 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 Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 104 | }); |