Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts" /> |
| 2 | |
| 3 | import * as $ from 'jquery'; |
| 4 | import 'jasmine-jquery'; |
| 5 | import * as angular from 'angular'; |
| 6 | import 'angular-mocks'; |
| 7 | import {IXosNavigationRoute} from '../services/navigation'; |
| 8 | import {xosNav} from './nav'; |
| 9 | |
| 10 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope; |
| 11 | |
| 12 | let baseRoutes: IXosNavigationRoute[] = [ |
| 13 | {label: 'Home', state: 'xos'}, |
| 14 | {label: 'Core', state: 'xos.core'} |
| 15 | ]; |
| 16 | |
| 17 | const NavigationService = function(){ |
| 18 | this.query = () => baseRoutes; |
| 19 | }; |
| 20 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 21 | const AuthMock = { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 22 | logout: jasmine.createSpy('logout').and.returnValue({then: () => { |
| 23 | return; |
| 24 | }}) |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 25 | }; |
| 26 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 27 | describe('Nav component', () => { |
| 28 | beforeEach(() => { |
| 29 | angular |
| 30 | .module('xosNav', ['app/core/nav/nav.html', 'ui.router']) |
| 31 | .component('xosNav', xosNav) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 32 | .service('XosNavigationService', NavigationService) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 33 | .value('AuthService', AuthMock) |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 34 | .value('StyleConfig', {}) |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 35 | .value('XosSidePanel', {}) |
| 36 | .value('XosComponentInjector', {}); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 37 | angular.mock.module('xosNav'); |
| 38 | }); |
| 39 | |
| 40 | beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => { |
| 41 | scope = $rootScope; |
| 42 | compile = $compile; |
| 43 | element = $compile('<xos-nav></xos-nav>')($rootScope); |
| 44 | $rootScope.$digest(); |
| 45 | isolatedScope = element.isolateScope(); |
| 46 | |
| 47 | // clear routes |
| 48 | isolatedScope.routes = []; |
| 49 | })); |
| 50 | |
| 51 | it('should render a list of routes', () => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 52 | const routes = $('.nav li:not(.nav-info)', element); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 53 | expect(routes.length).toBe(2); |
| 54 | }); |
| 55 | |
| 56 | it('should render child routes', () => { |
| 57 | baseRoutes = [ |
| 58 | {label: 'Home', state: 'xos'}, |
| 59 | {label: 'Core', state: 'xos.core', children: [ |
| 60 | {label: 'Slices', state: 'xos.core.slices', parent: 'xos.core'} |
| 61 | ]} |
| 62 | ]; |
| 63 | scope.$apply(); |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 64 | const childRouteContainer = $('.nav-second li', element); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 65 | expect(childRouteContainer.length).toBe(1); |
| 66 | }); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 67 | |
| 68 | it('should call the logout method', () => { |
| 69 | // NOTE upgrade to test the ng-click binding |
| 70 | // const btn = $(element).find('.nav-info .btn-block'); |
| 71 | // btn.click(); |
| 72 | // scope.$digest(); |
| 73 | isolatedScope.vm.logout(); |
| 74 | expect(AuthMock.logout).toHaveBeenCalled(); |
| 75 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 76 | }); |