//! Tests for `@burokku/react` on the test backend. They live in this example //! because it is the crate that bundles React: `tests/harness.ts` exposes //! `h` (React's `createElement`), `useState`, `Activity`, `createRoot`, //! `flushSync`, and `describe`, which prints a subtree as `tag(children)`. use burokku::{ test::{ElementState, InputEvent, MouseButton, PhysicalPosition}, Burokku, BurokkuError, TestApp, TestBackend, }; use std::future::Future; #[test] fn host_components_build_elements_and_text_nodes() -> Result<(), BurokkuError> { run(|mut app| async move { app.eval::<()>( r#" globalThis.root = createRoot(); flushSync(() => root.render( h("window", null, h("flex", null, h("text", null, "Hello, ", "world", h("text", null, "!")), h("div", null))))); "#, ) .await?; app.present().await?; assert_eq!( app.eval::("describe(app)").await?, r#"app(window(flex(text("Hello, ","world",text("!")),div())))"# ); Ok(()) }) } #[test] fn keyed_children_move_without_being_recreated() -> Result<(), BurokkuError> { run(|app| async move { app.eval::<()>( r#" globalThis.root = createRoot(); globalThis.list = keys => h("window", null, h("flex", null, keys.map(key => h("text", { key }, key)))); flushSync(() => root.render(list(["a", "b", "c"]))); globalThis.items = app.firstChild.firstChild; globalThis.before = [...items.childNodes]; flushSync(() => root.render(list(["c", "a"]))); "#, ) .await?; assert_eq!( app.eval::("describe(items)").await?, r#"flex(text("c"),text("a"))"# ); assert!( app.eval::( "items.childNodes[0] === before[2] && items.childNodes[1] === before[0] \ && before[1].parentNode === null" ) .await? ); Ok(()) }) } #[test] fn style_props_set_changed_keys_and_remove_dropped_ones() -> Result<(), BurokkuError> { run(|mut app| async move { app.eval::<()>( r#" globalThis.root = createRoot(); globalThis.boxed = style => h("window", null, h("div", { style })); flushSync(() => root.render(boxed({ width: "40px", height: "20px" }))); "#, ) .await?; app.present().await?; assert_eq!(box_size(&app).await?, [40.0, 20.0]); app.eval::<()>(r#"flushSync(() => root.render(boxed({ width: "60px" })))"#) .await?; app.present().await?; assert_eq!(box_size(&app).await?, [60.0, 0.0]); Ok(()) }) } #[test] fn other_props_set_attributes_and_nullish_values_remove_them() -> Result<(), BurokkuError> { run(|mut app| async move { app.eval::<()>( r#" globalThis.root = createRoot(); globalThis.titled = (title, tabindex) => h("window", { title }, h("div", { tabindex })); flushSync(() => root.render(titled("First", 0))); "#, ) .await?; app.present().await?; assert_eq!(app.window_title().as_deref(), Some("First")); assert_eq!( app.eval::("app.firstChild.firstChild.getAttribute('tabindex')") .await?, "0" ); app.eval::<()>(r#"flushSync(() => root.render(titled("Second", undefined)))"#) .await?; app.present().await?; assert_eq!(app.window_title().as_deref(), Some("Second")); assert!( !app.eval::("app.firstChild.firstChild.hasAttribute('tabindex')") .await? ); Ok(()) }) } #[test] fn clicks_call_the_handler_of_the_latest_props() -> Result<(), BurokkuError> { run(|mut app| async move { app.eval::<()>( r#" globalThis.root = createRoot(); globalThis.calls = []; globalThis.target = onClick => h("window", null, h("div", { style: { width: "100px", height: "100px" }, onClick })); flushSync(() => root.render(target(() => calls.push("first")))); "#, ) .await?; app.present().await?; click(&mut app, 50.0, 50.0)?; app.eval::<()>(r#"flushSync(() => root.render(target(() => calls.push("second"))))"#) .await?; click(&mut app, 50.0, 50.0)?; app.eval::<()>("flushSync(() => root.render(target(undefined)))") .await?; click(&mut app, 50.0, 50.0)?; assert_eq!(app.eval::("calls.join()").await?, "first,second"); Ok(()) }) } #[test] fn state_set_in_a_click_handler_commits_before_the_click_task_ends() -> Result<(), BurokkuError> { run(|mut app| async move { app.eval::<()>( r#" globalThis.seen = []; function Counter() { const [count, setCount] = useState(0); const onClick = () => { setCount(count + 1); // React queued its flush first, so this runs after the commit. queueMicrotask(() => seen.push(app.firstChild.textContent)); }; return h("window", null, h("flex", { style: { width: "100px", height: "100px" }, onClick }, h("text", null, `Clicked ${count}`))); } globalThis.root = createRoot(); flushSync(() => root.render(h(Counter))); "#, ) .await?; app.present().await?; click(&mut app, 50.0, 50.0)?; assert_eq!(app.eval::("seen.join()").await?, "Clicked 1"); Ok(()) }) } #[test] fn text_outside_a_text_element_is_an_uncaught_error() -> Result<(), BurokkuError> { run(|app| async move { app.eval::<()>( r#" globalThis.errors = []; globalThis.root = createRoot(app, { onUncaughtError: error => errors.push(error.message), }); flushSync(() => root.render(h("window", null, h("flex", null, "stray")))); "#, ) .await?; assert_eq!( app.eval::("errors.join()").await?, r#"Text must be inside a , , or