Lines
0 %
Functions
Branches
100 %
// -- -*- mode: rust -*-
//! Expanding a list row loads its detail once, through real htmx.
use nomisync_frontend::setup_htmx_listener;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;
use web_sys::Element;
wasm_bindgen_test_configure!(run_in_browser);
const ROWS: &str = include_str!("fixtures/transaction_list.html");
const HTMX: &str = include_str!("../../static/scripts/htmx.min.js");
// Each send() records its url and answers with the next `window.__replies`.
const FAKE_XHR: &str = r#"
(() => {
window.__replies = [];
window.__sent = [];
const Real = window.XMLHttpRequest;
window.XMLHttpRequest = class extends Real {
open(method, url) { this.__url = url; }
setRequestHeader() {}
send() {
window.__sent.push(this.__url);
const reply = window.__replies.shift();
const props = { status: 200, response: reply, responseText: reply, responseURL: location.href };
for (const [key, value] of Object.entries(props)) {
Object.defineProperty(this, key, { value });
}
this.getAllResponseHeaders = () => "content-type: text/html\r\n";
this.getResponseHeader = (name) => (name.toLowerCase() === "content-type" ? "text/html" : null);
setTimeout(() => this.onload(), 0);
};
})()
"#;
const DETAIL: &str = "<table class=\"splits-table\"><tr><td>Assets:Cash</td></tr></table>";
fn global_eval(source: &str) -> Result<JsValue, JsValue> {
js_sys::Function::new_with_args("src", "return (0, eval)(src)")
.call1(&JsValue::NULL, &source.into())
fn mount(html: &str) -> Result<Element, JsValue> {
global_eval(HTMX)?;
global_eval(FAKE_XHR)?;
setup_htmx_listener();
let document = web_sys::window()
.and_then(|w| w.document())
.ok_or("no document")?;
let host = document.create_element("div")?;
host.set_inner_html(html);
document.body().ok_or("no body")?.append_child(&host)?;
global_eval("htmx.process")?
.dyn_into::<js_sys::Function>()?
.call1(&JsValue::NULL, &host)?;
Ok(host)
async fn toggle(details: &Element, open: bool) -> Result<(), JsValue> {
let toggled = js_sys::eval(
"new Promise(r => document.addEventListener('toggle', r, { capture: true, once: true }))",
)?;
let toggled = JsFuture::from(toggled.dyn_into::<js_sys::Promise>()?);
js_sys::Reflect::set(details, &"open".into(), &open.into())?;
toggled.await?;
let settled = js_sys::eval("new Promise(r => setTimeout(r, 50))")?;
JsFuture::from(settled.dyn_into::<js_sys::Promise>()?).await?;
Ok(())
fn sent() -> Result<Vec<String>, JsValue> {
let json = global_eval("JSON.stringify(window.__sent)")?
.as_string()
.ok_or("no log")?;
serde_json::from_str(&json).map_err(|e| e.to_string().into())
#[wasm_bindgen_test]
async fn expanding_a_row_loads_its_detail_once() -> Result<(), JsValue> {
let host = mount(ROWS)?;
assert!(sent()?.is_empty(), "nothing loads before an expand");
global_eval(&format!(
"window.__replies.push({})",
serde_json::to_string(DETAIL).map_err(|e| e.to_string())?
))?;
let rows = host.query_selector_all("details")?;
let first: Element = rows.item(0).ok_or("no first row")?.dyn_into()?;
toggle(&first, true).await?;
toggle(&first, false).await?;
let log = sent()?;
assert_eq!(log.len(), 1, "{log:?}");
assert!(log[0].starts_with("/api/transaction/detail/"), "{log:?}");
let container = first
.query_selector(".splits-container")?
.ok_or("no container")?;
assert!(container.inner_html().contains("Assets:Cash"));
let second: Element = rows.item(1).ok_or("no second row")?.dyn_into()?;
let other = second
assert!(other.inner_html().is_empty());
host.remove();