blob: 44fc60fa7fc6740be3b343d05b35cbbd2d61c552 [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');
80
81 MockDiscover.discover = jasmine.createSpy('discover')
82 .and.callFake(() => {
83 const d = $q.defer();
84 d.resolve(true);
85 return d.promise;
86 });
87
88 MockOnboarder.onboard = jasmine.createSpy('onboard')
89 .and.callFake(() => {
90 const d = $q.defer();
91 d.resolve();
92 return d.promise;
93 });
94 }));
95
96 describe('when models are already loaded', () => {
97
98 beforeEach(() => {
99 compileElement();
100 spyOn(isolatedScope, 'moveOnTo');
101 isolatedScope.run();
102 timeout.flush();
103 });
104
105 it('should redirect to the last visited page', (done) => {
106 window.setTimeout(() => {
107 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/test');
108 expect(location.path).toHaveBeenCalledWith('/test');
109 done();
110 }, 600);
111 });
112 });
113
114 describe('when the last visited page is "loader"', () => {
115
116 beforeEach(() => {
117 MockConfig.lastVisitedUrl = '/loader';
118 compileElement();
119 spyOn(isolatedScope, 'moveOnTo');
120 isolatedScope.run();
121 });
122
123 it('should redirect to the "dashboard" page', (done) => {
124 window.setTimeout(() => {
125 expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/loader');
126 expect(location.path).toHaveBeenCalledWith('/dashboard');
127 done();
128 }, 600);
129 });
130 });
131
Matteo Scandolobd60dee2017-05-08 17:53:25 -0700132 describe('when user is not authenticated', () => {
133
134 beforeEach(() => {
135 loaded = false;
136 authenticated = false;
137 compileElement();
138 isolatedScope.run();
139 });
140
141 it('should redirect to the login page', () => {
142 expect(MockState.go).toHaveBeenCalledWith('xos.login');
143 });
144
145 afterEach(() => {
146 authenticated = true;
147 });
148 });
149
Matteo Scandolo9b460042017-04-14 16:24:45 -0700150 describe('when models are not loaded', () => {
151
152 beforeEach(() => {
153 loaded = false;
154 compileElement();
155 spyOn(isolatedScope, 'moveOnTo');
156 });
157
158 it('should call XosModelDiscoverer.discover', () => {
159 expect(MockDiscover.discover).toHaveBeenCalled();
160 });
161 });
162
163});