on master help sign in
tests: Move tests over to using the element builders for rendering
4be691bNick Fitzgerald
tests/web/cached.rs+8−6
@@ -1,5 +1,5 @@
1use super::{assert_rendered, create_element, RenderFn};
1use super::{assert_rendered, create_element, RenderFn};
2−use dodrio::{bumpalo::Bump, Cached, Node, Render, Vdom};
2+use dodrio::{builder::*, bumpalo::Bump, Cached, Node, Render, Vdom};
3use futures::prelude::*;
3use futures::prelude::*;
4use std::cell::Cell;
4use std::cell::Cell;
5use std::rc::Rc;
5use std::rc::Rc;
@@ -27,12 +27,14 @@ impl Render for CountRenders {
27 self.render_count.set(count);
27 self.render_count.set(count);
28
28
29 let s = bumpalo::format!(in bump, "{}", count);
29 let s = bumpalo::format!(in bump, "{}", count);
30− Node::text(s.into_bump_str())
30+ text(s.into_bump_str())
31 }
31 }
32}
32}
33
33
34#[wasm_bindgen_test(async)]
34#[wasm_bindgen_test(async)]
35fn uses_cached_render() -> impl Future<Item = (), Error = JsValue> {
35fn uses_cached_render() -> impl Future<Item = (), Error = JsValue> {
36+ use dodrio::builder::*;
37+
36 let cached = Cached::new(CountRenders::new());
38 let cached = Cached::new(CountRenders::new());
37
39
38 let container0 = create_element("div");
40 let container0 = create_element("div");
@@ -58,7 +60,7 @@ fn uses_cached_render() -> impl Future<Item = (), Error = JsValue> {
58 vdom0.weak().with_component(move |comp| {
60 vdom0.weak().with_component(move |comp| {
59 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
61 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
60 assert_eq!(comp.render_count.get(), 1);
62 assert_eq!(comp.render_count.get(), 1);
61− assert_rendered(&container1, &RenderFn(|_| Node::text("1")));
63+ assert_rendered(&container1, &RenderFn(|_| text("1")));
62 })
64 })
63 })
65 })
64 // We re-render, re-use the cached node, and get "1" again.
66 // We re-render, re-use the cached node, and get "1" again.
@@ -67,7 +69,7 @@ fn uses_cached_render() -> impl Future<Item = (), Error = JsValue> {
67 vdom2.weak().with_component(move |comp| {
69 vdom2.weak().with_component(move |comp| {
68 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
70 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
69 assert_eq!(comp.render_count.get(), 1);
71 assert_eq!(comp.render_count.get(), 1);
70− assert_rendered(&container2, &RenderFn(|_| Node::text("1")));
72+ assert_rendered(&container2, &RenderFn(|_| text("1")));
71 })
73 })
72 })
74 })
73 // We invalidate the cache, re-render, re-populate the cache, and should
75 // We invalidate the cache, re-render, re-populate the cache, and should
@@ -83,7 +85,7 @@ fn uses_cached_render() -> impl Future<Item = (), Error = JsValue> {
83 vdom5.weak().with_component(move |comp| {
85 vdom5.weak().with_component(move |comp| {
84 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
86 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
85 assert_eq!(comp.render_count.get(), 2);
87 assert_eq!(comp.render_count.get(), 2);
86− assert_rendered(&container3, &RenderFn(|_| Node::text("2")));
88+ assert_rendered(&container3, &RenderFn(|_| text("2")));
87 })
89 })
88 })
90 })
89 // We re-render, re-use the cached node, and get "2" again.
91 // We re-render, re-use the cached node, and get "2" again.
@@ -92,7 +94,7 @@ fn uses_cached_render() -> impl Future<Item = (), Error = JsValue> {
92 vdom7.weak().with_component(move |comp| {
94 vdom7.weak().with_component(move |comp| {
93 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
95 let comp = comp.unwrap_mut::<Cached<CountRenders>>();
94 assert_eq!(comp.render_count.get(), 2);
96 assert_eq!(comp.render_count.get(), 2);
95− assert_rendered(&container4, &RenderFn(|_| Node::text("2")));
97+ assert_rendered(&container4, &RenderFn(|_| text("2")));
96 })
98 })
97 })
99 })
98 .map_err(|e| JsValue::from(e.to_string()))
100 .map_err(|e| JsValue::from(e.to_string()))
tests/web/events.rs+23−46
@@ -1,6 +1,6 @@
1use super::create_element;
1use super::create_element;
2use bumpalo::Bump;
2use bumpalo::Bump;
3−use dodrio::{on, Attribute, Node, Render, Vdom};
3+use dodrio::{Node, Render, Vdom};
4use futures::Future;
4use futures::Future;
5use std::cell::{Cell, RefCell};
5use std::cell::{Cell, RefCell};
6use std::rc::Rc;
6use std::rc::Rc;
@@ -30,18 +30,13 @@ impl Render for EventContainer {
30 where
30 where
31 'a: 'bump,
31 'a: 'bump,
32 {
32 {
33− Node::element(
33+ use dodrio::builder::*;
34− bump,
35− "div",
34+ div(bump)
35+ .attr("id", "target")
36− [on(bump, self.event, |root, _scheduler, _event| {
36+ .on(self.event, |root, _scheduler, _event| {
37 (root.unwrap_mut::<EventContainer>().on_event)();
37 (root.unwrap_mut::<EventContainer>().on_event)();
38− })],
38+ })
39− [Attribute {
39+ .finish()
40− name: "id",
41− value: "target",
42− }],
43− [],
44− )
45 }
40 }
46}
41}
47
42
@@ -147,33 +142,18 @@ impl Render for ListensOnlyOnFirstRender {
147 where
142 where
148 'a: 'bump,
143 'a: 'bump,
149 {
144 {
145+ use dodrio::builder::*;
146+
150 let count = self.count.get();
147 let count = self.count.get();
151 self.count.set(count + 1);
148 self.count.set(count + 1);
149+
150+ let mut elem = div(bump).attr("id", "target");
152 if count == 0 {
151 if count == 0 {
153− Node::element(
154− bump,
155− "div",
156− [on(bump, "click", |root, _scheduler, _event| {
152+ elem = elem.on("click", |root, _scheduler, _event| {
157− (root.unwrap_mut::<ListensOnlyOnFirstRender>().callback)();
153+ (root.unwrap_mut::<ListensOnlyOnFirstRender>().callback)();
158− })],
159− [Attribute {
160− name: "id",
161− value: "target",
162− }],
163− [],
164− )
165− } else {
166− Node::element(
167− bump,
168− "div",
169− [],
170− [Attribute {
171− name: "id",
172− value: "target",
173− }],
174− [],
175− )
154+ });
176 }
155 }
156+ elem.finish()
177 }
157 }
178}
158}
179
159
@@ -315,23 +295,20 @@ impl Render for CountRendersAndListen {
315 where
295 where
316 'a: 'bump,
296 'a: 'bump,
317 {
297 {
298+ use dodrio::builder::*;
299+
318 let count = self.count.get() + 1;
300 let count = self.count.get() + 1;
319 self.count.set(count);
301 self.count.set(count);
320− Node::element(
302+
321− bump,
322− "button",
303+ button(bump)
304+ .attr("id", "target")
323− [on(bump, "click", |comp, _vdom, _event| {
305+ .on("click", |comp, _vdom, _event| {
324 let comp: &mut _ = comp.unwrap_mut::<dodrio::Cached<CountRendersAndListen>>();
306 let comp: &mut _ = comp.unwrap_mut::<dodrio::Cached<CountRendersAndListen>>();
325 let comp: &mut CountRendersAndListen = comp;
307 let comp: &mut CountRendersAndListen = comp;
326 let n = comp.count.get();
308 let n = comp.count.get();
327 (comp.callback)(n);
309 (comp.callback)(n);
328− })],
310+ })
329− [Attribute {
311+ .finish()
330− name: "id",
331− value: "target",
332− }],
333− [],
334− )
335 }
312 }
336}
313}
337
314
tests/web/render.rs+93−102
@@ -1,12 +1,12 @@
1use super::{assert_rendered, before_after, create_element, RenderFn};
1use super::{assert_rendered, before_after, create_element, RenderFn};
2−use dodrio::{Attribute, Node, Vdom};
2+use dodrio::{builder::*, Vdom};
3use futures::prelude::*;
3use futures::prelude::*;
4use std::rc::Rc;
4use std::rc::Rc;
5use wasm_bindgen_test::*;
5use wasm_bindgen_test::*;
6
6
7#[wasm_bindgen_test]
7#[wasm_bindgen_test]
8fn render_initial_text() {
8fn render_initial_text() {
9− let hello = Rc::new(RenderFn(|_bump| Node::text("hello")));
9+ let hello = Rc::new(RenderFn(|_bump| text("hello")));
10
10
11 let container = create_element("div");
11 let container = create_element("div");
12 let _vdom = Vdom::new(&container, hello.clone());
12 let _vdom = Vdom::new(&container, hello.clone());
@@ -16,19 +16,10 @@ fn render_initial_text() {
16#[wasm_bindgen_test]
16#[wasm_bindgen_test]
17fn render_initial_node() {
17fn render_initial_node() {
18 let hello = Rc::new(RenderFn(|bump| {
18 let hello = Rc::new(RenderFn(|bump| {
19− Node::element(
20− bump,
21− "div",
19+ div(bump)
22− [],
23− [Attribute {
24− name: "id",
25− value: "hello-world",
20+ .attr("id", "hello-world")
26− }],
21+ .children([text("Hello "), span(bump).child(text("World!")).finish()])
27− [
22+ .finish()
28− Node::text("Hello "),
29− Node::element(bump, "span", [], [], [Node::text("World!")]),
30− ],
31− )
32 }));
23 }));
33
24
34 let container = create_element("div");
25 let container = create_element("div");
@@ -39,7 +30,7 @@ fn render_initial_node() {
39#[wasm_bindgen_test]
30#[wasm_bindgen_test]
40fn container_is_emptied_upon_drop() {
31fn container_is_emptied_upon_drop() {
41 let container = create_element("div");
32 let container = create_element("div");
42− let vdom = Vdom::new(&container, RenderFn(|_bump| Node::text("blah")));
33+ let vdom = Vdom::new(&container, RenderFn(|_bump| text("blah")));
43 drop(vdom);
34 drop(vdom);
44 assert!(container.first_child().is_none());
35 assert!(container.first_child().is_none());
45}
36}
@@ -47,206 +38,206 @@ fn container_is_emptied_upon_drop() {
47before_after! {
38before_after! {
48 same_text {
39 same_text {
49 before(_bump) {
40 before(_bump) {
50− Node::text("hello")
41+ text("hello")
51 }
42 }
52 after(_bump) {
43 after(_bump) {
53− Node::text("hello")
44+ text("hello")
54 }
45 }
55 }
46 }
56
47
57 update_text {
48 update_text {
58 before(_bump) {
49 before(_bump) {
59− Node::text("before")
50+ text("before")
60 }
51 }
61 after(_bump) {
52 after(_bump) {
62− Node::text("after")
53+ text("after")
63 }
54 }
64 }
55 }
65
56
66 replace_text_with_elem {
57 replace_text_with_elem {
67 before(_bump) {
58 before(_bump) {
68− Node::text("before")
59+ text("before")
69 }
60 }
70 after(bump) {
61 after(bump) {
71− Node::element(bump, "div", [], [], [])
62+ div(bump).finish()
72 }
63 }
73 }
64 }
74
65
75 replace_elem_with_text {
66 replace_elem_with_text {
76 before(bump) {
67 before(bump) {
77− Node::element(bump, "div", [], [], [])
68+ div(bump).finish()
78 }
69 }
79 after(_bump) {
70 after(_bump) {
80− Node::text("before")
71+ text("before")
81 }
72 }
82 }
73 }
83
74
84 same_elem {
75 same_elem {
85 before(bump) {
76 before(bump) {
86− Node::element(bump, "div", [], [], [])
77+ div(bump).finish()
87 }
78 }
88 after(bump) {
79 after(bump) {
89− Node::element(bump, "div", [], [], [])
80+ div(bump).finish()
90 }
81 }
91 }
82 }
92
83
93 elems_with_different_tag_names {
84 elems_with_different_tag_names {
94 before(bump) {
85 before(bump) {
95− Node::element(bump, "span", [], [], [])
86+ span(bump).finish()
96 }
87 }
97 after(bump) {
88 after(bump) {
98− Node::element(bump, "div", [], [], [])
89+ div(bump).finish()
99 }
90 }
100 }
91 }
101
92
102 same_tag_name_update_attribute {
93 same_tag_name_update_attribute {
103 before(bump) {
94 before(bump) {
104− Node::element(bump, "div", [], [Attribute { name: "value", value: "1" }], [])
95+ div(bump).attr("value", "1").finish()
105 }
96 }
106 after(bump) {
97 after(bump) {
107− Node::element(bump, "div", [], [Attribute { name: "value", value: "2" }], [])
98+ div(bump).attr("value", "2").finish()
108 }
99 }
109 }
100 }
110
101
111 same_tag_name_remove_attribute {
102 same_tag_name_remove_attribute {
112 before(bump) {
103 before(bump) {
113− Node::element(bump, "div", [], [Attribute { name: "value", value: "1" }], [])
104+ div(bump).attr("value", "1").finish()
114 }
105 }
115 after(bump) {
106 after(bump) {
116− Node::element(bump, "div", [], [], [])
107+ div(bump).finish()
117 }
108 }
118 }
109 }
119
110
120 same_tag_name_add_attribute {
111 same_tag_name_add_attribute {
121 before(bump) {
112 before(bump) {
122− Node::element(bump, "div", [], [], [])
113+ div(bump).finish()
123 }
114 }
124 after(bump) {
115 after(bump) {
125− Node::element(bump, "div", [], [Attribute { name: "value", value: "2" }], [])
116+ div(bump).attr("value", "2").finish()
126 }
117 }
127 }
118 }
128
119
129 same_tag_name_many_attributes {
120 same_tag_name_many_attributes {
130 before(bump) {
121 before(bump) {
131− Node::element(bump, "div", [], [
122+ div(bump)
132− Attribute { name: "before-1", value: "1" },
123+ .attr("before-1", "1")
133− Attribute { name: "shared-1", value: "1" },
124+ .attr("shared-1", "1")
134− Attribute { name: "modified-1", value: "1" },
125+ .attr("modified-1", "1")
135− Attribute { name: "before-2", value: "2" },
126+ .attr("before-2", "2")
136− Attribute { name: "shared-2", value: "2" },
127+ .attr("shared-2", "2")
137− Attribute { name: "modified-2", value: "2" },
128+ .attr("modified-2", "2")
138− Attribute { name: "before-3", value: "3" },
129+ .attr("before-3", "3")
139− Attribute { name: "shared-3", value: "3" },
130+ .attr("shared-3", "3")
140− Attribute { name: "modified-3", value: "3" },
131+ .attr("modified-3", "3")
141− ], [])
132+ .finish()
142 }
133 }
143 after(bump) {
134 after(bump) {
144− Node::element(bump, "div", [], [
135+ div(bump)
145− Attribute { name: "after-1", value: "1" },
136+ .attr("after-1", "1")
146− Attribute { name: "shared-1", value: "1" },
137+ .attr("shared-1", "1")
147− Attribute { name: "modified-1", value: "100" },
138+ .attr("modified-1", "100")
148− Attribute { name: "after-2", value: "2" },
139+ .attr("after-2", "2")
149− Attribute { name: "shared-2", value: "2" },
140+ .attr("shared-2", "2")
150− Attribute { name: "modified-2", value: "200" },
141+ .attr("modified-2", "200")
151− Attribute { name: "after-3", value: "3" },
142+ .attr("after-3", "3")
152− Attribute { name: "shared-3", value: "3" },
143+ .attr("shared-3", "3")
153− Attribute { name: "modified-3", value: "300" },
144+ .attr("modified-3", "300")
154− ], [])
145+ .finish()
155 }
146 }
156 }
147 }
157
148
158 same_tag_same_children {
149 same_tag_same_children {
159 before(bump) {
150 before(bump) {
160− Node::element(bump, "div", [], [], [
161− Node::text("child")
151+ div(bump).child(text("child")).finish()
162− ])
163 }
152 }
164 after(bump) {
153 after(bump) {
165− Node::element(bump, "div", [], [], [
166− Node::text("child")
154+ div(bump).child(text("child")).finish()
167− ])
168 }
155 }
169 }
156 }
170
157
171 same_tag_update_child {
158 same_tag_update_child {
172 before(bump) {
159 before(bump) {
173− Node::element(bump, "div", [], [], [
174− Node::text("before")
160+ div(bump).child(text("before")).finish()
175− ])
176 }
161 }
177 after(bump) {
162 after(bump) {
178− Node::element(bump, "div", [], [], [
179− Node::text("after")
163+ div(bump).child(text("after")).finish()
180− ])
181 }
164 }
182 }
165 }
183
166
184 same_tag_add_child {
167 same_tag_add_child {
185 before(bump) {
168 before(bump) {
186− Node::element(bump, "div", [], [], [])
169+ div(bump).finish()
187 }
170 }
188 after(bump) {
171 after(bump) {
189− Node::element(bump, "div", [], [], [
190− Node::text("child")
172+ div(bump).child(text("child")).finish()
191− ])
192 }
173 }
193 }
174 }
194
175
195 same_tag_remove_child {
176 same_tag_remove_child {
196 before(bump) {
177 before(bump) {
197− Node::element(bump, "div", [], [], [
198− Node::text("child")
178+ div(bump).child(text("child")).finish()
199− ])
200 }
179 }
201 after(bump) {
180 after(bump) {
202− Node::element(bump, "div", [], [], [])
181+ div(bump).finish()
203 }
182 }
204 }
183 }
205
184
206 same_tag_update_many_children {
185 same_tag_update_many_children {
207 before(bump) {
186 before(bump) {
208− Node::element(bump, "div", [], [], [
187+ div(bump)
209− Node::element(bump, "div", [], [], []),
188+ .children([
210− Node::element(bump, "span", [], [], []),
189+ div(bump).finish(),
211− Node::element(bump, "p", [], [], []),
190+ span(bump).finish(),
191+ p(bump).finish(),
212− ])
192+ ])
193+ .finish()
213 }
194 }
214 after(bump) {
195 after(bump) {
215− Node::element(bump, "div", [], [], [
196+ div(bump)
216− Node::element(bump, "span", [], [], []),
197+ .children([
217− Node::element(bump, "p", [], [], []),
198+ span(bump).finish(),
218− Node::element(bump, "div", [], [], []),
199+ p(bump).finish(),
200+ div(bump).finish(),
219− ])
201+ ])
202+ .finish()
220 }
203 }
221 }
204 }
222
205
223 same_tag_remove_many_children {
206 same_tag_remove_many_children {
224 before(bump) {
207 before(bump) {
225− Node::element(bump, "div", [], [], [
208+ div(bump)
226− Node::element(bump, "div", [], [], []),
209+ .children([
227− Node::element(bump, "span", [], [], []),
210+ div(bump).finish(),
228− Node::element(bump, "p", [], [], []),
211+ span(bump).finish(),
212+ p(bump).finish(),
229− ])
213+ ])
214+ .finish()
230 }
215 }
231 after(bump) {
216 after(bump) {
232− Node::element(bump, "div", [], [], [
217+ div(bump)
233− Node::element(bump, "div", [], [], []),
218+ .children([
234− ])
219+ div(bump).finish(),
220+ ])
221+ .finish()
235 }
222 }
236 }
223 }
237
224
238 same_tag_add_many_children {
225 same_tag_add_many_children {
239 before(bump) {
226 before(bump) {
240− Node::element(bump, "div", [], [], [
227+ div(bump)
241− Node::element(bump, "div", [], [], []),
228+ .children([
242− ])
229+ div(bump).finish(),
230+ ])
231+ .finish()
243 }
232 }
244 after(bump) {
233 after(bump) {
245− Node::element(bump, "div", [], [], [
234+ div(bump)
246− Node::element(bump, "div", [], [], []),
235+ .children([
247− Node::element(bump, "span", [], [], []),
236+ div(bump).finish(),
248− Node::element(bump, "p", [], [], []),
237+ span(bump).finish(),
238+ p(bump).finish(),
249− ])
239+ ])
240+ .finish()
250 }
241 }
251 }
242 }
252}
243}