Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts" /> |
| 2 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 3 | import * as $ from 'jquery'; |
| 4 | import 'jasmine-jquery'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 5 | import * as angular from 'angular'; |
| 6 | import 'angular-mocks'; |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 7 | import {xosHeader, INotification} from './header'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 8 | import {StyleConfig} from '../../config/style.config'; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 9 | import {Subject} from 'rxjs'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 11 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope; |
| 12 | const events = new Subject(); |
| 13 | const sendEvent = (event: INotification): void => { |
| 14 | events.next(event); |
| 15 | }; |
| 16 | const MockStore = function() { |
| 17 | this.query = () => { |
| 18 | return events.asObservable(); |
| 19 | }; |
| 20 | }; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 21 | |
| 22 | interface ImockToastr { |
| 23 | info(msg: string, title: string): void; |
| 24 | } |
| 25 | |
| 26 | const MockToastr: ImockToastr = { |
| 27 | info: jasmine.createSpy('info') |
| 28 | }; |
| 29 | |
| 30 | const MockToastrConfig = {}; |
| 31 | |
| 32 | const infoNotification = { |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 33 | model: 'TestModel', |
| 34 | msg: { |
| 35 | changed_fields: ['backend_status'], |
| 36 | pk: 1, |
| 37 | object: { |
| 38 | name: 'TestName', |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 39 | backend_status: '1 - Test Status' |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | }; |
| 43 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 44 | describe('header component', () => { |
| 45 | beforeEach(() => { |
| 46 | angular |
| 47 | .module('xosHeader', ['app/core/header/header.html']) |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 48 | .component('xosHeader', xosHeader) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 49 | .service('SynchronizerStore', MockStore) |
| 50 | .value('toastr', MockToastr) |
| 51 | .value('toastrConfig', MockToastrConfig); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 52 | angular.mock.module('xosHeader'); |
| 53 | }); |
| 54 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 55 | beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => { |
| 56 | scope = $rootScope; |
| 57 | compile = $compile; |
| 58 | element = $compile('<xos-header></xos-header>')($rootScope); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 59 | $rootScope.$digest(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 60 | isolatedScope = element.isolateScope(); |
| 61 | |
| 62 | // clear notifications |
| 63 | isolatedScope.notifications = []; |
| 64 | })); |
| 65 | |
| 66 | it('should render the appropriate title', () => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 67 | const header = $('a.navbar-brand .brand-title', element).text(); |
| 68 | expect(header.trim()).toEqual(StyleConfig.projectName); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 69 | }); |
| 70 | |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame^] | 71 | it('should set the appropriate favicon', () => { |
| 72 | console.log($('#favicon').attr('href')); |
| 73 | }); |
| 74 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 75 | it('should configure toastr', () => { |
| 76 | expect(MockToastrConfig).toEqual({ |
| 77 | newestOnTop: false, |
| 78 | positionClass: 'toast-top-right', |
| 79 | preventDuplicates: false, |
| 80 | preventOpenDuplicates: false, |
| 81 | progressBar: true, |
| 82 | }); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 83 | }); |
| 84 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 85 | it('should display a toastr for a new notification', () => { |
| 86 | sendEvent(infoNotification); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 87 | scope.$digest(); |
| 88 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 89 | expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel'); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 90 | }); |
| 91 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 92 | // TODO test error and success toaster call |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 93 | }); |