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: { |
| 54 | changed_fields: ['backend_status'], |
| 55 | pk: 1, |
| 56 | object: { |
| 57 | name: 'TestName', |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 58 | backend_status: '0 - In Progress' |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | }; |
| 62 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 63 | const MockXosKeyboardShortcut = { |
| 64 | registerKeyBinding: jasmine.createSpy('registerKeyBinding') |
| 65 | }; |
| 66 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 67 | describe('header component', () => { |
| 68 | beforeEach(() => { |
| 69 | angular |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 70 | .module('xosHeader', ['app/core/header/header.html', 'ui.router']) |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 71 | .component('xosHeader', xosHeader) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 72 | .service('SynchronizerStore', MockStore) |
| 73 | .value('toastr', MockToastr) |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 74 | .value('toastrConfig', MockToastrConfig) |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 75 | .value('AuthService', MockAuth) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 76 | .value('XosNavigationService', {}) |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 77 | .value('XosKeyboardShortcut', MockXosKeyboardShortcut) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 78 | .value('StyleConfig', { |
| 79 | logo: 'cord-logo.png', |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 80 | }) |
| 81 | .value('SearchService', {}); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 82 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 83 | angular.mock.module('xosHeader'); |
| 84 | }); |
| 85 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 86 | 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 90 | $rootScope.$digest(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 91 | isolatedScope = element.isolateScope(); |
| 92 | |
| 93 | // clear notifications |
| 94 | isolatedScope.notifications = []; |
| 95 | })); |
| 96 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 97 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 101 | }); |
| 102 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 103 | 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 Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 114 | it('should print user email', () => { |
| 115 | expect($('.profile-address', element).text()).toBe('test@xos.us'); |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 116 | }); |
| 117 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 118 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 126 | }); |
| 127 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 128 | it('should display a toastr for a new notification', () => { |
| 129 | sendEvent(infoNotification); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 130 | scope.$digest(); |
| 131 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 132 | expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel'); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 133 | }); |
| 134 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 135 | // TODO test error and success toaster call |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 136 | }); |