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 | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 8 | import {Subject} from 'rxjs'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 9 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 10 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, isolatedScope; |
| 11 | const events = new Subject(); |
| 12 | const sendEvent = (event: INotification): void => { |
| 13 | events.next(event); |
| 14 | }; |
| 15 | const MockStore = function() { |
| 16 | this.query = () => { |
| 17 | return events.asObservable(); |
| 18 | }; |
| 19 | }; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 20 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 21 | const MockToastr = { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 22 | info: jasmine.createSpy('info') |
| 23 | }; |
| 24 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 25 | const MockAuth = { |
| 26 | getUser: () => { |
| 27 | return {email: 'test@xos.us'}; |
| 28 | } |
| 29 | }; |
| 30 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 31 | const MockToastrConfig = {}; |
| 32 | |
| 33 | const infoNotification = { |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 34 | model: 'TestModel', |
| 35 | msg: { |
| 36 | changed_fields: ['backend_status'], |
| 37 | pk: 1, |
| 38 | object: { |
| 39 | name: 'TestName', |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 40 | backend_status: '0 - In Progress' |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | }; |
| 44 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 45 | describe('header component', () => { |
| 46 | beforeEach(() => { |
| 47 | angular |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 48 | .module('xosHeader', ['app/core/header/header.html', 'ui.router']) |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 49 | .component('xosHeader', xosHeader) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 50 | .service('SynchronizerStore', MockStore) |
| 51 | .value('toastr', MockToastr) |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 52 | .value('toastrConfig', MockToastrConfig) |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 53 | .value('AuthService', MockAuth) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 54 | .value('NavigationService', {}) |
| 55 | .value('StyleConfig', { |
| 56 | logo: 'cord-logo.png', |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 57 | }) |
| 58 | .value('SearchService', {}); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 59 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 60 | angular.mock.module('xosHeader'); |
| 61 | }); |
| 62 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 63 | 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 67 | $rootScope.$digest(); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 68 | isolatedScope = element.isolateScope(); |
| 69 | |
| 70 | // clear notifications |
| 71 | isolatedScope.notifications = []; |
| 72 | })); |
| 73 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 74 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 78 | }); |
| 79 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 80 | it('should print user email', () => { |
| 81 | expect($('.profile-address', element).text()).toBe('test@xos.us'); |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 82 | }); |
| 83 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 84 | 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 Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 92 | }); |
| 93 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 94 | it('should display a toastr for a new notification', () => { |
| 95 | sendEvent(infoNotification); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 96 | scope.$digest(); |
| 97 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 98 | expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel'); |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 99 | }); |
| 100 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 101 | // TODO test error and success toaster call |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 102 | }); |