|
2 | 2 |
|
3 | 3 | describe('as', function() {
|
4 | 4 |
|
5 |
| - it('should bind in the current scope the controller of a component', function() { |
6 |
| - var expected = {some: 'value'}; |
7 |
| - |
8 |
| - angular.module('my', []).component('myComponent', { |
9 |
| - template: 'foo', |
10 |
| - controller: function() { |
11 |
| - this.property = expected; |
12 |
| - } |
13 |
| - }); |
14 |
| - module('my'); |
| 5 | + describe('given a component', function() { |
15 | 6 |
|
16 |
| - inject(function($compile, $rootScope) { |
17 |
| - var scope = $rootScope.$new(); |
| 7 | + var myComponentController, $rootScope, $compile; |
| 8 | + |
| 9 | + beforeEach(module(function($compileProvider) { |
| 10 | + $compileProvider.component('myComponent', { |
| 11 | + template: 'foo', |
| 12 | + controller: function() { |
| 13 | + myComponentController = this; |
| 14 | + } |
| 15 | + }); |
| 16 | + })); |
| 17 | + |
| 18 | + beforeEach(inject(function(_$compile_, _$rootScope_) { |
| 19 | + $rootScope = _$rootScope_; |
| 20 | + $compile = _$compile_; |
| 21 | + })); |
| 22 | + |
| 23 | + it('should bind in the current scope the controller of a component', function() { |
18 | 24 | var $ctrl = {undamaged: true};
|
19 |
| - scope.$ctrl = $ctrl; |
20 |
| - dealoc($compile('<my-component as="$ctrl.myComponent"></my-component>')(scope)); |
21 |
| - |
22 |
| - expect(scope.$ctrl).toBe($ctrl); |
23 |
| - expect(scope.$ctrl.undamaged).toBe(true); |
24 |
| - expect($ctrl.myComponent).not.toBeUndefined(); |
25 |
| - expect($ctrl.myComponent && $ctrl.myComponent.property).toBe(expected); |
26 |
| - scope.$destroy(); |
| 25 | + $rootScope.$ctrl = $ctrl; |
| 26 | + |
| 27 | + $compile('<my-component as="$ctrl.myComponent"></my-component>')($rootScope); |
| 28 | + expect($rootScope.$ctrl).toBe($ctrl); |
| 29 | + expect($rootScope.$ctrl.undamaged).toBe(true); |
| 30 | + expect($ctrl.myComponent).toBe(myComponentController); |
27 | 31 | });
|
28 |
| - }); |
29 | 32 |
|
30 |
| - it('should be parametrized with any variable', function() { |
31 |
| - var myComponentController; |
| 33 | + it('should be parametrized with any variable', function() { |
| 34 | + $compile('<my-component as="bar.myComponent"></my-component>')($rootScope); |
| 35 | + expect($rootScope.bar.myComponent).toBe(myComponentController); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should work with non normalized versions of the entity', function() { |
| 39 | + $compile('<my:component as="$ctrl.myComponent1"></my:component>')($rootScope); |
| 40 | + expect($rootScope.$ctrl.myComponent1).toBe(myComponentController); |
32 | 41 |
|
33 |
| - angular.module('my', []).component('myComponent', { |
34 |
| - template: 'foo', |
35 |
| - controller: function() { |
36 |
| - myComponentController = this; |
37 |
| - } |
| 42 | + $compile('<data-my-component as="$ctrl.myComponent2"></data-my-component>')($rootScope); |
| 43 | + expect($rootScope.$ctrl.myComponent2).toBe(myComponentController); |
| 44 | + |
| 45 | + $compile('<x-my-component as="$ctrl.myComponent3"></x-my-component>')($rootScope); |
| 46 | + expect($rootScope.$ctrl.myComponent3).toBe(myComponentController); |
38 | 47 | });
|
39 |
| - module('my'); |
40 | 48 |
|
41 |
| - inject(function($compile, $rootScope) { |
42 |
| - var scope = $rootScope.$new(); |
43 |
| - dealoc($compile('<my-component as="bar.myComponent"></my-component>')(scope)); |
| 49 | + it('should work with non normalized versions of as attribute', function() { |
| 50 | + $compile('<my-component data-as="$ctrl.myComponent1"></my-component>')($rootScope); |
| 51 | + expect($rootScope.$ctrl.myComponent1).toBe(myComponentController); |
44 | 52 |
|
45 |
| - expect(scope.bar).not.toBeUndefined(); |
46 |
| - expect(scope.bar && scope.bar.myComponent).toBe(myComponentController); |
47 |
| - scope.$destroy(); |
| 53 | + $compile('<my-component x-as="$ctrl.myComponent2"></my-component>')($rootScope); |
| 54 | + expect($rootScope.$ctrl.myComponent2).toBe(myComponentController); |
48 | 55 | });
|
| 56 | + |
49 | 57 | });
|
50 | 58 |
|
51 |
| - it('should be compatible with entity directives with controller', function() { |
| 59 | + it('should be compatible with directives on entities with controller', function() { |
52 | 60 | var myDirectiveController;
|
53 | 61 |
|
54 |
| - angular.module('my', []).directive('myDirective', function() { |
55 |
| - return { |
56 |
| - restrict: 'E', |
57 |
| - controller: function() { |
58 |
| - myDirectiveController = this; |
59 |
| - } |
60 |
| - }; |
| 62 | + module(function($compileProvider) { |
| 63 | + $compileProvider.directive('myDirective', function() { |
| 64 | + return { |
| 65 | + controller: function() { |
| 66 | + myDirectiveController = this; |
| 67 | + } |
| 68 | + }; |
| 69 | + }); |
61 | 70 | });
|
62 |
| - module('my'); |
63 | 71 |
|
64 | 72 | inject(function($compile, $rootScope) {
|
65 |
| - var scope = $rootScope.$new(); |
66 |
| - dealoc($compile('<my-directive as="bar.myDirective"></my-directive>')(scope)); |
| 73 | + $compile('<my-directive as="bar.myDirective"></my-directive>')($rootScope); |
67 | 74 |
|
68 |
| - expect(scope.bar).not.toBeUndefined(); |
69 |
| - expect(scope.bar && scope.bar.myDirective).toBe(myDirectiveController); |
70 |
| - scope.$destroy(); |
| 75 | + expect($rootScope.bar.myDirective).toBe(myDirectiveController); |
71 | 76 | });
|
72 | 77 | });
|
73 | 78 |
|
74 |
| - it('should bind non HTMLElements elements into the current scope if no component', inject(function($compile, $rootScope) { |
75 |
| - var scope = $rootScope.$new(); |
76 |
| - var $ctrl = {}; |
77 |
| - scope.$ctrl = $ctrl; |
78 |
| - var element = $compile('<div as="$ctrl.myDiv">SUCCESS</div>')(scope); |
79 |
| - |
80 |
| - expect(scope.$ctrl).toBe($ctrl); |
81 |
| - expect($ctrl.myDiv).not.toBeUndefined(); |
82 |
| - expect($ctrl.myDiv && $ctrl.myDiv.textContent).toBe('SUCCESS'); // https://jsperf.com/innerhtml-vs-innertext/49 |
83 |
| - dealoc(element); |
84 |
| - scope.$destroy(); |
85 |
| - })); |
86 |
| - |
87 | 79 | });
|
0 commit comments