Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 19 | /// <reference path="../../../../typings/index.d.ts" /> |
| 20 | |
| 21 | import * as $ from 'jquery'; |
| 22 | import 'jasmine-jquery'; |
| 23 | import * as angular from 'angular'; |
| 24 | import 'angular-mocks'; |
| 25 | import {IXosNavigationRoute} from '../services/navigation'; |
| 26 | import {xosNav} from './nav'; |
| 27 | |
| 28 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope; |
| 29 | |
| 30 | let baseRoutes: IXosNavigationRoute[] = [ |
| 31 | {label: 'Home', state: 'xos'}, |
| 32 | {label: 'Core', state: 'xos.core'} |
| 33 | ]; |
| 34 | |
| 35 | const NavigationService = function(){ |
| 36 | this.query = () => baseRoutes; |
| 37 | }; |
| 38 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 39 | const AuthMock = { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 40 | logout: jasmine.createSpy('logout').and.returnValue({then: () => { |
| 41 | return; |
| 42 | }}) |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 45 | describe('Nav component', () => { |
| 46 | beforeEach(() => { |
| 47 | angular |
| 48 | .module('xosNav', ['app/core/nav/nav.html', 'ui.router']) |
| 49 | .component('xosNav', xosNav) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 50 | .service('XosNavigationService', NavigationService) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 51 | .value('AuthService', AuthMock) |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 52 | .value('StyleConfig', {}) |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 53 | .value('XosSidePanel', {}) |
| 54 | .value('XosComponentInjector', {}); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 55 | 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 Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 70 | const routes = $('.nav li:not(.nav-info)', element); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 71 | 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 Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 82 | const childRouteContainer = $('.nav-second li', element); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 83 | expect(childRouteContainer.length).toBe(1); |
| 84 | }); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 85 | |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 94 | }); |