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 | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-ui-router'; |
| 22 | import * as $ from 'jquery'; |
| 23 | import {XosComponentInjector, IXosComponentInjectorService} from './component-injector.helpers'; |
| 24 | |
| 25 | let service: IXosComponentInjectorService; |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 26 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, $state: ng.ui.IStateService; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 27 | |
| 28 | describe('The XosComponentInjector service', () => { |
| 29 | beforeEach(() => { |
| 30 | angular |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 31 | .module('test', ['ui.router']) |
| 32 | .config(( |
| 33 | $stateProvider: ng.ui.IStateProvider, |
| 34 | $urlRouterProvider: ng.ui.IUrlRouterProvider |
| 35 | ) => { |
| 36 | $stateProvider |
| 37 | .state('empty', { |
| 38 | url: '/empty', |
| 39 | template: 'empty template', |
| 40 | }) |
| 41 | .state('home', { |
| 42 | url: '/', |
| 43 | component: 'target', |
| 44 | }); |
| 45 | $urlRouterProvider.otherwise('/'); |
| 46 | }) |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 47 | .component('extension', { |
| 48 | template: 'extended' |
| 49 | }) |
| 50 | .component('target', { |
| 51 | template: `<div id="target"></div>` |
| 52 | }) |
| 53 | .service('XosComponentInjector', XosComponentInjector); |
| 54 | |
| 55 | angular.mock.module('test'); |
| 56 | }); |
| 57 | |
| 58 | beforeEach(angular.mock.inject(( |
| 59 | XosComponentInjector: IXosComponentInjectorService, |
| 60 | ) => { |
| 61 | service = XosComponentInjector; |
| 62 | })); |
| 63 | |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 64 | beforeEach(angular.mock.inject(( |
| 65 | $rootScope: ng.IRootScopeService, |
| 66 | $compile: ng.ICompileService, |
| 67 | _$state_: ng.ui.IStateService |
| 68 | ) => { |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 69 | scope = $rootScope; |
| 70 | compile = $compile; |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 71 | $state = _$state_; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 72 | element = $compile('<target></target>')($rootScope); |
| 73 | $rootScope.$digest(); |
| 74 | })); |
| 75 | |
| 76 | it('should have an InjectComponent method', () => { |
| 77 | expect(service.injectComponent).toBeDefined(); |
| 78 | }); |
| 79 | |
| 80 | it('should have an removeInjectedComponents method', () => { |
| 81 | expect(service.removeInjectedComponents).toBeDefined(); |
| 82 | }); |
| 83 | |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 84 | xit('should add a component to the target container', () => { |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 85 | service.injectComponent($('#target', element), 'extension'); |
| 86 | scope.$apply(); |
| 87 | const extension = $('extension', element); |
| 88 | expect(extension.text()).toBe('extended'); |
| 89 | }); |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 90 | |
| 91 | it('should should store an injected components', () => { |
| 92 | spyOn(service, 'storeInjectedComponent').and.callThrough(); |
| 93 | service.injectComponent($('#target', element), 'extension'); |
| 94 | scope.$apply(); |
| 95 | expect(service['storeInjectedComponent']).toHaveBeenCalled(); |
| 96 | expect(service.injectedComponents.length).toBe(1); |
| 97 | }); |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 98 | }); |