blob: eb236b1d60b9f97ce82052b3e627267f411b2a9b [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 Scandolof6acdbe2016-12-13 10:29:37 -080027
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080028let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope;
29const events = new Subject();
30const sendEvent = (event: INotification): void => {
31 events.next(event);
32};
33const MockStore = function() {
34 this.query = () => {
35 return events.asObservable();
36 };
37};
Matteo Scandolo266907e2016-12-20 13:41:42 -080038
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080039const MockToastr = {
Matteo Scandolo266907e2016-12-20 13:41:42 -080040 info: jasmine.createSpy('info')
41};
42
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080043const MockAuth = {
44 getUser: () => {
45 return {email: 'test@xos.us'};
46 }
47};
48
Matteo Scandolo266907e2016-12-20 13:41:42 -080049const MockToastrConfig = {};
50
51const infoNotification = {
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080052 model: 'TestModel',
53 msg: {
Matteo Scandolo31daa802017-09-01 12:19:56 -070054 changed_fields: ['backend_status', 'backend_code'],
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080055 pk: 1,
56 object: {
57 name: 'TestName',
Matteo Scandolo31daa802017-09-01 12:19:56 -070058 backend_status: 'In Progress',
59 backend_code: 0
60 }
61 }
62};
63
64const noNotification = {
65 model: 'TestModel',
66 skip_notification: true,
67 msg: {
68 changed_fields: ['backend_status', 'backend_code'],
69 pk: 1,
70 object: {
71 name: 'TestName',
72 backend_status: 'In Progress',
73 backend_code: 0
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080074 }
75 }
76};
77
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080078const MockXosKeyboardShortcut = {
79 registerKeyBinding: jasmine.createSpy('registerKeyBinding')
80};
81
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080082describe('header component', () => {
83 beforeEach(() => {
84 angular
Matteo Scandolo67c105f2017-01-09 09:30:52 -080085 .module('xosHeader', ['app/core/header/header.html', 'ui.router'])
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080086 .component('xosHeader', xosHeader)
Matteo Scandolo266907e2016-12-20 13:41:42 -080087 .service('SynchronizerStore', MockStore)
88 .value('toastr', MockToastr)
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080089 .value('toastrConfig', MockToastrConfig)
Matteo Scandolo67c105f2017-01-09 09:30:52 -080090 .value('AuthService', MockAuth)
Matteo Scandolo1aee1982017-02-17 08:33:23 -080091 .value('XosNavigationService', {})
Matteo Scandolo31daa802017-09-01 12:19:56 -070092 .value('ConfigHelpers', {
93 stateWithParamsForJs: () => null
94 })
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080095 .value('XosKeyboardShortcut', MockXosKeyboardShortcut)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080096 .value('StyleConfig', {
97 logo: 'cord-logo.png',
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080098 })
99 .value('SearchService', {});
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800100
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800101 angular.mock.module('xosHeader');
102 });
103
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800104 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
105 scope = $rootScope;
106 compile = $compile;
107 element = $compile('<xos-header></xos-header>')($rootScope);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800108 $rootScope.$digest();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800109 isolatedScope = element.isolateScope();
110
111 // clear notifications
112 isolatedScope.notifications = [];
Matteo Scandolo31daa802017-09-01 12:19:56 -0700113 MockToastr.info.calls.reset();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800114 }));
115
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800116 it('should render the appropriate logo', () => {
117 const header = $('a.navbar-brand img', element).attr('src');
118 // webpack convert img to base64, how to test?
119 expect(header.trim()).not.toBeNull();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800120 });
121
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800122 it('should register a keyboard shortcut', () => {
123 expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalled();
124 // expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalledWith({
125 // key: 'f',
126 // description: 'Select search box',
127 // cb: () => {
128 // $('.navbar-form input').focus();
129 // },
130 // }, 'global');
131 });
132
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800133 it('should print user email', () => {
134 expect($('.profile-address', element).text()).toBe('test@xos.us');
Matteo Scandolo99fface2016-12-21 15:37:23 -0800135 });
136
Matteo Scandolo266907e2016-12-20 13:41:42 -0800137 it('should configure toastr', () => {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700138 delete MockToastrConfig['onTap'];
139
Matteo Scandolo266907e2016-12-20 13:41:42 -0800140 expect(MockToastrConfig).toEqual({
141 newestOnTop: false,
142 positionClass: 'toast-top-right',
143 preventDuplicates: false,
144 preventOpenDuplicates: false,
145 progressBar: true,
146 });
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800147 });
148
Matteo Scandolo266907e2016-12-20 13:41:42 -0800149 it('should display a toastr for a new notification', () => {
150 sendEvent(infoNotification);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800151 scope.$digest();
152
Matteo Scandolo31daa802017-09-01 12:19:56 -0700153 expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel', {extraData: {dest: null}});
154 });
155
156 it('should not display a toastr for a new event that use skip_notification', () => {
157 sendEvent(noNotification);
158 scope.$digest();
159
160 expect(MockToastr.info).not.toHaveBeenCalled();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800161 });
162
Matteo Scandolo266907e2016-12-20 13:41:42 -0800163 // TODO test error and success toaster call
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800164});