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 | |
| 7 | let service: IXosNavigationService; |
| 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(( |
| 38 | NavigationService: IXosNavigationService, |
| 39 | ) => { |
| 40 | service = NavigationService; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 41 | defaultRoutes = [ |
| 42 | { |
| 43 | label: 'Home', |
| 44 | state: 'xos.dashboard' |
| 45 | }, |
| 46 | { |
| 47 | label: 'Core', |
| 48 | state: 'xos.core' |
| 49 | } |
| 50 | ].concat(mockRoutes); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 51 | })); |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 64 | 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 Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 75 | }); |
| 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 | }); |