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, |
| 32 | discover: null |
| 33 | }; |
| 34 | |
| 35 | const MockOnboarder = { |
| 36 | onboard: null |
| 37 | }; |
| 38 | |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 39 | const MockAuth = { |
| 40 | isAuthenticated: jasmine.createSpy('isAuthenticated') |
| 41 | .and.callFake(() => authenticated) |
| 42 | }; |
| 43 | |
| 44 | const MockState = { |
| 45 | go: jasmine.createSpy('state.go') |
| 46 | }; |
| 47 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 48 | describe('The XosLoader component', () => { |
| 49 | beforeEach(() => { |
| 50 | angular |
| 51 | .module('loader', []) |
| 52 | .value('XosConfig', MockConfig) |
| 53 | .value('XosModelDiscoverer', MockDiscover) |
| 54 | .value('XosOnboarder', MockOnboarder) |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 55 | .value('AuthService', MockAuth) |
| 56 | .value('$state', MockState) |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 57 | .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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 80 | })); |
| 81 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame^] | 82 | describe('when chameleon is not responding', () => { |
| 83 | beforeEach(inject(($q: ng.IQService) => { |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 84 | loaded = false; |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 85 | authenticated = true; |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame^] | 86 | 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 Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 101 | }); |
| 102 | }); |
| 103 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame^] | 104 | describe('when chameleon is available', () => { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 105 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame^] | 106 | 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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 140 | }); |
| 141 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame^] | 142 | 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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 190 | }); |
| 191 | }); |
| 192 | |
| 193 | }); |