blob: 73ba58782a0f280427636afb034678710b9f933a [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 Scandolo63e43eb2016-12-14 14:18:53 -08008import {Subject} from 'rxjs';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08009
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080010let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope;
11const events = new Subject();
12const sendEvent = (event: INotification): void => {
13 events.next(event);
14};
15const MockStore = function() {
16 this.query = () => {
17 return events.asObservable();
18 };
19};
Matteo Scandolo266907e2016-12-20 13:41:42 -080020
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080021const MockToastr = {
Matteo Scandolo266907e2016-12-20 13:41:42 -080022 info: jasmine.createSpy('info')
23};
24
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080025const MockAuth = {
26 getUser: () => {
27 return {email: 'test@xos.us'};
28 }
29};
30
Matteo Scandolo266907e2016-12-20 13:41:42 -080031const MockToastrConfig = {};
32
33const infoNotification = {
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080034 model: 'TestModel',
35 msg: {
36 changed_fields: ['backend_status'],
37 pk: 1,
38 object: {
39 name: 'TestName',
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080040 backend_status: '0 - In Progress'
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080041 }
42 }
43};
44
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080045const MockXosKeyboardShortcut = {
46 registerKeyBinding: jasmine.createSpy('registerKeyBinding')
47};
48
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080049describe('header component', () => {
50 beforeEach(() => {
51 angular
Matteo Scandolo67c105f2017-01-09 09:30:52 -080052 .module('xosHeader', ['app/core/header/header.html', 'ui.router'])
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080053 .component('xosHeader', xosHeader)
Matteo Scandolo266907e2016-12-20 13:41:42 -080054 .service('SynchronizerStore', MockStore)
55 .value('toastr', MockToastr)
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080056 .value('toastrConfig', MockToastrConfig)
Matteo Scandolo67c105f2017-01-09 09:30:52 -080057 .value('AuthService', MockAuth)
Matteo Scandolo1aee1982017-02-17 08:33:23 -080058 .value('XosNavigationService', {})
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080059 .value('XosKeyboardShortcut', MockXosKeyboardShortcut)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080060 .value('StyleConfig', {
61 logo: 'cord-logo.png',
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080062 })
63 .value('SearchService', {});
Matteo Scandolo828d1e82017-01-17 14:49:38 -080064
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080065 angular.mock.module('xosHeader');
66 });
67
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080068 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
69 scope = $rootScope;
70 compile = $compile;
71 element = $compile('<xos-header></xos-header>')($rootScope);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080072 $rootScope.$digest();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080073 isolatedScope = element.isolateScope();
74
75 // clear notifications
76 isolatedScope.notifications = [];
77 }));
78
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080079 it('should render the appropriate logo', () => {
80 const header = $('a.navbar-brand img', element).attr('src');
81 // webpack convert img to base64, how to test?
82 expect(header.trim()).not.toBeNull();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080083 });
84
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080085 it('should register a keyboard shortcut', () => {
86 expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalled();
87 // expect(MockXosKeyboardShortcut.registerKeyBinding).toHaveBeenCalledWith({
88 // key: 'f',
89 // description: 'Select search box',
90 // cb: () => {
91 // $('.navbar-form input').focus();
92 // },
93 // }, 'global');
94 });
95
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080096 it('should print user email', () => {
97 expect($('.profile-address', element).text()).toBe('test@xos.us');
Matteo Scandolo99fface2016-12-21 15:37:23 -080098 });
99
Matteo Scandolo266907e2016-12-20 13:41:42 -0800100 it('should configure toastr', () => {
101 expect(MockToastrConfig).toEqual({
102 newestOnTop: false,
103 positionClass: 'toast-top-right',
104 preventDuplicates: false,
105 preventOpenDuplicates: false,
106 progressBar: true,
107 });
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800108 });
109
Matteo Scandolo266907e2016-12-20 13:41:42 -0800110 it('should display a toastr for a new notification', () => {
111 sendEvent(infoNotification);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800112 scope.$digest();
113
Matteo Scandolo266907e2016-12-20 13:41:42 -0800114 expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel');
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -0800115 });
116
Matteo Scandolo266907e2016-12-20 13:41:42 -0800117 // TODO test error and success toaster call
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800118});