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; |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 8 | let element, scope: angular.IRootScopeService, compile: ng.ICompileService, $state: ng.ui.IStateService; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 9 | |
| 10 | describe('The XosComponentInjector service', () => { |
| 11 | beforeEach(() => { |
| 12 | angular |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 13 | .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 Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 29 | .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 Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 46 | beforeEach(angular.mock.inject(( |
| 47 | $rootScope: ng.IRootScopeService, |
| 48 | $compile: ng.ICompileService, |
| 49 | _$state_: ng.ui.IStateService |
| 50 | ) => { |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 51 | scope = $rootScope; |
| 52 | compile = $compile; |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 53 | $state = _$state_; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 54 | 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 Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 66 | xit('should add a component to the target container', () => { |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 67 | service.injectComponent($('#target', element), 'extension'); |
| 68 | scope.$apply(); |
| 69 | const extension = $('extension', element); |
| 70 | expect(extension.text()).toBe('extended'); |
| 71 | }); |
Matteo Scandolo | e2643b9 | 2017-01-31 14:40:33 -0800 | [diff] [blame] | 72 | |
| 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 Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 80 | }); |