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 | |
| 11 | describe('The Navigation service', () => { |
| 12 | |
| 13 | beforeEach(angular.mock.module(xosCore)); |
| 14 | |
| 15 | beforeEach(angular.mock.inject(( |
| 16 | NavigationService: IXosNavigationService, |
| 17 | ) => { |
| 18 | service = NavigationService; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 19 | defaultRoutes = angular.copy(service.query()); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 20 | })); |
| 21 | |
| 22 | it('should return navigation routes', () => { |
| 23 | expect(service.query()).toEqual(defaultRoutes); |
| 24 | }); |
| 25 | |
| 26 | it('should add a route', () => { |
| 27 | const testRoutes: IXosNavigationRoute[] = [ |
| 28 | {label: 'TestState', state: 'xos.test'}, |
| 29 | {label: 'TestUrl', url: 'test'} |
| 30 | ]; |
| 31 | service.add(testRoutes[0]); |
| 32 | service.add(testRoutes[1]); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 33 | const serviceRoutes = service.query(); |
| 34 | expect(serviceRoutes).toEqual(defaultRoutes.concat(testRoutes)); |
| 35 | }); |
| 36 | |
| 37 | it('should add a child route', () => { |
| 38 | const testRoute: IXosNavigationRoute = { |
| 39 | label: 'TestState', state: 'xos.test', parent: 'xos.core' |
| 40 | }; |
| 41 | service.add(testRoute); |
| 42 | defaultRoutes[1].children = [testRoute]; |
| 43 | expect(service.query()).toEqual(defaultRoutes); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 44 | }); |
| 45 | |
| 46 | it('should not add route that have both url and state', () => { |
| 47 | function wrapper() { |
| 48 | service.add({ |
| 49 | label: 'Fail', |
| 50 | url: 'f', |
| 51 | state: 'f' |
| 52 | }); |
| 53 | } |
| 54 | expect(wrapper).toThrowError('[XosNavigation] You can\'t provide both state and url'); |
| 55 | }); |
| 56 | }); |