Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-resource'; |
| 22 | import {Subject} from 'rxjs'; |
| 23 | import {XosOnboarder, IXosOnboarder} from './onboard.service'; |
| 24 | import {IWSEventService} from '../../datasources/websocket/global'; |
| 25 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 26 | let service, $ocLazyLoad, $timeout; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 27 | |
| 28 | const subject = new Subject(); |
| 29 | |
| 30 | const MockWs: IWSEventService = { |
| 31 | list() { |
| 32 | return subject.asObservable(); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | const MockPromise = { |
| 37 | then: (cb) => { |
| 38 | cb('done'); |
| 39 | return MockPromise; |
| 40 | }, |
| 41 | catch: (cb) => { |
| 42 | cb('err'); |
| 43 | return MockPromise; |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | const MockLoad = { |
| 48 | load: () => { |
| 49 | return MockPromise; |
| 50 | } |
| 51 | }; |
| 52 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 53 | const MockModelStore = { |
| 54 | query: () => { |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 55 | return subject.asObservable(); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 59 | describe('The XosOnboarder service', () => { |
| 60 | |
| 61 | beforeEach(() => { |
| 62 | |
| 63 | angular |
| 64 | .module('XosOnboarder', []) |
| 65 | .value('WebSocket', MockWs) |
| 66 | .value('$ocLazyLoad', MockLoad) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 67 | .value('XosModelStore', MockModelStore) |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 68 | .service('XosOnboarder', XosOnboarder); |
| 69 | |
| 70 | angular.mock.module('XosOnboarder'); |
| 71 | }); |
| 72 | |
| 73 | beforeEach(angular.mock.inject(( |
| 74 | XosOnboarder: IXosOnboarder, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 75 | _$ocLazyLoad_: any, |
| 76 | _$timeout_: ng.ITimeoutService |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 77 | ) => { |
| 78 | $ocLazyLoad = _$ocLazyLoad_; |
| 79 | spyOn($ocLazyLoad, 'load').and.callThrough(); |
| 80 | service = XosOnboarder; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 81 | $timeout = _$timeout_; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 82 | |
| 83 | // start the service |
| 84 | service.onboard(); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 85 | })); |
| 86 | |
| 87 | describe('when receive an event', () => { |
| 88 | it('should use $ocLazyLoad to add modules to the app', () => { |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 89 | subject.next([ |
| 90 | { |
| 91 | files: 'vendor.js,app.js', |
| 92 | name: 'sample app' |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 93 | } |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 94 | ]); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 95 | expect($ocLazyLoad.load).toHaveBeenCalledWith('vendor.js'); |
| 96 | expect($ocLazyLoad.load).toHaveBeenCalledWith('app.js'); |
| 97 | }); |
| 98 | }); |
| 99 | }); |