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 = { |
| 22 | logout: jasmine.createSpy('logout') |
| 23 | }; |
| 24 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 25 | describe('Nav component', () => { |
| 26 | beforeEach(() => { |
| 27 | angular |
| 28 | .module('xosNav', ['app/core/nav/nav.html', 'ui.router']) |
| 29 | .component('xosNav', xosNav) |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 30 | .service('NavigationService', NavigationService) |
| 31 | .value('AuthService', AuthMock); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 32 | angular.mock.module('xosNav'); |
| 33 | }); |
| 34 | |
| 35 | beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => { |
| 36 | scope = $rootScope; |
| 37 | compile = $compile; |
| 38 | element = $compile('<xos-nav></xos-nav>')($rootScope); |
| 39 | $rootScope.$digest(); |
| 40 | isolatedScope = element.isolateScope(); |
| 41 | |
| 42 | // clear routes |
| 43 | isolatedScope.routes = []; |
| 44 | })); |
| 45 | |
| 46 | it('should render a list of routes', () => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 47 | const routes = $('.nav li:not(.nav-info)', element); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 48 | expect(routes.length).toBe(2); |
| 49 | }); |
| 50 | |
| 51 | it('should render child routes', () => { |
| 52 | baseRoutes = [ |
| 53 | {label: 'Home', state: 'xos'}, |
| 54 | {label: 'Core', state: 'xos.core', children: [ |
| 55 | {label: 'Slices', state: 'xos.core.slices', parent: 'xos.core'} |
| 56 | ]} |
| 57 | ]; |
| 58 | scope.$apply(); |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 59 | const childRouteContainer = $('.nav-second li', element); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 60 | expect(childRouteContainer.length).toBe(1); |
| 61 | }); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 62 | |
| 63 | it('should call the logout method', () => { |
| 64 | // NOTE upgrade to test the ng-click binding |
| 65 | // const btn = $(element).find('.nav-info .btn-block'); |
| 66 | // btn.click(); |
| 67 | // scope.$digest(); |
| 68 | isolatedScope.vm.logout(); |
| 69 | expect(AuthMock.logout).toHaveBeenCalled(); |
| 70 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 71 | }); |