-
Notifications
You must be signed in to change notification settings - Fork 0
/
NgDirectiveWithIsolatedScope.test.js
87 lines (69 loc) · 3.18 KB
/
NgDirectiveWithIsolatedScope.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
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(function() {
$scope = $rootScope.$new();
$scope.token = 'a';
element = InspectorHelpers.createDirective('directiveIsolatedScopeWithString', $scope);
});
afterEach(function() {
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();
//it creates its own scope
expect(scope).not.toBe($scope);
expect(scope.$parent).toBe($scope);
//the value of token in the parent scope is still 'a' because there token is referenced by '@' (string)
expect(scope.token).toBe('AAA');
expect($scope.token).toBe('a');
//additional tests (ng-scope-aware)
expect(scope).toHaveMembers('token');
expect(scope).not.toHaveInheritedMembers('token');
expect(scope).not.toShadow('token');
});
});
describe('when objects are used', function() {
var $scope, element;
beforeEach(function() {
$scope = $rootScope.$new();
$scope.alienTokenObj = { token : 'a' };
//<directive-isolated-scope-with-object tokenobj="alienTokenObj"></directive-isolated-scope-with-object>
element = InspectorHelpers.createDirective('directiveIsolatedScopeWithObject', $scope);
});
afterEach(function() {
console.log(Inspector.inspect($scope));
});
it("doesn't shadow objects 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();
//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.alienTokenObj.token).toBe('AAA');
//additional tests (ng-scope-aware)
expect(scope).toHaveMembers('tokenobj');
//there is no prototypal inheritance at all
expect(scope).not.toHaveInheritedMembers('tokenobj');
expect(scope).not.toHaveInheritedMembers('alienTokenObj');
//there is no shadowing
expect(scope).not.toShadow('tokenobj');
expect(scope).not.toShadow('alienTokenObj');
});
});
});