blob: 407e69e518929703061a5a80f03acd4a518580e7 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080019/// <reference path="../../../../typings/index.d.ts" />
20
21import * as $ from 'jquery';
22import 'jasmine-jquery';
23import * as angular from 'angular';
24import 'angular-mocks';
25import {IXosNavigationRoute} from '../services/navigation';
26import {xosNav} from './nav';
27
28let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope;
29
30let baseRoutes: IXosNavigationRoute[] = [
31 {label: 'Home', state: 'xos'},
32 {label: 'Core', state: 'xos.core'}
33];
34
35const NavigationService = function(){
36 this.query = () => baseRoutes;
37};
38
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080039const AuthMock = {
Matteo Scandolo04964232017-01-07 12:53:46 -080040 logout: jasmine.createSpy('logout').and.returnValue({then: () => {
41 return;
42 }})
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080043};
44
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080045describe('Nav component', () => {
46 beforeEach(() => {
47 angular
48 .module('xosNav', ['app/core/nav/nav.html', 'ui.router'])
49 .component('xosNav', xosNav)
Matteo Scandolo1aee1982017-02-17 08:33:23 -080050 .service('XosNavigationService', NavigationService)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080051 .value('AuthService', AuthMock)
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080052 .value('StyleConfig', {})
Matteo Scandolo4222a432017-01-23 12:18:40 -080053 .value('XosSidePanel', {})
54 .value('XosComponentInjector', {});
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080055 angular.mock.module('xosNav');
56 });
57
58 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
59 scope = $rootScope;
60 compile = $compile;
61 element = $compile('<xos-nav></xos-nav>')($rootScope);
62 $rootScope.$digest();
63 isolatedScope = element.isolateScope();
64
65 // clear routes
66 isolatedScope.routes = [];
67 }));
68
69 it('should render a list of routes', () => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080070 const routes = $('.nav li:not(.nav-info)', element);
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080071 expect(routes.length).toBe(2);
72 });
73
74 it('should render child routes', () => {
75 baseRoutes = [
76 {label: 'Home', state: 'xos'},
77 {label: 'Core', state: 'xos.core', children: [
78 {label: 'Slices', state: 'xos.core.slices', parent: 'xos.core'}
79 ]}
80 ];
81 scope.$apply();
Matteo Scandolo266907e2016-12-20 13:41:42 -080082 const childRouteContainer = $('.nav-second li', element);
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080083 expect(childRouteContainer.length).toBe(1);
84 });
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080085
86 it('should call the logout method', () => {
87 // NOTE upgrade to test the ng-click binding
88 // const btn = $(element).find('.nav-info .btn-block');
89 // btn.click();
90 // scope.$digest();
91 isolatedScope.vm.logout();
92 expect(AuthMock.logout).toHaveBeenCalled();
93 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080094});