blob: 0bd68777d18d7c64126e2af6fa8658c1a5c29b9e [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 Scandolof6acdbe2016-12-13 10:29:37 -080045describe('header component', () => {
46 beforeEach(() => {
47 angular
Matteo Scandolo67c105f2017-01-09 09:30:52 -080048 .module('xosHeader', ['app/core/header/header.html', 'ui.router'])
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080049 .component('xosHeader', xosHeader)
Matteo Scandolo266907e2016-12-20 13:41:42 -080050 .service('SynchronizerStore', MockStore)
51 .value('toastr', MockToastr)
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080052 .value('toastrConfig', MockToastrConfig)
Matteo Scandolo67c105f2017-01-09 09:30:52 -080053 .value('AuthService', MockAuth)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080054 .value('NavigationService', {})
55 .value('StyleConfig', {
56 logo: 'cord-logo.png',
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080057 })
58 .value('SearchService', {});
Matteo Scandolo828d1e82017-01-17 14:49:38 -080059
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080060 angular.mock.module('xosHeader');
61 });
62
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080063 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
64 scope = $rootScope;
65 compile = $compile;
66 element = $compile('<xos-header></xos-header>')($rootScope);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080067 $rootScope.$digest();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080068 isolatedScope = element.isolateScope();
69
70 // clear notifications
71 isolatedScope.notifications = [];
72 }));
73
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080074 it('should render the appropriate logo', () => {
75 const header = $('a.navbar-brand img', element).attr('src');
76 // webpack convert img to base64, how to test?
77 expect(header.trim()).not.toBeNull();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080078 });
79
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080080 it('should print user email', () => {
81 expect($('.profile-address', element).text()).toBe('test@xos.us');
Matteo Scandolo99fface2016-12-21 15:37:23 -080082 });
83
Matteo Scandolo266907e2016-12-20 13:41:42 -080084 it('should configure toastr', () => {
85 expect(MockToastrConfig).toEqual({
86 newestOnTop: false,
87 positionClass: 'toast-top-right',
88 preventDuplicates: false,
89 preventOpenDuplicates: false,
90 progressBar: true,
91 });
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080092 });
93
Matteo Scandolo266907e2016-12-20 13:41:42 -080094 it('should display a toastr for a new notification', () => {
95 sendEvent(infoNotification);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080096 scope.$digest();
97
Matteo Scandolo266907e2016-12-20 13:41:42 -080098 expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel');
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080099 });
100
Matteo Scandolo266907e2016-12-20 13:41:42 -0800101 // TODO test error and success toaster call
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800102});