|
| 1 | +angular.module('patternfly.pfTranscludeTestHelper',['patternfly.utils']) |
| 2 | + .controller( 'TestUtilCtrl', function($scope) { |
| 3 | + }) |
| 4 | + .config(function($provide){ |
| 5 | + $provide.decorator('ngTranscludeDirective', ['$delegate', function($delegate) { |
| 6 | + // Remove the original directive |
| 7 | + $delegate.shift(); |
| 8 | + return $delegate; |
| 9 | + }]); |
| 10 | + }) |
| 11 | + .directive( 'transcludeSibling', function() { |
| 12 | + return { |
| 13 | + restrict: 'EAC', |
| 14 | + transclude: true, |
| 15 | + scope: {}, |
| 16 | + template: |
| 17 | + '<div>' + |
| 18 | + ' <input id="directiveId" value="{{$id}}">' + |
| 19 | + ' <div pf-transclude></div>' + |
| 20 | + '</div>' |
| 21 | + } |
| 22 | + }) |
| 23 | + .directive( 'transcludeParent', function() { |
| 24 | + return { |
| 25 | + restrict: 'EAC', |
| 26 | + transclude: true, |
| 27 | + scope: {}, |
| 28 | + template: |
| 29 | + '<div>' + |
| 30 | + ' <input id="directiveId" value="{{$id}}">' + |
| 31 | + ' <div pf-transclude="parent"></div>' + |
| 32 | + '</div>' |
| 33 | + } |
| 34 | + }) |
| 35 | + .directive( 'transcludeChild', function() { |
| 36 | + return { |
| 37 | + restrict: 'EAC', |
| 38 | + transclude: true, |
| 39 | + scope: {}, |
| 40 | + template: |
| 41 | + '<div>' + |
| 42 | + ' <input id="directiveId" value="{{$id}}">' + |
| 43 | + ' <div pf-transclude="child"></div>' + |
| 44 | + '</div>' |
| 45 | + } |
| 46 | + }) |
| 47 | +; |
| 48 | + |
| 49 | +describe('Directive: pfTransclude', function () { |
| 50 | + var $scope; |
| 51 | + var $compile; |
| 52 | + var outerId; |
| 53 | + var directiveId; |
| 54 | + var childId; |
| 55 | + var parentId; |
| 56 | + |
| 57 | + // load the controller's module |
| 58 | + beforeEach(function () { |
| 59 | + module('patternfly.utils'); |
| 60 | + module('patternfly.pfTranscludeTestHelper'); |
| 61 | + }); |
| 62 | + |
| 63 | + beforeEach(inject(function (_$compile_, _$rootScope_) { |
| 64 | + $compile = _$compile_; |
| 65 | + $scope = _$rootScope_; |
| 66 | + })); |
| 67 | + |
| 68 | + var compileHTML = function (transludeDirective, scope) { |
| 69 | + var markup = '<div ng-controller="TestUtilCtrl" >' + |
| 70 | + ' <input id="outerId" value="{{$id}}">{{$id}}</input>' + |
| 71 | + ' <div ' + transludeDirective + ' class="pf-transclude-example">' + |
| 72 | + ' <input id="childId" value="{{$id}}">' + |
| 73 | + ' <input id="parentId" value="{{$parent.$id}}">' + |
| 74 | + ' </div>' + |
| 75 | + '</div'; |
| 76 | + var element = $compile(angular.element(markup))(scope); |
| 77 | + scope.$digest(); |
| 78 | + |
| 79 | + outerId = parseInt(element.find('#outerId')[0].value); |
| 80 | + directiveId = parseInt(element.find('#directiveId')[0].value); |
| 81 | + childId = parseInt(element.find('#childId')[0].value); |
| 82 | + parentId = parseInt(element.find('#parentId')[0].value); |
| 83 | + }; |
| 84 | + |
| 85 | + it('should have correct sibling scope', function () { |
| 86 | + compileHTML('transclude-sibling', $scope); |
| 87 | + |
| 88 | + expect(directiveId).toBe(outerId + 1); |
| 89 | + expect(childId).toBe(directiveId + 1); |
| 90 | + expect(parentId).toBe(outerId + 1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should have correct parent scope', function () { |
| 94 | + compileHTML('transclude-parent', $scope); |
| 95 | + |
| 96 | + expect(directiveId).toBe(outerId + 1); |
| 97 | + expect(childId).toBe(directiveId); |
| 98 | + expect(parentId).toBe(outerId); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should have correct child scope', function () { |
| 102 | + compileHTML('transclude-child', $scope); |
| 103 | + |
| 104 | + expect(directiveId).toBe(outerId + 1); |
| 105 | + expect(childId).toBe(directiveId + 1); |
| 106 | + expect(parentId).toBe(directiveId); |
| 107 | + }); |
| 108 | +}); |
0 commit comments