blob: 13f8b5b9d96e1db6d759c61745461709f1a61e5b [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: {
54 changed_fields: ['backend_status'],
55 pk: 1,
56 object: {
57 name: 'TestName',
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080058 backend_status: '0 - In Progress'
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080059 }
60 }
61};
62
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080063const MockXosKeyboardShortcut = {
64 registerKeyBinding: jasmine.createSpy('registerKeyBinding')
65};
66
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080067describe('header component', () => {
68 beforeEach(() => {
69 angular
Matteo Scandolo67c105f2017-01-09 09:30:52 -080070 .module('xosHeader', ['app/core/header/header.html', 'ui.router'])
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080071 .component('xosHeader', xosHeader)
Matteo Scandolo266907e2016-12-20 13:41:42 -080072 .service('SynchronizerStore', MockStore)
73 .value('toastr', MockToastr)
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080074 .value('toastrConfig', MockToastrConfig)
Matteo Scandolo67c105f2017-01-09 09:30:52 -080075 .value('AuthService', MockAuth)
Matteo Scandolo1aee1982017-02-17 08:33:23 -080076 .value('XosNavigationService', {})
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080077 .value('XosKeyboardShortcut', MockXosKeyboardShortcut)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080078 .value('StyleConfig', {
79 logo: 'cord-logo.png',
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080080 })
81 .value('SearchService', {});
Matteo Scandolo828d1e82017-01-17 14:49:38 -080082
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080083 angular.mock.module('xosHeader');
84 });
85
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080086 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
87 scope = $rootScope;
88 compile = $compile;
89 element = $compile('<xos-header></xos-header>')($rootScope);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080090 $rootScope.$digest();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080091 isolatedScope = element.isolateScope();
92
93 // clear notifications
94 isolatedScope.notifications = [];
95 }));
96
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080097 it('should render the appropriate logo', () => {
98 const header = $('a.navbar-brand img', element).attr('src');
99 // webpack convert img to base64, how to test?
100 expect(header.trim()).not.toBeNull();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800101 });
102
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800103 it('should register a keyboard shortcut', () => {
104 expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalled();
105 // expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalledWith({
106 // key: 'f',
107 // description: 'Select search box',
108 // cb: () => {
109 // $('.navbar-form input').focus();
110 // },
111 // }, 'global');
112 });
113
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800114 it('should print user email', () => {
115 expect($('.profile-address', element).text()).toBe('test@xos.us');
Matteo Scandolo99fface2016-12-21 15:37:23 -0800116 });
117
Matteo Scandolo266907e2016-12-20 13:41:42 -0800118 it('should configure toastr', () => {
119 expect(MockToastrConfig).toEqual({
120 newestOnTop: false,
121 positionClass: 'toast-top-right',
122 preventDuplicates: false,
123 preventOpenDuplicates: false,
124 progressBar: true,
125 });
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800126 });
127
Matteo Scandolo266907e2016-12-20 13:41:42 -0800128 it('should display a toastr for a new notification', () => {
129 sendEvent(infoNotification);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800130 scope.$digest();
131
Matteo Scandolo266907e2016-12-20 13:41:42 -0800132 expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel');
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800133 });
134
Matteo Scandolo266907e2016-12-20 13:41:42 -0800135 // TODO test error and success toaster call
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800136});