| 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; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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 |
| 31 * | 31 * |
| 32 * After the compilation the [rootElements] contains an array of compiled root
nodes, | 32 * After the compilation the [rootElements] contains an array of compiled root
nodes, |
| 33 * and [rootElement] contains the first element from the [rootElemets]. | 33 * and [rootElement] contains the first element from the [rootElemets]. |
| 34 * | 34 * |
| 35 * An option [scope] parameter can be supplied to link it with non root scope. | 35 * An option [scope] parameter can be supplied to link it with non root scope. |
| 36 */ | 36 */ |
| 37 Element compile(html, {Scope scope, DirectiveMap directives}) { | 37 Element compile(html, {Scope scope}) { |
| 38 var injector = this.injector; | 38 var injector = this.injector; |
| 39 if (scope != null) { | 39 if(scope != null) { |
| 40 injector = injector.createChild([new Module()..value(Scope, scope)]); | 40 injector = injector.createChild([new Module()..value(Scope, scope)]); |
| 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[0]; |
| 52 if (directives == null) { | 52 rootBlock = compiler(rootElements)(injector, rootElements); |
| 53 directives = injector.get(DirectiveMap); | |
| 54 } | |
| 55 rootBlock = compiler(rootElements, directives)(injector, rootElements); | |
| 56 return rootElement; | 53 return rootElement; |
| 57 } | 54 } |
| 58 | 55 |
| 59 /** | 56 /** |
| 60 * Convert an [html] String to a [List] of [Element]s. | 57 * Convert an [html] String to a [List] of [Element]s. |
| 61 */ | 58 */ |
| 62 List<Element> toNodeList(html) { | 59 List<Element> toNodeList(html) { |
| 63 var div = new DivElement(); | 60 var div = new DivElement(); |
| 64 div.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer()); | 61 div.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer()); |
| 65 var nodes = []; | 62 var nodes = []; |
| 66 for (var node in div.nodes) { | 63 for(var node in div.nodes) { |
| 67 nodes.add(node); | 64 nodes.add(node); |
| 68 } | 65 } |
| 69 return nodes; | 66 return nodes; |
| 70 } | 67 } |
| 71 | 68 |
| 72 /** | 69 /** |
| 73 * Triggern a specific DOM element on a given node to test directives | 70 * Triggern a specific DOM element on a given node to test directives |
| 74 * which listen to events. | 71 * which listen to events. |
| 75 */ | 72 */ |
| 76 triggerEvent(element, name, [type='MouseEvent']) { | 73 triggerEvent(element, name, [type='MouseEvent']) { |
| 77 element.dispatchEvent(new Event.eventType(type, name)); | 74 element.dispatchEvent(new Event.eventType(type, name)); |
| 78 // Since we are manually triggering event we need to simpulate apply(); | |
| 79 rootScope.apply(); | |
| 80 } | 75 } |
| 81 | 76 |
| 82 /** | 77 /** |
| 83 * Select an [OPTION] in a [SELECT] with a given name and trigger the | 78 * Select an [OPTION] in a [SELECT] with a given name and trigger the |
| 84 * appropriate DOM event. Used when testing [SELECT] controlls in forms. | 79 * appropriate DOM event. Used when testing [SELECT] controlls in forms. |
| 85 */ | 80 */ |
| 86 selectOption(element, text) { | 81 selectOption(element, text) { |
| 87 element.querySelectorAll('option').forEach((o) => o.selected = o.text == tex
t); | 82 element.querySelectorAll('option').forEach((o) => o.selected = o.text == tex
t); |
| 88 triggerEvent(element, 'change'); | 83 triggerEvent(element, 'change'); |
| 89 rootScope.apply(); | |
| 90 } | 84 } |
| 91 } | 85 } |
| OLD | NEW |