aboutsummaryrefslogtreecommitdiffstats
path: root/test/delete/ScopeTest.js
blob: 24febf19e27481b3ea26350c3255cf6b1fe286b3 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
ScopeTest = TestCase('ScopeTest');

ScopeTest.prototype.testGetScopeRetrieval = function(){
  var scope = {};
  var form = jQuery("<a><b><c></c></b></a>");
  form.data('scope', scope);
  var c = form.find('c');
  assertTrue(scope === c.scope());
};

ScopeTest.prototype.testGetScopeRetrievalIntermediateNode = function(){
  var scope = {};
  var form = jQuery("<a><b><c></c></b></a>");
  form.find("b").data('scope', scope);
  var b = form.find('b');
  assertTrue(scope === b.scope());
};

ScopeTest.prototype.testNoScopeDoesNotCauseInfiniteRecursion = function(){
  var form = jQuery("<a><b><c></c></b></a>");
  var c = form.find('c');
  assertTrue(!c.scope());
};

ScopeTest.prototype.testScopeEval = function(){
  var scope = new Scope({b:345});
  assertEquals(scope.eval('b = 123'), 123);
  assertEquals(scope.get('b'), 123);
};

ScopeTest.prototype.testScopeFromPrototype = function(){
  var scope = new Scope({b:123});
  scope.eval('a = b');
  scope.eval('b = 456');
  assertEquals(scope.get('a'), 123);
  assertEquals(scope.get('b'), 456);
};

ScopeTest.prototype.testSetScopeGet = function(){
  var scope = new Scope();
  assertEquals(987, scope.set('a', 987));
  assertEquals(scope.get('a'), 987);
  assertEquals(scope.eval('a'), 987);
};

ScopeTest.prototype.testGetChain = function(){
  var scope = new Scope({a:{b:987}});
  assertEquals(scope.get('a.b'), 987);
  assertEquals(scope.eval('a.b'), 987);
};

ScopeTest.prototype.testGetUndefinedChain = function(){
  var scope = new Scope();
  assertEquals(typeof scope.get('a.b'),  'undefined');
};

ScopeTest.prototype.testSetChain = function(){
  var scope = new Scope({a:{}});
  scope.set('a.b', 987);
  assertEquals(scope.get('a.b'), 987);
  assertEquals(scope.eval('a.b'), 987);
};

ScopeTest.prototype.testSetGetOnChain = function(){
  var scope = new Scope();
  scope.set('a.b', 987);
  assertEquals(scope.get('a.b'), 987);
  assertEquals(scope.eval('a.b'), 987);
};

ScopeTest.prototype.testGlobalFunctionAccess =function(){
  window['scopeAddTest'] = function (a, b) {return a+b;};
  var scope = new Scope({window:window});
  assertEquals(scope.eval('window.scopeAddTest(1,2)'), 3);

  scope.set('add', function (a, b) {return a+b;});
  assertEquals(scope.eval('add(1,2)'), 3);

  scope.set('math.add', function (a, b) {return a+b;});
  assertEquals(scope.eval('math.add(1,2)'), 3);
};

ScopeTest.prototype.testValidationEval = function(){
  expectAsserts(4);
  var scope = new Scope();
  scope.set("name", "misko");
  angular.validator.testValidator = function(value, expect){
    assertEquals("misko", this.name);
    return value == expect ? null : "Error text";
  };

  assertEquals("Error text", scope.validate("testValidator:'abc'", 'x'));
  assertEquals(null, scope.validate("testValidator:'abc'", 'abc'));

  delete angular.validator['testValidator'];
};

ScopeTest.prototype.testCallingNonExistantMethodShouldProduceFriendlyException = function() {
  expectAsserts(1);
  var scope = new Scope({obj:{}});
  try {
    scope.eval("obj.iDontExist()");
    fail();
  } catch (e) {
    assertEquals("Expression 'obj.iDontExist' is not a function.", e);
  }
};

ScopeTest.prototype.testAccessingWithInvalidPathShouldThrowError = function() {
  var scope = new Scope();
  try {
    scope.get('a.{{b}}');
    fail();
  } catch (e) {
    assertEquals("Expression 'a.{{b}}' is not a valid expression for accesing variables.", e);
  }
};

ScopeTest.prototype.testItShouldHave$parent = function() {
  var parent = new Scope({}, "ROOT");
  var child = new Scope(parent.state);
  assertSame("parent", child.state.$parent, parent.state);
  assertSame("root", child.state.$root, parent.state);
};

ScopeTest.prototype.testItShouldHave$root = function() {
  var scope = new Scope({}, "ROOT");
  assertSame(scope.state.$root, scope.state);
};

ScopeTest.prototype.testItShouldBuildPathOnUndefined = function(){
  var scope = new Scope({}, "ROOT");
  scope.setEval("a.$b.c", 1);
  assertJsonEquals({$b:{c:1}}, scope.get("a"));
};

ScopeTest.prototype.testItShouldMapUnderscoreFunctions = function(){
  var scope = new Scope({}, "ROOT");
  scope.set("a", [1,2,3]);
  assertEquals('function', typeof scope.get("a.$size"));
  scope.eval("a.$includeIf(4,true)");
  assertEquals(4, scope.get("a.$size")());
  assertEquals(4, scope.eval("a.$size()"));
  assertEquals('undefined', typeof scope.get("a.dontExist"));
};