blob: 8662719ed4ecd969412039c4c9ce6d3fe3eb9ae3 [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001/// <reference path="../../../../typings/index.d.ts" />
2
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -08003import * as $ from 'jquery';
4import 'jasmine-jquery';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08005import * as angular from 'angular';
6import 'angular-mocks';
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -08007import {xosHeader, INotification} from './header';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08008import {StyleConfig} from '../../config/style.config';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -08009import {Subject} from 'rxjs';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080010
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080011let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope;
12const events = new Subject();
13const sendEvent = (event: INotification): void => {
14 events.next(event);
15};
16const MockStore = function() {
17 this.query = () => {
18 return events.asObservable();
19 };
20};
Matteo Scandolo266907e2016-12-20 13:41:42 -080021
22interface ImockToastr {
23 info(msg: string, title: string): void;
24}
25
26const MockToastr: ImockToastr = {
27 info: jasmine.createSpy('info')
28};
29
30const MockToastrConfig = {};
31
32const infoNotification = {
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080033 model: 'TestModel',
34 msg: {
35 changed_fields: ['backend_status'],
36 pk: 1,
37 object: {
38 name: 'TestName',
Matteo Scandolo266907e2016-12-20 13:41:42 -080039 backend_status: '1 - Test Status'
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080040 }
41 }
42};
43
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080044describe('header component', () => {
45 beforeEach(() => {
46 angular
47 .module('xosHeader', ['app/core/header/header.html'])
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080048 .component('xosHeader', xosHeader)
Matteo Scandolo266907e2016-12-20 13:41:42 -080049 .service('SynchronizerStore', MockStore)
50 .value('toastr', MockToastr)
51 .value('toastrConfig', MockToastrConfig);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080052 angular.mock.module('xosHeader');
53 });
54
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080055 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 Scandolof6acdbe2016-12-13 10:29:37 -080059 $rootScope.$digest();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080060 isolatedScope = element.isolateScope();
61
62 // clear notifications
63 isolatedScope.notifications = [];
64 }));
65
66 it('should render the appropriate title', () => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080067 const header = $('a.navbar-brand .brand-title', element).text();
68 expect(header.trim()).toEqual(StyleConfig.projectName);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080069 });
70
Matteo Scandolo266907e2016-12-20 13:41:42 -080071 it('should configure toastr', () => {
72 expect(MockToastrConfig).toEqual({
73 newestOnTop: false,
74 positionClass: 'toast-top-right',
75 preventDuplicates: false,
76 preventOpenDuplicates: false,
77 progressBar: true,
78 });
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080079 });
80
Matteo Scandolo266907e2016-12-20 13:41:42 -080081 it('should display a toastr for a new notification', () => {
82 sendEvent(infoNotification);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080083 scope.$digest();
84
Matteo Scandolo266907e2016-12-20 13:41:42 -080085 expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel');
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080086 });
87
Matteo Scandolo266907e2016-12-20 13:41:42 -080088 // TODO test error and success toaster call
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080089});