blob: edec97c1d85bb6259665804813bbead1863ada72 [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 Scandolo4222a432017-01-23 12:18:40 -080019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-ui-router';
22import * as $ from 'jquery';
23import {XosComponentInjector, IXosComponentInjectorService} from './component-injector.helpers';
24
25let service: IXosComponentInjectorService;
Matteo Scandoloe2643b92017-01-31 14:40:33 -080026let element, scope: angular.IRootScopeService, compile: ng.ICompileService, $state: ng.ui.IStateService;
Matteo Scandolo4222a432017-01-23 12:18:40 -080027
28describe('The XosComponentInjector service', () => {
29 beforeEach(() => {
30 angular
Matteo Scandoloe2643b92017-01-31 14:40:33 -080031 .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 Scandolo4222a432017-01-23 12:18:40 -080047 .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 Scandoloe2643b92017-01-31 14:40:33 -080064 beforeEach(angular.mock.inject((
65 $rootScope: ng.IRootScopeService,
66 $compile: ng.ICompileService,
67 _$state_: ng.ui.IStateService
68 ) => {
Matteo Scandolo4222a432017-01-23 12:18:40 -080069 scope = $rootScope;
70 compile = $compile;
Matteo Scandoloe2643b92017-01-31 14:40:33 -080071 $state = _$state_;
Matteo Scandolo4222a432017-01-23 12:18:40 -080072 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 Scandoloe2643b92017-01-31 14:40:33 -080084 xit('should add a component to the target container', () => {
Matteo Scandolo4222a432017-01-23 12:18:40 -080085 service.injectComponent($('#target', element), 'extension');
86 scope.$apply();
87 const extension = $('extension', element);
88 expect(extension.text()).toBe('extended');
89 });
Matteo Scandoloe2643b92017-01-31 14:40:33 -080090
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 Scandolo4222a432017-01-23 12:18:40 -080098});