blob: 3601e5e7334fd14f1d3def09d4acdc5730f40355 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolo4e870232017-01-30 13:43:05 -080019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-resource';
22import {Subject} from 'rxjs';
23import {XosOnboarder, IXosOnboarder} from './onboard.service';
24import {IWSEventService} from '../../datasources/websocket/global';
25
Matteo Scandolo47860fe2017-02-02 12:05:55 -080026let service, $ocLazyLoad, $timeout;
Matteo Scandolo4e870232017-01-30 13:43:05 -080027
28const subject = new Subject();
29
30const MockWs: IWSEventService = {
31 list() {
32 return subject.asObservable();
33 }
34};
35
36const MockPromise = {
37 then: (cb) => {
38 cb('done');
39 return MockPromise;
40 },
41 catch: (cb) => {
42 cb('err');
43 return MockPromise;
44 }
45};
46
47const MockLoad = {
48 load: () => {
49 return MockPromise;
50 }
51};
52
Matteo Scandolo47860fe2017-02-02 12:05:55 -080053const MockModelStore = {
54 query: () => {
Matteo Scandolo11b4a632017-02-09 10:28:41 -080055 return subject.asObservable();
Matteo Scandolo47860fe2017-02-02 12:05:55 -080056 }
57};
58
Matteo Scandolo4e870232017-01-30 13:43:05 -080059describe('The XosOnboarder service', () => {
60
61 beforeEach(() => {
62
63 angular
64 .module('XosOnboarder', [])
65 .value('WebSocket', MockWs)
66 .value('$ocLazyLoad', MockLoad)
Matteo Scandolo1aee1982017-02-17 08:33:23 -080067 .value('XosModelStore', MockModelStore)
Matteo Scandolo4e870232017-01-30 13:43:05 -080068 .service('XosOnboarder', XosOnboarder);
69
70 angular.mock.module('XosOnboarder');
71 });
72
73 beforeEach(angular.mock.inject((
74 XosOnboarder: IXosOnboarder,
Matteo Scandolo47860fe2017-02-02 12:05:55 -080075 _$ocLazyLoad_: any,
76 _$timeout_: ng.ITimeoutService
Matteo Scandolo4e870232017-01-30 13:43:05 -080077 ) => {
78 $ocLazyLoad = _$ocLazyLoad_;
79 spyOn($ocLazyLoad, 'load').and.callThrough();
80 service = XosOnboarder;
Matteo Scandolo47860fe2017-02-02 12:05:55 -080081 $timeout = _$timeout_;
Matteo Scandolo9b460042017-04-14 16:24:45 -070082
83 // start the service
84 service.onboard();
Matteo Scandolo4e870232017-01-30 13:43:05 -080085 }));
86
87 describe('when receive an event', () => {
88 it('should use $ocLazyLoad to add modules to the app', () => {
Matteo Scandolo11b4a632017-02-09 10:28:41 -080089 subject.next([
90 {
91 files: 'vendor.js,app.js',
92 name: 'sample app'
Matteo Scandolo4e870232017-01-30 13:43:05 -080093 }
Matteo Scandolo11b4a632017-02-09 10:28:41 -080094 ]);
Matteo Scandolo4e870232017-01-30 13:43:05 -080095 expect($ocLazyLoad.load).toHaveBeenCalledWith('vendor.js');
96 expect($ocLazyLoad.load).toHaveBeenCalledWith('app.js');
97 });
98 });
99});