Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-ui-router'; |
| 4 | import * as $ from 'jquery'; |
| 5 | import {XosComponentInjector, IXosComponentInjectorService} from './component-injector.helpers'; |
| 6 | |
| 7 | let service: IXosComponentInjectorService; |
| 8 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService; |
| 9 | |
| 10 | describe('The XosComponentInjector service', () => { |
| 11 | beforeEach(() => { |
| 12 | angular |
| 13 | .module('test', []) |
| 14 | .component('extension', { |
| 15 | template: 'extended' |
| 16 | }) |
| 17 | .component('target', { |
| 18 | template: `<div id="target"></div>` |
| 19 | }) |
| 20 | .service('XosComponentInjector', XosComponentInjector); |
| 21 | |
| 22 | angular.mock.module('test'); |
| 23 | }); |
| 24 | |
| 25 | beforeEach(angular.mock.inject(( |
| 26 | XosComponentInjector: IXosComponentInjectorService, |
| 27 | ) => { |
| 28 | service = XosComponentInjector; |
| 29 | })); |
| 30 | |
| 31 | beforeEach(angular.mock.inject(($rootScope: ng.IRootScopeService, $compile: ng.ICompileService) => { |
| 32 | scope = $rootScope; |
| 33 | compile = $compile; |
| 34 | element = $compile('<target></target>')($rootScope); |
| 35 | $rootScope.$digest(); |
| 36 | })); |
| 37 | |
| 38 | it('should have an InjectComponent method', () => { |
| 39 | expect(service.injectComponent).toBeDefined(); |
| 40 | }); |
| 41 | |
| 42 | it('should have an removeInjectedComponents method', () => { |
| 43 | expect(service.removeInjectedComponents).toBeDefined(); |
| 44 | }); |
| 45 | |
| 46 | it('should add a component to the target container', () => { |
| 47 | service.injectComponent($('#target', element), 'extension'); |
| 48 | scope.$apply(); |
| 49 | const extension = $('extension', element); |
| 50 | expect(extension.text()).toBe('extended'); |
| 51 | }); |
| 52 | }); |