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 | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 27 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 28 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope; |
| 29 | const events = new Subject(); |
| 30 | const sendEvent = (event: INotification): void => { |
| 31 | events.next(event); |
| 32 | }; |
| 33 | const MockStore = function() { |
| 34 | this.query = () => { |
| 35 | return events.asObservable(); |
| 36 | }; |
| 37 | }; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 38 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 39 | const MockToastr = { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 40 | info: jasmine.createSpy('info') |
| 41 | }; |
| 42 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 43 | const MockAuth = { |
| 44 | getUser: () => { |
| 45 | return {email: 'test@xos.us'}; |
| 46 | } |
| 47 | }; |
| 48 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 49 | const MockToastrConfig = {}; |
| 50 | |
| 51 | const infoNotification = { |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 52 | model: 'TestModel', |
| 53 | msg: { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 54 | changed_fields: ['backend_status', 'backend_code'], |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 55 | pk: 1, |
| 56 | object: { |
| 57 | name: 'TestName', |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 58 | backend_status: 'In Progress', |
| 59 | backend_code: 0 |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | const 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | }; |
| 77 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 78 | const MockXosKeyboardShortcut = { |
| 79 | registerKeyBinding: jasmine.createSpy('registerKeyBinding') |
| 80 | }; |
| 81 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 82 | describe('header component', () => { |
| 83 | beforeEach(() => { |
| 84 | angular |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 85 | .module('xosHeader', ['app/core/header/header.html', 'ui.router']) |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 86 | .component('xosHeader', xosHeader) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 87 | .service('SynchronizerStore', MockStore) |
| 88 | .value('toastr', MockToastr) |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 89 | .value('toastrConfig', MockToastrConfig) |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 90 | .value('AuthService', MockAuth) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 91 | .value('XosNavigationService', {}) |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 92 | .value('ConfigHelpers', { |
| 93 | stateWithParamsForJs: () => null |
| 94 | }) |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 95 | .value('XosKeyboardShortcut', MockXosKeyboardShortcut) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 96 | .value('StyleConfig', { |
| 97 | logo: 'cord-logo.png', |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 98 | }) |
| 99 | .value('SearchService', {}); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 100 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 101 | angular.mock.module('xosHeader'); |
| 102 | }); |
| 103 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 104 | 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 108 | $rootScope.$digest(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 109 | isolatedScope = element.isolateScope(); |
| 110 | |
| 111 | // clear notifications |
| 112 | isolatedScope.notifications = []; |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 113 | MockToastr.info.calls.reset(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 114 | })); |
| 115 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 116 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 120 | }); |
| 121 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 122 | 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 Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 133 | it('should print user email', () => { |
| 134 | expect($('.profile-address', element).text()).toBe('test@xos.us'); |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 135 | }); |
| 136 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 137 | it('should configure toastr', () => { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 138 | delete MockToastrConfig['onTap']; |
| 139 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 140 | expect(MockToastrConfig).toEqual({ |
| 141 | newestOnTop: false, |
| 142 | positionClass: 'toast-top-right', |
| 143 | preventDuplicates: false, |
| 144 | preventOpenDuplicates: false, |
| 145 | progressBar: true, |
| 146 | }); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 147 | }); |
| 148 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 149 | it('should display a toastr for a new notification', () => { |
| 150 | sendEvent(infoNotification); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 151 | scope.$digest(); |
| 152 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 153 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 161 | }); |
| 162 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 163 | // TODO test error and success toaster call |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 164 | }); |