blob: cb6ee6d6d5c5dba23f70aa30177e26005ae0e48d [file] [log] [blame]
Matteo Scandolo4222a432017-01-23 12:18:40 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-ui-router';
4import * as $ from 'jquery';
5import {XosComponentInjector, IXosComponentInjectorService} from './component-injector.helpers';
6
7let service: IXosComponentInjectorService;
Matteo Scandoloe2643b92017-01-31 14:40:33 -08008let element, scope: angular.IRootScopeService, compile: ng.ICompileService, $state: ng.ui.IStateService;
Matteo Scandolo4222a432017-01-23 12:18:40 -08009
10describe('The XosComponentInjector service', () => {
11 beforeEach(() => {
12 angular
Matteo Scandoloe2643b92017-01-31 14:40:33 -080013 .module('test', ['ui.router'])
14 .config((
15 $stateProvider: ng.ui.IStateProvider,
16 $urlRouterProvider: ng.ui.IUrlRouterProvider
17 ) => {
18 $stateProvider
19 .state('empty', {
20 url: '/empty',
21 template: 'empty template',
22 })
23 .state('home', {
24 url: '/',
25 component: 'target',
26 });
27 $urlRouterProvider.otherwise('/');
28 })
Matteo Scandolo4222a432017-01-23 12:18:40 -080029 .component('extension', {
30 template: 'extended'
31 })
32 .component('target', {
33 template: `<div id="target"></div>`
34 })
35 .service('XosComponentInjector', XosComponentInjector);
36
37 angular.mock.module('test');
38 });
39
40 beforeEach(angular.mock.inject((
41 XosComponentInjector: IXosComponentInjectorService,
42 ) => {
43 service = XosComponentInjector;
44 }));
45
Matteo Scandoloe2643b92017-01-31 14:40:33 -080046 beforeEach(angular.mock.inject((
47 $rootScope: ng.IRootScopeService,
48 $compile: ng.ICompileService,
49 _$state_: ng.ui.IStateService
50 ) => {
Matteo Scandolo4222a432017-01-23 12:18:40 -080051 scope = $rootScope;
52 compile = $compile;
Matteo Scandoloe2643b92017-01-31 14:40:33 -080053 $state = _$state_;
Matteo Scandolo4222a432017-01-23 12:18:40 -080054 element = $compile('<target></target>')($rootScope);
55 $rootScope.$digest();
56 }));
57
58 it('should have an InjectComponent method', () => {
59 expect(service.injectComponent).toBeDefined();
60 });
61
62 it('should have an removeInjectedComponents method', () => {
63 expect(service.removeInjectedComponents).toBeDefined();
64 });
65
Matteo Scandoloe2643b92017-01-31 14:40:33 -080066 xit('should add a component to the target container', () => {
Matteo Scandolo4222a432017-01-23 12:18:40 -080067 service.injectComponent($('#target', element), 'extension');
68 scope.$apply();
69 const extension = $('extension', element);
70 expect(extension.text()).toBe('extended');
71 });
Matteo Scandoloe2643b92017-01-31 14:40:33 -080072
73 it('should should store an injected components', () => {
74 spyOn(service, 'storeInjectedComponent').and.callThrough();
75 service.injectComponent($('#target', element), 'extension');
76 scope.$apply();
77 expect(service['storeInjectedComponent']).toHaveBeenCalled();
78 expect(service.injectedComponents.length).toBe(1);
79 });
Matteo Scandolo4222a432017-01-23 12:18:40 -080080});