blob: fafec7d14f005f5260a149642fcffc1fadca6a9f [file] [log] [blame]
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08001/// <reference path="../../../../typings/index.d.ts" />
2
3import * as $ from 'jquery';
4import 'jasmine-jquery';
5import * as angular from 'angular';
6import 'angular-mocks';
7import {IXosNavigationRoute} from '../services/navigation';
8import {xosNav} from './nav';
9
10let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope;
11
12let baseRoutes: IXosNavigationRoute[] = [
13 {label: 'Home', state: 'xos'},
14 {label: 'Core', state: 'xos.core'}
15];
16
17const NavigationService = function(){
18 this.query = () => baseRoutes;
19};
20
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080021const AuthMock = {
Matteo Scandolo04964232017-01-07 12:53:46 -080022 logout: jasmine.createSpy('logout').and.returnValue({then: () => {
23 return;
24 }})
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080025};
26
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080027describe('Nav component', () => {
28 beforeEach(() => {
29 angular
30 .module('xosNav', ['app/core/nav/nav.html', 'ui.router'])
31 .component('xosNav', xosNav)
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080032 .service('NavigationService', NavigationService)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080033 .value('AuthService', AuthMock)
34 .value('StyleConfig', {});
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080035 angular.mock.module('xosNav');
36 });
37
38 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
39 scope = $rootScope;
40 compile = $compile;
41 element = $compile('<xos-nav></xos-nav>')($rootScope);
42 $rootScope.$digest();
43 isolatedScope = element.isolateScope();
44
45 // clear routes
46 isolatedScope.routes = [];
47 }));
48
49 it('should render a list of routes', () => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080050 const routes = $('.nav li:not(.nav-info)', element);
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080051 expect(routes.length).toBe(2);
52 });
53
54 it('should render child routes', () => {
55 baseRoutes = [
56 {label: 'Home', state: 'xos'},
57 {label: 'Core', state: 'xos.core', children: [
58 {label: 'Slices', state: 'xos.core.slices', parent: 'xos.core'}
59 ]}
60 ];
61 scope.$apply();
Matteo Scandolo266907e2016-12-20 13:41:42 -080062 const childRouteContainer = $('.nav-second li', element);
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080063 expect(childRouteContainer.length).toBe(1);
64 });
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080065
66 it('should call the logout method', () => {
67 // NOTE upgrade to test the ng-click binding
68 // const btn = $(element).find('.nav-info .btn-block');
69 // btn.click();
70 // scope.$digest();
71 isolatedScope.vm.logout();
72 expect(AuthMock.logout).toHaveBeenCalled();
73 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080074});