blob: 1bcff9cd46da4d59b9f493e0a9105f65f92a9f88 [file] [log] [blame]
Matteo Scandolo9b460042017-04-14 16:24:45 -07001import * as angular from 'angular';
2import 'angular-mocks';
3import {xosLoader} from './loader';
4
5let loaded = true;
6
7const MockConfig = {
8 lastVisitedUrl: '/test'
9};
10
11const MockDiscover = {
12 areModelsLoaded: () => loaded,
13 discover: null
14};
15
16const MockOnboarder = {
17 onboard: null
18};
19
20describe('The XosLoader component', () => {
21 beforeEach(() => {
22 angular
23 .module('loader', [])
24 .value('XosConfig', MockConfig)
25 .value('XosModelDiscoverer', MockDiscover)
26 .value('XosOnboarder', MockOnboarder)
27 .component('xosLoader', xosLoader);
28 angular.mock.module('loader');
29 });
30
31 let scope, element, isolatedScope, rootScope, compile, timeout, location;
32 const compileElement = () => {
33
34 if (!scope) {
35 scope = rootScope.$new();
36 }
37
38 element = angular.element('<xos-loader></xos-loader>');
39 compile(element)(scope);
40 scope.$digest();
41 isolatedScope = element.isolateScope().vm;
42 };
43
44 beforeEach(inject(function ($q: ng.IQService, $compile: ng.ICompileService, $rootScope: ng.IScope, $timeout: ng.ITimeoutService, $location: ng.ILocationService) {
45 compile = $compile;
46 rootScope = $rootScope;
47 timeout = $timeout;
48 location = $location;
49 spyOn(location, 'path');
50
51 MockDiscover.discover = jasmine.createSpy('discover')
52 .and.callFake(() => {
53 const d = $q.defer();
54 d.resolve(true);
55 return d.promise;
56 });
57
58 MockOnboarder.onboard = jasmine.createSpy('onboard')
59 .and.callFake(() => {
60 const d = $q.defer();
61 d.resolve();
62 return d.promise;
63 });
64 }));
65
66 describe('when models are already loaded', () => {
67
68 beforeEach(() => {
69 compileElement();
70 spyOn(isolatedScope, 'moveOnTo');
71 isolatedScope.run();
72 timeout.flush();
73 });
74
75 it('should redirect to the last visited page', (done) => {
76 window.setTimeout(() => {
77 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/test');
78 expect(location.path).toHaveBeenCalledWith('/test');
79 done();
80 }, 600);
81 });
82 });
83
84 describe('when the last visited page is "loader"', () => {
85
86 beforeEach(() => {
87 MockConfig.lastVisitedUrl = '/loader';
88 compileElement();
89 spyOn(isolatedScope, 'moveOnTo');
90 isolatedScope.run();
91 });
92
93 it('should redirect to the "dashboard" page', (done) => {
94 window.setTimeout(() => {
95 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/loader');
96 expect(location.path).toHaveBeenCalledWith('/dashboard');
97 done();
98 }, 600);
99 });
100 });
101
102 describe('when models are not loaded', () => {
103
104 beforeEach(() => {
105 loaded = false;
106 compileElement();
107 spyOn(isolatedScope, 'moveOnTo');
108 });
109
110 it('should call XosModelDiscoverer.discover', () => {
111 expect(MockDiscover.discover).toHaveBeenCalled();
112 });
113 });
114
115});