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 setUp(() { | |
17 // Load the MutationObserver polyfill if needed. | |
18 if (!MutationObserver.supported) { | |
19 var script = new ScriptElement() | |
20 ..src = '/packages/mutation_observer/mutation_observer.js'; | |
21 document.head.append(script); | |
22 return script.onLoad.first; | |
23 } | |
24 }); | |
25 | |
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 treeSanitizer: new NullTreeSanitizer()); | |
30 document.body.nodes.add(element); | |
31 | |
32 var xtag = null; | |
33 registerCustomElement('fancy-button', () => xtag = new FancyButton()); | |
34 expect(xtag, isNotNull, reason: 'FancyButton was created'); | |
35 expect(element.xtag, xtag, reason: 'xtag pointer should be set'); | |
36 expect(xtag.host, element, reason: 'host pointer should be set'); | |
37 expect(xtag.lifecycle, ['created']); | |
38 return new Future(() { | |
39 expect(xtag.lifecycle, ['created', 'inserted']); | |
40 element.remove(); | |
41 // TODO(jmesserly): the extra future here is to give IE9 time to deliver | |
42 // its event. This seems wrong. We'll probably need some cooperation | |
43 // between Dart and the polyfill to coordinate the microtask event loop. | |
44 return new Future(() => new Future(() { | |
45 expect(xtag.lifecycle, ['created', 'inserted', 'removed']); | |
46 })); | |
47 }); | |
48 }); | |
49 | |
50 test('create a component in code', () { | |
51 var element = createElement('super-button'); | |
52 expect(element.xtag, element, reason: 'element not registered'); | |
53 | |
54 var xtag = null; | |
55 registerCustomElement('super-button', () => xtag = new FancyButton()); | |
56 | |
57 element = createElement('super-button'); | |
58 expect(xtag, isNotNull, reason: 'FancyButton was created'); | |
59 expect(element.xtag, xtag, reason: 'xtag pointer should be set'); | |
60 expect(xtag.host, element, reason: 'host pointer should be set'); | |
61 expect(xtag.lifecycle, ['created']); | |
62 return new Future(() { | |
63 expect(xtag.lifecycle, ['created'], reason: 'not inserted into document'); | |
64 | |
65 document.body.nodes.add(element); | |
66 return new Future(() { | |
67 expect(xtag.lifecycle, ['created'], | |
68 reason: 'mutation observer not implemented yet'); | |
69 | |
70 element.remove(); | |
71 return new Future(() { | |
72 expect(xtag.lifecycle, ['created'], | |
73 reason: 'mutation observer not implemented yet'); | |
74 }); | |
75 }); | |
76 }); | |
77 }); | |
78 } | |
79 | |
80 class FancyButton extends CustomElement { | |
81 final lifecycle = []; | |
82 created() { | |
83 super.created(); | |
84 lifecycle.add('created'); | |
85 } | |
86 inserted() { | |
87 super.inserted(); | |
88 lifecycle.add('inserted'); | |
89 } | |
90 removed() { | |
91 super.removed(); | |
92 lifecycle.add('removed'); | |
93 } | |
94 } | |
95 | |
96 /** | |
97 * Sanitizer which does nothing. | |
98 */ | |
99 class NullTreeSanitizer implements NodeTreeSanitizer { | |
100 void sanitizeTree(Node node) {} | |
101 } | |
OLD | NEW |