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 | }; |
| 21 | const 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 33 | describe('header component', () => { |
| 34 | beforeEach(() => { |
| 35 | angular |
| 36 | .module('xosHeader', ['app/core/header/header.html']) |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 37 | .component('xosHeader', xosHeader) |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 38 | .service('SynchronizerStore', MockStore); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 39 | angular.mock.module('xosHeader'); |
| 40 | }); |
| 41 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 42 | 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 46 | $rootScope.$digest(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 47 | isolatedScope = element.isolateScope(); |
| 48 | |
| 49 | // clear notifications |
| 50 | isolatedScope.notifications = []; |
| 51 | })); |
| 52 | |
| 53 | it('should render the appropriate title', () => { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 54 | const header = element.find('a'); |
| 55 | expect(header.html().trim()).toEqual(StyleConfig.projectName); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 56 | |
| 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 107 | }); |