Clone in a terminal:
Commit 6136236
Johan Mattsson - 2026-07-12T19:46:24+00:00
613623627710c07eeb7e21a1910c715c58c34263johan.mattsson.m@gmail.com

Tests and examples

Changed files (8)

Diff

diff --git a/Cargo.lock b/Cargo.lockindex 9676dc1..1fb6455 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3,6 +3,23 @@ version = 4  [[package]]+name = "async-trait"+version = "0.1.89"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 2.0.117",+]++[[package]]+name = "autocfg"+version = "1.5.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"++[[package]] name = "bincode" version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index"@@ -24,6 +41,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"  [[package]]+name = "cast"+version = "0.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"++[[package]]+name = "cc"+version = "1.2.67"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e17dd265a7d0f31ef544e1b20e03add05d3b45b491b633b10d67145d2acc1a38"+dependencies = [+ "find-msvc-tools",+ "shlex",+]++[[package]] name = "cfg-if" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index"@@ -36,6 +69,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"  [[package]]+name = "find-msvc-tools"+version = "0.1.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"++[[package]] name = "form_urlencoded" version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index"@@ -370,13 +409,12 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"  [[package]] name = "js-sys"-version = "0.3.99"+version = "0.3.103" source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "142bc4740e452c1e57ade0cbc129f139c9093e354346f0872ef985f4f5cf5f11"+checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" dependencies = [  "cfg-if",  "futures-util",- "once_cell",  "wasm-bindgen", ] @@ -417,6 +455,7 @@ dependencies = [  "serde-wasm-bindgen 0.5.0",  "wasm-bindgen",  "wasm-bindgen-futures",+ "wasm-bindgen-test",  "web-sys", ] @@ -441,18 +480,59 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"  [[package]]+name = "libm"+version = "0.2.16"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"++[[package]] name = "memchr" version = "2.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8"  [[package]]+name = "minicov"+version = "0.3.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4869b6a491569605d66d3952bcdf03df789e5b536e5f0cf7758a7f08a55ae24d"+dependencies = [+ "cc",+ "walkdir",+]++[[package]]+name = "nu-ansi-term"+version = "0.50.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"+dependencies = [+ "windows-sys",+]++[[package]]+name = "num-traits"+version = "0.2.19"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"+dependencies = [+ "autocfg",+ "libm",+]++[[package]] name = "once_cell" version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"  [[package]]+name = "oorandom"+version = "11.1.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"++[[package]] name = "percent-encoding" version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index"@@ -559,6 +639,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"  [[package]]+name = "same-file"+version = "1.0.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"+dependencies = [+ "winapi-util",+]++[[package]] name = "serde" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index"@@ -636,6 +725,12 @@ dependencies = [ ]  [[package]]+name = "shlex"+version = "2.0.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"++[[package]] name = "slab" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index"@@ -732,6 +827,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"  [[package]]+name = "walkdir"+version = "2.5.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"+dependencies = [+ "same-file",+ "winapi-util",+]++[[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index"@@ -739,9 +844,9 @@ checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"  [[package]] name = "wasm-bindgen"-version = "0.2.122"+version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "3ed04576f974d2b2fba0f38c51dbc5518011e38c36bf1143164be765528fd409"+checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" dependencies = [  "cfg-if",  "once_cell",@@ -752,9 +857,9 @@ dependencies = [  [[package]] name = "wasm-bindgen-futures"-version = "0.4.72"+version = "0.4.76" source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9473dbd2991ae90b6291c3c32c30c6187ac49aa32f9905d1cce280ec1e110b0f"+checksum = "c62df1340f32221cb9c54d6a27b030e3dba64361d4a95bed55f9aacb44da291d" dependencies = [  "js-sys",  "wasm-bindgen",@@ -762,9 +867,9 @@ dependencies = [  [[package]] name = "wasm-bindgen-macro"-version = "0.2.122"+version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "916151b09da36bd82f6615cbf3a419e2f0ba23a03c6160e8e92eb6bd4aa1dec6"+checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" dependencies = [  "quote",  "wasm-bindgen-macro-support",@@ -772,9 +877,9 @@ dependencies = [  [[package]] name = "wasm-bindgen-macro-support"-version = "0.2.122"+version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "299047362ccbfce148b67ab7e73349f77748e00c8296f9542adfad2ad82c5c5e"+checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" dependencies = [  "bumpalo",  "proc-macro2",@@ -785,24 +890,87 @@ dependencies = [  [[package]] name = "wasm-bindgen-shared"-version = "0.2.122"+version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9a929b2c61f11ba3e9bc35b50c1f25cb38e0e892c0c231ae2b8cf78d5dad4437"+checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" dependencies = [  "unicode-ident", ]  [[package]]+name = "wasm-bindgen-test"+version = "0.3.76"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2a0d555ca874445df8d314f94f5c948a4e74e5418f332c89f660a3d8310a96f4"+dependencies = [+ "async-trait",+ "cast",+ "js-sys",+ "libm",+ "minicov",+ "nu-ansi-term",+ "num-traits",+ "oorandom",+ "serde",+ "serde_json",+ "wasm-bindgen",+ "wasm-bindgen-futures",+ "wasm-bindgen-test-macro",+ "wasm-bindgen-test-shared",+]++[[package]]+name = "wasm-bindgen-test-macro"+version = "0.3.76"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "94eb68555b95bcea5e8cf4abe280b529049479fa995bfc23734af96a6aedc120"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 2.0.117",+]++[[package]]+name = "wasm-bindgen-test-shared"+version = "0.2.126"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c31d56021e873866c968588ed85ccdf56db5c426e44afdb4618c39895104b920"++[[package]] name = "web-sys"-version = "0.3.99"+version = "0.3.103" source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "6d621441cfc37b84979402712047321980c178f299193a3589d05b99e8763436"+checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141" dependencies = [  "js-sys",  "wasm-bindgen", ]  [[package]]+name = "winapi-util"+version = "0.1.11"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"+dependencies = [+ "windows-sys",+]++[[package]]+name = "windows-link"+version = "0.2.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"++[[package]]+name = "windows-sys"+version = "0.61.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"+dependencies = [+ "windows-link",+]++[[package]] name = "winnow" version = "0.7.15" source = "registry+https://github.com/rust-lang/crates.io-index"diff --git a/Cargo.toml b/Cargo.tomlindex 75a14b3..ab60443 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -1,5 +1,3 @@-cargo-features = ["edition2024"]- [package] name = "kaja-web" version = "1.0.2"@@ -15,7 +13,7 @@ gloo = "0.12.0" gloo-console = "0.4.0" kaja-html-macro = "1.0.3" wasm-bindgen = "0.2.122"-web-sys = { version = "0.3.99", features = ["Document", "HtmlDocument", "Element", "HtmlElement", "HtmlInputElement", "NodeList", "CssStyleDeclaration", "DomTokenList", "MessageEvent"] }+web-sys = { version = "0.3.99", features = ["Window", "Document", "HtmlDocument", "Element", "HtmlElement", "HtmlInputElement", "NodeList", "CssStyleDeclaration", "DomTokenList", "MessageEvent", "CustomElementRegistry", "ElementDefinitionOptions"] } serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.5" inventory = "0.3.24"@@ -23,3 +21,6 @@ js-sys = "0.3" wasm-bindgen-futures = "0.4.72" kaja-callback-macro = { path = "../kaja-callback-macro" } kaja-web-component = { path = "../kaja-web-component" }++[dev-dependencies]+wasm-bindgen-test = "0.3.76"diff --git a/examples/main-component/src/main_component.rs b/examples/main-component/src/main_component.rsindex 7ee5ff8..142643f 100644--- a/examples/main-component/src/main_component.rs+++ b/examples/main-component/src/main_component.rs@@ -11,19 +11,17 @@ struct Other {     the_number: i32, } -impl Component for Other {}- impl Component for Main {     fn connected(id: &str) {         log!("Main connected:", id);-        start_background(&id);+        start_background_task(&id);     }      fn update(id: &str) {         log!("Main update:", id);          let Some(element) = get_element_by_id(id) else {-            error!("Element not found:", id);+            error!("Main element not found:", id);             return;         }; @@ -32,27 +30,74 @@ impl Component for Main {             return;         }; -        let component = update_shared_component(&handle, |component| {-            component.test = !component.test;-        });+        let mut component = handle.borrow_mut();+        component.test = !component.test;++        component.render(&element);+    }+}++impl Component for Other {+    fn connected(id: &str) {+        log!("Other connected:", id);++        let Some(handle) = Self::get_component(&id) else {+            error!("Other component not found:", id);+            return;+        };++        let mut component = handle.borrow_mut();+        component.the_number += 1;+        component.render(id);+    }++    fn update(id: &str) {+        log!("Other update:", id);+    }+}++impl Other {+    fn render(&self, id: &str) {+        let Some(element) = get_element_by_id(id) else {+            error!("Other element not found:", id);+            return;+        };++        let content = html! {{+            <div>The number: $(self.the_number)</div>+        }}; -        let comp = handle.borrow();-        comp.render(&element);+        log!("Render Other");+        element.set_inner_html(&content);     } }  impl Main {     fn render(&self, element: &HtmlElement) {+        let content = html! {{+            <div>Known: $(self.test)</div>+            <other-component></other-component>+        }};+         log!("Render Main", self.test);-        element.set_inner_text(&self.test.to_string());+        element.set_inner_html(&content);     } } -fn start_background(id: &str) {+fn start_background_task(id: &str) {     let component_id = id.to_string(); +    log!("Starting background thread:", id);+     wasm_bindgen_futures::spawn_local(async move {         loop {+            let component_exists = Main::component_exists(&component_id);++            if !component_exists {+                log!("Stopping background thread:", component_id);+                break;+            }+             Main::update(component_id.as_str());             gloo_timers::future::TimeoutFuture::new(1000).await;         }diff --git a/src/lib.rs b/src/lib.rsindex 85fdadb..8671c0e 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -53,14 +53,6 @@ pub trait ComponentStorage: Default + Sized {     >; } -pub fn update_shared_component<T: Clone, F>(handle: &SharedComponent<T>, updater: F)-where-    F: FnOnce(&mut T),-{-    let mut component = handle.borrow_mut();-    updater(&mut component);-}- pub fn dispatch_component_update(component: &str, id: &str) -> bool {     for update in inventory::iter::<UpdateComponentFn> {         if update.component == component {diff --git a/src/prelude.rs b/src/prelude.rsindex 59bfe47..f95167e 100644--- a/src/prelude.rs+++ b/src/prelude.rs@@ -3,14 +3,15 @@ pub use crate::{     UpdateComponentFn, dispatch_component_update, get_callback, get_cookie_value, get_element,     get_element_by_id, get_elements, get_value, init_callbacks, inner_html, inner_text,     try_get_value, try_handle_component_update_message, try_inner_html, try_inner_text,-    update_shared_component, };  pub use gloo::console::error; pub use gloo::console::log; pub use serde_wasm_bindgen; +pub use std::cell::RefCell; pub use std::collections::HashMap;+pub use std::rc::Rc; pub use std::sync::{MutexGuard, RwLock};  pub use wasm_bindgen::JsCast;diff --git a/test.sh b/test.shnew file mode 100755index 0000000..ccdeb6a--- /dev/null+++ b/test.sh@@ -0,0 +1,4 @@+#!/bin/bash+set -e++wasm-pack test --chromediff --git a/tests/component.rs b/tests/component.rsnew file mode 100644index 0000000..70d5f69--- /dev/null+++ b/tests/component.rs@@ -0,0 +1,90 @@+use kaja_web::prelude::*;+use kaja_web_component::component;+use wasm_bindgen_test::*;+use web_sys::HtmlElement;++wasm_bindgen_test_configure!(run_in_browser);++fn setup_test() {+    let window = web_sys::window().unwrap();+    let document = window.document().unwrap();+    let body = document.body().unwrap();++    let test_div = document.get_element_by_id("test-div");++    if test_div.is_some() {+        let test_div = test_div.unwrap().unchecked_into::<HtmlElement>();+        test_div.set_inner_html("");+        return;+    }++    let test_div = document.create_element("div").unwrap();+    test_div.set_id("test-div");+    body.append_child(&test_div).unwrap();++    init_callbacks();+}++#[wasm_bindgen_test]+fn test_component() {+    setup_test();++    let content = html! {{+        <test-component1></test-component1>+    }};++    try_inner_html("#test-div", &content);++    let element = get_element("test-component1").unwrap();+    let id = element.id();+    log!("id", &id);+    assert!(id.starts_with("test-component1-"));++    let component = TestComponent1::get_component(&id).unwrap();+    let test_component = component.borrow();+    assert!(test_component.tested);+}+#[wasm_bindgen_test]+fn test_custom_id() {+    setup_test();++    let content = html! {{+        <test-component2 id="custom-id"></test-component2>+    }};++    try_inner_html("#test-div", &content);++    let element = get_element("test-component2").unwrap();+    assert_eq!(element.id(), "custom-id");++    let component = TestComponent2::get_component("custom-id").unwrap();+    let test_component = component.borrow();+    assert!(test_component.tested);+}++#[component("test-component1")]+struct TestComponent1 {+    tested: bool,+}++impl Component for TestComponent1 {+    fn connected(id: &str) {+        let component = TestComponent1::get_component(id).unwrap();+        let mut test_component = component.borrow_mut();+        test_component.tested = true;+    }+}++#[component("test-component2")]+struct TestComponent2 {+    tested: bool,+}++impl Component for TestComponent2 {+    fn connected(id: &str) {+        log!("connected TestComponent2:", id);+        let component = TestComponent2::get_component(id).unwrap();+        let mut test_component = component.borrow_mut();+        test_component.tested = true;+    }+}diff --git a/tests/dom.rs b/tests/dom.rsnew file mode 100644index 0000000..25fcfea--- /dev/null+++ b/tests/dom.rs@@ -0,0 +1,10 @@+use wasm_bindgen_test::*;++wasm_bindgen_test_configure!(run_in_browser);++#[wasm_bindgen_test]+fn test_dom() {+    let window = web_sys::window().unwrap();+    let document = window.document().unwrap();+    assert!(document.body().is_some());+}

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112