Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: third_party/pkg/angular/lib/mock/test_bed.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 part of angular.mock;
2
3 /**
4 * Class which simplifies bootstraping of angular for unit tests.
5 *
6 * Simply inject [TestBed] into the test, then use [compile] to
7 * match directives against the view.
8 */
9 class TestBed {
10 final Injector injector;
11 final Scope rootScope;
12 final Compiler compiler;
13 final Parser parser;
14
15
16 Element rootElement;
17 List<Node> rootElements;
18 Block rootBlock;
19
20 TestBed(this.injector, this.rootScope, this.compiler, this.parser);
21
22
23 /**
24 * Use to compile HTML and activate its directives.
25 *
26 * If [html] parameter is:
27 *
28 * - [String] then treat it as HTML
29 * - [Node] then treat it as the root node
30 * - [List<Node>] then treat it as a collection of nods
31 *
32 * After the compilation the [rootElements] contains an array of compiled root nodes,
33 * and [rootElement] contains the first element from the [rootElemets].
34 *
35 * An option [scope] parameter can be supplied to link it with non root scope.
36 */
37 Element compile(html, {Scope scope}) {
38 var injector = this.injector;
39 if(scope != null) {
40 injector = injector.createChild([new Module()..value(Scope, scope)]);
41 }
42 if (html is String) {
43 rootElements = toNodeList(html);
44 } else if (html is Node) {
45 rootElements = [html];
46 } else if (html is List<Node>) {
47 rootElements = html;
48 } else {
49 throw 'Expecting: String, Node, or List<Node> got $html.';
50 }
51 rootElement = rootElements[0];
52 rootBlock = compiler(rootElements)(injector, rootElements);
53 return rootElement;
54 }
55
56 /**
57 * Convert an [html] String to a [List] of [Element]s.
58 */
59 List<Element> toNodeList(html) {
60 var div = new DivElement();
61 div.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer());
62 var nodes = [];
63 for(var node in div.nodes) {
64 nodes.add(node);
65 }
66 return nodes;
67 }
68
69 /**
70 * Triggern a specific DOM element on a given node to test directives
71 * which listen to events.
72 */
73 triggerEvent(element, name, [type='MouseEvent']) {
74 element.dispatchEvent(new Event.eventType(type, name));
75 }
76
77 /**
78 * Select an [OPTION] in a [SELECT] with a given name and trigger the
79 * appropriate DOM event. Used when testing [SELECT] controlls in forms.
80 */
81 selectOption(element, text) {
82 element.querySelectorAll('option').forEach((o) => o.selected = o.text == tex t);
83 triggerEvent(element, 'change');
84 }
85 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698