blob: 17a7543d2714c455c0e3f07a10ba500ebf684d99 [file] [log] [blame]
Matteo Scandolo4e870232017-01-30 13:43:05 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-resource';
4import {Subject} from 'rxjs';
5import {XosOnboarder, IXosOnboarder} from './onboard.service';
6import {IWSEventService} from '../../datasources/websocket/global';
7
Matteo Scandolo47860fe2017-02-02 12:05:55 -08008let service, $ocLazyLoad, $timeout;
Matteo Scandolo4e870232017-01-30 13:43:05 -08009
10const subject = new Subject();
11
12const MockWs: IWSEventService = {
13 list() {
14 return subject.asObservable();
15 }
16};
17
18const MockPromise = {
19 then: (cb) => {
20 cb('done');
21 return MockPromise;
22 },
23 catch: (cb) => {
24 cb('err');
25 return MockPromise;
26 }
27};
28
29const MockLoad = {
30 load: () => {
31 return MockPromise;
32 }
33};
34
Matteo Scandolo47860fe2017-02-02 12:05:55 -080035const MockModelStore = {
36 query: () => {
37 return {
38 subscribe: () => {
39 return;
40 }
41 };
42 }
43};
44
Matteo Scandolo4e870232017-01-30 13:43:05 -080045describe('The XosOnboarder service', () => {
46
47 beforeEach(() => {
48
49 angular
50 .module('XosOnboarder', [])
51 .value('WebSocket', MockWs)
52 .value('$ocLazyLoad', MockLoad)
Matteo Scandolo1aee1982017-02-17 08:33:23 -080053 .value('XosModelStore', MockModelStore)
Matteo Scandolo4e870232017-01-30 13:43:05 -080054 .service('XosOnboarder', XosOnboarder);
55
56 angular.mock.module('XosOnboarder');
57 });
58
59 beforeEach(angular.mock.inject((
60 XosOnboarder: IXosOnboarder,
Matteo Scandolo47860fe2017-02-02 12:05:55 -080061 _$ocLazyLoad_: any,
62 _$timeout_: ng.ITimeoutService
Matteo Scandolo4e870232017-01-30 13:43:05 -080063 ) => {
64 $ocLazyLoad = _$ocLazyLoad_;
65 spyOn($ocLazyLoad, 'load').and.callThrough();
66 service = XosOnboarder;
Matteo Scandolo47860fe2017-02-02 12:05:55 -080067 $timeout = _$timeout_;
Matteo Scandolo4e870232017-01-30 13:43:05 -080068 }));
69
70 describe('when receive an event', () => {
71 it('should use $ocLazyLoad to add modules to the app', () => {
72 subject.next({
Matteo Scandolo47860fe2017-02-02 12:05:55 -080073 model: 'XOSComponent',
Matteo Scandolo4e870232017-01-30 13:43:05 -080074 msg: {
75 app: 'sample',
Matteo Scandolo47860fe2017-02-02 12:05:55 -080076 object: {
77 extra: '["vendor.js", "app.js"]',
78 name: 'sample app'
79 }
Matteo Scandolo4e870232017-01-30 13:43:05 -080080 }
81 });
Matteo Scandolo47860fe2017-02-02 12:05:55 -080082 $timeout.flush();
Matteo Scandolo4e870232017-01-30 13:43:05 -080083 expect($ocLazyLoad.load).toHaveBeenCalledWith('vendor.js');
84 expect($ocLazyLoad.load).toHaveBeenCalledWith('app.js');
85 });
86 });
87});