blob: 1a2d55346de44053a1ddde46dfa8e451c6f3da6d [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 Scandolo9b460042017-04-14 16:24:45 -070019import * as angular from 'angular';
20import 'angular-mocks';
21import {xosLoader} from './loader';
22
23let loaded = true;
Matteo Scandolobd60dee2017-05-08 17:53:25 -070024let authenticated = true;
Matteo Scandolo9b460042017-04-14 16:24:45 -070025
26const MockConfig = {
27 lastVisitedUrl: '/test'
28};
29
30const MockDiscover = {
31 areModelsLoaded: () => loaded,
Matteo Scandolobf4e8402019-06-24 12:22:49 -070032 getStatusMessage: () => 'Test Message',
Matteo Scandolo9b460042017-04-14 16:24:45 -070033 discover: null
34};
35
36const MockOnboarder = {
37 onboard: null
38};
39
Matteo Scandolobd60dee2017-05-08 17:53:25 -070040const MockAuth = {
41 isAuthenticated: jasmine.createSpy('isAuthenticated')
42 .and.callFake(() => authenticated)
43};
44
45const MockState = {
46 go: jasmine.createSpy('state.go')
47};
48
Matteo Scandolo9b460042017-04-14 16:24:45 -070049describe('The XosLoader component', () => {
50 beforeEach(() => {
51 angular
52 .module('loader', [])
53 .value('XosConfig', MockConfig)
54 .value('XosModelDiscoverer', MockDiscover)
55 .value('XosOnboarder', MockOnboarder)
Matteo Scandolobd60dee2017-05-08 17:53:25 -070056 .value('AuthService', MockAuth)
57 .value('$state', MockState)
Matteo Scandolo9b460042017-04-14 16:24:45 -070058 .component('xosLoader', xosLoader);
59 angular.mock.module('loader');
60 });
61
62 let scope, element, isolatedScope, rootScope, compile, timeout, location;
63 const compileElement = () => {
64
65 if (!scope) {
66 scope = rootScope.$new();
67 }
68
69 element = angular.element('<xos-loader></xos-loader>');
70 compile(element)(scope);
71 scope.$digest();
72 isolatedScope = element.isolateScope().vm;
73 };
74
75 beforeEach(inject(function ($q: ng.IQService, $compile: ng.ICompileService, $rootScope: ng.IScope, $timeout: ng.ITimeoutService, $location: ng.ILocationService) {
76 compile = $compile;
77 rootScope = $rootScope;
78 timeout = $timeout;
79 location = $location;
80 spyOn(location, 'path');
Matteo Scandolo9b460042017-04-14 16:24:45 -070081 }));
82
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020083 describe('when chameleon is not responding', () => {
84 beforeEach(inject(($q: ng.IQService) => {
Matteo Scandolobd60dee2017-05-08 17:53:25 -070085 loaded = false;
Matteo Scandolobd60dee2017-05-08 17:53:25 -070086 authenticated = true;
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020087 MockDiscover.discover = jasmine.createSpy('discover')
88 .and.callFake(() => {
89 const d = $q.defer();
90 d.resolve('chameleon');
91 return d.promise;
92 });
93 compileElement();
94 spyOn(isolatedScope, 'moveOnTo');
95 isolatedScope.run();
96 }));
97
98 it('should print an error', () => {
99 expect(isolatedScope.moveOnTo).not.toHaveBeenCalled();
100 expect(isolatedScope.error).toBe('chameleon');
101 expect(isolatedScope.loader).toBeFalsy();
Matteo Scandolobd60dee2017-05-08 17:53:25 -0700102 });
103 });
104
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200105 describe('when chameleon is available', () => {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700106
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200107 beforeEach(inject(($q: ng.IQService) => {
108 loaded = true;
109 authenticated = true;
110 MockDiscover.discover = jasmine.createSpy('discover')
111 .and.callFake(() => {
112 const d = $q.defer();
113 d.resolve(true);
114 return d.promise;
115 });
116
117 MockOnboarder.onboard = jasmine.createSpy('onboard')
118 .and.callFake(() => {
119 const d = $q.defer();
120 d.resolve();
121 return d.promise;
122 });
123 }));
124
125 describe('when models are already loaded', () => {
126
127 beforeEach(() => {
128 compileElement();
129 spyOn(isolatedScope, 'moveOnTo');
130 isolatedScope.run();
131 timeout.flush();
132 });
133
134 it('should redirect to the last visited page', (done) => {
135 window.setTimeout(() => {
136 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/test');
137 expect(location.path).toHaveBeenCalledWith('/test');
138 done();
139 }, 600);
140 });
Matteo Scandolo9b460042017-04-14 16:24:45 -0700141 });
142
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200143 describe('when the last visited page is "loader"', () => {
144
145 beforeEach(() => {
146 MockConfig.lastVisitedUrl = '/loader';
147 compileElement();
148 spyOn(isolatedScope, 'moveOnTo');
149 isolatedScope.run();
150 });
151
152 it('should redirect to the "dashboard" page', (done) => {
153 window.setTimeout(() => {
154 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/loader');
155 expect(location.path).toHaveBeenCalledWith('/dashboard');
156 done();
157 }, 600);
158 });
159 });
160
161 describe('when user is not authenticated', () => {
162
163 beforeEach(() => {
164 loaded = false;
165 authenticated = false;
166 compileElement();
167 isolatedScope.run();
168 });
169
170 it('should redirect to the login page', () => {
171 expect(MockState.go).toHaveBeenCalledWith('xos.login');
172 });
173
174 afterEach(() => {
175 authenticated = true;
176 });
177 });
178
179 describe('when models are not loaded', () => {
180
181 beforeEach(() => {
182 loaded = false;
183 authenticated = true;
184 compileElement();
185 spyOn(isolatedScope, 'moveOnTo');
186 });
187
188 it('should call XosModelDiscoverer.discover', () => {
189 expect(MockDiscover.discover).toHaveBeenCalled();
190 });
Matteo Scandolo9b460042017-04-14 16:24:45 -0700191 });
192 });
193
194});