Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 06dd166

Browse files
committed
chore(test): Add TestBed class for easy test bootstrapping
1 parent 898a6de commit 06dd166

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

lib/directive.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ String _ATTR_DIRECTIVE = '-attr' + _DIRECTIVE;
66

77
class Directive {
88
Type type;
9+
// TODO(misko): this should be renamed to selector once we change over to meta-data.
910
String $name;
1011
Function $generate;
1112
String $transclude;
@@ -30,7 +31,10 @@ class Directive {
3031
onMatch: (m) => '-' + m.group(0).toLowerCase())
3132
.substring(1);
3233

33-
if ($name.endsWith(_ATTR_DIRECTIVE)) {
34+
var $selector = reflectStaticField(type, r'$selector');
35+
if ($selector != null) {
36+
$name = $selector;
37+
} else if ($name.endsWith(_ATTR_DIRECTIVE)) {
3438
$name = '[${$name.substring(0, $name.length - _ATTR_DIRECTIVE.length)}]';
3539
} else if ($name.endsWith(_DIRECTIVE)) {
3640
$name = $name.substring(0, $name.length - _DIRECTIVE.length);
@@ -54,10 +58,6 @@ class Directive {
5458
$map = reflectStaticField(type, '\$map');
5559
$publishAs = reflectStaticField(type, r'$publishAs');
5660
isStructural = $transclude != null;
57-
var $selector = reflectStaticField(type, r'$selector');
58-
if ($selector != null) {
59-
$name = $selector;
60-
}
6161
if (isComponent && $map == null) {
6262
$map = new Map<String, String>();
6363
}

lib/directives/ng_bind.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
part of angular;
22

3-
class NgBindAttrDirective {
3+
class NgBindAttrDirective {
44

55
NgBindAttrDirective(dom.Element element, NodeAttrs attrs, Scope scope) {
66
scope.$watch(attrs[this], (value) => element.text = value);

test/_specs.dart

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class JQuery implements List<Node> {
139139
eq(num childIndex) => $(this[childIndex]);
140140
remove() => forEach((n) => n.remove());
141141
attr([String name]) => accessor((n) => n.attributes[name], (n, v) => n.attributes[name] = v);
142+
prop([String name]) => accessor((n) => getter(n, null, name), (n, v) => setter(n, name, v));
142143
textWithShadow() => fold('', (t, n) => '${t}${renderedText(n)}');
143144
find(selector) => fold(new JQuery(), (jq, n) => jq..addAll(n.queryAll(selector)));
144145
hasClass(String name) => fold(false, (hasClass, node) => hasClass ? true : node.classes.contains(name));

test/_test_bed.dart

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import "_specs.dart";
2+
3+
4+
/**
5+
* Class which simplifies bootstraping angular for tests.
6+
*/
7+
class TestBed {
8+
Injector injector;
9+
Scope rootScope;
10+
Compiler compiler;
11+
Parser parser;
12+
13+
14+
JQuery rootElement;
15+
Block rootBlock;
16+
17+
TestBed(
18+
Injector this.injector,
19+
Scope this.rootScope,
20+
Compiler this.compiler,
21+
Parser this.parser);
22+
23+
24+
compile(html) {
25+
rootElement = $(html);
26+
rootBlock = compiler(rootElement)(injector, rootElement);
27+
}
28+
}
29+
30+
beforeEachTestBed(assign) {
31+
return module((AngularModule module) {
32+
module.type(TestBed, TestBed);
33+
module.directive(Probe);
34+
return (TestBed tb) => assign(tb);
35+
});
36+
}
37+
38+
/*
39+
* Use Probe directive to capture the Scope, Injector, Element from any DOM
40+
* location into root-scope. This is useful for testing to get a hold of
41+
* any directive.
42+
*
43+
* <pre>
44+
* <div some-directive probe="myProbje">..</div>
45+
*
46+
* rootScope.myProbe.directive(SomeAttrDirective);
47+
*/
48+
class Probe {
49+
static String $selector = '[probe]';
50+
51+
Scope scope;
52+
Injector injector;
53+
Element element;
54+
55+
Probe(Scope this.scope, Injector this.injector, Element this.element, NodeAttrs attrs) {
56+
scope.$root[attrs[this]] = this;
57+
}
58+
59+
directive(Type type) => injector.get(type);
60+
}

0 commit comments

Comments
 (0)