| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library custom_element.test.custom_element_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:html'; | |
| 9 import 'package:custom_element/custom_element.dart'; | |
| 10 import 'package:unittest/html_config.dart'; | |
| 11 import 'package:unittest/unittest.dart'; | |
| 12 | |
| 13 main() { | |
| 14 useHtmlConfiguration(); | |
| 15 | |
| 16 // Load the MutationObserver polyfill. | |
| 17 HttpRequest.getString('/root_dart/pkg/mutation_observer/lib/' | |
| 18 'mutation_observer.js').then((code) { | |
| 19 document.head.children.add(new ScriptElement()..text = code); | |
| 20 | |
| 21 customElementTests(); | |
| 22 }); | |
| 23 } | |
| 24 | |
| 25 customElementTests() { | |
| 26 test('register creates the element and calls lifecycle methods', () { | |
| 27 // Add element to the page. | |
| 28 var element = new Element.html('<fancy-button>foo bar</fancy-button>'); | |
| 29 document.body.nodes.add(element); | |
| 30 | |
| 31 var xtag = null; | |
| 32 registerCustomElement('fancy-button', () => xtag = new FancyButton()); | |
| 33 expect(xtag, isNotNull, reason: 'FancyButton was created'); | |
| 34 expect(element.xtag, xtag, reason: 'xtag pointer should be set'); | |
| 35 expect(xtag.host, element, reason: 'host pointer should be set'); | |
| 36 expect(xtag.lifecycle, ['created']); | |
| 37 return new Future(() { | |
| 38 expect(xtag.lifecycle, ['created', 'inserted']); | |
| 39 element.remove(); | |
| 40 // TODO(jmesserly): the extra future here is to give IE9 time to deliver | |
| 41 // its event. This seems wrong. We'll probably need some cooperation | |
| 42 // between Dart and the polyfill to coordinate the microtask event loop. | |
| 43 return new Future(() => new Future(() { | |
| 44 expect(xtag.lifecycle, ['created', 'inserted', 'removed']); | |
| 45 })); | |
| 46 }); | |
| 47 }); | |
| 48 | |
| 49 test('create a component in code', () { | |
| 50 var element = createElement('super-button'); | |
| 51 expect(element.xtag, element, reason: 'element not registered'); | |
| 52 | |
| 53 var xtag = null; | |
| 54 registerCustomElement('super-button', () => xtag = new FancyButton()); | |
| 55 | |
| 56 element = createElement('super-button'); | |
| 57 expect(xtag, isNotNull, reason: 'FancyButton was created'); | |
| 58 expect(element.xtag, xtag, reason: 'xtag pointer should be set'); | |
| 59 expect(xtag.host, element, reason: 'host pointer should be set'); | |
| 60 expect(xtag.lifecycle, ['created']); | |
| 61 return new Future(() { | |
| 62 expect(xtag.lifecycle, ['created'], reason: 'not inserted into document'); | |
| 63 | |
| 64 document.body.nodes.add(element); | |
| 65 return new Future(() { | |
| 66 expect(xtag.lifecycle, ['created'], | |
| 67 reason: 'mutation observer not implemented yet'); | |
| 68 | |
| 69 element.remove(); | |
| 70 return new Future(() { | |
| 71 expect(xtag.lifecycle, ['created'], | |
| 72 reason: 'mutation observer not implemented yet'); | |
| 73 }); | |
| 74 }); | |
| 75 }); | |
| 76 }); | |
| 77 } | |
| 78 | |
| 79 class FancyButton extends CustomElement { | |
| 80 final lifecycle = []; | |
| 81 created() { | |
| 82 super.created(); | |
| 83 lifecycle.add('created'); | |
| 84 } | |
| 85 inserted() { | |
| 86 super.inserted(); | |
| 87 lifecycle.add('inserted'); | |
| 88 } | |
| 89 removed() { | |
| 90 super.removed(); | |
| 91 lifecycle.add('removed'); | |
| 92 } | |
| 93 } | |
| OLD | NEW |