blob: 1f2c1aabd17a333a9907d1aeecd4ea6fd927df20 [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};
21const sampleNotification = {
22 model: 'TestModel',
23 msg: {
24 changed_fields: ['backend_status'],
25 pk: 1,
26 object: {
27 name: 'TestName',
28 backend_status: 'Test Status'
29 }
30 }
31};
32
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080033describe('header component', () => {
34 beforeEach(() => {
35 angular
36 .module('xosHeader', ['app/core/header/header.html'])
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080037 .component('xosHeader', xosHeader)
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080038 .service('SynchronizerStore', MockStore);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080039 angular.mock.module('xosHeader');
40 });
41
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080042 beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => {
43 scope = $rootScope;
44 compile = $compile;
45 element = $compile('<xos-header></xos-header>')($rootScope);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080046 $rootScope.$digest();
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080047 isolatedScope = element.isolateScope();
48
49 // clear notifications
50 isolatedScope.notifications = [];
51 }));
52
53 it('should render the appropriate title', () => {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080054 const header = element.find('a');
55 expect(header.html().trim()).toEqual(StyleConfig.projectName);
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080056
57 const badge = $('i.badge', element);
58 expect(badge.length).toBe(0);
59 });
60
61 it('should display a badge if there are unread notifications', () => {
62 sendEvent(sampleNotification);
63 scope.$digest();
64
65 const badge = $('i.badge', element);
66 expect(badge.length).toBe(1);
67 });
68
69 it('should not display a badge if there are notifications have been read', () => {
70 sendEvent(angular.extend({viewed: true}, sampleNotification));
71 scope.$digest();
72
73 const badge = $('i.badge', element);
74 expect(badge.length).toBe(0);
75 });
76
77 it('should display a list of notifications', () => {
78 isolatedScope.showNotification = true;
79 sendEvent(angular.extend({viewed: true}, sampleNotification));
80 sendEvent(angular.extend({viewed: false}, sampleNotification));
81 scope.$digest();
82
83 const badge = $('i.badge', element);
84 expect(badge.length).toBe(1);
85 const notificationPanel = $('.notification-panel', element);
86 expect(notificationPanel.length).toBe(1);
87
88 expect($('.notification-panel li', element).length).toBe(2);
89 });
90
91 it('should add the viewed class to an readed notification', () => {
92 isolatedScope.showNotification = true;
93 sendEvent(angular.extend({viewed: true}, sampleNotification));
94 scope.$digest();
95 expect($('.notification-panel li:first-child', element)).toHaveClass('viewed');
96 scope.$digest();
97 });
98
99 it('should not add the viewed class to an unread notification', () => {
100 isolatedScope.showNotification = true;
101 sendEvent(angular.extend({viewed: false}, sampleNotification));
102 scope.$digest();
103 expect($('.notification-panel li:first-child', element)).not.toHaveClass('viewed');
104 scope.$digest();
105 });
106
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800107});