Clone in a terminal:
Commit b678d76
Johan Mattsson - 2026-07-13T08:11:17+00:00
b678d768aa71e48856cb777192155c2f08de84f9johan.mattsson.m@gmail.com

More tests

Changed files (7)

Diff

diff --git a/test-web.sh b/test-web.shnew file mode 100755index 0000000..a61c060--- /dev/null+++ b/test-web.sh@@ -0,0 +1,4 @@+#!/bin/bash+set -e++wasm-pack test --chrome --test alltestsdiff --git a/test.sh b/test.shindex a61c060..2fed319 100755--- a/test.sh+++ b/test.sh@@ -1,4 +1,4 @@ #!/bin/bash set -e -wasm-pack test --chrome --test alltests+wasm-pack test --headless --chrome --test alltestsdiff --git a/tests/alltests.rs b/tests/alltests.rsnew file mode 100644index 0000000..ae83f3a--- /dev/null+++ b/tests/alltests.rs@@ -0,0 +1,10 @@+use wasm_bindgen_test::*;++pub mod common;+pub use common::setup_test;++wasm_bindgen_test_configure!(run_in_browser);++mod attributes;+mod component;+mod simple;diff --git a/tests/attributes.rs b/tests/attributes.rsnew file mode 100644index 0000000..5ab7cc1--- /dev/null+++ b/tests/attributes.rs@@ -0,0 +1,112 @@+#[path = "common.rs"]+mod common;++use kaja_web::prelude::*;+use kaja_web_component::component;+use wasm_bindgen_test::*;++use std::str::FromStr;++fn parse_value<T>(s: &str) -> Option<T>+where+    T: FromStr,+{+    s.parse::<T>().ok()+}++fn set() {+    let n: u32 = parse_value("42").unwrap();+    let f: f64 = parse_value("3.14").unwrap();+    let b: bool = parse_value("true").unwrap();+}++#[wasm_bindgen_test]+fn test_getter_and_setter() {+    common::setup_test();++    let id = "test-get-set-id";+    let content = html! {{+        <test-component2 id="$id" test_attribute="initial-value-for-attribute"></test-component1>+    }};++    inner_html("#test-div", &content).unwrap();++    {+        // borrows first time and release+        let component = TestComponent2::get_component(id).unwrap();+        let test_component = component.borrow();+        assert_eq!(test_component.test_attribute, "initial-value-for-attribute");+    }++    {+        // borrows again+        let res = TestComponent2::set_test_attribute(id, "updated-value-in-component".to_string());+        assert!(res.is_ok());+    }++    let test_attribute_from_getter = TestComponent2::get_test_attribute(id).unwrap();+    assert_eq!(test_attribute_from_getter, "updated-value-in-component");+}++#[component("test-component2")]+struct TestComponent2 {+    attribute_updated: bool,++    //#[html_attribute]+    test_attribute: String,+}++impl TestComponent2 {+    fn set_test_attribute(id: &str, value: String) -> Result<()> {+        let attribute_name = "test_attribute";+        let attribute_string = value.clone();++        let element = get_element_by_id(id).ok_or(Error)?;++        return element+            .set_attribute(attribute_name, &attribute_string)+            .map_err(|_| Error);+    }++    fn get_test_attribute(id: &str) -> Option<String> {+        let component = Self::get_component(id)?;+        let test_attribute = component.borrow().test_attribute.clone();+        Some(test_attribute)+    }++    fn observed_attributes() -> &'static [&'static str] {+        &["test_attribute"]+    }++    fn attribute_changed_update_struct(id: &str, name: &str, _old_value: &str, new_value: &str) {+        log!("Attribute changed internally", name, "to", new_value);+        let component = Self::get_component(id);++        if component.is_none() {+            error!("No component found for", id);+            return;+        }++        let component = component.unwrap();+        let component = component.try_borrow_mut();++        if component.is_err() {+            error!(+                "Failed to borrow component for",+                id, "in attribute_changed of ", name+            );+            return;+        }++        if name == "test_attribute" {+            component.unwrap().test_attribute = new_value.to_string();+        }+    }+}++impl Component for TestComponent2 {+    fn attribute_changed(id: &str, name: &str, old_value: &str, new_value: &str) {+        Self::attribute_changed_update_struct(id, name, old_value, new_value);+        log!("Attribute changed", name, "to", new_value);+    }+}diff --git a/tests/common.rs b/tests/common.rsnew file mode 100644index 0000000..236066d--- /dev/null+++ b/tests/common.rs@@ -0,0 +1,22 @@+use kaja_web::prelude::*;+use web_sys::HtmlElement;++pub 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();+}diff --git a/tests/component.rs b/tests/component.rsindex 72b9756..f3f10e5 100644--- a/tests/component.rs+++ b/tests/component.rs@@ -77,35 +77,6 @@ fn test_attribute_updated() {     } } -#[wasm_bindgen_test]-fn test_getter_and_setter() {-    common::setup_test();--    let content = html! {{-        <test-component1 id="test-get-set-id" test_attribute="initial-value-for-attribute"></test-component1>-    }};--    try_inner_html("#test-div", &content);--    let id = "test-get-set-id";--    {-        let component = TestComponent1::get_component(id).unwrap();-        let test_component = component.borrow();--        assert_eq!(test_component.test_attribute, "initial-value-for-attribute");-    }--    {-        // borrows again-        let res = TestComponent1::set_test_attribute(id, "updated-value-in-component".to_string());-        assert!(res.is_ok());-    }--    let test_attribute_from_getter = TestComponent1::get_test_attribute(id).unwrap();-    assert_eq!(test_attribute_from_getter, "updated-value-in-component");-}- #[component("test-component1")] struct TestComponent1 {     tested: bool,@@ -115,36 +86,6 @@ struct TestComponent1 {     test_attribute: String, } -impl TestComponent1 {-    fn set_test_attribute(id: &str, value: String) -> Result<()> {-        let attribute_name = "test_attribute";-        let attribute_string = value.clone();--        let element = get_element_by_id(id).ok_or(Error)?;--        {-            let component = Self::get_component(id).ok_or(Error)?;-            let mut component = component.try_borrow_mut().map_err(|_| Error)?;--            component.test_attribute = value;-        }--        return element-            .set_attribute(attribute_name, &attribute_string)-            .map_err(|_| Error);-    }--    fn get_test_attribute(id: &str) -> Option<String> {-        let component = Self::get_component(id)?;-        let test_attribute = component.borrow().test_attribute.clone();-        Some(test_attribute)-    }--    fn observed_attributes() -> &'static [&'static str] {-        &["test_attribute"]-    }-}- impl Component for TestComponent1 {     fn connected(id: &str) {         let component = TestComponent1::get_component(id).unwrap();diff --git a/tests/simple.rs b/tests/simple.rsnew file mode 100644index 0000000..04c1638--- /dev/null+++ b/tests/simple.rs@@ -0,0 +1,22 @@+use kaja_web::{get_cookie_value, set_cookie_value};+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());+}++#[wasm_bindgen_test]+fn test_cookie() {+    set_cookie_value("test-cookie-value", "some-value").unwrap();++    let val = get_cookie_value("test-cookie-value").unwrap();+    assert_eq!(val, "some-value");++    let not_in_cookie = get_cookie_value("not-in-cookie");+    assert!(not_in_cookie.is_none());+}

Contact

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