OLD | NEW |
1 part of angular.mock; | 1 part of angular.mock; |
2 | 2 |
3 /** | 3 /** |
4 * Class which simplifies bootstraping of angular for unit tests. | 4 * Class which simplifies bootstraping of angular for unit tests. |
5 * | 5 * |
6 * Simply inject [TestBed] into the test, then use [compile] to | 6 * Simply inject [TestBed] into the test, then use [compile] to |
7 * match directives against the view. | 7 * match directives against the view. |
8 */ | 8 */ |
9 class TestBed { | 9 class TestBed { |
10 final Injector injector; | 10 final Injector injector; |
11 final Scope rootScope; | 11 final Scope rootScope; |
12 final Compiler compiler; | 12 final Compiler compiler; |
13 final Parser parser; | 13 final Parser _parser; |
14 | 14 final Expando expando; |
15 | 15 |
16 Element rootElement; | 16 Element rootElement; |
17 List<Node> rootElements; | 17 List<Node> rootElements; |
18 Block rootBlock; | 18 View rootView; |
19 | 19 |
20 TestBed(this.injector, this.rootScope, this.compiler, this.parser); | 20 TestBed(this.injector, this.rootScope, this.compiler, this._parser, this.expan
do); |
21 | 21 |
22 | 22 |
23 /** | 23 /** |
24 * Use to compile HTML and activate its directives. | 24 * Use to compile HTML and activate its directives. |
25 * | 25 * |
26 * If [html] parameter is: | 26 * If [html] parameter is: |
27 * | 27 * |
28 * - [String] then treat it as HTML | 28 * - [String] then treat it as HTML |
29 * - [Node] then treat it as the root node | 29 * - [Node] then treat it as the root node |
30 * - [List<Node>] then treat it as a collection of nods | 30 * - [List<Node>] then treat it as a collection of nods |
(...skipping 10 matching lines...) Expand all Loading... |
41 } | 41 } |
42 if (html is String) { | 42 if (html is String) { |
43 rootElements = toNodeList(html); | 43 rootElements = toNodeList(html); |
44 } else if (html is Node) { | 44 } else if (html is Node) { |
45 rootElements = [html]; | 45 rootElements = [html]; |
46 } else if (html is List<Node>) { | 46 } else if (html is List<Node>) { |
47 rootElements = html; | 47 rootElements = html; |
48 } else { | 48 } else { |
49 throw 'Expecting: String, Node, or List<Node> got $html.'; | 49 throw 'Expecting: String, Node, or List<Node> got $html.'; |
50 } | 50 } |
51 rootElement = rootElements[0]; | 51 rootElement = rootElements.length > 0 && rootElements[0] is Element ? rootEl
ements[0] : null; |
52 if (directives == null) { | 52 if (directives == null) { |
53 directives = injector.get(DirectiveMap); | 53 directives = injector.get(DirectiveMap); |
54 } | 54 } |
55 rootBlock = compiler(rootElements, directives)(injector, rootElements); | 55 rootView = compiler(rootElements, directives)(injector, rootElements); |
56 return rootElement; | 56 return rootElement; |
57 } | 57 } |
58 | 58 |
59 /** | 59 /** |
60 * Convert an [html] String to a [List] of [Element]s. | 60 * Convert an [html] String to a [List] of [Element]s. |
61 */ | 61 */ |
62 List<Element> toNodeList(html) { | 62 List<Element> toNodeList(html) { |
63 var div = new DivElement(); | 63 var div = new DivElement(); |
64 div.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer()); | 64 div.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer()); |
65 var nodes = []; | 65 var nodes = []; |
66 for (var node in div.nodes) { | 66 for (var node in div.nodes) { |
67 nodes.add(node); | 67 nodes.add(node); |
68 } | 68 } |
69 return nodes; | 69 return nodes; |
70 } | 70 } |
71 | 71 |
72 /** | 72 /** |
73 * Triggern a specific DOM element on a given node to test directives | 73 * Trigger a specific DOM element on a given node to test directives |
74 * which listen to events. | 74 * which listen to events. |
75 */ | 75 */ |
76 triggerEvent(element, name, [type='MouseEvent']) { | 76 triggerEvent(element, name, [type='MouseEvent']) { |
77 element.dispatchEvent(new Event.eventType(type, name)); | 77 element.dispatchEvent(new Event.eventType(type, name)); |
78 // Since we are manually triggering event we need to simpulate apply(); | 78 // Since we are manually triggering event we need to simulate apply(); |
79 rootScope.apply(); | 79 rootScope.apply(); |
80 } | 80 } |
81 | 81 |
82 /** | 82 /** |
83 * Select an [OPTION] in a [SELECT] with a given name and trigger the | 83 * Select an [OPTION] in a [SELECT] with a given name and trigger the |
84 * appropriate DOM event. Used when testing [SELECT] controlls in forms. | 84 * appropriate DOM event. Used when testing [SELECT] controlls in forms. |
85 */ | 85 */ |
86 selectOption(element, text) { | 86 selectOption(element, text) { |
87 element.querySelectorAll('option').forEach((o) => o.selected = o.text == tex
t); | 87 element.querySelectorAll('option').forEach((o) => o.selected = o.text == tex
t); |
88 triggerEvent(element, 'change'); | 88 triggerEvent(element, 'change'); |
89 rootScope.apply(); | 89 rootScope.apply(); |
90 } | 90 } |
| 91 |
| 92 getProbe(Node node) { |
| 93 while (node != null) { |
| 94 ElementProbe probe = expando[node]; |
| 95 if (probe != null) return probe; |
| 96 node = node.parent; |
| 97 } |
| 98 throw 'Probe not found.'; |
| 99 } |
| 100 |
| 101 getScope(Node node) => getProbe(node).scope; |
91 } | 102 } |
OLD | NEW |