blob: a4a7d905a1c814c03f4e68b5843722f4e69bb804 [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 Scandolof6acdbe2016-12-13 10:29:37 -080019/// <reference path="../../../../typings/index.d.ts" />
20
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080021import * as $ from 'jquery';
22import 'jasmine-jquery';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080023import * as angular from 'angular';
24import 'angular-mocks';
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080025import {xosHeader, INotification} from './header';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080026import {Subject} from 'rxjs';
Matteo Scandolo71d74a42017-10-11 15:38:54 -070027import {IXosDebugService} from '../debug/debug.service';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080028
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080029let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope;
30const events = new Subject();
31const sendEvent = (event: INotification): void => {
32 events.next(event);
33};
34const MockStore = function() {
35 this.query = () => {
36 return events.asObservable();
37 };
38};
Matteo Scandolo266907e2016-12-20 13:41:42 -080039
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080040const MockToastr = {
Matteo Scandolo266907e2016-12-20 13:41:42 -080041 info: jasmine.createSpy('info')
42};
43
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080044const MockAuth = {
45 getUser: () => {
46 return {email: 'test@xos.us'};
47 }
48};
49
Matteo Scandolo266907e2016-12-20 13:41:42 -080050const MockToastrConfig = {};
51
52const infoNotification = {
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080053 model: 'TestModel',
54 msg: {
Matteo Scandoloe6f9c352017-09-01 12:19:56 -070055 changed_fields: ['backend_status', 'backend_code'],
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080056 pk: 1,
57 object: {
58 name: 'TestName',
Matteo Scandoloe6f9c352017-09-01 12:19:56 -070059 backend_status: 'In Progress',
60 backend_code: 0
61 }
62 }
63};
64
65const noNotification = {
66 model: 'TestModel',
67 skip_notification: true,
68 msg: {
69 changed_fields: ['backend_status', 'backend_code'],
70 pk: 1,
71 object: {
72 name: 'TestName',
73 backend_status: 'In Progress',
74 backend_code: 0
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080075 }
76 }
77};
78
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080079const MockXosKeyboardShortcut = {
80 registerKeyBinding: jasmine.createSpy('registerKeyBinding')
81};
82
Matteo Scandolo71d74a42017-10-11 15:38:54 -070083const MockXosDebug: IXosDebugService = {
84 status: {
85 global: false,
86 events: false,
87 modelsTab: false,
88 notifications: true
89 },
90 setupShortcuts: jasmine.createSpy('debug.createShortcuts'),
91 toggleDebug: jasmine.createSpy('debug.toggleDebug')
92};
93
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080094describe('header component', () => {
95 beforeEach(() => {
96 angular
Matteo Scandolo67c105f2017-01-09 09:30:52 -080097 .module('xosHeader', ['app/core/header/header.html', 'ui.router'])
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080098 .component('xosHeader', xosHeader)
Matteo Scandolo266907e2016-12-20 13:41:42 -080099 .service('SynchronizerStore', MockStore)
100 .value('toastr', MockToastr)
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800101 .value('toastrConfig', MockToastrConfig)
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800102 .value('AuthService', MockAuth)
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800103 .value('XosNavigationService', {})
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700104 .value('ConfigHelpers', {
105 stateWithParamsForJs: () => null
106 })
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800107 .value('XosKeyboardShortcut', MockXosKeyboardShortcut)
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800108 .value('StyleConfig', {
109 logo: 'cord-logo.png',
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800110 })
Matteo Scandolo71d74a42017-10-11 15:38:54 -0700111 .value('SearchService', {})
112 .value('XosDebug', MockXosDebug);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800113
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800114 angular.mock.module('xosHeader');
115 });
116
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800117 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
118 scope = $rootScope;
119 compile = $compile;
120 element = $compile('<xos-header></xos-header>')($rootScope);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800121 $rootScope.$digest();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800122 isolatedScope = element.isolateScope();
123
124 // clear notifications
125 isolatedScope.notifications = [];
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700126 MockToastr.info.calls.reset();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800127 }));
128
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800129 it('should render the appropriate logo', () => {
130 const header = $('a.navbar-brand img', element).attr('src');
131 // webpack convert img to base64, how to test?
132 expect(header.trim()).not.toBeNull();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800133 });
134
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800135 it('should register a keyboard shortcut', () => {
136 expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalled();
137 // expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalledWith({
138 // key: 'f',
139 // description: 'Select search box',
140 // cb: () => {
141 // $('.navbar-form input').focus();
142 // },
143 // }, 'global');
144 });
145
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800146 it('should print user email', () => {
147 expect($('.profile-address', element).text()).toBe('test@xos.us');
Matteo Scandolo99fface2016-12-21 15:37:23 -0800148 });
149
Matteo Scandolo266907e2016-12-20 13:41:42 -0800150 it('should configure toastr', () => {
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700151 delete MockToastrConfig['onTap'];
152
Matteo Scandolo266907e2016-12-20 13:41:42 -0800153 expect(MockToastrConfig).toEqual({
154 newestOnTop: false,
155 positionClass: 'toast-top-right',
156 preventDuplicates: false,
157 preventOpenDuplicates: false,
158 progressBar: true,
159 });
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800160 });
161
Matteo Scandolo71d74a42017-10-11 15:38:54 -0700162 it('should not display a toastr for a new notification (if notifications are disabled)', () => {
163 MockXosDebug.status.notifications = false;
164 sendEvent(infoNotification);
165 scope.$digest();
166
167 expect(MockToastr.info).not.toHaveBeenCalled();
168 });
169
170 it('should display a toastr for a new notification (if notifications are enabled)', () => {
171 MockXosDebug.status.notifications = true;
Matteo Scandolo266907e2016-12-20 13:41:42 -0800172 sendEvent(infoNotification);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800173 scope.$digest();
174
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700175 expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel', {extraData: {dest: null}});
176 });
177
178 it('should not display a toastr for a new event that use skip_notification', () => {
179 sendEvent(noNotification);
180 scope.$digest();
181
182 expect(MockToastr.info).not.toHaveBeenCalled();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800183 });
184
Matteo Scandolo266907e2016-12-20 13:41:42 -0800185 // TODO test error and success toaster call
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800186});