blob: 0744c11ae7d4567448194e454350f46c02356869 [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,
32 discover: null
33};
34
35const MockOnboarder = {
36 onboard: null
37};
38
Matteo Scandolobd60dee2017-05-08 17:53:25 -070039const MockAuth = {
40 isAuthenticated: jasmine.createSpy('isAuthenticated')
41 .and.callFake(() => authenticated)
42};
43
44const MockState = {
45 go: jasmine.createSpy('state.go')
46};
47
Matteo Scandolo9b460042017-04-14 16:24:45 -070048describe('The XosLoader component', () => {
49 beforeEach(() => {
50 angular
51 .module('loader', [])
52 .value('XosConfig', MockConfig)
53 .value('XosModelDiscoverer', MockDiscover)
54 .value('XosOnboarder', MockOnboarder)
Matteo Scandolobd60dee2017-05-08 17:53:25 -070055 .value('AuthService', MockAuth)
56 .value('$state', MockState)
Matteo Scandolo9b460042017-04-14 16:24:45 -070057 .component('xosLoader', xosLoader);
58 angular.mock.module('loader');
59 });
60
61 let scope, element, isolatedScope, rootScope, compile, timeout, location;
62 const compileElement = () => {
63
64 if (!scope) {
65 scope = rootScope.$new();
66 }
67
68 element = angular.element('<xos-loader></xos-loader>');
69 compile(element)(scope);
70 scope.$digest();
71 isolatedScope = element.isolateScope().vm;
72 };
73
74 beforeEach(inject(function ($q: ng.IQService, $compile: ng.ICompileService, $rootScope: ng.IScope, $timeout: ng.ITimeoutService, $location: ng.ILocationService) {
75 compile = $compile;
76 rootScope = $rootScope;
77 timeout = $timeout;
78 location = $location;
79 spyOn(location, 'path');
Matteo Scandolo9b460042017-04-14 16:24:45 -070080 }));
81
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020082 describe('when chameleon is not responding', () => {
83 beforeEach(inject(($q: ng.IQService) => {
Matteo Scandolobd60dee2017-05-08 17:53:25 -070084 loaded = false;
Matteo Scandolobd60dee2017-05-08 17:53:25 -070085 authenticated = true;
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020086 MockDiscover.discover = jasmine.createSpy('discover')
87 .and.callFake(() => {
88 const d = $q.defer();
89 d.resolve('chameleon');
90 return d.promise;
91 });
92 compileElement();
93 spyOn(isolatedScope, 'moveOnTo');
94 isolatedScope.run();
95 }));
96
97 it('should print an error', () => {
98 expect(isolatedScope.moveOnTo).not.toHaveBeenCalled();
99 expect(isolatedScope.error).toBe('chameleon');
100 expect(isolatedScope.loader).toBeFalsy();
Matteo Scandolobd60dee2017-05-08 17:53:25 -0700101 });
102 });
103
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200104 describe('when chameleon is available', () => {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700105
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200106 beforeEach(inject(($q: ng.IQService) => {
107 loaded = true;
108 authenticated = true;
109 MockDiscover.discover = jasmine.createSpy('discover')
110 .and.callFake(() => {
111 const d = $q.defer();
112 d.resolve(true);
113 return d.promise;
114 });
115
116 MockOnboarder.onboard = jasmine.createSpy('onboard')
117 .and.callFake(() => {
118 const d = $q.defer();
119 d.resolve();
120 return d.promise;
121 });
122 }));
123
124 describe('when models are already loaded', () => {
125
126 beforeEach(() => {
127 compileElement();
128 spyOn(isolatedScope, 'moveOnTo');
129 isolatedScope.run();
130 timeout.flush();
131 });
132
133 it('should redirect to the last visited page', (done) => {
134 window.setTimeout(() => {
135 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/test');
136 expect(location.path).toHaveBeenCalledWith('/test');
137 done();
138 }, 600);
139 });
Matteo Scandolo9b460042017-04-14 16:24:45 -0700140 });
141
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200142 describe('when the last visited page is "loader"', () => {
143
144 beforeEach(() => {
145 MockConfig.lastVisitedUrl = '/loader';
146 compileElement();
147 spyOn(isolatedScope, 'moveOnTo');
148 isolatedScope.run();
149 });
150
151 it('should redirect to the "dashboard" page', (done) => {
152 window.setTimeout(() => {
153 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/loader');
154 expect(location.path).toHaveBeenCalledWith('/dashboard');
155 done();
156 }, 600);
157 });
158 });
159
160 describe('when user is not authenticated', () => {
161
162 beforeEach(() => {
163 loaded = false;
164 authenticated = false;
165 compileElement();
166 isolatedScope.run();
167 });
168
169 it('should redirect to the login page', () => {
170 expect(MockState.go).toHaveBeenCalledWith('xos.login');
171 });
172
173 afterEach(() => {
174 authenticated = true;
175 });
176 });
177
178 describe('when models are not loaded', () => {
179
180 beforeEach(() => {
181 loaded = false;
182 authenticated = true;
183 compileElement();
184 spyOn(isolatedScope, 'moveOnTo');
185 });
186
187 it('should call XosModelDiscoverer.discover', () => {
188 expect(MockDiscover.discover).toHaveBeenCalled();
189 });
Matteo Scandolo9b460042017-04-14 16:24:45 -0700190 });
191 });
192
193});