on master help sign in
Add integration tests for JS rendering components
23b4a66Nick Fitzgerald
Cargo.lock+12−10
@@ -85,12 +85,13 @@ dependencies = [
85 "bumpalo 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
85 "bumpalo 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
86 "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
86 "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
87 "console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
87 "console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
88+ "dodrio-js-api 0.1.0",
88 "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
89 "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
89 "js-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
90 "js-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
90 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
91 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
91 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
92 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
92 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
93 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
93− "wasm-bindgen-test 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
94+ "wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
94 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
95 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
95]
96]
96
97
@@ -103,7 +104,7 @@ dependencies = [
103 "dodrio 0.1.0",
104 "dodrio 0.1.0",
104 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
105 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
105 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
106 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
106− "wasm-bindgen-test 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
107+ "wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
107 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
108 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
108]
109]
109
110
@@ -114,7 +115,7 @@ dependencies = [
114 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
115 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
115 "dodrio 0.1.0",
116 "dodrio 0.1.0",
116 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
117 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
117− "wasm-bindgen-test 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
118+ "wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
118 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
119 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
119]
120]
120
121
@@ -125,7 +126,7 @@ dependencies = [
125 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
126 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
126 "dodrio 0.1.0",
127 "dodrio 0.1.0",
127 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
128 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
128− "wasm-bindgen-test 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
129+ "wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
129 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
130 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
130]
131]
131
132
@@ -138,6 +139,7 @@ dependencies = [
138 "js-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
139 "js-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
139 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
140 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
140 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
141 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
142+ "wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
141]
143]
142
144
143[[package]]
145[[package]]
@@ -167,7 +169,7 @@ dependencies = [
167 "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
169 "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
168 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
170 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
169 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
171 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
170− "wasm-bindgen-test 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
172+ "wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
171 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
173 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
172]
174]
173
175
@@ -502,7 +504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
502
504
503[[package]]
505[[package]]
504name = "wasm-bindgen-test"
506name = "wasm-bindgen-test"
505−version = "0.2.36"
507+version = "0.2.37"
506source = "registry+https://github.com/rust-lang/crates.io-index"
508source = "registry+https://github.com/rust-lang/crates.io-index"
507dependencies = [
509dependencies = [
508 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
510 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -511,12 +513,12 @@ dependencies = [
511 "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
513 "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
512 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
514 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
513 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
515 "wasm-bindgen-futures 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
514− "wasm-bindgen-test-macro 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
516+ "wasm-bindgen-test-macro 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
515]
517]
516
518
517[[package]]
519[[package]]
518name = "wasm-bindgen-test-macro"
520name = "wasm-bindgen-test-macro"
519−version = "0.2.36"
521+version = "0.2.37"
520source = "registry+https://github.com/rust-lang/crates.io-index"
522source = "registry+https://github.com/rust-lang/crates.io-index"
521dependencies = [
523dependencies = [
522 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
524 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -649,8 +651,8 @@ dependencies = [
649"checksum wasm-bindgen-macro 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "316e6e568644bfa2009698217cec8f612a4e2cd7460b127f52573991c7ad9f53"
651"checksum wasm-bindgen-macro 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "316e6e568644bfa2009698217cec8f612a4e2cd7460b127f52573991c7ad9f53"
650"checksum wasm-bindgen-macro-support 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "29982ea76a7a35b5c3c052b6035abe9b84d8b6422d8881b68bd3472270a53eec"
652"checksum wasm-bindgen-macro-support 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "29982ea76a7a35b5c3c052b6035abe9b84d8b6422d8881b68bd3472270a53eec"
651"checksum wasm-bindgen-shared 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "59305c5b959d51437636db05bac82de2b52da2c05157e38e018c0bd54cfd9bbf"
653"checksum wasm-bindgen-shared 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "59305c5b959d51437636db05bac82de2b52da2c05157e38e018c0bd54cfd9bbf"
652−"checksum wasm-bindgen-test 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "e403218d15f3a0be08ad984a4b97f4ce4d05f6d3df98ce6708c32f58cdd76286"
654+"checksum wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "8e8b80ffed02a5971865b6e6769398c3581b234a47c301193c44e7766b1a7a64"
653−"checksum wasm-bindgen-test-macro 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "e1feb1af4e075b19eab744a8ed0da087b024ae79afc0b35e49c3ca9a4a1c327b"
655+"checksum wasm-bindgen-test-macro 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "92b66e86a39d33a9d7d46ae7a8c90e692ff4b7ecc0a69075a060511bc687562d"
654"checksum wasm-bindgen-webidl 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "780c16a1a26d28695ac9af7176e11dbc3c188e6d0b3095ad51cc2d0e9e31fcc9"
656"checksum wasm-bindgen-webidl 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "780c16a1a26d28695ac9af7176e11dbc3c188e6d0b3095ad51cc2d0e9e31fcc9"
655"checksum web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "02c19f8ab50a089f9d7c8070c191a919c85d10a4a37c5777a08e8f94ec12c0fc"
657"checksum web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "02c19f8ab50a089f9d7c8070c191a919c85d10a4a37c5777a08e8f94ec12c0fc"
656"checksum weedle 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26a4c67f132386d965390b8a734d5d10adbcd30eb5cc74bd9229af8b83f10044"
658"checksum weedle 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26a4c67f132386d965390b8a734d5d10adbcd30eb5cc74bd9229af8b83f10044"
Cargo.toml+1
@@ -26,6 +26,7 @@ features = [
26
26
27[dev-dependencies]
27[dev-dependencies]
28console_log = "0.1.2"
28console_log = "0.1.2"
29+dodrio-js-api = { version = "=0.1.0", path = "./crates/js-api" }
29log = "0.4.6"
30log = "0.4.6"
30wasm-bindgen-test = "0.2.36"
31wasm-bindgen-test = "0.2.36"
31
32
crates/js-api/Cargo.toml+3
@@ -10,3 +10,6 @@ wasm-bindgen = "0.2.37"
10js-sys = "0.3.14"
10js-sys = "0.3.14"
11wasm-bindgen-futures = "0.3.14"
11wasm-bindgen-futures = "0.3.14"
12futures = "0.1.25"
12futures = "0.1.25"
13+
14+[dev-dependencies]
15+wasm-bindgen-test = "0.2.37"
tests/web/js_api.rsnew file+163
@@ -0,0 +1,163 @@
1+use super::{assert_rendered, create_element, RenderFn};
2+use dodrio::{builder::*, bumpalo::Bump, Node, Render, Vdom};
3+use dodrio_js_api::JsRender;
4+use futures::prelude::*;
5+use futures::sync::oneshot;
6+use js_sys::Object;
7+use std::rc::Rc;
8+use wasm_bindgen::prelude::*;
9+use wasm_bindgen::JsCast;
10+use wasm_bindgen_test::*;
11+
12+pub struct WrapJs {
13+ inner: JsRender,
14+}
15+
16+impl WrapJs {
17+ pub fn new() -> (WrapJs, oneshot::Receiver<()>, Closure<FnMut()>) {
18+ let (sender, receiver) = oneshot::channel();
19+
20+ let on_after_render = Closure::wrap(Box::new({
21+ let mut sender = Some(sender);
22+ move || {
23+ sender
24+ .take()
25+ .expect_throw("on_after_render should only be called once")
26+ .send(())
27+ .expect_throw("receiver should not have been dropped");
28+ }
29+ }) as Box<FnMut()>);
30+
31+ let component = WrapJs {
32+ inner: JsRender::new(JsComponent::new(&on_after_render)),
33+ };
34+
35+ (component, receiver, on_after_render)
36+ }
37+}
38+
39+impl Render for WrapJs {
40+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
41+ where
42+ 'a: 'bump,
43+ {
44+ div(bump)
45+ .attr("class", "wrap-js")
46+ .children([self.inner.render(bump)])
47+ .finish()
48+ }
49+}
50+
51+#[wasm_bindgen]
52+extern "C" {
53+ #[wasm_bindgen(extends = Object)]
54+ #[derive(Clone, Debug)]
55+ type JsComponent;
56+
57+ #[wasm_bindgen(constructor)]
58+ fn new(on_after_render: &Closure<FnMut()>) -> JsComponent;
59+}
60+
61+fn eval_js_rendering_component() {
62+ use std::sync::Once;
63+ static EVAL: Once = Once::new();
64+ EVAL.call_once(|| {
65+ super::init_logging();
66+ js_sys::eval(
67+ r#"
68+ window.JsComponent = class JsComponent {
69+ constructor(onAfterRender) {
70+ this.count = 0;
71+ this.onAfterRender = onAfterRender;
72+ }
73+
74+ render() {
75+ return {
76+ tagName: "span",
77+ attributes: [{ name: "class", value: "js-component" }],
78+ listeners: [{ on: "click", callback: this.onClick.bind(this) }],
79+ children: [
80+ "Here is some plain text",
81+ {
82+ tagName: "b",
83+ children: ["...and here is some bold text"]
84+ },
85+ String(this.count),
86+ ]
87+ };
88+ }
89+
90+ async onClick(vdom, event) {
91+ this.count++;
92+ await vdom.render();
93+ this.onAfterRender();
94+ }
95+ };
96+ "#,
97+ )
98+ .expect_throw("should eval JS component OK");
99+ });
100+}
101+
102+#[wasm_bindgen_test(async)]
103+fn can_use_js_rendering_components() -> impl Future<Item = (), Error = JsValue> {
104+ eval_js_rendering_component();
105+
106+ let container = create_element("div");
107+ let (component, receiver, closure) = WrapJs::new();
108+
109+ let vdom = Rc::new(Vdom::new(&container, component));
110+ assert_rendered(
111+ &container,
112+ &RenderFn(|bump| {
113+ div(bump)
114+ .attr("class", "wrap-js")
115+ .children([span(bump)
116+ .attr("class", "js-component")
117+ .children([
118+ text("Here is some plain text"),
119+ b(bump)
120+ .children([text("...and here is some bold text")])
121+ .finish(),
122+ text("0"),
123+ ])
124+ .finish()])
125+ .finish()
126+ }),
127+ );
128+
129+ container
130+ .query_selector(".js-component")
131+ .expect_throw("should querySelector OK")
132+ .expect_throw("should find `.js-component` element OK")
133+ .dyn_into::<web_sys::HtmlElement>()
134+ .expect_throw("should be an `HTMLElement` object")
135+ .click();
136+
137+ receiver
138+ .map(move |_| {
139+ assert_rendered(
140+ &container,
141+ &RenderFn(|bump| {
142+ div(bump)
143+ .attr("class", "wrap-js")
144+ .children([span(bump)
145+ .attr("class", "js-component")
146+ .children([
147+ text("Here is some plain text"),
148+ b(bump)
149+ .children([text("...and here is some bold text")])
150+ .finish(),
151+ text("1"),
152+ ])
153+ .finish()])
154+ .finish()
155+ }),
156+ );
157+
158+ drop(vdom);
159+ drop(container);
160+ drop(closure);
161+ })
162+ .map_err(|e| e.to_string().into())
163+}
tests/web/main.rs+1
@@ -15,6 +15,7 @@ wasm_bindgen_test_configure!(run_in_browser);
15
15
16pub mod cached;
16pub mod cached;
17pub mod events;
17pub mod events;
18+pub mod js_api;
18pub mod render;
19pub mod render;
19
20
20pub fn window() -> web_sys::Window {
21pub fn window() -> web_sys::Window {