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: () => { |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 37 | return subject.asObservable(); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 38 | } |
| 39 | }; |
| 40 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 41 | describe('The XosOnboarder service', () => { |
| 42 | |
| 43 | beforeEach(() => { |
| 44 | |
| 45 | angular |
| 46 | .module('XosOnboarder', []) |
| 47 | .value('WebSocket', MockWs) |
| 48 | .value('$ocLazyLoad', MockLoad) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 49 | .value('XosModelStore', MockModelStore) |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 50 | .service('XosOnboarder', XosOnboarder); |
| 51 | |
| 52 | angular.mock.module('XosOnboarder'); |
| 53 | }); |
| 54 | |
| 55 | beforeEach(angular.mock.inject(( |
| 56 | XosOnboarder: IXosOnboarder, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 57 | _$ocLazyLoad_: any, |
| 58 | _$timeout_: ng.ITimeoutService |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 59 | ) => { |
| 60 | $ocLazyLoad = _$ocLazyLoad_; |
| 61 | spyOn($ocLazyLoad, 'load').and.callThrough(); |
| 62 | service = XosOnboarder; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 63 | $timeout = _$timeout_; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 64 | |
| 65 | // start the service |
| 66 | service.onboard(); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 67 | })); |
| 68 | |
| 69 | describe('when receive an event', () => { |
| 70 | it('should use $ocLazyLoad to add modules to the app', () => { |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 71 | subject.next([ |
| 72 | { |
| 73 | files: 'vendor.js,app.js', |
| 74 | name: 'sample app' |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 75 | } |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 76 | ]); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 77 | expect($ocLazyLoad.load).toHaveBeenCalledWith('vendor.js'); |
| 78 | expect($ocLazyLoad.load).toHaveBeenCalledWith('app.js'); |
| 79 | }); |
| 80 | }); |
| 81 | }); |