-
Notifications
You must be signed in to change notification settings - Fork 0
/
NgDirectiveWithIsolatedScopeAndTransclude.test.js
99 lines (81 loc) · 4.19 KB
/
NgDirectiveWithIsolatedScopeAndTransclude.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
describe('Inspector abilities for ngDirective({scope:{...}}) scope behavior', function () {
"use strict";
var $rootScope, Inspector, InspectorHelpers;
beforeEach(function () {
module('inspector-test-helpers');
module('scope-aware');
});
beforeEach(inject(function (_$rootScope_, _Inspector_, _InspectorHelpers_) {
$rootScope = _$rootScope_;
Inspector = _Inspector_;
InspectorHelpers = _InspectorHelpers_;
}));
describe('when primitives are used', function() {
var $scope, element;
beforeEach(inject(function($compile) {
$scope = $rootScope.$new();
$scope.token = 'token';
$scope.alienToken = 'alienToken';
$scope.alienTokenObj = {token : 'token_value'};
element = InspectorHelpers.createDirective('directiveIsolatedScopeWithPrimitiveAndObjectWithTransclude', $scope);
}));
afterEach(function() {
console.log('**************');
console.log(Inspector.inspect($scope));
});
it("doesn't shadow primitives because isolated scope is used", function () {
var input = element.find('input')[0];
var scope = angular.element(input).scope();
angular.element(input).val('AAA').triggerHandler('input');
$scope.$digest();
//Note: transclude will create a new child scope which is child of the isolate scope
//TODO: copy it from angular JS page
expect(scope.$parent.$parent).toBe($scope);
//the value of token in the parent scope is still 'a' because there token is referenced by '@' (string)
// and therefore not changed in the child scope
expect(scope.token).toBe('AAA');
expect($scope.token).toBe('token');
//additional tests (ng-scope-aware)
expect(scope).toHaveMembers('token', 'alienToken', 'alienTokenObj');
//token is not inherited because it's shadowed
expect(scope).toHaveInheritedMembers('alienToken', 'alienTokenObj');
expect(scope).toShadow('token');
});
});
describe('when objects are used', function() {
var $scope, element;
beforeEach(function() {
$scope = $rootScope.$new();
$scope.token = 'token';
$scope.tokenObj = {token : 'from_token_obj'};
$scope.alienToken = 'alienToken';
$scope.alienTokenObj = {token : 'from_alien_token_obj'};
element = InspectorHelpers.createDirective('directiveIsolatedScopeWithPrimitiveAndObjectWithTransclude', $scope);
});
afterEach(function() {
console.log(Inspector.inspect($scope));
});
it("doesn't shadow objects because isolated scope is used", function () {
var input = element.find('input')[1];
var scope = angular.element(input).scope();
angular.element(input).val('AAA').triggerHandler('input');
$scope.$digest();
//Note: transclude will create a new child scope which is child of the isolate scope
//
//From guide: https://docs.angularjs.org/api/ng/service/$compile
// "When you call a transclude function it returns a DOM fragment that is pre-bound to a transclusion scope.
// This scope is special, in that it is a child of the directive's scope (and so gets destroyed when the directive's
// scope gets destroyed) but it inherits the properties of the scope from which it was taken."
expect(scope.$parent.$parent).toBe($scope);
//the value of token in the parent scope is 'AAA' because object is used (= operator for isolated scope)
expect(scope.tokenObj.token).toBe('AAA');
expect($scope.tokenObj.token).toBe('AAA');
//additional tests (ng-scope-aware)
expect(scope).toHaveMembers('token', 'tokenObj', 'alienToken', 'alienTokenObj');
expect(scope).toHaveInheritedMembers('token', 'tokenObj', 'alienToken', 'alienTokenObj');
//there is no shadowing
expect(scope).not.toShadow('token');
expect(scope).not.toShadow('tokenObj');
});
});
});