Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import {xosLoader} from './loader'; |
| 22 | |
| 23 | let loaded = true; |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 24 | let authenticated = true; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 25 | |
| 26 | const MockConfig = { |
| 27 | lastVisitedUrl: '/test' |
| 28 | }; |
| 29 | |
| 30 | const MockDiscover = { |
| 31 | areModelsLoaded: () => loaded, |
Matteo Scandolo | bf4e840 | 2019-06-24 12:22:49 -0700 | [diff] [blame] | 32 | getStatusMessage: () => 'Test Message', |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 33 | discover: null |
| 34 | }; |
| 35 | |
| 36 | const MockOnboarder = { |
| 37 | onboard: null |
| 38 | }; |
| 39 | |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 40 | const MockAuth = { |
| 41 | isAuthenticated: jasmine.createSpy('isAuthenticated') |
| 42 | .and.callFake(() => authenticated) |
| 43 | }; |
| 44 | |
| 45 | const MockState = { |
| 46 | go: jasmine.createSpy('state.go') |
| 47 | }; |
| 48 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 49 | describe('The XosLoader component', () => { |
| 50 | beforeEach(() => { |
| 51 | angular |
| 52 | .module('loader', []) |
| 53 | .value('XosConfig', MockConfig) |
| 54 | .value('XosModelDiscoverer', MockDiscover) |
| 55 | .value('XosOnboarder', MockOnboarder) |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 56 | .value('AuthService', MockAuth) |
| 57 | .value('$state', MockState) |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 58 | .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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 81 | })); |
| 82 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 83 | describe('when chameleon is not responding', () => { |
| 84 | beforeEach(inject(($q: ng.IQService) => { |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 85 | loaded = false; |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 86 | authenticated = true; |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 87 | 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 Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 102 | }); |
| 103 | }); |
| 104 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 105 | describe('when chameleon is available', () => { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 106 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 107 | 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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 141 | }); |
| 142 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 143 | 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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 191 | }); |
| 192 | }); |
| 193 | |
| 194 | }); |