on master help sign in
Add integration tests for JS rendering components
commit23b4a6670c23da1ccdeb2a8093792a71faaa9ba8 authorNick Fitzgerald <fitzgen@gmail.com> dateThu 28 Feb 2019, 12:27 (8 years ago) parentca87755
5 files changed+180−10
Cargo.lock+12−10 Cargo.toml+1 crates/js-api/Cargo.toml+3 tests/web/js_api.rs+163 tests/web/main.rs+1
Cargo.lock+12−10
@@ -85,12 +85,13 @@ dependencies = [
8585 "bumpalo 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
8686 "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
8787 "console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
88+ "dodrio-js-api 0.1.0",
8889 "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
8990 "js-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
9091 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
9192 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
9293 "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)",
9495 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
9596]
9697
@@ -103,7 +104,7 @@ dependencies = [
103104 "dodrio 0.1.0",
104105 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
105106 "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)",
107108 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
108109]
109110
@@ -114,7 +115,7 @@ dependencies = [
114115 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
115116 "dodrio 0.1.0",
116117 "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)",
118119 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
119120]
120121
@@ -125,7 +126,7 @@ dependencies = [
125126 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
126127 "dodrio 0.1.0",
127128 "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)",
129130 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
130131]
131132
@@ -138,6 +139,7 @@ dependencies = [
138139 "js-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
139140 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
140141 "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)",
141143]
142144
143145[[package]]
@@ -167,7 +169,7 @@ dependencies = [
167169 "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
168170 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
169171 "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)",
171173 "web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
172174]
173175
@@ -502,7 +504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
502504
503505[[package]]
504506name = "wasm-bindgen-test"
505−version = "0.2.36"
507+version = "0.2.37"
506508source = "registry+https://github.com/rust-lang/crates.io-index"
507509dependencies = [
508510 "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -511,12 +513,12 @@ dependencies = [
511513 "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
512514 "wasm-bindgen 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
513515 "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)",
515517]
516518
517519[[package]]
518520name = "wasm-bindgen-test-macro"
519−version = "0.2.36"
521+version = "0.2.37"
520522source = "registry+https://github.com/rust-lang/crates.io-index"
521523dependencies = [
522524 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -649,8 +651,8 @@ dependencies = [
649651"checksum wasm-bindgen-macro 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "316e6e568644bfa2009698217cec8f612a4e2cd7460b127f52573991c7ad9f53"
650652"checksum wasm-bindgen-macro-support 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "29982ea76a7a35b5c3c052b6035abe9b84d8b6422d8881b68bd3472270a53eec"
651653"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"
653−"checksum wasm-bindgen-test-macro 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "e1feb1af4e075b19eab744a8ed0da087b024ae79afc0b35e49c3ca9a4a1c327b"
654+"checksum wasm-bindgen-test 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "8e8b80ffed02a5971865b6e6769398c3581b234a47c301193c44e7766b1a7a64"
655+"checksum wasm-bindgen-test-macro 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "92b66e86a39d33a9d7d46ae7a8c90e692ff4b7ecc0a69075a060511bc687562d"
654656"checksum wasm-bindgen-webidl 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "780c16a1a26d28695ac9af7176e11dbc3c188e6d0b3095ad51cc2d0e9e31fcc9"
655657"checksum web-sys 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "02c19f8ab50a089f9d7c8070c191a919c85d10a4a37c5777a08e8f94ec12c0fc"
656658"checksum weedle 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26a4c67f132386d965390b8a734d5d10adbcd30eb5cc74bd9229af8b83f10044"
Cargo.toml+1
@@ -26,6 +26,7 @@ features = [
2626
2727[dev-dependencies]
2828console_log = "0.1.2"
29+dodrio-js-api = { version = "=0.1.0", path = "./crates/js-api" }
2930log = "0.4.6"
3031wasm-bindgen-test = "0.2.36"
3132
crates/js-api/Cargo.toml+3
@@ -10,3 +10,6 @@ wasm-bindgen = "0.2.37"
1010js-sys = "0.3.14"
1111wasm-bindgen-futures = "0.3.14"
1212futures = "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);
1515
1616pub mod cached;
1717pub mod events;
18+pub mod js_api;
1819pub mod render;
1920
2021pub fn window() -> web_sys::Window {