Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-resource'; |
| 4 | import {Subject} from 'rxjs'; |
| 5 | import {XosOnboarder, IXosOnboarder} from './onboard.service'; |
| 6 | import {IWSEventService} from '../../datasources/websocket/global'; |
| 7 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 8 | let service, $ocLazyLoad, $timeout; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 9 | |
| 10 | const subject = new Subject(); |
| 11 | |
| 12 | const MockWs: IWSEventService = { |
| 13 | list() { |
| 14 | return subject.asObservable(); |
| 15 | } |
| 16 | }; |
| 17 | |
| 18 | const MockPromise = { |
| 19 | then: (cb) => { |
| 20 | cb('done'); |
| 21 | return MockPromise; |
| 22 | }, |
| 23 | catch: (cb) => { |
| 24 | cb('err'); |
| 25 | return MockPromise; |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | const MockLoad = { |
| 30 | load: () => { |
| 31 | return MockPromise; |
| 32 | } |
| 33 | }; |
| 34 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 35 | const MockModelStore = { |
| 36 | query: () => { |
| 37 | return { |
| 38 | subscribe: () => { |
| 39 | return; |
| 40 | } |
| 41 | }; |
| 42 | } |
| 43 | }; |
| 44 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 45 | describe('The XosOnboarder service', () => { |
| 46 | |
| 47 | beforeEach(() => { |
| 48 | |
| 49 | angular |
| 50 | .module('XosOnboarder', []) |
| 51 | .value('WebSocket', MockWs) |
| 52 | .value('$ocLazyLoad', MockLoad) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 53 | .value('XosModelStore', MockModelStore) |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 54 | .service('XosOnboarder', XosOnboarder); |
| 55 | |
| 56 | angular.mock.module('XosOnboarder'); |
| 57 | }); |
| 58 | |
| 59 | beforeEach(angular.mock.inject(( |
| 60 | XosOnboarder: IXosOnboarder, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 61 | _$ocLazyLoad_: any, |
| 62 | _$timeout_: ng.ITimeoutService |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 63 | ) => { |
| 64 | $ocLazyLoad = _$ocLazyLoad_; |
| 65 | spyOn($ocLazyLoad, 'load').and.callThrough(); |
| 66 | service = XosOnboarder; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 67 | $timeout = _$timeout_; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 68 | })); |
| 69 | |
| 70 | describe('when receive an event', () => { |
| 71 | it('should use $ocLazyLoad to add modules to the app', () => { |
| 72 | subject.next({ |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 73 | model: 'XOSComponent', |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 74 | msg: { |
| 75 | app: 'sample', |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 76 | object: { |
| 77 | extra: '["vendor.js", "app.js"]', |
| 78 | name: 'sample app' |
| 79 | } |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 80 | } |
| 81 | }); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 82 | $timeout.flush(); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 83 | expect($ocLazyLoad.load).toHaveBeenCalledWith('vendor.js'); |
| 84 | expect($ocLazyLoad.load).toHaveBeenCalledWith('app.js'); |
| 85 | }); |
| 86 | }); |
| 87 | }); |