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 | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 19 | /// <reference path="../../../../typings/index.d.ts" /> |
| 20 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 21 | import * as $ from 'jquery'; |
| 22 | import 'jasmine-jquery'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 23 | import * as angular from 'angular'; |
| 24 | import 'angular-mocks'; |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 25 | import {xosHeader, INotification} from './header'; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 26 | import {Subject} from 'rxjs'; |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 27 | import {IXosDebugService} from '../debug/debug.service'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 28 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 29 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope; |
| 30 | const events = new Subject(); |
| 31 | const sendEvent = (event: INotification): void => { |
| 32 | events.next(event); |
| 33 | }; |
| 34 | const MockStore = function() { |
| 35 | this.query = () => { |
| 36 | return events.asObservable(); |
| 37 | }; |
| 38 | }; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 39 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 40 | const MockToastr = { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 41 | info: jasmine.createSpy('info') |
| 42 | }; |
| 43 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 44 | const MockAuth = { |
| 45 | getUser: () => { |
| 46 | return {email: 'test@xos.us'}; |
| 47 | } |
| 48 | }; |
| 49 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 50 | const MockToastrConfig = {}; |
| 51 | |
| 52 | const infoNotification = { |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 53 | model: 'TestModel', |
| 54 | msg: { |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 55 | changed_fields: ['backend_status', 'backend_code'], |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 56 | pk: 1, |
| 57 | object: { |
| 58 | name: 'TestName', |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 59 | backend_status: 'In Progress', |
| 60 | backend_code: 0 |
| 61 | } |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | const 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | }; |
| 78 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 79 | const MockXosKeyboardShortcut = { |
| 80 | registerKeyBinding: jasmine.createSpy('registerKeyBinding') |
| 81 | }; |
| 82 | |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 83 | const 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 94 | describe('header component', () => { |
| 95 | beforeEach(() => { |
| 96 | angular |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 97 | .module('xosHeader', ['app/core/header/header.html', 'ui.router']) |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 98 | .component('xosHeader', xosHeader) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 99 | .service('SynchronizerStore', MockStore) |
| 100 | .value('toastr', MockToastr) |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 101 | .value('toastrConfig', MockToastrConfig) |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 102 | .value('AuthService', MockAuth) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 103 | .value('XosNavigationService', {}) |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 104 | .value('ConfigHelpers', { |
| 105 | stateWithParamsForJs: () => null |
| 106 | }) |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 107 | .value('XosKeyboardShortcut', MockXosKeyboardShortcut) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 108 | .value('StyleConfig', { |
| 109 | logo: 'cord-logo.png', |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 110 | }) |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 111 | .value('SearchService', {}) |
| 112 | .value('XosDebug', MockXosDebug); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 113 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 114 | angular.mock.module('xosHeader'); |
| 115 | }); |
| 116 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 117 | 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 121 | $rootScope.$digest(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 122 | isolatedScope = element.isolateScope(); |
| 123 | |
| 124 | // clear notifications |
| 125 | isolatedScope.notifications = []; |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 126 | MockToastr.info.calls.reset(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 127 | })); |
| 128 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 129 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 133 | }); |
| 134 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 135 | 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 Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 146 | it('should print user email', () => { |
| 147 | expect($('.profile-address', element).text()).toBe('test@xos.us'); |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 148 | }); |
| 149 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 150 | it('should configure toastr', () => { |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 151 | delete MockToastrConfig['onTap']; |
| 152 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 153 | expect(MockToastrConfig).toEqual({ |
| 154 | newestOnTop: false, |
| 155 | positionClass: 'toast-top-right', |
| 156 | preventDuplicates: false, |
| 157 | preventOpenDuplicates: false, |
| 158 | progressBar: true, |
| 159 | }); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 160 | }); |
| 161 | |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 162 | 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 Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 172 | sendEvent(infoNotification); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 173 | scope.$digest(); |
| 174 | |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 175 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 183 | }); |
| 184 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 185 | // TODO test error and success toaster call |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 186 | }); |