| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use makeover_layout as layout; |
| 11 |
use quasi_http::Serves; |
| 12 |
use quasi_router::screen::{ |
| 13 |
Accepted, Act, Candidate, Canvas, Cell, CellKey, Choice, Column, Consult, Document, Field, |
| 14 |
Figure, Jump, Meter, Prose, Repeat, Repeating, Rest, Row, Tag, ThemeChoice, |
| 15 |
}; |
| 16 |
use quasi_router::{Action, Frame, Node, RegionKind, Reveal, Richness, Run, Screen, Slot, Trust}; |
| 17 |
|
| 18 |
use crate::{Emit, Shell, Webview}; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
fn strip_discovery(head: &str) -> String { |
| 26 |
let mut out = String::with_capacity(head.len()); |
| 27 |
let mut rest = head; |
| 28 |
while let Some(start) = rest.find("<meta property=\"og:").or_else(|| { |
| 29 |
rest.find("<meta name=\"twitter:") |
| 30 |
.or_else(|| rest.find("<meta name=\"robots\"")) |
| 31 |
.or_else(|| rest.find("<link rel=\"canonical\"")) |
| 32 |
}) { |
| 33 |
out.push_str(&rest[..start]); |
| 34 |
let end = rest[start..].find('>').expect("a tag closes") + start + 1; |
| 35 |
rest = &rest[end..]; |
| 36 |
} |
| 37 |
out.push_str(rest); |
| 38 |
out |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
fn banded() -> quasi_router::Band { |
| 47 |
quasi_router::Band::new() |
| 48 |
.branded(quasi_router::Brand::new("Makenot.work", Action::get("/")).marking(".")) |
| 49 |
.searching(Field::new(layout::FieldKind::Text, "q", "Search")) |
| 50 |
.disclosing(quasi_router::Disclose::Narrow) |
| 51 |
} |
| 52 |
|
| 53 |
fn render(screen: &Screen) -> String { |
| 54 |
Webview::new().screen(screen) |
| 55 |
} |
| 56 |
|
| 57 |
fn fragment(node: &Node) -> String { |
| 58 |
Webview::new().fragment(node) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
fn one_of_everything() -> Vec<Node> { |
| 68 |
vec![ |
| 69 |
Node::page("Tasks"), |
| 70 |
Node::text("plain"), |
| 71 |
Node::rich("**bold** and `code`"), |
| 72 |
Node::Act(Act::new("Save", Action::post("/save"))), |
| 73 |
Node::Link { |
| 74 |
text: "Docs".to_owned(), |
| 75 |
action: Action::get("/docs"), |
| 76 |
}, |
| 77 |
Node::Figure(Figure::new("17", "Streak")), |
| 78 |
Node::since(std::time::SystemTime::UNIX_EPOCH), |
| 79 |
Node::until(std::time::SystemTime::UNIX_EPOCH), |
| 80 |
Node::age(std::time::SystemTime::UNIX_EPOCH), |
| 81 |
Node::Image(quasi_router::Image::new("/cover.png", "The library view")), |
| 82 |
Node::Token(Tag::badge("beta")), |
| 83 |
Node::banner(layout::Tone::Info, "Saved"), |
| 84 |
Node::empty("Nothing here yet"), |
| 85 |
Node::Field(Box::new(Field::new( |
| 86 |
layout::FieldKind::Text, |
| 87 |
"title", |
| 88 |
"Title", |
| 89 |
))), |
| 90 |
Node::Form { |
| 91 |
marks: ::quasi_router::stage::Marks::none(), |
| 92 |
action: Action::post("/save"), |
| 93 |
submit: "Save".to_owned(), |
| 94 |
fields: vec![Field::new(layout::FieldKind::Text, "title", "Title")], |
| 95 |
}, |
| 96 |
Node::list([Row::new("One")]), |
| 97 |
Node::Table { |
| 98 |
marks: ::quasi_router::stage::Marks::none(), |
| 99 |
columns: vec![Column::new("Name")], |
| 100 |
rows: vec![Row::cells([Cell::new("One")])], |
| 101 |
more: None, |
| 102 |
}, |
| 103 |
Node::Timeline { |
| 104 |
marks: ::quasi_router::stage::Marks::none(), |
| 105 |
track: layout::Track::DAY, |
| 106 |
entries: vec![quasi_router::Placed::new(540, 45, Row::new("Standup"))], |
| 107 |
focus: Some(540), |
| 108 |
}, |
| 109 |
Node::Meter(Meter::new(3, 6)), |
| 110 |
Node::stats([Figure::new("17", "Streak")]), |
| 111 |
Node::Region(Slot::new("nested", RegionKind::Pane)), |
| 112 |
] |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn every_described_node_emits_markup() { |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
for node in one_of_everything() { |
| 121 |
let html = fragment(&node); |
| 122 |
assert!(!html.is_empty(), "nothing came out for {node:?}"); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn the_exhaustiveness_list_holds_one_of_every_member() { |
| 128 |
|
| 129 |
|
| 130 |
assert_eq!( |
| 131 |
one_of_everything().len(), |
| 132 |
21, |
| 133 |
"one of every `Node` member, in declaration order" |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn a_screen_is_a_whole_document() { |
| 139 |
let html = render(&Screen::list_detail("Tasks", false)); |
| 140 |
assert!(html.starts_with("<!doctype html><html lang=\"en\">")); |
| 141 |
assert!(html.contains("<title>Tasks</title>")); |
| 142 |
assert!(html.ends_with("</body></html>")); |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn a_fragment_is_not() { |
| 147 |
let html = fragment(&Node::text("hello")); |
| 148 |
assert!(!html.contains("<html")); |
| 149 |
assert!(!html.contains("<body")); |
| 150 |
assert_eq!(html, "<p class=\"text\">hello</p>"); |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn the_document_configures_nothing_about_how_a_status_swaps() { |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
let html = render(&Screen::list_detail("Tasks", false)); |
| 161 |
assert!(!html.contains("htmx-config"), "{html}"); |
| 162 |
assert!(!html.contains("responseHandling"), "{html}"); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn the_title_is_escaped_into_the_head() { |
| 167 |
let html = render(&Screen::list_detail("</title><script>x()</script>", false)); |
| 168 |
assert!(!html.contains("<script>x()")); |
| 169 |
assert!(html.contains("</title>")); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn a_control_asks_for_the_morph_swap_htmx_ships() { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
let screen = Screen::list_detail("Tasks", false) |
| 180 |
.with(Slot::new("main", RegionKind::Pane).with(Node::act("Go", Action::get("/go")))); |
| 181 |
let html = render(&screen); |
| 182 |
assert!(html.contains("hx-swap=\"outerMorph\""), "{html}"); |
| 183 |
assert!(!html.contains("hx-ext"), "{html}"); |
| 184 |
assert!(!html.contains("idiomorph"), "{html}"); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn an_action_becomes_the_verb_it_names() { |
| 189 |
let get = fragment(&Node::act("Open", Action::get("/tasks/1"))); |
| 190 |
assert!(get.contains("hx-get=\"/tasks/1\"")); |
| 191 |
assert!(!get.contains("hx-post")); |
| 192 |
|
| 193 |
let post = fragment(&Node::act("Delete", Action::post("/tasks/1/delete"))); |
| 194 |
assert!(post.contains("hx-post=\"/tasks/1/delete\"")); |
| 195 |
assert!(!post.contains("hx-get")); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn a_delete_and_a_put_reach_the_route_the_server_actually_answers() { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
let removed = fragment(&Node::act("Delete", Action::delete("/api/blog/7"))); |
| 205 |
assert!(removed.contains("hx-delete=\"/api/blog/7\""), "{removed}"); |
| 206 |
|
| 207 |
let replaced = fragment(&Node::act("Move", Action::put("/api/items/7/move"))); |
| 208 |
assert!( |
| 209 |
replaced.contains("hx-put=\"/api/items/7/move\""), |
| 210 |
"{replaced}" |
| 211 |
); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
for html in [&removed, &replaced] { |
| 216 |
assert!(html.contains("<button"), "{html}"); |
| 217 |
assert!(!html.contains("href"), "{html}"); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn a_read_of_a_route_is_a_link_and_a_write_is_not() { |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
let read = fragment(&Node::act("Open", Action::get("/tasks/1"))); |
| 229 |
assert!(read.contains("<a ")); |
| 230 |
assert!(read.contains("href=\"/tasks/1\"")); |
| 231 |
assert!(read.contains("hx-get=\"/tasks/1\"")); |
| 232 |
assert!(!read.contains("<button")); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
let write = fragment(&Node::act("Delete", Action::post("/tasks/1/delete"))); |
| 239 |
assert!(write.contains("<button")); |
| 240 |
assert!(!write.contains("href")); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
let away = fragment(&Node::act("Docs", Action::external("https://example.com"))); |
| 245 |
assert!(away.contains("href=\"https://example.com\"")); |
| 246 |
assert!(away.contains("rel=\"noopener noreferrer\"")); |
| 247 |
assert!(!away.contains("hx-get")); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn a_reads_values_are_its_address_and_nothing_is_sent_beside_it() { |
| 252 |
let action = Action::get("/tasks") |
| 253 |
.with("filter", "open") |
| 254 |
.with("sort", "due"); |
| 255 |
let html = fragment(&Node::act("Filter", action)); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
assert!(html.contains("hx-get=\"/tasks?filter=open&sort=due\"")); |
| 260 |
assert!(!html.contains("hx-vals")); |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
assert!(html.contains("href=\"/tasks?filter=open&sort=due\"")); |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
fn a_write_from_a_filtered_list_keeps_the_two_status_values_apart() { |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
let action = Action::post("/problems/7/status") |
| 276 |
.with("status", "Dismissed") |
| 277 |
.carrying("status", "Open"); |
| 278 |
let html = fragment(&Node::act("Dismiss", action)); |
| 279 |
|
| 280 |
assert!(html.contains("hx-post=\"/problems/7/status?status=Open\"")); |
| 281 |
assert!(html.contains("hx-vals=\"{"status":"Dismissed"}\"")); |
| 282 |
|
| 283 |
|
| 284 |
assert!(!html.contains("href=")); |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
fn a_repeated_name_survives_the_wire_rather_than_collapsing() { |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
let action = Action::get("/tags") |
| 294 |
.with("tag", "rust") |
| 295 |
.with("tag", "router"); |
| 296 |
let html = fragment(&Node::act("Both", action)); |
| 297 |
|
| 298 |
assert!(html.contains("hx-get=\"/tags?tag=rust&tag=router\"")); |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn a_param_value_cannot_escape_the_attribute_or_the_json() { |
| 303 |
let action = Action::post("/search").with("q", "\" onload=\"steal()"); |
| 304 |
let html = fragment(&Node::act("Search", action)); |
| 305 |
|
| 306 |
assert!(!html.contains("onload=\"steal()")); |
| 307 |
assert!(html.contains("\\"")); |
| 308 |
|
| 309 |
let action = Action::post("/search").with("q", "a\\b\nc"); |
| 310 |
let html = fragment(&Node::act("Search", action)); |
| 311 |
assert!(html.contains("a\\\\b\\nc")); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn no_control_ever_names_its_own_target() { |
| 316 |
|
| 317 |
|
| 318 |
let screen = Screen::list_detail("Tasks", false) |
| 319 |
.with( |
| 320 |
Slot::new("list", RegionKind::Pane) |
| 321 |
.with(Node::list([ |
| 322 |
Row::new("One").activate(Action::get("/tasks/1")) |
| 323 |
])) |
| 324 |
.with(Node::act("New", Action::post("/tasks"))), |
| 325 |
) |
| 326 |
.with(Slot::new("detail", RegionKind::Pane).with(Node::text("Nothing selected"))); |
| 327 |
|
| 328 |
let html = render(&screen); |
| 329 |
assert!(!html.contains("hx-target")); |
| 330 |
assert!(!html.contains("hx-retarget")); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
fn htmx_lines(source: &str) -> usize { |
| 339 |
source |
| 340 |
.lines() |
| 341 |
.filter(|line| !line.trim_start().starts_with("//")) |
| 342 |
.filter(|line| line.contains("hx-")) |
| 343 |
.count() |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
fn body_of<'a>(source: &'a str, signature: &str) -> &'a str { |
| 348 |
source |
| 349 |
.split(signature) |
| 350 |
.nth(1) |
| 351 |
.unwrap_or_else(|| panic!("{signature} exists")) |
| 352 |
.split("\n/// ") |
| 353 |
.next() |
| 354 |
.expect("the function has a body") |
| 355 |
} |
| 356 |
|
| 357 |
#[test] |
| 358 |
fn htmx_enters_in_exactly_three_functions() { |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
let source = include_str!("node.rs"); |
| 379 |
let seam = [ |
| 380 |
"pub(crate) fn action_attrs", |
| 381 |
"pub(crate) fn oob_html", |
| 382 |
"fn preserve_attr", |
| 383 |
] |
| 384 |
.iter() |
| 385 |
.map(|signature| htmx_lines(body_of(source, signature))) |
| 386 |
.sum::<usize>(); |
| 387 |
|
| 388 |
assert_eq!( |
| 389 |
htmx_lines(source), |
| 390 |
seam, |
| 391 |
"every emitted hx- attribute should come from action_attrs, oob_html or \ |
| 392 |
preserve_attr" |
| 393 |
); |
| 394 |
assert!(seam >= 4, "verb, vals, swap and the out-of-band address"); |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
fn a_disabled_control_carries_no_address() { |
| 399 |
let act = Act::new("Delete", Action::post("/tasks/1/delete")).disabled(); |
| 400 |
let html = fragment(&Node::Act(act)); |
| 401 |
|
| 402 |
assert!(html.contains("disabled")); |
| 403 |
assert!(!html.contains("hx-post")); |
| 404 |
} |
| 405 |
|
| 406 |
#[test] |
| 407 |
fn a_row_is_a_control_only_when_selecting_it_does_something() { |
| 408 |
|
| 409 |
let live = fragment(&Node::list([Row::new("One").activate(Action::get("/1"))])); |
| 410 |
assert!(live.contains("<a class=\"row-activate\"")); |
| 411 |
assert!(live.contains("href=\"/1\"")); |
| 412 |
|
| 413 |
let inert = fragment(&Node::list([Row::new("One")])); |
| 414 |
assert!(!inert.contains("<button")); |
| 415 |
assert!(!inert.contains("<a ")); |
| 416 |
assert!(inert.contains("<span class=\"row-primary\">One</span>")); |
| 417 |
} |
| 418 |
|
| 419 |
#[test] |
| 420 |
fn the_current_row_says_so_to_a_screen_reader() { |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
let html = fragment(&Node::list([Row { |
| 425 |
current: true, |
| 426 |
..Row::new("One") |
| 427 |
}])); |
| 428 |
assert!(html.contains("aria-current=\"true\"")); |
| 429 |
assert!(html.contains("row-current")); |
| 430 |
|
| 431 |
assert!(!html.contains("type=\"checkbox\"")); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn a_flat_row_says_nothing_about_an_outline() { |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
let html = fragment(&Node::list([Row::new("One"), Row::new("Two")])); |
| 440 |
assert!(!html.contains("--row-depth"), "{html}"); |
| 441 |
assert!(!html.contains("data-disclose"), "{html}"); |
| 442 |
assert!(!html.contains(" hidden"), "{html}"); |
| 443 |
assert!(!html.contains("row-nested"), "{html}"); |
| 444 |
} |
| 445 |
|
| 446 |
#[test] |
| 447 |
fn a_nested_row_carries_its_depth_as_a_property_the_stylesheet_reads() { |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
let html = fragment(&Node::list([ |
| 452 |
Row::new("drums"), |
| 453 |
Row::new("drums.kick").depth(quasi_router::layout::Nesting::at(1)), |
| 454 |
])); |
| 455 |
assert!(html.contains("data-depth=\"1\""), "{html}"); |
| 456 |
assert!(html.contains("style=\"--row-depth:1\""), "{html}"); |
| 457 |
assert!(html.contains("aria-level=\"2\""), "{html}"); |
| 458 |
assert!(html.contains("row-nested"), "{html}"); |
| 459 |
} |
| 460 |
|
| 461 |
#[test] |
| 462 |
fn a_branch_draws_a_chevron_beside_the_label_rather_than_on_it() { |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
let html = fragment(&Node::list([Row::new("drums") |
| 467 |
.disclosing(true) |
| 468 |
.activate(Action::get("/tags/drums"))])); |
| 469 |
assert!(html.contains("data-disclose"), "{html}"); |
| 470 |
assert!(html.contains("aria-expanded=\"true\""), "{html}"); |
| 471 |
assert!(html.contains("aria-label=\"Collapse\""), "{html}"); |
| 472 |
assert!(html.contains("row-branch"), "{html}"); |
| 473 |
|
| 474 |
|
| 475 |
assert_eq!(html.matches("/tags/drums").count(), 2, "{html}"); |
| 476 |
let chevron = html.find("data-disclose").expect("the chevron"); |
| 477 |
let ends = html[chevron..] |
| 478 |
.find("</button>") |
| 479 |
.expect("the chevron closes"); |
| 480 |
assert!(!html[chevron..chevron + ends].contains("hx-"), "{html}"); |
| 481 |
} |
| 482 |
|
| 483 |
#[test] |
| 484 |
fn a_shut_branch_hides_what_is_under_it_and_keeps_it_in_the_document() { |
| 485 |
|
| 486 |
|
| 487 |
let html = fragment(&Node::list([ |
| 488 |
Row::new("drums").disclosing(false), |
| 489 |
Row::new("drums.kick").depth(quasi_router::layout::Nesting::at(1)), |
| 490 |
Row::new("genre"), |
| 491 |
])); |
| 492 |
assert!(html.contains("drums.kick"), "{html}"); |
| 493 |
assert!(html.contains("aria-expanded=\"false\""), "{html}"); |
| 494 |
assert!(html.contains("aria-label=\"Expand\""), "{html}"); |
| 495 |
assert_eq!(html.matches(" hidden").count(), 1, "{html}"); |
| 496 |
|
| 497 |
let genre = html.find("genre").expect("the third row"); |
| 498 |
assert!(!html[..genre].ends_with(" hidden>"), "{html}"); |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn a_table_says_the_outline_the_way_a_list_does() { |
| 503 |
let html = fragment(&Node::Table { |
| 504 |
marks: ::quasi_router::stage::Marks::none(), |
| 505 |
columns: vec![Column::new("Name")], |
| 506 |
rows: vec![ |
| 507 |
Row::cells(["kit"]).disclosing(false), |
| 508 |
Row::cells(["kit/ride.wav"]).depth(quasi_router::layout::Nesting::at(1)), |
| 509 |
], |
| 510 |
more: None, |
| 511 |
}); |
| 512 |
assert!(html.contains("table-disclose"), "{html}"); |
| 513 |
assert!(html.contains("table-disclose-head"), "{html}"); |
| 514 |
assert!(html.contains("style=\"--row-depth:1\""), "{html}"); |
| 515 |
assert_eq!(html.matches(" hidden").count(), 1, "{html}"); |
| 516 |
} |
| 517 |
|
| 518 |
#[test] |
| 519 |
fn a_table_with_no_branch_grows_no_gutter_for_one() { |
| 520 |
let html = fragment(&Node::Table { |
| 521 |
marks: ::quasi_router::stage::Marks::none(), |
| 522 |
columns: vec![Column::new("Name")], |
| 523 |
rows: vec![Row::cells(["kick.wav"])], |
| 524 |
more: None, |
| 525 |
}); |
| 526 |
assert!(!html.contains("table-disclose"), "{html}"); |
| 527 |
} |
| 528 |
|
| 529 |
#[test] |
| 530 |
fn a_selectable_row_gets_a_real_checkbox_and_an_unselectable_one_gets_nothing() { |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
let untickable = fragment(&Node::list([Row::new("One")])); |
| 535 |
assert!(!untickable.contains("type=\"checkbox\"")); |
| 536 |
|
| 537 |
let unticked = fragment(&Node::list([Row::new("One").selectable(false)])); |
| 538 |
assert!(unticked.contains("type=\"checkbox\"")); |
| 539 |
assert!(!unticked.contains(" checked")); |
| 540 |
assert!(!unticked.contains("row-selected")); |
| 541 |
|
| 542 |
let ticked = fragment(&Node::list([Row::new("One").selectable(true)])); |
| 543 |
assert!(ticked.contains("type=\"checkbox\"")); |
| 544 |
assert!(ticked.contains(" checked")); |
| 545 |
assert!(ticked.contains("row-selected")); |
| 546 |
|
| 547 |
assert!(!ticked.contains("aria-current")); |
| 548 |
} |
| 549 |
|
| 550 |
#[test] |
| 551 |
fn a_row_carries_its_tokens_as_tokens_rather_than_as_joined_text() { |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
let html = fragment(&Node::list([Row::new("Mine") |
| 556 |
.meta("3 files") |
| 557 |
.token(Tag::badge("Side Project")) |
| 558 |
.token(Tag::badge("On Hold").tone(layout::Tone::Warning))])); |
| 559 |
|
| 560 |
assert!(html.contains("class=\"row-tokens\"")); |
| 561 |
assert!(html.contains("On Hold")); |
| 562 |
|
| 563 |
assert!(html.contains("warning")); |
| 564 |
|
| 565 |
assert!(html.contains("class=\"row-meta\">3 files</span>")); |
| 566 |
} |
| 567 |
|
| 568 |
#[test] |
| 569 |
fn an_external_destination_is_an_anchor_and_never_a_route() { |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
let html = fragment(&Node::act( |
| 574 |
"Profile", |
| 575 |
Action::external("https://example.com/@ada"), |
| 576 |
)); |
| 577 |
|
| 578 |
assert!(html.contains("<a ")); |
| 579 |
assert!(html.contains("href=\"https://example.com/@ada\"")); |
| 580 |
assert!(html.contains("rel=\"noopener noreferrer\"")); |
| 581 |
|
| 582 |
assert!(!html.contains("hx-get")); |
| 583 |
assert!(!html.contains("hx-post")); |
| 584 |
assert!(!html.contains("<button")); |
| 585 |
} |
| 586 |
|
| 587 |
#[test] |
| 588 |
fn a_local_destination_asks_nothing_and_goes_nowhere() { |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
let html = fragment(&Node::act("Dismiss", Action::local().with("id", "7"))); |
| 593 |
|
| 594 |
assert!(html.contains("data-local")); |
| 595 |
|
| 596 |
assert!(html.contains("<button")); |
| 597 |
assert!(!html.contains("<a ")); |
| 598 |
assert!(!html.contains("href")); |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
assert!(!html.contains("hx-get")); |
| 603 |
assert!(!html.contains("hx-post")); |
| 604 |
|
| 605 |
|
| 606 |
assert!(html.contains("data-vals")); |
| 607 |
assert!(html.contains("id")); |
| 608 |
} |
| 609 |
|
| 610 |
#[test] |
| 611 |
fn an_external_url_cannot_break_out_of_its_own_attribute() { |
| 612 |
let html = fragment(&Node::act( |
| 613 |
"Profile", |
| 614 |
Action::external("\"><script>alert(1)</script>"), |
| 615 |
)); |
| 616 |
assert!(!html.contains("<script>")); |
| 617 |
} |
| 618 |
|
| 619 |
#[test] |
| 620 |
fn a_meter_renders_through_makeover_and_not_through_a_second_emitter() { |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
let html = fragment(&Node::Meter( |
| 625 |
Meter::new(3, 7) |
| 626 |
.tone(layout::Tone::Success) |
| 627 |
.label("subtasks"), |
| 628 |
)); |
| 629 |
assert_eq!( |
| 630 |
html, |
| 631 |
makeover_webview::meter::meter_html( |
| 632 |
&layout::Meter::new(3, 7) |
| 633 |
.tone(layout::Tone::Success) |
| 634 |
.label("subtasks"), |
| 635 |
&Emit::default(), |
| 636 |
) |
| 637 |
); |
| 638 |
assert!(html.contains(r#"aria-label="3 of 7 subtasks""#)); |
| 639 |
} |
| 640 |
|
| 641 |
#[test] |
| 642 |
fn a_link_and_a_figure_in_a_row_cost_no_release() { |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
let html = fragment(&Node::list([Row::new("Invoice 41") |
| 649 |
.part( |
| 650 |
layout::RowPart::Meta, |
| 651 |
Node::Link { |
| 652 |
text: "makenot.work".into(), |
| 653 |
action: Action::get("/sites/1"), |
| 654 |
}, |
| 655 |
) |
| 656 |
.part( |
| 657 |
layout::RowPart::Tokens, |
| 658 |
Node::Figure(Figure::new("41", "days")), |
| 659 |
)])); |
| 660 |
|
| 661 |
assert!(html.contains("makenot.work"), "{html}"); |
| 662 |
assert!(html.contains("/sites/1"), "{html}"); |
| 663 |
assert!(html.contains("41"), "{html}"); |
| 664 |
|
| 665 |
|
| 666 |
assert!(html.contains("class=\"row-meta\""), "{html}"); |
| 667 |
assert!(html.contains("class=\"row-tokens\""), "{html}"); |
| 668 |
} |
| 669 |
|
| 670 |
#[test] |
| 671 |
#[should_panic(expected = "a row is an inline run and holds leaves")] |
| 672 |
fn a_row_refuses_a_block() { |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
let _ = Row::new("Ship it").part( |
| 677 |
layout::RowPart::Meta, |
| 678 |
Node::Table { |
| 679 |
marks: ::quasi_router::stage::Marks::none(), |
| 680 |
columns: Vec::new(), |
| 681 |
rows: Vec::new(), |
| 682 |
more: None, |
| 683 |
}, |
| 684 |
); |
| 685 |
} |
| 686 |
|
| 687 |
#[test] |
| 688 |
fn a_rows_parts_draw_in_the_order_the_description_says_them() { |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
let html = fragment(&Node::list([Row::new("Ship it") |
| 693 |
.token(Tag::badge("beta")) |
| 694 |
.meta("2 files")])); |
| 695 |
|
| 696 |
let tokens = html.find("row-tokens").expect("a tokens strip"); |
| 697 |
let meta = html.find("row-meta").expect("a meta part"); |
| 698 |
assert!(tokens < meta, "{html}"); |
| 699 |
} |
| 700 |
|
| 701 |
#[test] |
| 702 |
fn consecutive_parts_of_one_role_share_a_strip() { |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
let html = fragment(&Node::list([Row::new("Ship it") |
| 708 |
.token(Tag::badge("beta")) |
| 709 |
.token(Tag::badge("draft"))])); |
| 710 |
|
| 711 |
assert_eq!(html.matches("row-tokens").count(), 1, "{html}"); |
| 712 |
assert!(html.contains("beta"), "{html}"); |
| 713 |
assert!(html.contains("draft"), "{html}"); |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn a_relaxed_part_carries_the_clamp_and_a_tight_one_carries_nothing() { |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
let relaxed = fragment(&Node::list([ |
| 722 |
Row::new("A headline long enough to wrap").relaxed() |
| 723 |
])); |
| 724 |
assert!(relaxed.contains("row-relaxed"), "{relaxed}"); |
| 725 |
|
| 726 |
let tight = fragment(&Node::list([Row::new("A headline long enough to wrap")])); |
| 727 |
assert!(!tight.contains("row-relaxed"), "{tight}"); |
| 728 |
} |
| 729 |
|
| 730 |
#[test] |
| 731 |
fn relaxing_applies_to_the_part_that_was_just_added() { |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
let html = fragment(&Node::list([Row::new("Title") |
| 736 |
.relaxed() |
| 737 |
.secondary("Excerpt")])); |
| 738 |
|
| 739 |
let primary = html.find("row-primary").expect("a primary"); |
| 740 |
let secondary = html.find("row-secondary").expect("a secondary"); |
| 741 |
let clamp = html.find("row-relaxed").expect("a clamp"); |
| 742 |
assert!(primary < clamp && clamp < secondary, "{html}"); |
| 743 |
} |
| 744 |
|
| 745 |
#[test] |
| 746 |
fn parts_sharing_a_role_but_not_a_flow_do_not_share_a_span() { |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
let html = fragment(&Node::list([Row::new("Title") |
| 751 |
.part(layout::RowPart::Secondary, Node::text("Clamped")) |
| 752 |
.relaxed() |
| 753 |
.part(layout::RowPart::Secondary, Node::text("Loose"))])); |
| 754 |
assert_eq!(html.matches("row-secondary").count(), 2, "{html}"); |
| 755 |
assert_eq!(html.matches("row-relaxed").count(), 1, "{html}"); |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn a_proportion_in_a_row_is_a_bar_and_not_flattened_text() { |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
let html = fragment(&Node::list([ |
| 765 |
Row::new("Ship it").meter(Meter::new(3, 7).label("subtasks")) |
| 766 |
])); |
| 767 |
|
| 768 |
assert!(html.contains("class=\"row-proportion\"")); |
| 769 |
assert!(html.contains(r#"aria-label="3 of 7 subtasks""#)); |
| 770 |
|
| 771 |
|
| 772 |
assert!(html.contains(&makeover_webview::meter::meter_html( |
| 773 |
&layout::Meter::new(3, 7).label("subtasks"), |
| 774 |
&Emit::default(), |
| 775 |
))); |
| 776 |
} |
| 777 |
|
| 778 |
#[test] |
| 779 |
fn a_strip_of_figures_is_one_node_because_a_renderer_cannot_infer_a_set() { |
| 780 |
|
| 781 |
|
| 782 |
let html = fragment(&Node::stats([ |
| 783 |
Figure::new("17", "Current Streak"), |
| 784 |
Figure::new("84%", "Completion Rate").tone(layout::Tone::Success), |
| 785 |
])); |
| 786 |
|
| 787 |
assert!(html.starts_with("<div class=\"figures\">")); |
| 788 |
assert!(html.contains(r#"aria-label="Current Streak: 17""#)); |
| 789 |
assert!(html.contains(r#"data-tone="success""#)); |
| 790 |
|
| 791 |
assert!(!html.contains("<button")); |
| 792 |
} |
| 793 |
|
| 794 |
#[test] |
| 795 |
fn a_figure_whose_value_is_a_control_becomes_one_without_a_second_emitter() { |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
let html = fragment(&Node::Stats { |
| 800 |
marks: ::quasi_router::stage::Marks::none(), |
| 801 |
figures: vec![( |
| 802 |
Figure::new("3", "Not Applied").tone(layout::Tone::Warning), |
| 803 |
Some(Action::get("/settings/held")), |
| 804 |
)], |
| 805 |
}); |
| 806 |
|
| 807 |
assert!(html.contains("<a class=\"figure-act\"")); |
| 808 |
assert!(html.contains("href=\"/settings/held\"")); |
| 809 |
assert!(html.contains("hx-get=\"/settings/held\"")); |
| 810 |
|
| 811 |
assert!(html.contains(&makeover_webview::figure::figure_html( |
| 812 |
&layout::Figure::new("3", "Not Applied").tone(layout::Tone::Warning), |
| 813 |
&Emit::default(), |
| 814 |
))); |
| 815 |
} |
| 816 |
|
| 817 |
#[test] |
| 818 |
fn a_meter_that_ran_over_says_so_after_the_width_is_clamped() { |
| 819 |
|
| 820 |
|
| 821 |
let html = fragment(&Node::Meter( |
| 822 |
Meter::new(45, 30) |
| 823 |
.tone(layout::Tone::Danger) |
| 824 |
.label("minutes"), |
| 825 |
)); |
| 826 |
assert!(html.contains("width: 100%")); |
| 827 |
assert!(html.contains(r#"data-over="true""#)); |
| 828 |
assert!(html.contains(r#"aria-label="45 of 30 minutes""#)); |
| 829 |
} |
| 830 |
|
| 831 |
#[test] |
| 832 |
fn a_badge_is_not_a_button_and_a_chip_is() { |
| 833 |
let badge = fragment(&Node::Token(Tag { |
| 834 |
tone: layout::Tone::Info, |
| 835 |
action: Some(Action::get("/x")), |
| 836 |
..Tag::badge("3") |
| 837 |
})); |
| 838 |
|
| 839 |
|
| 840 |
assert!(!badge.contains("<button")); |
| 841 |
assert!(!badge.contains("<a ")); |
| 842 |
assert!(!badge.contains("hx-get")); |
| 843 |
assert!(!badge.contains("href")); |
| 844 |
|
| 845 |
let chip = fragment(&Node::Token(Tag::chip("open", Action::get("/x")).latched())); |
| 846 |
|
| 847 |
|
| 848 |
assert!(chip.contains("<a ")); |
| 849 |
assert!(chip.contains("aria-current=\"true\"")); |
| 850 |
assert!(!chip.contains("aria-pressed")); |
| 851 |
assert!(chip.contains("href=\"/x\"")); |
| 852 |
assert!(chip.contains("hx-get=\"/x\"")); |
| 853 |
|
| 854 |
|
| 855 |
let toggle = fragment(&Node::Token( |
| 856 |
Tag::chip("open", Action::post("/x/toggle")).latched(), |
| 857 |
)); |
| 858 |
assert!(toggle.contains("<button")); |
| 859 |
assert!(toggle.contains("aria-pressed=\"true\"")); |
| 860 |
assert!(!toggle.contains("href")); |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
#[test] |
| 866 |
fn a_badge_carries_the_detail_behind_its_label_as_a_title() { |
| 867 |
let html = fragment(&Node::Token(Tag::badge("Blocked").hinted("3 steps away"))); |
| 868 |
|
| 869 |
assert!(html.contains("title=\"3 steps away\""), "{html}"); |
| 870 |
|
| 871 |
assert!(!html.contains("<button"), "{html}"); |
| 872 |
assert!(!html.contains("data-act"), "{html}"); |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
#[test] |
| 878 |
fn a_hint_is_escaped_like_every_other_attribute() { |
| 879 |
let html = fragment(&Node::Token( |
| 880 |
Tag::badge("Cycle").hinted("remove an edge: a -> b -> \"a\""), |
| 881 |
)); |
| 882 |
|
| 883 |
assert!(!html.contains("-> \"a\">"), "{html}"); |
| 884 |
assert!(html.contains(""a""), "{html}"); |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
#[test] |
| 889 |
fn a_tag_with_nothing_behind_its_label_emits_no_title() { |
| 890 |
let html = fragment(&Node::Token(Tag::badge("Blocked"))); |
| 891 |
assert!(!html.contains("title="), "{html}"); |
| 892 |
} |
| 893 |
|
| 894 |
#[test] |
| 895 |
fn a_notice_carrying_a_way_back_emits_it_inside_the_notice() { |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
let html = fragment( |
| 901 |
&Node::toast(layout::Tone::Success, "Deleted") |
| 902 |
.about(Act::new("Undo", Action::post("/tasks/7/restore"))), |
| 903 |
); |
| 904 |
|
| 905 |
assert!(html.contains("Deleted"), "{html}"); |
| 906 |
assert!(html.contains(r#"hx-post="/tasks/7/restore""#), "{html}"); |
| 907 |
|
| 908 |
|
| 909 |
let opened = html.find("Deleted").expect("the text"); |
| 910 |
let control = html.find("hx-post").expect("the control"); |
| 911 |
let closed = html.rfind("</div>").expect("the notice closes"); |
| 912 |
assert!(opened < control && control < closed, "{html}"); |
| 913 |
} |
| 914 |
|
| 915 |
#[test] |
| 916 |
fn a_notice_with_nothing_to_do_about_it_emits_no_control() { |
| 917 |
let html = fragment(&Node::toast(layout::Tone::Success, "Saved")); |
| 918 |
assert!(!html.contains("<button"), "{html}"); |
| 919 |
assert!(!html.contains("hx-post"), "{html}"); |
| 920 |
} |
| 921 |
|
| 922 |
#[test] |
| 923 |
fn a_control_that_goes_back_carries_the_behaviour_and_no_address() { |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
let html = fragment(&Node::Act(Act::new("Close", Action::back()))); |
| 929 |
|
| 930 |
assert!(html.contains("call history.back()"), "{html}"); |
| 931 |
assert!(!html.contains("hx-get"), "{html}"); |
| 932 |
assert!(!html.contains("href"), "{html}"); |
| 933 |
|
| 934 |
|
| 935 |
assert!(!html.contains("data-local"), "{html}"); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
fn conditions(standing: usize, least: usize) -> Slot { |
| 943 |
let mut group = Slot::new("conditions", RegionKind::Group).repeating( |
| 944 |
Repeating::new( |
| 945 |
"Condition", |
| 946 |
Act::new("Add condition", Action::post("/rules/conditions/add")), |
| 947 |
) |
| 948 |
.least(least), |
| 949 |
); |
| 950 |
for at in 0..standing { |
| 951 |
group = group.with(Node::Region( |
| 952 |
Slot::new(format!("condition-{at}"), RegionKind::Group) |
| 953 |
.with(Node::Field(Box::new(Field::new( |
| 954 |
layout::FieldKind::Text, |
| 955 |
format!("value-{at}"), |
| 956 |
"Value", |
| 957 |
)))) |
| 958 |
.removes(Act::new( |
| 959 |
"Remove condition", |
| 960 |
Action::post(format!("/rules/conditions/{at}/remove")), |
| 961 |
)), |
| 962 |
)); |
| 963 |
} |
| 964 |
group |
| 965 |
} |
| 966 |
|
| 967 |
#[test] |
| 968 |
fn the_slots_of_a_repeating_question_are_numbered_for_a_reader() { |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
let html = fragment(&Node::Region(conditions(2, 1))); |
| 973 |
|
| 974 |
assert!(html.contains("Condition 1"), "{html}"); |
| 975 |
assert!(html.contains("Condition 2"), "{html}"); |
| 976 |
assert!(!html.contains("Condition 0"), "{html}"); |
| 977 |
|
| 978 |
|
| 979 |
let numbered = html.find("Condition 1").expect("the caption"); |
| 980 |
assert!(html[..numbered].ends_with('>'), "{html}"); |
| 981 |
} |
| 982 |
|
| 983 |
#[test] |
| 984 |
fn the_floor_disables_the_last_remove_rather_than_the_app_doing_it() { |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
let alone = fragment(&Node::Region(conditions(1, 1))); |
| 989 |
assert!(alone.contains("Remove condition"), "{alone}"); |
| 990 |
assert!(alone.contains("disabled"), "{alone}"); |
| 991 |
|
| 992 |
let pair = fragment(&Node::Region(conditions(2, 1))); |
| 993 |
assert!(!pair.contains("disabled"), "{pair}"); |
| 994 |
} |
| 995 |
|
| 996 |
#[test] |
| 997 |
fn the_ceiling_disables_the_add_when_the_group_is_full() { |
| 998 |
let mut group = conditions(2, 0); |
| 999 |
group.repeating = Some(Box::new( |
| 1000 |
Repeating::new( |
| 1001 |
"Condition", |
| 1002 |
Act::new("Add condition", Action::post("/rules/conditions/add")), |
| 1003 |
) |
| 1004 |
.most(2), |
| 1005 |
)); |
| 1006 |
let html = fragment(&Node::Region(group)); |
| 1007 |
|
| 1008 |
assert!(html.contains("Add condition"), "{html}"); |
| 1009 |
assert!(html.contains("disabled"), "{html}"); |
| 1010 |
} |
| 1011 |
|
| 1012 |
#[test] |
| 1013 |
fn a_region_that_repeats_nothing_emits_what_it_always_did() { |
| 1014 |
|
| 1015 |
|
| 1016 |
let plain = Slot::new("body", RegionKind::Group).with(Node::text("inside")); |
| 1017 |
let html = fragment(&Node::Region(plain)); |
| 1018 |
|
| 1019 |
assert!(html.contains("inside"), "{html}"); |
| 1020 |
assert!(!html.contains("Add"), "{html}"); |
| 1021 |
} |
| 1022 |
|
| 1023 |
#[test] |
| 1024 |
fn a_field_whose_value_is_the_readers_survives_a_redraw() { |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
let html = fragment(&Node::Field(Box::new( |
| 1030 |
Field::new(layout::FieldKind::Text, "tag", "Tag").keeping_value(), |
| 1031 |
))); |
| 1032 |
|
| 1033 |
assert!(html.contains(r#"hx-preserve="true""#), "{html}"); |
| 1034 |
|
| 1035 |
|
| 1036 |
assert!(html.contains(r#"id="tag""#), "{html}"); |
| 1037 |
} |
| 1038 |
|
| 1039 |
#[test] |
| 1040 |
fn a_field_that_says_nothing_is_replaced_as_it_always_was() { |
| 1041 |
|
| 1042 |
|
| 1043 |
let html = fragment(&Node::Field(Box::new(Field::new( |
| 1044 |
layout::FieldKind::Text, |
| 1045 |
"tag", |
| 1046 |
"Tag", |
| 1047 |
)))); |
| 1048 |
assert!(!html.contains("hx-preserve"), "{html}"); |
| 1049 |
} |
| 1050 |
|
| 1051 |
#[test] |
| 1052 |
fn a_box_that_owns_a_list_can_also_keep_what_was_typed_into_it() { |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
let html = fragment(&Node::Field(Box::new( |
| 1057 |
Field::new(layout::FieldKind::Text, "tag", "Tag") |
| 1058 |
.suggesting(Consult::new(Action::post("/discover/tags"))) |
| 1059 |
.keeping_value(), |
| 1060 |
))); |
| 1061 |
|
| 1062 |
assert!(html.contains(r#"hx-preserve="true""#), "{html}"); |
| 1063 |
assert!(html.contains(r#"role="combobox""#), "{html}"); |
| 1064 |
} |
| 1065 |
|
| 1066 |
#[test] |
| 1067 |
fn a_pending_region_says_it_is_waiting() { |
| 1068 |
let screen = |
| 1069 |
Screen::list_detail("Tasks", false).with(Slot::new("detail", RegionKind::Pane).pending()); |
| 1070 |
assert!(render(&screen).contains("aria-busy=\"true\"")); |
| 1071 |
} |
| 1072 |
|
| 1073 |
#[test] |
| 1074 |
fn a_group_is_the_one_region_that_comes_out_as_a_section() { |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
let screen = Screen::list_detail("Settings", false).with( |
| 1079 |
Slot::new("body", RegionKind::Pane).with(Node::Region( |
| 1080 |
Slot::group("appearance") |
| 1081 |
.with(Node::section("Appearance")) |
| 1082 |
.with(Node::text("Theme")), |
| 1083 |
)), |
| 1084 |
); |
| 1085 |
let html = render(&screen); |
| 1086 |
|
| 1087 |
assert!(html.contains("<section id=\"appearance\""), "{html}"); |
| 1088 |
assert!(html.contains("</section>"), "{html}"); |
| 1089 |
assert!(html.contains("region group"), "{html}"); |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
assert_eq!(html.matches("<section").count(), 1, "{html}"); |
| 1094 |
assert!(html.contains("<div id=\"body\""), "{html}"); |
| 1095 |
} |
| 1096 |
|
| 1097 |
#[test] |
| 1098 |
fn a_bespoke_region_is_a_place_and_nothing_else() { |
| 1099 |
|
| 1100 |
|
| 1101 |
let screen = Screen::list_detail("Tasks", false).with(Slot::handover("player", "media-player")); |
| 1102 |
let html = render(&screen); |
| 1103 |
|
| 1104 |
assert!(html.contains("id=\"player\"")); |
| 1105 |
assert!(html.contains("data-bespoke=\"media-player\"")); |
| 1106 |
|
| 1107 |
assert!(html.contains("data-bespoke=\"media-player\"></div>")); |
| 1108 |
} |
| 1109 |
|
| 1110 |
#[test] |
| 1111 |
fn a_bespoke_region_the_host_filled_carries_its_markup() { |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
let screen = Screen::list_detail("Files", false).with(Slot::handover("file", "git-file")); |
| 1116 |
let html = Webview::new() |
| 1117 |
.with_fill("file", "<pre class=\"hl\">fn main() {}</pre>") |
| 1118 |
.screen(&screen); |
| 1119 |
|
| 1120 |
assert!( |
| 1121 |
html.contains("<pre class=\"hl\">fn main() {}</pre></div>"), |
| 1122 |
"{html}" |
| 1123 |
); |
| 1124 |
} |
| 1125 |
|
| 1126 |
#[test] |
| 1127 |
fn a_bespoke_region_with_no_fill_is_still_empty() { |
| 1128 |
|
| 1129 |
|
| 1130 |
let screen = Screen::list_detail("Files", false) |
| 1131 |
.with(Slot::handover("file", "git-file")) |
| 1132 |
.with(Slot::handover("player", "media-player")); |
| 1133 |
let html = Webview::new() |
| 1134 |
.with_fill("file", "<pre></pre>") |
| 1135 |
.screen(&screen); |
| 1136 |
|
| 1137 |
assert!( |
| 1138 |
html.contains("data-bespoke=\"media-player\"></div>"), |
| 1139 |
"{html}" |
| 1140 |
); |
| 1141 |
} |
| 1142 |
|
| 1143 |
#[test] |
| 1144 |
fn two_regions_sharing_a_name_are_filled_by_id() { |
| 1145 |
|
| 1146 |
|
| 1147 |
let screen = Screen::list_detail("Files", false) |
| 1148 |
.with(Slot::handover("row-1", "diff")) |
| 1149 |
.with(Slot::handover("row-2", "diff")); |
| 1150 |
let html = Webview::new() |
| 1151 |
.with_fill("row-1", "<i>one</i>") |
| 1152 |
.with_fill("row-2", "<i>two</i>") |
| 1153 |
.screen(&screen); |
| 1154 |
|
| 1155 |
assert!(html.contains("id=\"row-1\""), "{html}"); |
| 1156 |
let one = html.find("<i>one</i>").expect("the first row is filled"); |
| 1157 |
let two = html.find("<i>two</i>").expect("the second row is filled"); |
| 1158 |
assert!(one < two); |
| 1159 |
assert!(html.find("id=\"row-2\"").expect("the second row exists") < two); |
| 1160 |
} |
| 1161 |
|
| 1162 |
#[test] |
| 1163 |
fn a_fill_is_markup_and_is_not_escaped() { |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
let screen = Screen::list_detail("Files", false).with(Slot::handover("file", "git-file")); |
| 1169 |
let html = Webview::new() |
| 1170 |
.with_fill("file", "<span data-x=\"1\">&</span>") |
| 1171 |
.screen(&screen); |
| 1172 |
|
| 1173 |
assert!(html.contains("<span data-x=\"1\">&</span>"), "{html}"); |
| 1174 |
assert!(!html.contains("<span"), "{html}"); |
| 1175 |
} |
| 1176 |
|
| 1177 |
#[test] |
| 1178 |
fn a_fill_for_a_region_the_screen_lacks_goes_nowhere() { |
| 1179 |
|
| 1180 |
|
| 1181 |
let screen = Screen::list_detail("Files", false).with(Slot::handover("file", "git-file")); |
| 1182 |
let plain = render(&screen); |
| 1183 |
let html = Webview::new() |
| 1184 |
.with_fill("absent", "<b>stray</b>") |
| 1185 |
.screen(&screen); |
| 1186 |
|
| 1187 |
assert!(!html.contains("stray"), "{html}"); |
| 1188 |
assert_eq!(html, plain); |
| 1189 |
} |
| 1190 |
|
| 1191 |
#[test] |
| 1192 |
fn a_fill_is_only_for_a_bespoke_region() { |
| 1193 |
|
| 1194 |
|
| 1195 |
let screen = Screen::list_detail("Files", false).with(Slot::new("detail", RegionKind::Pane)); |
| 1196 |
let html = Webview::new() |
| 1197 |
.with_fill("detail", "<b>stray</b>") |
| 1198 |
.screen(&screen); |
| 1199 |
|
| 1200 |
assert!(!html.contains("stray"), "{html}"); |
| 1201 |
} |
| 1202 |
|
| 1203 |
#[test] |
| 1204 |
fn a_slot_id_survives_intact_because_a_fragment_is_aimed_at_it() { |
| 1205 |
let screen = Screen::sidebar_content("Feeds").with(Slot::new("feed-list", RegionKind::Sidebar)); |
| 1206 |
assert!(render(&screen).contains("id=\"feed-list\"")); |
| 1207 |
} |
| 1208 |
|
| 1209 |
#[test] |
| 1210 |
fn a_modal_says_it_takes_input_until_dismissed() { |
| 1211 |
let screen = Screen::list_detail("Tasks", false).with(Slot::new("confirm", RegionKind::Modal)); |
| 1212 |
let html = render(&screen); |
| 1213 |
assert!(html.contains("role=\"dialog\"")); |
| 1214 |
assert!(html.contains("aria-modal=\"true\"")); |
| 1215 |
} |
| 1216 |
|
| 1217 |
#[test] |
| 1218 |
fn a_table_addresses_its_cells_by_column_never_by_position() { |
| 1219 |
let columns = vec![ |
| 1220 |
Column::new("Name").width(layout::Width::Fill), |
| 1221 |
Column::new("Size").width(layout::Width::Content), |
| 1222 |
]; |
| 1223 |
let html = fragment(&Node::Table { |
| 1224 |
marks: ::quasi_router::stage::Marks::none(), |
| 1225 |
columns, |
| 1226 |
rows: vec![Row::cells(["kick.wav", "2.1 MB"])], |
| 1227 |
more: None, |
| 1228 |
}); |
| 1229 |
|
| 1230 |
assert!(html.contains("role=\"table\"")); |
| 1231 |
assert!(html.contains("role=\"columnheader\"")); |
| 1232 |
assert!(html.contains("kick.wav")); |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
assert!(!html.contains("grid-template-columns")); |
| 1238 |
assert!(!html.contains("style=")); |
| 1239 |
} |
| 1240 |
|
| 1241 |
#[test] |
| 1242 |
fn a_table_row_says_current_the_same_way_a_list_row_does() { |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
let html = fragment(&Node::Table { |
| 1249 |
marks: ::quasi_router::stage::Marks::none(), |
| 1250 |
columns: vec![Column::new("Name").width(layout::Width::Fill)], |
| 1251 |
rows: vec![Row::cells(["kick.wav"]).current(true)], |
| 1252 |
more: None, |
| 1253 |
}); |
| 1254 |
assert!(html.contains("aria-current=\"true\"")); |
| 1255 |
assert!(html.contains("table-row-current")); |
| 1256 |
assert!(!html.contains("table-row-selected")); |
| 1257 |
|
| 1258 |
|
| 1259 |
assert!(!html.contains("type=\"checkbox\"")); |
| 1260 |
} |
| 1261 |
|
| 1262 |
#[test] |
| 1263 |
fn a_cell_value_is_text_and_cannot_become_markup() { |
| 1264 |
let html = fragment(&Node::Table { |
| 1265 |
marks: ::quasi_router::stage::Marks::none(), |
| 1266 |
columns: vec![Column::new("Name").width(layout::Width::Fill)], |
| 1267 |
rows: vec![Row::cells(["<img src=x onerror=alert(1)>"])], |
| 1268 |
more: None, |
| 1269 |
}); |
| 1270 |
assert!(!html.contains("<img")); |
| 1271 |
assert!(html.contains("<img")); |
| 1272 |
} |
| 1273 |
|
| 1274 |
#[test] |
| 1275 |
fn a_table_row_carries_its_controls_in_the_cell_they_belong_to() { |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
let html = fragment(&Node::Table { |
| 1280 |
marks: ::quasi_router::stage::Marks::none(), |
| 1281 |
columns: vec![ |
| 1282 |
Column::new("Fingerprint").width(layout::Width::Fill), |
| 1283 |
Column::new("Label").width(layout::Width::Content), |
| 1284 |
Column::new("").width(layout::Width::Content), |
| 1285 |
], |
| 1286 |
rows: vec![Row::cells([ |
| 1287 |
Cell::new("SHA256:abc"), |
| 1288 |
Cell::new("fw13"), |
| 1289 |
Cell::acts([ |
| 1290 |
Act::new("Remove", Action::post("/keys/7/delete")).tone(layout::Tone::Danger) |
| 1291 |
]), |
| 1292 |
])], |
| 1293 |
more: None, |
| 1294 |
}); |
| 1295 |
|
| 1296 |
assert!(html.contains("role=\"table\"")); |
| 1297 |
assert!(html.contains("SHA256:abc")); |
| 1298 |
assert!(html.contains("hx-post=\"/keys/7/delete\"")); |
| 1299 |
assert!(html.contains("cell-actions")); |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
assert!(!html.contains("row-actions")); |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
assert!(html.contains("cell-value"), "{html}"); |
| 1309 |
assert!(!html.contains("<span class=\"cell-value\""), "{html}"); |
| 1310 |
} |
| 1311 |
|
| 1312 |
#[test] |
| 1313 |
fn a_cell_that_mixes_parts_names_them_inside_rather_than_on_itself() { |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
let html = fragment(&Node::Table { |
| 1319 |
marks: ::quasi_router::stage::Marks::none(), |
| 1320 |
columns: vec![Column::new("Item").width(layout::Width::Fill)], |
| 1321 |
rows: vec![Row::cells([Cell::new("Release notes") |
| 1322 |
.token(Tag::badge("Published")) |
| 1323 |
.act(Act::new("Remove", Action::post("/blog/7/delete")))])], |
| 1324 |
more: None, |
| 1325 |
}); |
| 1326 |
|
| 1327 |
|
| 1328 |
assert!(html.contains("<span class=\"cell-value\">"), "{html}"); |
| 1329 |
assert!(html.contains("cell-tokens"), "{html}"); |
| 1330 |
assert!(html.contains("cell-actions"), "{html}"); |
| 1331 |
|
| 1332 |
|
| 1333 |
assert!(!html.contains("cell-keeps cell-value"), "{html}"); |
| 1334 |
} |
| 1335 |
|
| 1336 |
#[test] |
| 1337 |
fn a_figure_carries_its_delta_through_to_the_strip() { |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
let html = fragment(&Node::Stats { |
| 1342 |
marks: ::quasi_router::stage::Marks::none(), |
| 1343 |
figures: vec![( |
| 1344 |
Figure::new("1,204", "Views") |
| 1345 |
.change("+12.5%") |
| 1346 |
.tone(layout::Tone::Success), |
| 1347 |
None, |
| 1348 |
)], |
| 1349 |
}); |
| 1350 |
|
| 1351 |
assert!(html.contains("+12.5%"), "{html}"); |
| 1352 |
assert!(html.contains("figure-change"), "{html}"); |
| 1353 |
assert!(html.contains("data-tone=\"success\""), "{html}"); |
| 1354 |
|
| 1355 |
|
| 1356 |
assert!(html.contains("Views: 1,204, +12.5%"), "{html}"); |
| 1357 |
|
| 1358 |
|
| 1359 |
let plain = fragment(&Node::Stats { |
| 1360 |
marks: ::quasi_router::stage::Marks::none(), |
| 1361 |
figures: vec![(Figure::new("3.1%", "Conversion"), None)], |
| 1362 |
}); |
| 1363 |
assert!(!plain.contains("figure-change"), "{plain}"); |
| 1364 |
} |
| 1365 |
|
| 1366 |
#[test] |
| 1367 |
fn a_control_calling_an_undescribed_route_can_say_where_the_answer_goes() { |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
let html = fragment(&Node::list([Row::new("fw13").act(Act::new( |
| 1375 |
"Remove", |
| 1376 |
Action::delete("/api/users/me/ssh-keys/7").replacing("ssh-keys-list"), |
| 1377 |
))])); |
| 1378 |
|
| 1379 |
assert!(html.contains("hx-target=\"#ssh-keys-list\""), "{html}"); |
| 1380 |
assert!( |
| 1381 |
html.contains("hx-delete=\"/api/users/me/ssh-keys/7\""), |
| 1382 |
"{html}" |
| 1383 |
); |
| 1384 |
} |
| 1385 |
|
| 1386 |
#[test] |
| 1387 |
fn nothing_else_ever_emits_a_target() { |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
let html = fragment(&Node::list([Row::new("fw13") |
| 1392 |
.act(Act::new("Open", Action::get("/keys/7"))) |
| 1393 |
.activate(Action::get("/keys/7"))])); |
| 1394 |
assert!(!html.contains("hx-target"), "{html}"); |
| 1395 |
|
| 1396 |
let form = fragment(&Node::Form { |
| 1397 |
marks: ::quasi_router::stage::Marks::none(), |
| 1398 |
action: Action::post("/keys"), |
| 1399 |
submit: "Add".into(), |
| 1400 |
fields: vec![Field::new(layout::FieldKind::Text, "label", "Label")], |
| 1401 |
}); |
| 1402 |
assert!(!form.contains("hx-target"), "{form}"); |
| 1403 |
} |
| 1404 |
|
| 1405 |
#[test] |
| 1406 |
fn a_region_id_cannot_break_out_of_the_target_attribute() { |
| 1407 |
let html = fragment(&Node::act( |
| 1408 |
"Remove", |
| 1409 |
Action::delete("/api/x").replacing("a\" onload=\"x()"), |
| 1410 |
)); |
| 1411 |
assert!(!html.contains("\" onload="), "{html}"); |
| 1412 |
} |
| 1413 |
|
| 1414 |
#[test] |
| 1415 |
fn a_control_whose_answer_is_a_file_says_so() { |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
let read = fragment(&Node::act( |
| 1423 |
"Download LICENSE.txt", |
| 1424 |
Action::get("/api/items/7/license.txt").saving("LICENSE.txt"), |
| 1425 |
)); |
| 1426 |
assert!(read.contains("download=\"LICENSE.txt\""), "{read}"); |
| 1427 |
assert!( |
| 1428 |
read.contains("<a "), |
| 1429 |
"a read that saves is still a link: {read}" |
| 1430 |
); |
| 1431 |
|
| 1432 |
|
| 1433 |
let write = fragment(&Node::act( |
| 1434 |
"Export CSV", |
| 1435 |
Action::post("/api/export/contacts").saving("contacts.csv"), |
| 1436 |
)); |
| 1437 |
assert!(write.contains("data-saves=\"contacts.csv\""), "{write}"); |
| 1438 |
assert!( |
| 1439 |
write.contains("hx-post=\"/api/export/contacts\""), |
| 1440 |
"{write}" |
| 1441 |
); |
| 1442 |
assert!( |
| 1443 |
!write.contains("download="), |
| 1444 |
"a button is not a link: {write}" |
| 1445 |
); |
| 1446 |
} |
| 1447 |
|
| 1448 |
#[test] |
| 1449 |
fn a_control_that_saves_nothing_says_nothing() { |
| 1450 |
let plain = fragment(&Node::act("Export", Action::post("/api/export/contacts"))); |
| 1451 |
assert!(!plain.contains("data-saves"), "{plain}"); |
| 1452 |
assert!(!plain.contains("download="), "{plain}"); |
| 1453 |
} |
| 1454 |
|
| 1455 |
#[test] |
| 1456 |
fn a_filename_cannot_break_out_of_its_attribute() { |
| 1457 |
|
| 1458 |
|
| 1459 |
let html = fragment(&Node::act( |
| 1460 |
"Export", |
| 1461 |
Action::post("/api/export").saving("\" onload=\"x()"), |
| 1462 |
)); |
| 1463 |
assert!(!html.contains("\" onload="), "{html}"); |
| 1464 |
} |
| 1465 |
|
| 1466 |
#[test] |
| 1467 |
fn a_cell_value_that_is_a_link_is_the_link() { |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
let html = fragment(&Node::Table { |
| 1472 |
marks: ::quasi_router::stage::Marks::none(), |
| 1473 |
columns: vec![ |
| 1474 |
Column::new("Title").width(layout::Width::Fill), |
| 1475 |
Column::new("Status").width(layout::Width::Content), |
| 1476 |
], |
| 1477 |
rows: vec![Row::cells([ |
| 1478 |
Cell::new("Release notes").activate(Action::get("/blog/7")), |
| 1479 |
Cell::tag(Tag::badge("Published")), |
| 1480 |
])], |
| 1481 |
more: None, |
| 1482 |
}); |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
assert!(html.contains("href=\"/blog/7\""), "{html}"); |
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
assert!(html.contains("cell-link"), "{html}"); |
| 1491 |
assert!(html.contains(">Release notes</a>"), "{html}"); |
| 1492 |
|
| 1493 |
|
| 1494 |
let hostile = fragment(&Node::Table { |
| 1495 |
marks: ::quasi_router::stage::Marks::none(), |
| 1496 |
columns: vec![Column::new("Title").width(layout::Width::Fill)], |
| 1497 |
rows: vec![Row::cells([ |
| 1498 |
Cell::new("<img src=x onerror=alert(1)>").activate(Action::get("/blog/7")) |
| 1499 |
])], |
| 1500 |
more: None, |
| 1501 |
}); |
| 1502 |
assert!(!hostile.contains("<img"), "{hostile}"); |
| 1503 |
} |
| 1504 |
|
| 1505 |
#[test] |
| 1506 |
fn a_linked_value_does_not_also_open_the_row() { |
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
let html = fragment(&Node::Table { |
| 1511 |
marks: ::quasi_router::stage::Marks::none(), |
| 1512 |
columns: vec![Column::new("Title").width(layout::Width::Fill)], |
| 1513 |
rows: vec![ |
| 1514 |
Row::cells([Cell::new("Release notes").activate(Action::get("/blog/7"))]) |
| 1515 |
.activate(Action::get("/blog/7/edit")), |
| 1516 |
], |
| 1517 |
more: None, |
| 1518 |
}); |
| 1519 |
|
| 1520 |
assert!( |
| 1521 |
html.contains("closest("), |
| 1522 |
"the row filters the link out: {html}" |
| 1523 |
); |
| 1524 |
assert!(html.contains("hx-get=\"/blog/7/edit\""), "{html}"); |
| 1525 |
assert!(html.contains("hx-get=\"/blog/7\""), "{html}"); |
| 1526 |
} |
| 1527 |
|
| 1528 |
#[test] |
| 1529 |
fn a_control_beside_a_value_does_not_also_open_the_row() { |
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
let html = fragment(&Node::Table { |
| 1535 |
marks: ::quasi_router::stage::Marks::none(), |
| 1536 |
columns: vec![Column::new("Slug").width(layout::Width::Fill)], |
| 1537 |
rows: vec![ |
| 1538 |
Row::cells([ |
| 1539 |
Cell::new("my-app").act(Act::new("Set slug", Action::post("/apps/3/slug"))) |
| 1540 |
]) |
| 1541 |
.activate(Action::get("/apps/3")), |
| 1542 |
], |
| 1543 |
more: None, |
| 1544 |
}); |
| 1545 |
|
| 1546 |
assert!(html.contains("hx-get=\"/apps/3\"")); |
| 1547 |
assert!(html.contains("hx-post=\"/apps/3/slug\"")); |
| 1548 |
assert!(html.contains("data-act")); |
| 1549 |
assert!(html.contains("closest(")); |
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
let plain = fragment(&Node::Table { |
| 1554 |
marks: ::quasi_router::stage::Marks::none(), |
| 1555 |
columns: vec![Column::new("Slug").width(layout::Width::Fill)], |
| 1556 |
rows: vec![Row::cells(["my-app"]).activate(Action::get("/apps/3"))], |
| 1557 |
more: None, |
| 1558 |
}); |
| 1559 |
assert!(!plain.contains("hx-trigger")); |
| 1560 |
} |
| 1561 |
|
| 1562 |
#[test] |
| 1563 |
fn the_ssh_keys_tab_gaps_stay_shut() { |
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
let html = fragment(&Node::list([Row::new("fw13").act( |
| 1569 |
Act::new("Remove", Action::post("/keys/7/delete")).tone(layout::Tone::Danger), |
| 1570 |
)])); |
| 1571 |
|
| 1572 |
assert!(html.contains("class=\"button\""), "{html}"); |
| 1573 |
assert!(html.contains("data-tone=\"danger\""), "{html}"); |
| 1574 |
assert!(!html.contains("\"act\""), "{html}"); |
| 1575 |
assert!(!html.contains("tone-danger"), "{html}"); |
| 1576 |
|
| 1577 |
let chip = fragment(&Node::Token( |
| 1578 |
Tag::chip("Open", Action::get("/tasks?open=1")).latched(), |
| 1579 |
)); |
| 1580 |
assert!(chip.contains("latched"), "{chip}"); |
| 1581 |
assert!(!chip.contains("chip-latched"), "{chip}"); |
| 1582 |
} |
| 1583 |
|
| 1584 |
#[test] |
| 1585 |
fn a_table_heading_drops_with_the_cells_below_it() { |
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
let html = fragment(&Node::Table { |
| 1591 |
marks: ::quasi_router::stage::Marks::none(), |
| 1592 |
columns: vec![ |
| 1593 |
Column::new("Name") |
| 1594 |
.width(layout::Width::Fill) |
| 1595 |
.priority(layout::Priority::Essential), |
| 1596 |
Column::new("Used") |
| 1597 |
.width(layout::Width::Content) |
| 1598 |
.priority(layout::Priority::Optional), |
| 1599 |
], |
| 1600 |
rows: vec![Row::cells(["deploy", "Aug 9"])], |
| 1601 |
more: None, |
| 1602 |
}); |
| 1603 |
|
| 1604 |
for classes in [ |
| 1605 |
"col-Name cell-fill cell-keeps", |
| 1606 |
"col-Used cell-content cell-drops-first", |
| 1607 |
] { |
| 1608 |
assert_eq!( |
| 1609 |
html.matches(classes).count(), |
| 1610 |
2, |
| 1611 |
"the heading and its cell must carry the same classes: {html}" |
| 1612 |
); |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
#[test] |
| 1617 |
fn a_status_column_keeps_its_tone_instead_of_flattening_to_text() { |
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
let html = fragment(&Node::Table { |
| 1623 |
marks: ::quasi_router::stage::Marks::none(), |
| 1624 |
columns: vec![ |
| 1625 |
Column::new("Amount").width(layout::Width::Content), |
| 1626 |
Column::new("Status").width(layout::Width::Content), |
| 1627 |
], |
| 1628 |
rows: vec![Row::cells([ |
| 1629 |
Cell::new("$12.00"), |
| 1630 |
Cell::tag(Tag::badge("Refunded").tone(layout::Tone::Warning)), |
| 1631 |
])], |
| 1632 |
more: None, |
| 1633 |
}); |
| 1634 |
|
| 1635 |
assert!(html.contains("cell-tokens"), "{html}"); |
| 1636 |
assert!( |
| 1637 |
html.contains("class=\"badge\" data-tone=\"warning\""), |
| 1638 |
"{html}" |
| 1639 |
); |
| 1640 |
assert!(html.contains("Refunded"), "{html}"); |
| 1641 |
} |
| 1642 |
|
| 1643 |
#[test] |
| 1644 |
fn a_meter_and_a_figure_in_a_cell_were_the_two_gaps_the_enumeration_left_open() { |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
let html = fragment(&Node::Table { |
| 1651 |
marks: ::quasi_router::stage::Marks::none(), |
| 1652 |
columns: vec![ |
| 1653 |
Column::new("Task").width(layout::Width::Fill), |
| 1654 |
Column::new("Done").width(layout::Width::Content), |
| 1655 |
Column::new("Revenue").width(layout::Width::Content), |
| 1656 |
], |
| 1657 |
rows: vec![Row::cells([ |
| 1658 |
Cell::new("Write the renderer"), |
| 1659 |
Cell::new("").part(Node::Meter(Meter::new(3, 7))), |
| 1660 |
Cell::new("").part(Node::Figure(Figure::new("$12.00", "this month"))), |
| 1661 |
])], |
| 1662 |
more: None, |
| 1663 |
}); |
| 1664 |
|
| 1665 |
assert!(html.contains("progress"), "the meter is drawn: {html}"); |
| 1666 |
assert!(html.contains("figure"), "the figure is drawn: {html}"); |
| 1667 |
assert!(html.contains("$12.00"), "{html}"); |
| 1668 |
} |
| 1669 |
|
| 1670 |
#[test] |
| 1671 |
fn a_run_says_its_own_order_rather_than_hoisting_the_tags_to_the_end() { |
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
let html = fragment(&Node::Table { |
| 1677 |
marks: ::quasi_router::stage::Marks::none(), |
| 1678 |
columns: vec![Column::new("Note").width(layout::Width::Fill)], |
| 1679 |
rows: vec![Row::cells([Cell::new("shipped") |
| 1680 |
.token(Tag::badge("beta")) |
| 1681 |
.part(Node::text("to staging"))])], |
| 1682 |
more: None, |
| 1683 |
}); |
| 1684 |
|
| 1685 |
let badge = html.find("beta").expect("the tag is drawn"); |
| 1686 |
let before = html.find("shipped").expect("the first text"); |
| 1687 |
let after = html.find("to staging").expect("the second text"); |
| 1688 |
assert!(before < badge && badge < after, "{html}"); |
| 1689 |
} |
| 1690 |
|
| 1691 |
#[test] |
| 1692 |
#[should_panic(expected = "a cell is an inline run and holds leaves")] |
| 1693 |
fn a_cell_refuses_a_block() { |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
let _ = Cell::new("x").part(Node::Table { |
| 1698 |
marks: ::quasi_router::stage::Marks::none(), |
| 1699 |
columns: Vec::new(), |
| 1700 |
rows: Vec::new(), |
| 1701 |
more: None, |
| 1702 |
}); |
| 1703 |
} |
| 1704 |
|
| 1705 |
#[test] |
| 1706 |
fn a_chip_in_a_cell_is_a_control_and_a_badge_is_not() { |
| 1707 |
|
| 1708 |
|
| 1709 |
let chip = fragment(&Node::Table { |
| 1710 |
marks: ::quasi_router::stage::Marks::none(), |
| 1711 |
columns: vec![Column::new("Tag").width(layout::Width::Content)], |
| 1712 |
rows: vec![ |
| 1713 |
Row::cells([Cell::tag(Tag::chip("Open", Action::get("/tasks?open=1")))]) |
| 1714 |
.activate(Action::get("/tasks/1")), |
| 1715 |
], |
| 1716 |
more: None, |
| 1717 |
}); |
| 1718 |
assert!(chip.contains("data-act"), "{chip}"); |
| 1719 |
assert!(chip.contains("closest("), "{chip}"); |
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
let badge = fragment(&Node::Table { |
| 1724 |
marks: ::quasi_router::stage::Marks::none(), |
| 1725 |
columns: vec![Column::new("Status").width(layout::Width::Content)], |
| 1726 |
rows: vec![Row::cells([Cell::tag(Tag::badge("Paid"))]).activate(Action::get("/sales/1"))], |
| 1727 |
more: None, |
| 1728 |
}); |
| 1729 |
assert!(!badge.contains("data-act"), "{badge}"); |
| 1730 |
assert!(!badge.contains("hx-trigger"), "{badge}"); |
| 1731 |
} |
| 1732 |
|
| 1733 |
#[test] |
| 1734 |
fn a_cell_of_plain_text_is_still_a_string() { |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
let cells = Row::cells(["kick.wav", "2.1 MB"]); |
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
assert_eq!( |
| 1744 |
cells |
| 1745 |
.cells |
| 1746 |
.iter() |
| 1747 |
.map(|cell| cell.content.clone()) |
| 1748 |
.collect::<Vec<_>>(), |
| 1749 |
vec![Cell::new("kick.wav").content, Cell::new("2.1 MB").content] |
| 1750 |
); |
| 1751 |
assert_eq!( |
| 1752 |
cells |
| 1753 |
.cells |
| 1754 |
.iter() |
| 1755 |
.map(|cell| cell.key.clone()) |
| 1756 |
.collect::<Vec<_>>(), |
| 1757 |
vec![CellKey::Column(0), CellKey::Column(1)] |
| 1758 |
); |
| 1759 |
|
| 1760 |
|
| 1761 |
assert!( |
| 1762 |
cells |
| 1763 |
.cells |
| 1764 |
.iter() |
| 1765 |
.all(|cell| matches!(cell.content.as_slice(), [Node::Text { .. }])) |
| 1766 |
); |
| 1767 |
} |
| 1768 |
|
| 1769 |
#[test] |
| 1770 |
fn a_form_borrows_its_fields_rather_than_emitting_them_twice() { |
| 1771 |
let html = fragment(&Node::Form { |
| 1772 |
marks: ::quasi_router::stage::Marks::none(), |
| 1773 |
action: Action::post("/tasks"), |
| 1774 |
submit: "Save".into(), |
| 1775 |
fields: vec![Field::new(layout::FieldKind::Text, "title", "Title").required()], |
| 1776 |
}); |
| 1777 |
|
| 1778 |
assert!(html.contains("hx-post=\"/tasks\"")); |
| 1779 |
assert!(html.contains("name=\"title\"")); |
| 1780 |
assert!(html.contains("required")); |
| 1781 |
assert!(html.contains("<button type=\"submit\"")); |
| 1782 |
|
| 1783 |
|
| 1784 |
assert!(html.contains("<label")); |
| 1785 |
} |
| 1786 |
|
| 1787 |
#[test] |
| 1788 |
fn a_units_symbol_reaches_the_renderer_and_the_label_keeps_none() { |
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
let html = fragment(&Node::field( |
| 1795 |
Field::new(layout::FieldKind::Number, "fade", "Fade").unit("ms"), |
| 1796 |
)); |
| 1797 |
|
| 1798 |
assert!(html.contains(">ms</span>"), "{html}"); |
| 1799 |
assert!(html.contains("fade-unit"), "{html}"); |
| 1800 |
assert!(html.contains(">Fade</label>"), "{html}"); |
| 1801 |
} |
| 1802 |
|
| 1803 |
#[test] |
| 1804 |
fn a_theme_picker_reaches_the_renderer_grouped_and_marked() { |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
let html = fragment(&Node::field( |
| 1810 |
Field::theme( |
| 1811 |
"theme", |
| 1812 |
"Theme", |
| 1813 |
vec![ |
| 1814 |
ThemeChoice::new( |
| 1815 |
"goingson", |
| 1816 |
"GoingsOn", |
| 1817 |
layout::ThemeVariant::Light, |
| 1818 |
layout::Contrast::High, |
| 1819 |
), |
| 1820 |
ThemeChoice::new( |
| 1821 |
"carbonfox", |
| 1822 |
"Carbonfox", |
| 1823 |
layout::ThemeVariant::Dark, |
| 1824 |
layout::Contrast::Standard, |
| 1825 |
), |
| 1826 |
], |
| 1827 |
) |
| 1828 |
.following(Choice::new("system", "Follow System")) |
| 1829 |
.value("carbonfox"), |
| 1830 |
)); |
| 1831 |
|
| 1832 |
assert!(html.contains(r#"<optgroup label="Light""#), "{html}"); |
| 1833 |
assert!(html.contains(r#"<optgroup label="Dark""#), "{html}"); |
| 1834 |
assert!(html.contains("GoingsOn (AA)"), "{html}"); |
| 1835 |
assert!(html.contains("Carbonfox (OK)"), "{html}"); |
| 1836 |
assert!(html.contains("Follow System"), "{html}"); |
| 1837 |
assert!( |
| 1838 |
html.contains(r#"value="carbonfox" data-contrast="standard" selected"#), |
| 1839 |
"{html}" |
| 1840 |
); |
| 1841 |
} |
| 1842 |
|
| 1843 |
#[test] |
| 1844 |
fn an_interval_reaches_the_renderer_as_one_group_with_two_boxes() { |
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
let html = fragment(&Node::field( |
| 1850 |
Field { |
| 1851 |
min: Some("0".into()), |
| 1852 |
max: Some("300".into()), |
| 1853 |
..Field::interval("bpm_min", "bpm_max", "BPM range") |
| 1854 |
} |
| 1855 |
.value("90") |
| 1856 |
.upper_value("130"), |
| 1857 |
)); |
| 1858 |
|
| 1859 |
assert!(html.contains("role=\"group\""), "{html}"); |
| 1860 |
assert!(html.contains("name=\"bpm_min\""), "{html}"); |
| 1861 |
assert!(html.contains("name=\"bpm_max\""), "{html}"); |
| 1862 |
assert!(html.contains("value=\"90\""), "{html}"); |
| 1863 |
assert!(html.contains("value=\"130\""), "{html}"); |
| 1864 |
|
| 1865 |
assert_eq!(html.matches("max=\"300\"").count(), 2, "{html}"); |
| 1866 |
} |
| 1867 |
|
| 1868 |
#[test] |
| 1869 |
fn a_refused_interval_comes_back_with_both_ends_in_it() { |
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
let params = quasi_router::Params::new() |
| 1874 |
.with("bpm_min", "90") |
| 1875 |
.with("bpm_max", "130"); |
| 1876 |
let html = fragment(&Node::Form { |
| 1877 |
marks: ::quasi_router::stage::Marks::none(), |
| 1878 |
action: Action::post("/filters"), |
| 1879 |
submit: "Apply".into(), |
| 1880 |
fields: vec![Field::interval("bpm_min", "bpm_max", "BPM range").refilled(¶ms)], |
| 1881 |
}); |
| 1882 |
|
| 1883 |
assert!( |
| 1884 |
html.contains("name=\"bpm_min\" aria-label=\"lower\""), |
| 1885 |
"{html}" |
| 1886 |
); |
| 1887 |
assert!(html.contains("value=\"90\""), "{html}"); |
| 1888 |
assert!(html.contains("value=\"130\""), "{html}"); |
| 1889 |
} |
| 1890 |
|
| 1891 |
#[test] |
| 1892 |
fn a_control_that_writes_on_change_says_so_without_a_form_around_it() { |
| 1893 |
|
| 1894 |
|
| 1895 |
let html = fragment(&Node::field( |
| 1896 |
Field::select( |
| 1897 |
"theme", |
| 1898 |
"Theme", |
| 1899 |
vec![Choice::new("dark", "Dark"), Choice::new("light", "Light")], |
| 1900 |
) |
| 1901 |
.writes(Action::post("/settings/theme")), |
| 1902 |
)); |
| 1903 |
|
| 1904 |
assert!(html.contains("hx-post=\"/settings/theme\"")); |
| 1905 |
assert!(html.contains("hx-trigger=\"change\"")); |
| 1906 |
|
| 1907 |
|
| 1908 |
assert!(html.contains("hx-include=")); |
| 1909 |
assert!(html.contains("name=\"theme\"")); |
| 1910 |
|
| 1911 |
assert!(!html.contains("<form")); |
| 1912 |
assert!(!html.contains("type=\"submit\"")); |
| 1913 |
} |
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
#[test] |
| 1918 |
fn rich_markdown_is_still_strict_when_nothing_says_otherwise() { |
| 1919 |
let html = fragment(&Node::rich("[docs](/docs) and <b>raw</b>")); |
| 1920 |
|
| 1921 |
assert!( |
| 1922 |
html.contains(r#"rel="noopener noreferrer nofollow""#), |
| 1923 |
"{html}" |
| 1924 |
); |
| 1925 |
assert!(!html.contains("<b>raw</b>"), "raw markup survived: {html}"); |
| 1926 |
} |
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
#[test] |
| 1931 |
fn the_apps_own_prose_does_not_nofollow_its_own_links() { |
| 1932 |
let html = fragment(&Node::rich("[docs](/docs)").trust(Trust::Trusted)); |
| 1933 |
|
| 1934 |
assert!(html.contains(r#"href="/docs""#), "{html}"); |
| 1935 |
assert!( |
| 1936 |
!html.contains("nofollow"), |
| 1937 |
"the app nofollowed itself: {html}" |
| 1938 |
); |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
#[test] |
| 1945 |
fn richness_and_trust_move_separately() { |
| 1946 |
let table = "| a | b |\n| - | - |\n| 1 | 2 |"; |
| 1947 |
|
| 1948 |
|
| 1949 |
let trusted_prose = fragment(&Node::rich(table).trust(Trust::Trusted)); |
| 1950 |
assert!(!trusted_prose.contains("<table"), "{trusted_prose}"); |
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
let untrusted_document = fragment( |
| 1955 |
&Node::rich(format!("{table}\n\n[x](/y) <b>raw</b>")).richness(Richness::Document), |
| 1956 |
); |
| 1957 |
assert!( |
| 1958 |
untrusted_document.contains("<table"), |
| 1959 |
"{untrusted_document}" |
| 1960 |
); |
| 1961 |
assert!( |
| 1962 |
untrusted_document.contains("nofollow"), |
| 1963 |
"{untrusted_document}" |
| 1964 |
); |
| 1965 |
assert!( |
| 1966 |
!untrusted_document.contains("<b>raw</b>"), |
| 1967 |
"a document is still not trusted: {untrusted_document}" |
| 1968 |
); |
| 1969 |
} |
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
#[test] |
| 1974 |
fn the_scripting_fence_holds_for_trusted_prose_too() { |
| 1975 |
let html = fragment(&Node::rich("hello").trust(Trust::Trusted)); |
| 1976 |
|
| 1977 |
assert!(html.contains("data-disable-scripting"), "{html}"); |
| 1978 |
} |
| 1979 |
|
| 1980 |
#[test] |
| 1981 |
fn a_markdown_field_reaches_the_markup_saying_what_its_value_is() { |
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
let html = fragment(&Node::field( |
| 1986 |
Field::new(layout::FieldKind::Rich, "body", "Body").value("# Heading"), |
| 1987 |
)); |
| 1988 |
assert!(html.contains("<textarea"), "{html}"); |
| 1989 |
assert!(html.contains(r#"data-format="markdown""#), "{html}"); |
| 1990 |
assert!(html.contains("name=\"body\""), "{html}"); |
| 1991 |
|
| 1992 |
|
| 1993 |
assert!(!html.contains("<input"), "{html}"); |
| 1994 |
} |
| 1995 |
|
| 1996 |
#[test] |
| 1997 |
fn a_call_the_host_makes_gets_the_address_and_no_transport() { |
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
let html = fragment(&Node::field( |
| 2004 |
Field::upload("audio", "Audio", [Accepted::family(layout::Family::Audio)]) |
| 2005 |
.writes(Action::post("/api/upload/presign").awaiting().by_host()), |
| 2006 |
)); |
| 2007 |
|
| 2008 |
assert!( |
| 2009 |
html.contains(r#"data-sends="/api/upload/presign""#), |
| 2010 |
"{html}" |
| 2011 |
); |
| 2012 |
|
| 2013 |
|
| 2014 |
assert!(!html.contains("hx-post"), "{html}"); |
| 2015 |
assert!(!html.contains("hx-trigger"), "{html}"); |
| 2016 |
assert!(!html.contains("href="), "{html}"); |
| 2017 |
|
| 2018 |
assert!(html.contains(r#"type="file""#), "{html}"); |
| 2019 |
assert!(html.contains(r#"accept="audio/*""#), "{html}"); |
| 2020 |
} |
| 2021 |
|
| 2022 |
#[test] |
| 2023 |
fn a_host_made_call_still_says_it_waits_and_is_locked_by_nobody() { |
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
let html = fragment(&Node::field( |
| 2029 |
Field::upload("audio", "Audio", []).writes( |
| 2030 |
Action::post("/api/upload/presign") |
| 2031 |
.awaiting_amount(41_943_040) |
| 2032 |
.by_host(), |
| 2033 |
), |
| 2034 |
)); |
| 2035 |
|
| 2036 |
assert!(html.contains(r#"data-awaiting="determinate""#), "{html}"); |
| 2037 |
assert!( |
| 2038 |
html.contains(r#"data-awaiting-amount="41943040""#), |
| 2039 |
"{html}" |
| 2040 |
); |
| 2041 |
assert!(!html.contains("hx-disable"), "{html}"); |
| 2042 |
} |
| 2043 |
|
| 2044 |
#[test] |
| 2045 |
fn a_host_made_call_carries_its_payload_rather_than_dropping_it() { |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
let html = fragment(&Node::field( |
| 2050 |
Field::upload("audio", "Audio", []).writes( |
| 2051 |
Action::post("/api/upload/presign") |
| 2052 |
.with("file_type", "audio") |
| 2053 |
.by_host(), |
| 2054 |
), |
| 2055 |
)); |
| 2056 |
|
| 2057 |
assert!(html.contains("data-vals="), "{html}"); |
| 2058 |
assert!(html.contains("file_type"), "{html}"); |
| 2059 |
assert!(!html.contains("hx-vals"), "{html}"); |
| 2060 |
} |
| 2061 |
|
| 2062 |
#[test] |
| 2063 |
fn a_navigating_act_emits_the_anchor_and_no_swap() { |
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
let html = fragment(&Node::Act(Act::new( |
| 2068 |
"Slow Reader", |
| 2069 |
Action::get("/p/slow-reader").navigating(), |
| 2070 |
))); |
| 2071 |
|
| 2072 |
assert!(html.contains(r#"href="/p/slow-reader""#), "{html}"); |
| 2073 |
assert!(!html.contains("hx-get"), "{html}"); |
| 2074 |
assert!(!html.contains("hx-swap"), "{html}"); |
| 2075 |
assert!(!html.contains("hx-trigger"), "{html}"); |
| 2076 |
|
| 2077 |
|
| 2078 |
assert!(html.contains("<a "), "{html}"); |
| 2079 |
} |
| 2080 |
|
| 2081 |
#[test] |
| 2082 |
fn a_navigating_act_carries_the_view_it_was_offered_under() { |
| 2083 |
|
| 2084 |
|
| 2085 |
let html = fragment(&Node::Act(Act::new( |
| 2086 |
"Slow Reader", |
| 2087 |
Action::get("/p/slow-reader") |
| 2088 |
.navigating() |
| 2089 |
.carrying("from", "discover"), |
| 2090 |
))); |
| 2091 |
|
| 2092 |
assert!(html.contains("from=discover"), "{html}"); |
| 2093 |
assert!(!html.contains("hx-get"), "{html}"); |
| 2094 |
} |
| 2095 |
|
| 2096 |
#[test] |
| 2097 |
fn a_navigating_write_is_still_performed() { |
| 2098 |
|
| 2099 |
|
| 2100 |
let html = fragment(&Node::Act(Act::new( |
| 2101 |
"Publish", |
| 2102 |
Action::post("/p/slow-reader/publish").navigating(), |
| 2103 |
))); |
| 2104 |
|
| 2105 |
assert!( |
| 2106 |
html.contains(r#"hx-post="/p/slow-reader/publish""#), |
| 2107 |
"{html}" |
| 2108 |
); |
| 2109 |
} |
| 2110 |
|
| 2111 |
#[test] |
| 2112 |
fn a_navigating_act_that_asks_first_keeps_its_question() { |
| 2113 |
|
| 2114 |
|
| 2115 |
let html = fragment(&Node::Act( |
| 2116 |
Act::new("Slow Reader", Action::get("/p/slow-reader").navigating()) |
| 2117 |
.confirm("Leave this page?"), |
| 2118 |
)); |
| 2119 |
|
| 2120 |
assert!(html.contains("hx-confirm="), "{html}"); |
| 2121 |
assert!(html.contains("hx-get="), "{html}"); |
| 2122 |
} |
| 2123 |
|
| 2124 |
#[test] |
| 2125 |
fn an_ordinary_read_keeps_its_swap() { |
| 2126 |
|
| 2127 |
|
| 2128 |
let html = fragment(&Node::Act(Act::new( |
| 2129 |
"Slow Reader", |
| 2130 |
Action::get("/p/slow-reader"), |
| 2131 |
))); |
| 2132 |
|
| 2133 |
assert!(html.contains(r#"href="/p/slow-reader""#), "{html}"); |
| 2134 |
assert!(html.contains(r#"hx-get="/p/slow-reader""#), "{html}"); |
| 2135 |
assert!(html.contains(r#"hx-swap="outerMorph""#), "{html}"); |
| 2136 |
} |
| 2137 |
|
| 2138 |
#[test] |
| 2139 |
fn a_call_that_goes_elsewhere_gets_the_address_and_no_transport() { |
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
let html = fragment(&Node::Act(Act::new( |
| 2145 |
"Open in a window", |
| 2146 |
Action::get("/compose/7").elsewhere(), |
| 2147 |
))); |
| 2148 |
|
| 2149 |
assert!(html.contains(r#"data-mount="/compose/7""#), "{html}"); |
| 2150 |
|
| 2151 |
|
| 2152 |
assert!(!html.contains("data-sends"), "{html}"); |
| 2153 |
|
| 2154 |
|
| 2155 |
assert!(!html.contains("href="), "{html}"); |
| 2156 |
assert!(!html.contains("hx-get"), "{html}"); |
| 2157 |
} |
| 2158 |
|
| 2159 |
#[test] |
| 2160 |
fn a_call_that_goes_elsewhere_carries_the_view_it_was_offered_under() { |
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
let html = fragment(&Node::Act(Act::new( |
| 2165 |
"Open in a window", |
| 2166 |
Action::get("/compose/7") |
| 2167 |
.elsewhere() |
| 2168 |
.carrying("folder", "drafts"), |
| 2169 |
))); |
| 2170 |
|
| 2171 |
assert!(html.contains("folder=drafts"), "{html}"); |
| 2172 |
} |
| 2173 |
|
| 2174 |
#[test] |
| 2175 |
fn a_call_that_goes_elsewhere_draws_no_wait() { |
| 2176 |
|
| 2177 |
|
| 2178 |
|
| 2179 |
let html = fragment(&Node::Act(Act::new( |
| 2180 |
"Open in a window", |
| 2181 |
Action::get("/compose/7").awaiting().elsewhere(), |
| 2182 |
))); |
| 2183 |
|
| 2184 |
assert!(!html.contains("data-awaiting"), "{html}"); |
| 2185 |
} |
| 2186 |
|
| 2187 |
#[test] |
| 2188 |
fn an_ordinary_call_is_untouched_by_the_member_existing() { |
| 2189 |
|
| 2190 |
|
| 2191 |
let html = fragment(&Node::field( |
| 2192 |
Field::new(layout::FieldKind::Text, "name", "Name") |
| 2193 |
.writes(Action::post("/rename").awaiting()), |
| 2194 |
)); |
| 2195 |
|
| 2196 |
assert!(html.contains("hx-post"), "{html}"); |
| 2197 |
assert!(html.contains("hx-disable"), "{html}"); |
| 2198 |
assert!(!html.contains("data-sends"), "{html}"); |
| 2199 |
} |
| 2200 |
|
| 2201 |
#[test] |
| 2202 |
fn an_upload_reaches_the_markup_with_what_it_takes_and_how_many() { |
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
let html = fragment(&Node::field( |
| 2210 |
Field::upload( |
| 2211 |
"media", |
| 2212 |
"Media", |
| 2213 |
[ |
| 2214 |
Accepted::family(layout::Family::Image), |
| 2215 |
Accepted::family(layout::Family::Video), |
| 2216 |
Accepted::suffix(".tar.gz"), |
| 2217 |
], |
| 2218 |
) |
| 2219 |
.many() |
| 2220 |
.writes(Action::post("/media").awaiting_amount(41_943_040)), |
| 2221 |
)); |
| 2222 |
assert!(html.contains(r#"type="file""#), "{html}"); |
| 2223 |
assert!( |
| 2224 |
html.contains(r#"accept="image/*,video/*,.tar.gz""#), |
| 2225 |
"{html}" |
| 2226 |
); |
| 2227 |
assert!(html.contains(" multiple"), "{html}"); |
| 2228 |
assert!(html.contains("/media"), "{html}"); |
| 2229 |
} |
| 2230 |
|
| 2231 |
#[test] |
| 2232 |
fn a_field_with_no_route_is_the_plain_group_it_always_was() { |
| 2233 |
let html = fragment(&Node::field(Field::new( |
| 2234 |
layout::FieldKind::Text, |
| 2235 |
"title", |
| 2236 |
"Title", |
| 2237 |
))); |
| 2238 |
assert!(html.contains("name=\"title\"")); |
| 2239 |
assert!(!html.contains("hx-")); |
| 2240 |
} |
| 2241 |
|
| 2242 |
#[test] |
| 2243 |
fn a_tick_that_is_the_write_calls_a_route_and_a_tick_that_is_not_does_not() { |
| 2244 |
|
| 2245 |
|
| 2246 |
let checklist = fragment(&Node::list(vec![ |
| 2247 |
Row::new("Buy milk").toggling(false, Action::post("/subtasks/1/toggle")), |
| 2248 |
])); |
| 2249 |
assert!(checklist.contains("type=\"checkbox\"")); |
| 2250 |
assert!(checklist.contains("hx-post=\"/subtasks/1/toggle\"")); |
| 2251 |
assert!(checklist.contains("hx-trigger=\"change\"")); |
| 2252 |
|
| 2253 |
let mut bulk = Row::new("Ada Lovelace"); |
| 2254 |
bulk.selected = Some(false); |
| 2255 |
let bulk = fragment(&Node::list(vec![bulk])); |
| 2256 |
assert!(bulk.contains("type=\"checkbox\"")); |
| 2257 |
assert!(!bulk.contains("hx-")); |
| 2258 |
} |
| 2259 |
|
| 2260 |
#[test] |
| 2261 |
fn rich_text_is_rendered_from_source_and_still_cannot_smuggle_markup() { |
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
let html = fragment(&Node::rich("A **bold** claim\n\n<script>alert(1)</script>")); |
| 2266 |
|
| 2267 |
assert!(html.contains("<strong>bold</strong>")); |
| 2268 |
assert!(!html.contains("<script")); |
| 2269 |
|
| 2270 |
|
| 2271 |
assert!(html.contains("class=\"rich\"")); |
| 2272 |
} |
| 2273 |
|
| 2274 |
#[test] |
| 2275 |
fn a_refused_form_comes_back_with_what_was_typed_in_it() { |
| 2276 |
|
| 2277 |
|
| 2278 |
let params = quasi_router::Params::new() |
| 2279 |
.with("title", "a name with <angles> in it") |
| 2280 |
.with("done", "on"); |
| 2281 |
let html = fragment(&Node::Form { |
| 2282 |
marks: ::quasi_router::stage::Marks::none(), |
| 2283 |
action: Action::post("/tasks"), |
| 2284 |
submit: "Save".into(), |
| 2285 |
fields: vec![ |
| 2286 |
Field::new(layout::FieldKind::Text, "title", "Title").refilled(¶ms), |
| 2287 |
Field::new(layout::FieldKind::Checkbox, "done", "Done").refilled(¶ms), |
| 2288 |
|
| 2289 |
|
| 2290 |
Field::new(layout::FieldKind::Text, "notes", "Notes").refilled(¶ms), |
| 2291 |
], |
| 2292 |
}); |
| 2293 |
|
| 2294 |
assert!(html.contains("value=\"a name with <angles> in it\"")); |
| 2295 |
assert!(html.contains("checked")); |
| 2296 |
|
| 2297 |
assert!(!html.contains("<angles>")); |
| 2298 |
|
| 2299 |
|
| 2300 |
assert!(html.contains("name=\"notes\" value=\"\"")); |
| 2301 |
} |
| 2302 |
|
| 2303 |
#[test] |
| 2304 |
fn a_secret_is_never_offered_back_however_it_was_set() { |
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
let params = quasi_router::Params::new().with("password", "hunter2"); |
| 2309 |
|
| 2310 |
let refused = Field::new(layout::FieldKind::Secret, "password", "Password").refilled(¶ms); |
| 2311 |
assert_eq!(refused.value, None); |
| 2312 |
|
| 2313 |
let mut forced = Field::new(layout::FieldKind::Secret, "password", "Password"); |
| 2314 |
forced.value = Some("hunter2".to_owned()); |
| 2315 |
let html = fragment(&Node::Form { |
| 2316 |
marks: ::quasi_router::stage::Marks::none(), |
| 2317 |
action: Action::post("/login"), |
| 2318 |
submit: "Sign in".into(), |
| 2319 |
fields: vec![forced], |
| 2320 |
}); |
| 2321 |
assert!(!html.contains("hunter2")); |
| 2322 |
} |
| 2323 |
|
| 2324 |
#[test] |
| 2325 |
fn an_empty_region_says_so_instead_of_rendering_an_empty_box() { |
| 2326 |
|
| 2327 |
|
| 2328 |
let empty = render( |
| 2329 |
&Screen::list_detail("Projects", false).with( |
| 2330 |
Slot::new("list", RegionKind::Pane) |
| 2331 |
.with(Node::section("Projects")) |
| 2332 |
.with(Node::empty("No projects yet")), |
| 2333 |
), |
| 2334 |
); |
| 2335 |
|
| 2336 |
assert!(empty.contains("No projects yet")); |
| 2337 |
assert!(empty.contains(r#"data-state="empty""#)); |
| 2338 |
|
| 2339 |
|
| 2340 |
assert!(empty.contains(">Projects<")); |
| 2341 |
|
| 2342 |
|
| 2343 |
assert!(!empty.contains(r#"role="alert""#)); |
| 2344 |
} |
| 2345 |
|
| 2346 |
#[test] |
| 2347 |
fn a_failed_region_is_a_different_state_from_an_empty_one_and_offers_a_way_out() { |
| 2348 |
let failed = render( |
| 2349 |
&Screen::list_detail("Events", false).with( |
| 2350 |
Slot::new("list", RegionKind::Pane).with( |
| 2351 |
Node::failed("Failed to load events") |
| 2352 |
.offering(Act::new("Try again", Action::get("/events"))), |
| 2353 |
), |
| 2354 |
), |
| 2355 |
); |
| 2356 |
|
| 2357 |
assert!(failed.contains(r#"data-state="failed""#)); |
| 2358 |
assert!(failed.contains(r#"data-tone="danger""#)); |
| 2359 |
assert!(failed.contains(r#"role="alert""#)); |
| 2360 |
|
| 2361 |
assert!(failed.contains("hx-get=\"/events\"")); |
| 2362 |
} |
| 2363 |
|
| 2364 |
#[test] |
| 2365 |
fn a_destructive_act_asks_first_and_a_shortcut_reaches_it() { |
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
let html = fragment(&Node::Act( |
| 2370 |
Act::new("Delete", Action::post("/tasks/1/delete")) |
| 2371 |
.tone(layout::Tone::Danger) |
| 2372 |
.confirm("Delete this task? This cannot be undone.") |
| 2373 |
.key("d"), |
| 2374 |
)); |
| 2375 |
|
| 2376 |
assert!(html.contains(r#"hx-confirm="Delete this task? This cannot be undone.""#)); |
| 2377 |
assert!(html.contains(r#"accesskey="d""#)); |
| 2378 |
assert!(html.contains("hx-post=\"/tasks/1/delete\"")); |
| 2379 |
} |
| 2380 |
|
| 2381 |
#[test] |
| 2382 |
fn a_row_offers_what_it_does_not_show() { |
| 2383 |
|
| 2384 |
|
| 2385 |
let html = fragment(&Node::list([Row::new("Buy milk") |
| 2386 |
.act(Act::new("Done", Action::post("/tasks/1/complete"))) |
| 2387 |
.offers(Act::new("Duplicate", Action::post("/tasks/1/copy"))) |
| 2388 |
.offers( |
| 2389 |
Act::new("Delete", Action::post("/tasks/1/delete")).confirm("Delete this task?"), |
| 2390 |
)])); |
| 2391 |
|
| 2392 |
assert!(html.contains(r#"data-menu="row""#)); |
| 2393 |
|
| 2394 |
|
| 2395 |
assert!(html.contains(" hidden>")); |
| 2396 |
assert!(html.contains("Duplicate")); |
| 2397 |
assert!(html.contains(r#"hx-confirm="Delete this task?""#)); |
| 2398 |
|
| 2399 |
assert!(html.contains("Done")); |
| 2400 |
} |
| 2401 |
|
| 2402 |
#[test] |
| 2403 |
fn a_list_says_how_much_more_there_is_and_how_to_ask() { |
| 2404 |
|
| 2405 |
|
| 2406 |
let counted = fragment( |
| 2407 |
&Node::list([Row::new("One")]) |
| 2408 |
.and_more(Rest::more(50, Action::get("/tasks?page=2")).of(400)), |
| 2409 |
); |
| 2410 |
assert!(counted.contains("50 of 400")); |
| 2411 |
assert!(counted.contains("hx-get=\"/tasks?page=2\"")); |
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
let uncounted = |
| 2416 |
fragment(&Node::list([Row::new("One")]).and_more(Rest::more(50, Action::get("/more")))); |
| 2417 |
assert!(uncounted.contains("Show more")); |
| 2418 |
assert!(!uncounted.contains(" of ")); |
| 2419 |
|
| 2420 |
let plain = fragment(&Node::list([Row::new("One")])); |
| 2421 |
assert!(!plain.contains("rest")); |
| 2422 |
} |
| 2423 |
|
| 2424 |
#[test] |
| 2425 |
fn a_table_can_say_there_is_more_and_the_pager_is_not_a_row() { |
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
|
| 2430 |
let html = fragment(&Node::Table { |
| 2431 |
marks: ::quasi_router::stage::Marks::none(), |
| 2432 |
columns: vec![Column::new("Name")], |
| 2433 |
rows: vec![Row::cells([Cell::new("One")])], |
| 2434 |
more: Some( |
| 2435 |
Rest::page(100, 50) |
| 2436 |
.of(400) |
| 2437 |
.back(Action::get("/tasks?page=2")) |
| 2438 |
.forward(Action::get("/tasks?page=4")), |
| 2439 |
), |
| 2440 |
}); |
| 2441 |
assert!(html.contains("3 / 8")); |
| 2442 |
assert!(html.contains("hx-get=\"/tasks?page=4\"")); |
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
let table_end = html.rfind("</div>").expect("the table closes"); |
| 2448 |
let pager = html.find("rest-position").expect("the pager is drawn"); |
| 2449 |
assert!( |
| 2450 |
pager < table_end, |
| 2451 |
"the pager should follow the table's rows, not sit inside its last cell" |
| 2452 |
); |
| 2453 |
|
| 2454 |
let plain = fragment(&Node::Table { |
| 2455 |
marks: ::quasi_router::stage::Marks::none(), |
| 2456 |
columns: vec![Column::new("Name")], |
| 2457 |
rows: vec![Row::cells([Cell::new("One")])], |
| 2458 |
more: None, |
| 2459 |
}); |
| 2460 |
assert!(!plain.contains("rest")); |
| 2461 |
} |
| 2462 |
|
| 2463 |
#[test] |
| 2464 |
fn a_paged_list_prints_its_page_and_keeps_both_ends() { |
| 2465 |
|
| 2466 |
|
| 2467 |
let middle = fragment( |
| 2468 |
&Node::list([Row::new("One")]).and_more( |
| 2469 |
Rest::page(100, 50) |
| 2470 |
.of(400) |
| 2471 |
.back(Action::get("/tasks?page=2")) |
| 2472 |
.forward(Action::get("/tasks?page=4")), |
| 2473 |
), |
| 2474 |
); |
| 2475 |
assert!(middle.contains("3 / 8")); |
| 2476 |
assert!(middle.contains("hx-get=\"/tasks?page=2\"")); |
| 2477 |
assert!(middle.contains("hx-get=\"/tasks?page=4\"")); |
| 2478 |
assert!(!middle.contains("disabled")); |
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 2484 |
|
| 2485 |
let first = fragment( |
| 2486 |
&Node::list([Row::new("One")]).and_more( |
| 2487 |
Rest::page(0, 50) |
| 2488 |
.of(400) |
| 2489 |
.forward(Action::get("/tasks?page=2")), |
| 2490 |
), |
| 2491 |
); |
| 2492 |
assert!(first.contains("1 / 8")); |
| 2493 |
assert!(!first.contains("rest-previous"), "{first}"); |
| 2494 |
assert!(first.contains("rest-next"), "{first}"); |
| 2495 |
assert!(!first.contains("disabled"), "{first}"); |
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
let last = fragment( |
| 2500 |
&Node::list([Row::new("One")]).and_more( |
| 2501 |
Rest::page(350, 50) |
| 2502 |
.of(400) |
| 2503 |
.back(Action::get("/tasks?page=7")), |
| 2504 |
), |
| 2505 |
); |
| 2506 |
assert!(last.contains("8 / 8")); |
| 2507 |
assert!(last.contains("rest-previous"), "{last}"); |
| 2508 |
assert!(!last.contains("rest-next"), "{last}"); |
| 2509 |
} |
| 2510 |
|
| 2511 |
#[test] |
| 2512 |
fn a_pager_that_offers_pages_draws_a_strip_and_marks_the_one_being_read() { |
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
let html = fragment( |
| 2517 |
&Node::list([Row::new("One")]).and_more( |
| 2518 |
Rest::page(100, 50) |
| 2519 |
.of(400) |
| 2520 |
.back(Action::get("/feed?page=2")) |
| 2521 |
.forward(Action::get("/feed?page=4")) |
| 2522 |
.jumping(Jump::new(2, Action::get("/feed?page=2"))) |
| 2523 |
.jumping(Jump::new(3, Action::get("/feed?page=3")).here()) |
| 2524 |
.jumping(Jump::new(4, Action::get("/feed?page=4"))), |
| 2525 |
), |
| 2526 |
); |
| 2527 |
|
| 2528 |
assert!(html.contains("rest-pages"), "{html}"); |
| 2529 |
assert!(!html.contains("hx-get=\"/feed?page=3\""), "{html}"); |
| 2530 |
assert!(html.contains("hx-get=\"/feed?page=2\""), "{html}"); |
| 2531 |
assert!(html.contains("hx-get=\"/feed?page=4\""), "{html}"); |
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
assert!(html.contains("aria-current=\"page\""), "{html}"); |
| 2536 |
assert!(html.contains("rest-page-here"), "{html}"); |
| 2537 |
|
| 2538 |
|
| 2539 |
assert!(!html.contains("3 / 8"), "{html}"); |
| 2540 |
assert!(!html.contains("rest-position"), "{html}"); |
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
assert!(html.contains("rest-previous"), "{html}"); |
| 2545 |
assert!(html.contains("rest-next"), "{html}"); |
| 2546 |
} |
| 2547 |
|
| 2548 |
#[test] |
| 2549 |
fn a_pager_that_offers_no_pages_prints_the_position_it_always_did() { |
| 2550 |
|
| 2551 |
|
| 2552 |
let html = fragment( |
| 2553 |
&Node::list([Row::new("One")]).and_more( |
| 2554 |
Rest::page(100, 50) |
| 2555 |
.of(400) |
| 2556 |
.back(Action::get("/feed?page=2")) |
| 2557 |
.forward(Action::get("/feed?page=4")), |
| 2558 |
), |
| 2559 |
); |
| 2560 |
assert!(html.contains("rest-position"), "{html}"); |
| 2561 |
assert!(!html.contains("rest-pages"), "{html}"); |
| 2562 |
} |
| 2563 |
|
| 2564 |
#[test] |
| 2565 |
fn a_sortable_column_says_which_way_and_offers_the_press() { |
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
let html = fragment(&Node::Table { |
| 2570 |
marks: ::quasi_router::stage::Marks::none(), |
| 2571 |
columns: vec![ |
| 2572 |
Column::new("Title") |
| 2573 |
.reorder(Action::get("/tasks?sort=title")) |
| 2574 |
.sorted(layout::Sort::Ascending), |
| 2575 |
Column::new("Due").reorder(Action::get("/tasks?sort=due")), |
| 2576 |
Column::new("Notes"), |
| 2577 |
], |
| 2578 |
rows: vec![Row::cells(["Ship it", "Tomorrow", "None"])], |
| 2579 |
more: None, |
| 2580 |
}); |
| 2581 |
|
| 2582 |
assert!(html.contains(r#"aria-sort="ascending""#)); |
| 2583 |
assert_eq!(html.matches("data-sortable").count(), 2); |
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
assert!(html.contains(r#"<a class="table-sort""#)); |
| 2588 |
assert!(html.contains("href=\"/tasks?sort=due\"")); |
| 2589 |
assert!(html.contains("hx-get=\"/tasks?sort=due\"")); |
| 2590 |
} |
| 2591 |
|
| 2592 |
#[test] |
| 2593 |
fn a_notice_interrupts_only_when_its_tone_says_to() { |
| 2594 |
let danger = fragment(&Node::banner(layout::Tone::Danger, "Disk full")); |
| 2595 |
assert!(danger.contains("role=\"alert\"")); |
| 2596 |
|
| 2597 |
let info = fragment(&Node::toast(layout::Tone::Info, "Saved")); |
| 2598 |
assert!(info.contains("role=\"status\"")); |
| 2599 |
assert!(info.contains("aria-live=\"polite\"")); |
| 2600 |
} |
| 2601 |
|
| 2602 |
#[test] |
| 2603 |
fn a_screens_notices_come_before_its_regions() { |
| 2604 |
let screen = Screen::list_detail("Tasks", false) |
| 2605 |
.with(Slot::new("list", RegionKind::Pane)) |
| 2606 |
.saying(Node::toast(layout::Tone::Success, "Saved")); |
| 2607 |
let html = render(&screen); |
| 2608 |
|
| 2609 |
let notice = html.find("Saved").expect("the notice is rendered"); |
| 2610 |
let region = html.find("id=\"list\"").expect("the region is rendered"); |
| 2611 |
assert!(notice < region); |
| 2612 |
} |
| 2613 |
|
| 2614 |
#[test] |
| 2615 |
fn every_arrangement_has_a_class_and_they_differ() { |
| 2616 |
let plain = render(&Screen::list_detail("A", false)); |
| 2617 |
let tabbed = render(&Screen::list_detail("A", true)); |
| 2618 |
let sidebar = render(&Screen::sidebar_content("A")); |
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
assert!(plain.contains("class=\"list-detail measure-wide\"")); |
| 2624 |
assert!(tabbed.contains("class=\"list-detail-tabbed measure-wide\"")); |
| 2625 |
assert!(sidebar.contains("class=\"sidebar-content measure-wide\"")); |
| 2626 |
|
| 2627 |
let reading = render(&Screen::list_detail("A", false).measured(layout::Measure::Reading)); |
| 2628 |
assert!(reading.contains("class=\"list-detail measure-reading\"")); |
| 2629 |
} |
| 2630 |
|
| 2631 |
#[test] |
| 2632 |
fn a_screen_carries_the_share_as_a_number_rather_than_a_class() { |
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
let wide = render(&Screen::sidebar_content("A")); |
| 2638 |
assert!( |
| 2639 |
wide.contains("--region-share:25fr;--region-rest:75fr"), |
| 2640 |
"{wide}" |
| 2641 |
); |
| 2642 |
|
| 2643 |
let narrow = render(&Screen::new( |
| 2644 |
"A", |
| 2645 |
layout::Arrangement::sidebar_content().with_share(layout::Share::percent(20)), |
| 2646 |
)); |
| 2647 |
assert!( |
| 2648 |
narrow.contains("--region-share:20fr;--region-rest:80fr"), |
| 2649 |
"{narrow}" |
| 2650 |
); |
| 2651 |
} |
| 2652 |
|
| 2653 |
#[test] |
| 2654 |
fn a_class_prefix_reaches_every_emitted_name() { |
| 2655 |
let emit = crate::Emit { |
| 2656 |
class_prefix: "qs-", |
| 2657 |
..crate::Emit::default() |
| 2658 |
}; |
| 2659 |
|
| 2660 |
let screen = Screen::list_detail("Tasks", false).with( |
| 2661 |
Slot::new("list", RegionKind::Pane) |
| 2662 |
.with(Node::page("Tasks")) |
| 2663 |
.with(Node::list([Row::new("One")])), |
| 2664 |
); |
| 2665 |
let html = Webview::new().with_emit(emit).screen(&screen); |
| 2666 |
|
| 2667 |
assert!(html.contains("class=\"qs-list-detail qs-measure-wide\"")); |
| 2668 |
assert!(html.contains("qs-region qs-pane")); |
| 2669 |
assert!(html.contains("qs-heading")); |
| 2670 |
assert!(html.contains("qs-list")); |
| 2671 |
|
| 2672 |
|
| 2673 |
assert!(!html.contains("class=\"list\"")); |
| 2674 |
assert!(!html.contains("class=\"heading\"")); |
| 2675 |
} |
| 2676 |
|
| 2677 |
#[test] |
| 2678 |
fn the_shell_serves_its_assets_from_where_the_host_says() { |
| 2679 |
let html = Webview::under("/assets").screen(&Screen::list_detail("A", false)); |
| 2680 |
assert!(html.contains("src=\"/assets/htmx.min.js\"")); |
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
let tauri = Webview::under("quasi://localhost/assets").screen(&Screen::list_detail("A", false)); |
| 2685 |
assert!(tauri.contains("src=\"quasi://localhost/assets/htmx.min.js\"")); |
| 2686 |
} |
| 2687 |
|
| 2688 |
#[test] |
| 2689 |
fn stylesheets_link_in_the_order_they_were_added() { |
| 2690 |
let shell = Shell::default().styled("/a.css").styled("/b.css"); |
| 2691 |
let html = Webview::new() |
| 2692 |
.with_shell(shell) |
| 2693 |
.screen(&Screen::list_detail("A", false)); |
| 2694 |
|
| 2695 |
let a = html.find("/a.css").expect("a is linked"); |
| 2696 |
let b = html.find("/b.css").expect("b is linked"); |
| 2697 |
assert!(a < b); |
| 2698 |
} |
| 2699 |
|
| 2700 |
#[test] |
| 2701 |
fn injected_head_markup_lands_last_so_it_can_override() { |
| 2702 |
let shell = Shell::default().with_head("<link rel=\"icon\" href=\"/f.png\">"); |
| 2703 |
let html = Webview::new() |
| 2704 |
.with_shell(shell) |
| 2705 |
.screen(&Screen::list_detail("A", false)); |
| 2706 |
|
| 2707 |
let icon = html.find("/f.png").expect("the icon is linked"); |
| 2708 |
let htmx = html.find("htmx.min.js").expect("htmx is linked"); |
| 2709 |
assert!(htmx < icon); |
| 2710 |
assert!(icon < html.find("</head>").expect("the head closes")); |
| 2711 |
} |
| 2712 |
|
| 2713 |
#[test] |
| 2714 |
fn injected_opening_body_markup_precedes_everything_the_screen_draws() { |
| 2715 |
|
| 2716 |
let shell = Shell::default().with_body_first("<a href=\"#pane\">Skip</a>"); |
| 2717 |
let html = Webview::new() |
| 2718 |
.with_shell(shell) |
| 2719 |
.screen(&Screen::list_detail("A", false)); |
| 2720 |
|
| 2721 |
let skip = html.find("Skip").expect("the link is emitted"); |
| 2722 |
assert!(html.find("<body").expect("the body opens") < skip); |
| 2723 |
assert!(skip < html.find("<main").expect("the content opens")); |
| 2724 |
} |
| 2725 |
|
| 2726 |
#[test] |
| 2727 |
fn injected_body_markup_lands_after_the_content_and_before_the_close() { |
| 2728 |
|
| 2729 |
|
| 2730 |
|
| 2731 |
let shell = Shell::default().with_body_last("<script src=\"/tail.js\"></script>"); |
| 2732 |
let html = Webview::new() |
| 2733 |
.with_shell(shell) |
| 2734 |
.screen(&Screen::list_detail("A", false)); |
| 2735 |
|
| 2736 |
let tail = html.find("/tail.js").expect("the script is emitted"); |
| 2737 |
assert!(html.find("</main>").expect("the content closes") < tail); |
| 2738 |
assert!(tail < html.find("</body>").expect("the body closes")); |
| 2739 |
} |
| 2740 |
|
| 2741 |
#[test] |
| 2742 |
fn a_document_with_nothing_to_append_is_the_one_it_was_before() { |
| 2743 |
|
| 2744 |
|
| 2745 |
let html = Webview::new().screen(&Screen::list_detail("A", false)); |
| 2746 |
assert!(html.ends_with("</body></html>")); |
| 2747 |
} |
| 2748 |
|
| 2749 |
#[test] |
| 2750 |
fn the_layer_statement_precedes_every_stylesheet() { |
| 2751 |
|
| 2752 |
|
| 2753 |
let shell = Shell::default() |
| 2754 |
.layered(["base", "components", "responsive"]) |
| 2755 |
.styled("/geometry.css") |
| 2756 |
.styled("/style.css"); |
| 2757 |
let html = Webview::new() |
| 2758 |
.with_shell(shell) |
| 2759 |
.screen(&Screen::list_detail("A", false)); |
| 2760 |
|
| 2761 |
let stmt = html |
| 2762 |
.find("@layer makeover, base, components, responsive;") |
| 2763 |
.expect("the order is stated"); |
| 2764 |
let first_sheet = html.find("/geometry.css").expect("the sheet is linked"); |
| 2765 |
assert!(stmt < first_sheet); |
| 2766 |
} |
| 2767 |
|
| 2768 |
#[test] |
| 2769 |
fn the_layer_statement_is_emitted_even_with_no_app_layers() { |
| 2770 |
|
| 2771 |
|
| 2772 |
let html = Webview::new().screen(&Screen::list_detail("A", false)); |
| 2773 |
assert!(html.contains("@layer makeover;")); |
| 2774 |
} |
| 2775 |
|
| 2776 |
#[test] |
| 2777 |
fn a_layer_name_cannot_escape_the_style_element() { |
| 2778 |
|
| 2779 |
|
| 2780 |
let shell = Shell::default().layered(["base</style><script>alert(1)</script>"]); |
| 2781 |
let html = Webview::new() |
| 2782 |
.with_shell(shell) |
| 2783 |
.screen(&Screen::list_detail("A", false)); |
| 2784 |
|
| 2785 |
assert!(!html.contains("<script>alert(1)")); |
| 2786 |
assert!(html.contains("@layer makeover, basestylescriptalert1script;")); |
| 2787 |
} |
| 2788 |
|
| 2789 |
#[test] |
| 2790 |
fn head_first_markup_lands_before_the_layer_statement_and_the_sheets() { |
| 2791 |
|
| 2792 |
let shell = Shell::default() |
| 2793 |
.with_head_first("<link rel=\"preload\" href=\"/f.woff2\" as=\"font\">") |
| 2794 |
.styled("/style.css"); |
| 2795 |
let html = Webview::new() |
| 2796 |
.with_shell(shell) |
| 2797 |
.screen(&Screen::list_detail("A", false)); |
| 2798 |
|
| 2799 |
let preload = html.find("/f.woff2").expect("the font is preloaded"); |
| 2800 |
let stmt = html.find("@layer makeover").expect("the order is stated"); |
| 2801 |
let sheet = html.find("/style.css").expect("the sheet is linked"); |
| 2802 |
assert!(preload < stmt); |
| 2803 |
assert!(stmt < sheet); |
| 2804 |
} |
| 2805 |
|
| 2806 |
#[test] |
| 2807 |
fn head_first_and_head_are_different_ends_of_the_same_head() { |
| 2808 |
let shell = Shell::default() |
| 2809 |
.with_head_first("<meta name=\"first\">") |
| 2810 |
.with_head("<meta name=\"last\">"); |
| 2811 |
let html = Webview::new() |
| 2812 |
.with_shell(shell) |
| 2813 |
.screen(&Screen::list_detail("A", false)); |
| 2814 |
|
| 2815 |
let first = html.find("name=\"first\"").expect("first is emitted"); |
| 2816 |
let last = html.find("name=\"last\"").expect("last is emitted"); |
| 2817 |
let htmx = html.find("htmx.min.js").expect("htmx is linked"); |
| 2818 |
assert!(first < htmx); |
| 2819 |
assert!(htmx < last); |
| 2820 |
} |
| 2821 |
|
| 2822 |
#[test] |
| 2823 |
fn repeated_head_first_calls_keep_their_call_order() { |
| 2824 |
let shell = Shell::default() |
| 2825 |
.with_head_first("<meta name=\"a\">") |
| 2826 |
.with_head_first("<meta name=\"b\">"); |
| 2827 |
let html = Webview::new() |
| 2828 |
.with_shell(shell) |
| 2829 |
.screen(&Screen::list_detail("A", false)); |
| 2830 |
|
| 2831 |
assert!(html.find("name=\"a\"") < html.find("name=\"b\"")); |
| 2832 |
} |
| 2833 |
|
| 2834 |
#[test] |
| 2835 |
fn a_document_wraps_markup_the_renderer_did_not_write() { |
| 2836 |
|
| 2837 |
|
| 2838 |
let shell = Shell::default().layered(["base"]).styled("/style.css"); |
| 2839 |
let html = shell.document("Console", "<main>hand-written</main>"); |
| 2840 |
|
| 2841 |
assert!(html.starts_with("<!doctype html><html lang=\"en\">")); |
| 2842 |
assert!(html.contains("<title>Console</title>")); |
| 2843 |
assert!(html.contains("<main>hand-written</main>")); |
| 2844 |
assert!(html.ends_with("</body></html>")); |
| 2845 |
} |
| 2846 |
|
| 2847 |
#[test] |
| 2848 |
fn a_document_states_the_layers_before_the_sheets_like_a_screen_does() { |
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
let shell = Shell::default().layered(["base"]).styled("/style.css"); |
| 2853 |
let html = shell.document("Console", "<main>x</main>"); |
| 2854 |
|
| 2855 |
let stmt = html |
| 2856 |
.find("@layer makeover, base;") |
| 2857 |
.expect("the order is stated"); |
| 2858 |
let sheet = html.find("/style.css").expect("the sheet is linked"); |
| 2859 |
let body = html.find("<main>").expect("the body is placed"); |
| 2860 |
assert!(stmt < sheet); |
| 2861 |
assert!(sheet < body); |
| 2862 |
} |
| 2863 |
|
| 2864 |
#[test] |
| 2865 |
fn a_documents_body_tag_is_the_shells_own() { |
| 2866 |
|
| 2867 |
|
| 2868 |
let shell = Shell { |
| 2869 |
body_class: Some("console".into()), |
| 2870 |
..Shell::default() |
| 2871 |
}; |
| 2872 |
let html = shell.document("Console", "x"); |
| 2873 |
|
| 2874 |
assert!(html.contains("<body class=\"console\">x</body>")); |
| 2875 |
} |
| 2876 |
|
| 2877 |
#[test] |
| 2878 |
fn a_screens_body_class_joins_the_shells_rather_than_replacing_it() { |
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
let shell = Shell { |
| 2883 |
body_class: Some("mnw".into()), |
| 2884 |
..Shell::default() |
| 2885 |
}; |
| 2886 |
let screen = |
| 2887 |
Screen::list_detail("Admin", false).documented(Document::default().classed("admin-page")); |
| 2888 |
let html = Webview::new().with_shell(shell).screen(&screen); |
| 2889 |
|
| 2890 |
assert!(html.contains("<body class=\"mnw admin-page\""), "{html}"); |
| 2891 |
} |
| 2892 |
|
| 2893 |
#[test] |
| 2894 |
fn a_screen_that_says_nothing_about_the_document_emits_what_it_always_did() { |
| 2895 |
|
| 2896 |
|
| 2897 |
let plain = Webview::new().screen(&Screen::list_detail("A", false)); |
| 2898 |
assert!(plain.contains("<html lang=\"en\">"), "{plain}"); |
| 2899 |
assert!(plain.contains("<body>"), "{plain}"); |
| 2900 |
} |
| 2901 |
|
| 2902 |
#[test] |
| 2903 |
fn a_screen_puts_its_own_attributes_on_the_root() { |
| 2904 |
|
| 2905 |
|
| 2906 |
|
| 2907 |
let screen = Screen::list_detail("Settings", false) |
| 2908 |
.documented(Document::default().rooted("data-theme", "slate")); |
| 2909 |
let html = Webview::new().screen(&screen); |
| 2910 |
|
| 2911 |
|
| 2912 |
assert!( |
| 2913 |
html.contains("<html lang=\"en\" data-theme=\"slate\">"), |
| 2914 |
"{html}" |
| 2915 |
); |
| 2916 |
} |
| 2917 |
|
| 2918 |
#[test] |
| 2919 |
fn a_root_attribute_this_renderer_will_not_write_is_dropped_and_the_screen_still_draws() { |
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
let screen = Screen::list_detail("A", false).documented( |
| 2924 |
Document::default() |
| 2925 |
.rooted("x\" onload=alert(1) y", "1") |
| 2926 |
.rooted("data-theme", "slate"), |
| 2927 |
); |
| 2928 |
let html = Webview::new().screen(&screen); |
| 2929 |
|
| 2930 |
assert!(!html.contains("onload"), "{html}"); |
| 2931 |
assert!(html.contains("data-theme=\"slate\""), "{html}"); |
| 2932 |
assert!(html.contains("<main"), "{html}"); |
| 2933 |
} |
| 2934 |
|
| 2935 |
#[test] |
| 2936 |
fn a_reference_opens_aside_and_a_handoff_replaces_the_page() { |
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
|
| 2941 |
let aside = fragment(&Node::Link { |
| 2942 |
text: "Docs".into(), |
| 2943 |
action: Action::external("https://example.com"), |
| 2944 |
}); |
| 2945 |
let onward = fragment(&Node::Link { |
| 2946 |
text: "View on makenot.work".into(), |
| 2947 |
action: Action::leaving("https://makenot.work/p/x"), |
| 2948 |
}); |
| 2949 |
|
| 2950 |
assert!(aside.contains("target=\"_blank\""), "{aside}"); |
| 2951 |
assert!(!onward.contains("target="), "{onward}"); |
| 2952 |
|
| 2953 |
|
| 2954 |
assert!(aside.contains("rel=\"noopener noreferrer\""), "{aside}"); |
| 2955 |
assert!(onward.contains("rel=\"noopener noreferrer\""), "{onward}"); |
| 2956 |
|
| 2957 |
assert!(!onward.contains("hx-get"), "{onward}"); |
| 2958 |
} |
| 2959 |
|
| 2960 |
#[test] |
| 2961 |
fn a_scriptless_shell_writes_no_script_at_all() { |
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
let html = Webview::new() |
| 2966 |
.with_shell(Shell::under("/static").without_scripts()) |
| 2967 |
.screen(&Screen::list_detail("A", false)); |
| 2968 |
|
| 2969 |
assert!(!html.contains("<script"), "{html}"); |
| 2970 |
} |
| 2971 |
|
| 2972 |
#[test] |
| 2973 |
fn a_canvas_writes_its_markup_out_and_scopes_it_the_way_the_app_said() { |
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
let html = fragment(&Node::Canvas(Box::new( |
| 2978 |
Canvas::new("<h1>Hello</h1>") |
| 2979 |
.classed("user-canvas") |
| 2980 |
.identified("uc-11111111"), |
| 2981 |
))); |
| 2982 |
|
| 2983 |
assert!( |
| 2984 |
html.contains("<div class=\"user-canvas\" id=\"uc-11111111\"><h1>Hello</h1></div>"), |
| 2985 |
"{html}" |
| 2986 |
); |
| 2987 |
} |
| 2988 |
|
| 2989 |
#[test] |
| 2990 |
fn a_canvas_draws_the_apps_own_nodes_inside_the_scope_after_the_markup() { |
| 2991 |
|
| 2992 |
|
| 2993 |
let html = fragment(&Node::Canvas(Box::new( |
| 2994 |
Canvas::new("<p>mine</p>") |
| 2995 |
.classed("user-canvas") |
| 2996 |
.with(Node::text("Free")), |
| 2997 |
))); |
| 2998 |
|
| 2999 |
let markup = html.find("<p>mine</p>").expect("the markup is written"); |
| 3000 |
let node = html.find("Free").expect("the node is written"); |
| 3001 |
let close = html.rfind("</div>").expect("the scope closes"); |
| 3002 |
assert!(markup < node, "{html}"); |
| 3003 |
assert!(node < close, "{html}"); |
| 3004 |
} |
| 3005 |
|
| 3006 |
#[test] |
| 3007 |
fn a_canvas_with_no_scope_is_markup_with_nothing_aimed_at_it() { |
| 3008 |
|
| 3009 |
|
| 3010 |
let html = fragment(&Node::Canvas(Box::new(Canvas::new("<p>x</p>")))); |
| 3011 |
|
| 3012 |
assert!(html.contains("<div><p>x</p></div>"), "{html}"); |
| 3013 |
} |
| 3014 |
|
| 3015 |
#[test] |
| 3016 |
fn a_canvas_scope_cannot_leave_the_attribute_it_is_written_in() { |
| 3017 |
|
| 3018 |
|
| 3019 |
|
| 3020 |
let html = fragment(&Node::Canvas(Box::new( |
| 3021 |
Canvas::new("<p>x</p>").classed("a\" onload=alert(1) b"), |
| 3022 |
))); |
| 3023 |
|
| 3024 |
|
| 3025 |
|
| 3026 |
assert!( |
| 3027 |
html.contains("class=\"a" onload=alert(1) b\""), |
| 3028 |
"{html}" |
| 3029 |
); |
| 3030 |
} |
| 3031 |
|
| 3032 |
#[test] |
| 3033 |
fn a_screens_own_stylesheet_is_last_in_the_head_and_unlayered() { |
| 3034 |
|
| 3035 |
|
| 3036 |
|
| 3037 |
let screen = Screen::list_detail("Profile", false) |
| 3038 |
.documented(Document::default().styled(".user-canvas p { color: red }")); |
| 3039 |
let html = Webview::new() |
| 3040 |
.with_shell(Shell::under("/static").layered(["base"])) |
| 3041 |
.screen(&screen); |
| 3042 |
|
| 3043 |
let layers = html.find("@layer").expect("the layer statement is written"); |
| 3044 |
let sheet = html |
| 3045 |
.find(".user-canvas p { color: red }") |
| 3046 |
.expect("the screen's sheet is written"); |
| 3047 |
let head_end = html.find("</head>").expect("the head closes"); |
| 3048 |
assert!(layers < sheet, "{html}"); |
| 3049 |
assert!(sheet < head_end, "{html}"); |
| 3050 |
} |
| 3051 |
|
| 3052 |
#[test] |
| 3053 |
fn a_screen_that_says_nothing_about_its_style_emits_no_extra_element() { |
| 3054 |
|
| 3055 |
|
| 3056 |
let bare = Webview::new().screen(&Screen::list_detail("A", false)); |
| 3057 |
let styled = Webview::new() |
| 3058 |
.screen(&Screen::list_detail("A", false).documented(Document::default().styled("p{}"))); |
| 3059 |
|
| 3060 |
assert_eq!( |
| 3061 |
bare.matches("<style>").count() + 1, |
| 3062 |
styled.matches("<style>").count(), |
| 3063 |
"{styled}" |
| 3064 |
); |
| 3065 |
} |
| 3066 |
|
| 3067 |
#[test] |
| 3068 |
fn a_stylesheet_cannot_close_the_element_it_is_written_into() { |
| 3069 |
|
| 3070 |
|
| 3071 |
|
| 3072 |
|
| 3073 |
let screen = Screen::list_detail("A", false).documented( |
| 3074 |
Document::default().styled("p::after { content: '</style><script>alert(1)</script>' }"), |
| 3075 |
); |
| 3076 |
let html = Webview::new().screen(&screen); |
| 3077 |
|
| 3078 |
|
| 3079 |
|
| 3080 |
|
| 3081 |
let sheet = html |
| 3082 |
.rfind("<style>") |
| 3083 |
.map(|at| at + "<style>".len()) |
| 3084 |
.expect("the screen's sheet is written"); |
| 3085 |
let ends = html[sheet..] |
| 3086 |
.find("</style") |
| 3087 |
.expect("the sheet's element closes"); |
| 3088 |
let inside = &html[sheet..sheet + ends]; |
| 3089 |
assert!(inside.contains("alert(1)</script>' }"), "{html}"); |
| 3090 |
|
| 3091 |
|
| 3092 |
assert!(inside.contains("\\3c/style"), "{html}"); |
| 3093 |
} |
| 3094 |
|
| 3095 |
#[test] |
| 3096 |
fn the_close_sequence_is_caught_whatever_its_case() { |
| 3097 |
|
| 3098 |
let screen = Screen::list_detail("A", false) |
| 3099 |
.documented(Document::default().styled("p::after { content: '</STYLE>' }")); |
| 3100 |
let html = Webview::new().screen(&screen); |
| 3101 |
|
| 3102 |
assert!(!html.contains("</STYLE>"), "{html}"); |
| 3103 |
assert!(html.contains("\\3c/STYLE"), "{html}"); |
| 3104 |
} |
| 3105 |
|
| 3106 |
#[test] |
| 3107 |
fn a_root_attribute_named_twice_is_written_once() { |
| 3108 |
|
| 3109 |
|
| 3110 |
let screen = Screen::list_detail("A", false).documented( |
| 3111 |
Document::default() |
| 3112 |
.rooted("data-theme", "slate") |
| 3113 |
.rooted("data-theme", "paper"), |
| 3114 |
); |
| 3115 |
let html = Webview::new().screen(&screen); |
| 3116 |
|
| 3117 |
assert_eq!(html.matches("data-theme=").count(), 1, "{html}"); |
| 3118 |
assert!(html.contains("data-theme=\"slate\""), "{html}"); |
| 3119 |
} |
| 3120 |
|
| 3121 |
#[test] |
| 3122 |
fn a_host_assembling_its_own_document_gets_the_same_head_as_a_screen() { |
| 3123 |
|
| 3124 |
|
| 3125 |
|
| 3126 |
let shell = Shell::default() |
| 3127 |
.layered(["base", "components"]) |
| 3128 |
.styled("/style.css") |
| 3129 |
.with_head_first("<link rel=\"preload\" href=\"/f.woff2\" as=\"font\">"); |
| 3130 |
let parts = shell.parts(); |
| 3131 |
let screen = Webview::new() |
| 3132 |
.with_shell(shell) |
| 3133 |
.screen(&Screen::list_detail("Console", false)); |
| 3134 |
|
| 3135 |
|
| 3136 |
|
| 3137 |
|
| 3138 |
|
| 3139 |
|
| 3140 |
|
| 3141 |
let head = screen |
| 3142 |
.split("</head>") |
| 3143 |
.next() |
| 3144 |
.expect("the screen has a head") |
| 3145 |
.replace("<title>Console</title>", ""); |
| 3146 |
let head = strip_discovery(&head); |
| 3147 |
assert_eq!(parts.head, head); |
| 3148 |
assert!(screen.contains(&format!("<body{}>", parts.body_attrs))); |
| 3149 |
} |
| 3150 |
|
| 3151 |
#[test] |
| 3152 |
fn the_body_attributes_compose_with_the_hosts_own() { |
| 3153 |
|
| 3154 |
|
| 3155 |
let parts = Shell::default().sending("X-CSRF-Token", "abc").parts(); |
| 3156 |
assert!(parts.body_attrs.starts_with(' '), "{}", parts.body_attrs); |
| 3157 |
assert!(!parts.head.contains("</head>")); |
| 3158 |
assert!(!parts.head.contains("<title>")); |
| 3159 |
|
| 3160 |
|
| 3161 |
assert_eq!(Shell::default().parts().body_attrs, ""); |
| 3162 |
} |
| 3163 |
|
| 3164 |
#[test] |
| 3165 |
fn a_declared_header_travels_with_every_request_the_document_makes() { |
| 3166 |
|
| 3167 |
|
| 3168 |
let parts = Shell::default().sending("X-CSRF-Token", "abc123").parts(); |
| 3169 |
|
| 3170 |
|
| 3171 |
assert_eq!( |
| 3172 |
parts.body_attrs, |
| 3173 |
" hx-headers:inherited=\"{"X-CSRF-Token":"abc123"}\"" |
| 3174 |
); |
| 3175 |
|
| 3176 |
|
| 3177 |
|
| 3178 |
assert_eq!(Shell::default().parts().body_attrs, ""); |
| 3179 |
} |
| 3180 |
|
| 3181 |
#[test] |
| 3182 |
fn a_declared_header_cannot_break_out_of_its_attribute() { |
| 3183 |
|
| 3184 |
|
| 3185 |
|
| 3186 |
|
| 3187 |
let parts = Shell::default() |
| 3188 |
.sending("X-Token", "a\"><script>alert(1)</script>") |
| 3189 |
.parts(); |
| 3190 |
assert!( |
| 3191 |
!parts.body_attrs.contains("<script>"), |
| 3192 |
"{}", |
| 3193 |
parts.body_attrs |
| 3194 |
); |
| 3195 |
assert!( |
| 3196 |
parts.body_attrs.contains("<script>"), |
| 3197 |
"{}", |
| 3198 |
parts.body_attrs |
| 3199 |
); |
| 3200 |
} |
| 3201 |
|
| 3202 |
#[test] |
| 3203 |
fn headers_accumulate_in_the_order_they_were_declared() { |
| 3204 |
let parts = Shell::default().sending("A", "1").sending("B", "2").parts(); |
| 3205 |
assert!( |
| 3206 |
parts.body_attrs.contains( |
| 3207 |
"hx-headers:inherited=\"{"A":"1","B":"2"}\"" |
| 3208 |
), |
| 3209 |
"{}", |
| 3210 |
parts.body_attrs |
| 3211 |
); |
| 3212 |
} |
| 3213 |
|
| 3214 |
#[test] |
| 3215 |
fn a_document_that_calls_no_route_ships_no_transport() { |
| 3216 |
|
| 3217 |
|
| 3218 |
|
| 3219 |
let full = Shell::default().parts(); |
| 3220 |
assert!(full.head.contains("htmx.min.js")); |
| 3221 |
|
| 3222 |
let bare = Shell::default().without_htmx().parts(); |
| 3223 |
assert!(!bare.head.contains("htmx.min.js"), "{}", bare.head); |
| 3224 |
|
| 3225 |
|
| 3226 |
assert!(!bare.head.contains("quasi-selection.js"), "{}", bare.head); |
| 3227 |
assert_eq!(bare.body_attrs, ""); |
| 3228 |
|
| 3229 |
|
| 3230 |
assert!(bare.head.contains("@layer makeover")); |
| 3231 |
} |
| 3232 |
|
| 3233 |
#[test] |
| 3234 |
fn a_nested_region_renders_inside_its_parent() { |
| 3235 |
let screen = |
| 3236 |
Screen::list_detail("Tasks", false).with(Slot::new("outer", RegionKind::Pane).with( |
| 3237 |
Node::Region(Slot::new("inner", RegionKind::Pane).with(Node::text("in"))), |
| 3238 |
)); |
| 3239 |
let html = render(&screen); |
| 3240 |
|
| 3241 |
let outer = html.find("id=\"outer\"").expect("outer renders"); |
| 3242 |
let inner = html.find("id=\"inner\"").expect("inner renders"); |
| 3243 |
assert!(outer < inner); |
| 3244 |
|
| 3245 |
|
| 3246 |
|
| 3247 |
assert!( |
| 3248 |
html.contains( |
| 3249 |
"in</p><div class=\"anchored\" id=\"inner-anchored\" data-menu=\"anchored\" \ |
| 3250 |
hidden></div></div><div class=\"anchored\" id=\"outer-anchored\" \ |
| 3251 |
data-menu=\"anchored\" hidden></div></div>" |
| 3252 |
), |
| 3253 |
"{html}" |
| 3254 |
); |
| 3255 |
} |
| 3256 |
|
| 3257 |
#[test] |
| 3258 |
fn text_from_a_description_can_never_become_markup() { |
| 3259 |
|
| 3260 |
|
| 3261 |
|
| 3262 |
|
| 3263 |
let hostile = "<script>alert(1)</script>"; |
| 3264 |
let screen = Screen::list_detail(hostile, false) |
| 3265 |
.saying(Node::banner(layout::Tone::Danger, hostile)) |
| 3266 |
.with( |
| 3267 |
Slot::new("s", RegionKind::Pane) |
| 3268 |
.with(Node::page(hostile)) |
| 3269 |
.with(Node::text(hostile)) |
| 3270 |
.with(Node::act(hostile, Action::get("/x"))) |
| 3271 |
.with(Node::list([Row::new(hostile) |
| 3272 |
.secondary(hostile) |
| 3273 |
.meta(hostile) |
| 3274 |
.act(Act::new(hostile, Action::post("/y")))])) |
| 3275 |
.with(Node::Token(Tag::removable(hostile, Action::get("/z")))), |
| 3276 |
); |
| 3277 |
|
| 3278 |
let html = render(&screen); |
| 3279 |
assert!(!html.contains("<script>")); |
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
assert_eq!(html.matches("<script>").count(), 12); |
| 3287 |
} |
| 3288 |
|
| 3289 |
#[test] |
| 3290 |
fn a_rows_plain_prose_is_escaped_exactly_as_it_always_was() { |
| 3291 |
|
| 3292 |
|
| 3293 |
let row = Row::new("Atlas").secondary("**not bold** <b>not bold either</b>"); |
| 3294 |
let node = Node::list(vec![row]); |
| 3295 |
|
| 3296 |
let html = Webview::new().fragment(&node); |
| 3297 |
|
| 3298 |
assert!(html.contains("**not bold**"), "got: {html}"); |
| 3299 |
assert!(!html.contains("<strong>"), "got: {html}"); |
| 3300 |
assert!(!html.contains("<b>"), "got: {html}"); |
| 3301 |
assert!(html.contains("<b>"), "got: {html}"); |
| 3302 |
} |
| 3303 |
|
| 3304 |
#[test] |
| 3305 |
fn a_rows_rich_prose_is_rendered_inline() { |
| 3306 |
|
| 3307 |
|
| 3308 |
let row = Row::new("Atlas").secondary(Prose::rich("**Ships Q3.** `soon`")); |
| 3309 |
let node = Node::list(vec![row]); |
| 3310 |
|
| 3311 |
let html = Webview::new().fragment(&node); |
| 3312 |
|
| 3313 |
assert!(html.contains("<strong>Ships Q3.</strong>"), "got: {html}"); |
| 3314 |
assert!(html.contains("<code>soon</code>"), "got: {html}"); |
| 3315 |
} |
| 3316 |
|
| 3317 |
#[test] |
| 3318 |
fn a_rows_rich_prose_keeps_no_blocks() { |
| 3319 |
|
| 3320 |
|
| 3321 |
let row = Row::new("Borealis").secondary(Prose::rich("# Goal\n\n> ship it\n\n- one\n- two")); |
| 3322 |
let node = Node::list(vec![row]); |
| 3323 |
|
| 3324 |
let html = Webview::new().fragment(&node); |
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
let secondary = html |
| 3329 |
.split_once(r#"<span class="row-secondary">"#) |
| 3330 |
.expect("the row draws its secondary") |
| 3331 |
.1 |
| 3332 |
.split_once("</span>") |
| 3333 |
.expect("the part closes") |
| 3334 |
.0; |
| 3335 |
for block in ["<h1", "<blockquote", "<ul", "<li", "<p"] { |
| 3336 |
assert!(!secondary.contains(block), "no {block} in a row: {html}"); |
| 3337 |
} |
| 3338 |
for word in ["Goal", "ship it", "one", "two"] { |
| 3339 |
assert!(html.contains(word), "{word} survives: {html}"); |
| 3340 |
} |
| 3341 |
} |
| 3342 |
|
| 3343 |
#[test] |
| 3344 |
fn a_rows_rich_prose_carries_no_second_click_target() { |
| 3345 |
|
| 3346 |
|
| 3347 |
let row = Row::new("Atlas") |
| 3348 |
.secondary(Prose::rich( |
| 3349 |
"see [the brief](https://example.com/a/long/path)", |
| 3350 |
)) |
| 3351 |
.activate(Action::get("/projects/1")); |
| 3352 |
let node = Node::list(vec![row]); |
| 3353 |
|
| 3354 |
let html = Webview::new().fragment(&node); |
| 3355 |
|
| 3356 |
assert!(html.contains("see the brief"), "the text survives: {html}"); |
| 3357 |
assert!(!html.contains("example.com"), "no href: {html}"); |
| 3358 |
|
| 3359 |
|
| 3360 |
assert_eq!( |
| 3361 |
html.matches("<a ").count(), |
| 3362 |
1, |
| 3363 |
"the row is the only target: {html}" |
| 3364 |
); |
| 3365 |
} |
| 3366 |
|
| 3367 |
#[test] |
| 3368 |
fn a_rows_rich_prose_cannot_smuggle_markup() { |
| 3369 |
|
| 3370 |
|
| 3371 |
let row = Row::new("Atlas").secondary(Prose::rich( |
| 3372 |
"hi <script>alert(1)</script> <img src=x onerror=alert(1)> [x](javascript:alert(1))", |
| 3373 |
)); |
| 3374 |
let node = Node::list(vec![row]); |
| 3375 |
|
| 3376 |
let html = Webview::new().fragment(&node); |
| 3377 |
|
| 3378 |
assert!(!html.contains("<script"), "got: {html}"); |
| 3379 |
assert!(!html.contains("onerror"), "got: {html}"); |
| 3380 |
assert!(!html.contains("javascript:"), "got: {html}"); |
| 3381 |
} |
| 3382 |
|
| 3383 |
#[test] |
| 3384 |
fn no_control_ever_says_whether_its_answer_is_a_place() { |
| 3385 |
|
| 3386 |
|
| 3387 |
|
| 3388 |
|
| 3389 |
|
| 3390 |
let html = fragment(&Node::Table { |
| 3391 |
marks: ::quasi_router::stage::Marks::none(), |
| 3392 |
columns: vec![Column::new("Title").width(layout::Width::Fill)], |
| 3393 |
rows: vec![ |
| 3394 |
Row::cells([Cell::new("Release notes").activate(Action::get("/blog/7"))]) |
| 3395 |
.activate(Action::get("/blog/7/edit")), |
| 3396 |
], |
| 3397 |
more: None, |
| 3398 |
}); |
| 3399 |
assert!(!html.contains("push-url"), "{html}"); |
| 3400 |
assert!(!html.contains("replace-url"), "{html}"); |
| 3401 |
} |
| 3402 |
|
| 3403 |
#[test] |
| 3404 |
fn a_screen_that_says_nothing_is_still_a_findable_page() { |
| 3405 |
|
| 3406 |
|
| 3407 |
let html = render(&Screen::sidebar_content("Projects")); |
| 3408 |
|
| 3409 |
assert!( |
| 3410 |
html.contains("<meta property=\"og:title\" content=\"Projects\">"), |
| 3411 |
"{html}" |
| 3412 |
); |
| 3413 |
assert!( |
| 3414 |
html.contains("<meta property=\"og:type\" content=\"website\">"), |
| 3415 |
"{html}" |
| 3416 |
); |
| 3417 |
assert!(!html.contains("robots"), "{html}"); |
| 3418 |
|
| 3419 |
|
| 3420 |
|
| 3421 |
|
| 3422 |
assert!(!html.contains("og:description"), "{html}"); |
| 3423 |
assert!(!html.contains("name=\"description\""), "{html}"); |
| 3424 |
assert!(!html.contains("og:image"), "{html}"); |
| 3425 |
assert!(!html.contains("canonical"), "{html}"); |
| 3426 |
} |
| 3427 |
|
| 3428 |
|
| 3429 |
|
| 3430 |
|
| 3431 |
|
| 3432 |
#[test] |
| 3433 |
fn a_summary_reaches_the_plain_description_meta_and_not_only_the_social_pair() { |
| 3434 |
let html = render(&Screen::sidebar_content("Pricing").summarised("What you keep.")); |
| 3435 |
|
| 3436 |
assert!( |
| 3437 |
html.contains("<meta name=\"description\" content=\"What you keep.\">"), |
| 3438 |
"{html}" |
| 3439 |
); |
| 3440 |
|
| 3441 |
|
| 3442 |
assert!( |
| 3443 |
html.contains("<meta property=\"og:description\" content=\"What you keep.\">"), |
| 3444 |
"{html}" |
| 3445 |
); |
| 3446 |
assert!( |
| 3447 |
html.contains("<meta name=\"twitter:description\" content=\"What you keep.\">"), |
| 3448 |
"{html}" |
| 3449 |
); |
| 3450 |
} |
| 3451 |
|
| 3452 |
|
| 3453 |
|
| 3454 |
|
| 3455 |
#[test] |
| 3456 |
fn a_screen_that_refuses_indexing_keeps_its_preview_and_drops_its_description() { |
| 3457 |
let html = render( |
| 3458 |
&Screen::sidebar_content("Downloads") |
| 3459 |
.summarised("Your purchases.") |
| 3460 |
.indexed(false), |
| 3461 |
); |
| 3462 |
|
| 3463 |
assert!(!html.contains("name=\"description\""), "{html}"); |
| 3464 |
assert!( |
| 3465 |
html.contains("<meta property=\"og:description\" content=\"Your purchases.\">"), |
| 3466 |
"{html}" |
| 3467 |
); |
| 3468 |
assert!( |
| 3469 |
html.contains("<meta name=\"robots\" content=\"noindex\">"), |
| 3470 |
"{html}" |
| 3471 |
); |
| 3472 |
} |
| 3473 |
|
| 3474 |
#[test] |
| 3475 |
fn a_purchased_content_screen_can_say_it_is_not_for_crawlers() { |
| 3476 |
|
| 3477 |
|
| 3478 |
|
| 3479 |
let html = render(&Screen::sidebar_content("Downloads").indexed(false)); |
| 3480 |
assert!( |
| 3481 |
html.contains("<meta name=\"robots\" content=\"noindex\">"), |
| 3482 |
"{html}" |
| 3483 |
); |
| 3484 |
} |
| 3485 |
|
| 3486 |
#[test] |
| 3487 |
fn a_screen_that_offers_a_feed_says_so_where_a_reader_looks() { |
| 3488 |
let html = render( |
| 3489 |
&Screen::sidebar_content("Blue Hour").syndicating(quasi_router::Feed::new( |
| 3490 |
quasi_router::FeedKind::Rss, |
| 3491 |
"Blue Hour updates", |
| 3492 |
"/p/blue-hour/feed.xml", |
| 3493 |
)), |
| 3494 |
); |
| 3495 |
|
| 3496 |
|
| 3497 |
|
| 3498 |
assert!( |
| 3499 |
html.contains( |
| 3500 |
"<link rel=\"alternate\" type=\"application/rss+xml\" \ |
| 3501 |
title=\"Blue Hour updates\" href=\"/p/blue-hour/feed.xml\">" |
| 3502 |
), |
| 3503 |
"{html}" |
| 3504 |
); |
| 3505 |
|
| 3506 |
assert!( |
| 3507 |
!render(&Screen::sidebar_content("Blue Hour")).contains("rel=\"alternate\""), |
| 3508 |
"a screen with no feed claimed one" |
| 3509 |
); |
| 3510 |
} |
| 3511 |
|
| 3512 |
#[test] |
| 3513 |
fn a_feed_title_is_escaped_like_every_other_string_in_the_head() { |
| 3514 |
|
| 3515 |
|
| 3516 |
let html = render( |
| 3517 |
&Screen::sidebar_content("Blog").syndicating(quasi_router::Feed::new( |
| 3518 |
quasi_router::FeedKind::Atom, |
| 3519 |
"A \" quote", |
| 3520 |
"/feed", |
| 3521 |
)), |
| 3522 |
); |
| 3523 |
assert!(html.contains("type=\"application/atom+xml\""), "{html}"); |
| 3524 |
assert!(!html.contains("title=\"A \" quote\""), "unescaped: {html}"); |
| 3525 |
} |
| 3526 |
|
| 3527 |
#[test] |
| 3528 |
fn the_caret_starts_where_a_whole_document_said_it_does() { |
| 3529 |
let screen = Screen::single("Log in").opening_at("email").with( |
| 3530 |
Slot::new("form", RegionKind::Pane).with(Node::Form { |
| 3531 |
marks: ::quasi_router::stage::Marks::none(), |
| 3532 |
action: Action::post("/login"), |
| 3533 |
submit: "Log in".into(), |
| 3534 |
fields: vec![ |
| 3535 |
Field::new(layout::FieldKind::Text, "email", "Email"), |
| 3536 |
Field::new(layout::FieldKind::Secret, "password", "Password"), |
| 3537 |
], |
| 3538 |
}), |
| 3539 |
); |
| 3540 |
let html = render(&screen); |
| 3541 |
assert!(html.contains("autofocus"), "{html}"); |
| 3542 |
|
| 3543 |
|
| 3544 |
assert_eq!(html.matches("autofocus").count(), 1, "{html}"); |
| 3545 |
let at = html.find("autofocus").expect("emitted"); |
| 3546 |
assert!( |
| 3547 |
html[..at].rfind("\"email\"").is_some(), |
| 3548 |
"the caret landed somewhere other than the named box: {html}" |
| 3549 |
); |
| 3550 |
} |
| 3551 |
|
| 3552 |
#[test] |
| 3553 |
fn a_name_no_question_on_the_screen_carries_moves_no_caret() { |
| 3554 |
|
| 3555 |
|
| 3556 |
|
| 3557 |
let html = render( |
| 3558 |
&Screen::single("Log in") |
| 3559 |
.opening_at("nothing-is-called-this") |
| 3560 |
.with( |
| 3561 |
Slot::new("form", RegionKind::Pane).with(Node::Field(Box::new(Field::new( |
| 3562 |
layout::FieldKind::Text, |
| 3563 |
"email", |
| 3564 |
"Email", |
| 3565 |
)))), |
| 3566 |
), |
| 3567 |
); |
| 3568 |
assert!(!html.contains("autofocus"), "{html}"); |
| 3569 |
} |
| 3570 |
|
| 3571 |
#[test] |
| 3572 |
fn a_fragment_never_moves_the_caret() { |
| 3573 |
|
| 3574 |
|
| 3575 |
|
| 3576 |
let field = Node::Field(Box::new(Field::new( |
| 3577 |
layout::FieldKind::Text, |
| 3578 |
"email", |
| 3579 |
"Email", |
| 3580 |
))); |
| 3581 |
assert!( |
| 3582 |
!fragment(&field).contains("autofocus"), |
| 3583 |
"a fragment claimed it" |
| 3584 |
); |
| 3585 |
|
| 3586 |
let over = |
| 3587 |
Screen::single("Search") |
| 3588 |
.opening_at("q") |
| 3589 |
.with( |
| 3590 |
Slot::new("body", RegionKind::Pane).with(Node::Field(Box::new(Field::new( |
| 3591 |
layout::FieldKind::Text, |
| 3592 |
"q", |
| 3593 |
"Search", |
| 3594 |
)))), |
| 3595 |
); |
| 3596 |
assert!( |
| 3597 |
!Webview::new().overlay(&over).contains("autofocus"), |
| 3598 |
"an overlay claimed it" |
| 3599 |
); |
| 3600 |
} |
| 3601 |
|
| 3602 |
#[test] |
| 3603 |
fn a_band_is_one_element_and_the_nav_is_inside_it() { |
| 3604 |
|
| 3605 |
|
| 3606 |
|
| 3607 |
let document = Webview::new() |
| 3608 |
.with_shell( |
| 3609 |
Shell::under("/static").with_chrome( |
| 3610 |
quasi_router::Chrome::new() |
| 3611 |
.offering(quasi_router::Place::new( |
| 3612 |
"discover", |
| 3613 |
"Discover", |
| 3614 |
Action::get("/discover"), |
| 3615 |
)) |
| 3616 |
.banded(banded()), |
| 3617 |
), |
| 3618 |
) |
| 3619 |
.screen(&Screen::single("Home")); |
| 3620 |
|
| 3621 |
let band = document.find("chrome-band").expect("emitted"); |
| 3622 |
let nav = document.find("chrome-nav").expect("emitted"); |
| 3623 |
let close = document.find("</header>").expect("closed"); |
| 3624 |
let main = document.find("<main").expect("emitted"); |
| 3625 |
assert!( |
| 3626 |
band < nav && nav < close, |
| 3627 |
"the nav left the band: {document}" |
| 3628 |
); |
| 3629 |
assert!( |
| 3630 |
close < main, |
| 3631 |
"the band landed after the content: {document}" |
| 3632 |
); |
| 3633 |
|
| 3634 |
|
| 3635 |
|
| 3636 |
let toggle = document.find("chrome-disclose-state").expect("emitted"); |
| 3637 |
let search = document.find("chrome-search").expect("emitted"); |
| 3638 |
assert!(toggle < search && search < nav, "{document}"); |
| 3639 |
|
| 3640 |
|
| 3641 |
assert!( |
| 3642 |
document.contains(">Makenot<span class=\"chrome-brand-mark\">.</span>work</a>"), |
| 3643 |
"{document}" |
| 3644 |
); |
| 3645 |
} |
| 3646 |
|
| 3647 |
#[test] |
| 3648 |
fn a_navigating_read_that_is_not_a_click_keeps_its_transport() { |
| 3649 |
|
| 3650 |
|
| 3651 |
|
| 3652 |
|
| 3653 |
|
| 3654 |
let document = Webview::new() |
| 3655 |
.with_shell( |
| 3656 |
Shell::under("/static").with_chrome( |
| 3657 |
quasi_router::Chrome::new().banded( |
| 3658 |
quasi_router::Band::new().searching( |
| 3659 |
Field::new(layout::FieldKind::Text, "q", "Search") |
| 3660 |
.writes(Action::get("/discover").navigating()), |
| 3661 |
), |
| 3662 |
), |
| 3663 |
), |
| 3664 |
) |
| 3665 |
.screen(&Screen::single("Home")); |
| 3666 |
|
| 3667 |
assert!(document.contains("hx-get=\"/discover\""), "{document}"); |
| 3668 |
assert!( |
| 3669 |
!document.contains("<div class=\"field-writes\" href="), |
| 3670 |
"an href on a wrapper: {document}" |
| 3671 |
); |
| 3672 |
} |
| 3673 |
|
| 3674 |
#[test] |
| 3675 |
fn an_app_with_no_band_gets_the_nav_it_had_before_bands_existed() { |
| 3676 |
let document = Webview::new() |
| 3677 |
.with_shell( |
| 3678 |
Shell::under("/static").with_chrome(quasi_router::Chrome::new().offering( |
| 3679 |
quasi_router::Place::new("discover", "Discover", Action::get("/discover")), |
| 3680 |
)), |
| 3681 |
) |
| 3682 |
.screen(&Screen::single("Home")); |
| 3683 |
assert!(document.contains("chrome-nav"), "{document}"); |
| 3684 |
assert!(!document.contains("chrome-band"), "{document}"); |
| 3685 |
assert!(!document.contains("<header"), "{document}"); |
| 3686 |
} |
| 3687 |
|
| 3688 |
#[test] |
| 3689 |
fn a_screen_names_what_it_is_about_and_how_it_previews() { |
| 3690 |
let html = render( |
| 3691 |
&Screen::sidebar_content("Blue Hour") |
| 3692 |
.summarised("Nine tracks recorded in one night.") |
| 3693 |
.illustrated("https://makenot.work/media/cover.png") |
| 3694 |
.about(quasi_router::SocialKind::Song) |
| 3695 |
.canonical_at("https://makenot.work/i/7"), |
| 3696 |
); |
| 3697 |
|
| 3698 |
assert!( |
| 3699 |
html.contains("content=\"Nine tracks recorded in one night.\""), |
| 3700 |
"{html}" |
| 3701 |
); |
| 3702 |
assert!( |
| 3703 |
html.contains("content=\"https://makenot.work/media/cover.png\""), |
| 3704 |
"{html}" |
| 3705 |
); |
| 3706 |
assert!( |
| 3707 |
html.contains("<meta property=\"og:type\" content=\"music.song\">"), |
| 3708 |
"{html}" |
| 3709 |
); |
| 3710 |
assert!( |
| 3711 |
html.contains("<link rel=\"canonical\" href=\"https://makenot.work/i/7\">"), |
| 3712 |
"{html}" |
| 3713 |
); |
| 3714 |
|
| 3715 |
|
| 3716 |
|
| 3717 |
|
| 3718 |
assert!( |
| 3719 |
html.contains("<meta name=\"twitter:card\" content=\"summary_large_image\">"), |
| 3720 |
"{html}" |
| 3721 |
); |
| 3722 |
assert!(!html.contains("property=\"twitter:"), "{html}"); |
| 3723 |
} |
| 3724 |
|
| 3725 |
#[test] |
| 3726 |
fn a_summary_is_escaped_because_a_person_wrote_it() { |
| 3727 |
|
| 3728 |
|
| 3729 |
let html = |
| 3730 |
render(&Screen::sidebar_content("Item").summarised("She said \"hi\" & <b>waved</b>")); |
| 3731 |
|
| 3732 |
assert!(!html.contains("<b>waved"), "{html}"); |
| 3733 |
assert!(html.contains(""hi""), "{html}"); |
| 3734 |
assert!(html.contains("&"), "{html}"); |
| 3735 |
} |
| 3736 |
|
| 3737 |
|
| 3738 |
|
| 3739 |
|
| 3740 |
|
| 3741 |
|
| 3742 |
|
| 3743 |
|
| 3744 |
fn every_kind_of_screen() -> Vec<String> { |
| 3745 |
let mut htmls = Vec::new(); |
| 3746 |
|
| 3747 |
for kind in [ |
| 3748 |
RegionKind::Band, |
| 3749 |
RegionKind::Sidebar, |
| 3750 |
RegionKind::Pane, |
| 3751 |
RegionKind::TabGroup, |
| 3752 |
RegionKind::Modal, |
| 3753 |
] { |
| 3754 |
htmls.push(render( |
| 3755 |
&Screen::list_detail("Everything", false) |
| 3756 |
.saying(Node::banner(layout::Tone::Warning, "heads up")) |
| 3757 |
.saying(Node::toast(layout::Tone::Success, "saved")) |
| 3758 |
.with(Slot::new("region", kind).with(Node::text("in a region"))), |
| 3759 |
)); |
| 3760 |
} |
| 3761 |
htmls.push(render(&Screen::sidebar_content("Everything").with( |
| 3762 |
Slot::handover("bespoke", "map").with(Node::text("beside a fill")), |
| 3763 |
))); |
| 3764 |
htmls.push(render(&Screen::list_detail("Tabbed", true))); |
| 3765 |
|
| 3766 |
|
| 3767 |
for measure in [ |
| 3768 |
layout::Measure::Wide, |
| 3769 |
layout::Measure::Contained, |
| 3770 |
layout::Measure::Reading, |
| 3771 |
] { |
| 3772 |
htmls.push(render( |
| 3773 |
&Screen::list_detail("Measured", false).measured(measure), |
| 3774 |
)); |
| 3775 |
} |
| 3776 |
|
| 3777 |
let acts = || Act::new("Remove", Action::post("/keys/7/delete")).tone(layout::Tone::Danger); |
| 3778 |
for node in [ |
| 3779 |
Node::page("A page"), |
| 3780 |
Node::section("A section"), |
| 3781 |
Node::text("plain"), |
| 3782 |
Node::rich("**bold** and a [link](https://example.com)"), |
| 3783 |
Node::act("Save", Action::post("/save")), |
| 3784 |
Node::Token(Tag::badge("Paid").tone(layout::Tone::Success)), |
| 3785 |
Node::Token(Tag::chip("Open", Action::get("/tasks?open=1")).latched()), |
| 3786 |
Node::banner(layout::Tone::Danger, "it broke"), |
| 3787 |
Node::toast(layout::Tone::Info, "it saved"), |
| 3788 |
Node::empty("nothing here").offering(acts()), |
| 3789 |
Node::failed("it broke").offering(acts()), |
| 3790 |
Node::field(Field::new(layout::FieldKind::Text, "name", "Name").required()), |
| 3791 |
Node::field(Field::new(layout::FieldKind::Secret, "pw", "Password").error("too short")), |
| 3792 |
Node::field( |
| 3793 |
Field::new(layout::FieldKind::Checkbox, "live", "Live").writes(Action::post("/live")), |
| 3794 |
), |
| 3795 |
Node::field( |
| 3796 |
Field::new(layout::FieldKind::Text, "slug", "Slug") |
| 3797 |
.consults(Action::get("/api/validate/slug").replacing("slug-status")), |
| 3798 |
), |
| 3799 |
Node::field(Field::select( |
| 3800 |
"size", |
| 3801 |
"Size", |
| 3802 |
vec![Choice::plain("small"), Choice::new("l", "large")], |
| 3803 |
)), |
| 3804 |
Node::field(Field::radio( |
| 3805 |
"mode", |
| 3806 |
"Mode", |
| 3807 |
vec![Choice::plain("one"), Choice::plain("two")], |
| 3808 |
)), |
| 3809 |
Node::Form { |
| 3810 |
marks: ::quasi_router::stage::Marks::none(), |
| 3811 |
action: Action::post("/new"), |
| 3812 |
submit: "Create".into(), |
| 3813 |
fields: vec![Field::new(layout::FieldKind::Text, "title", "Title")], |
| 3814 |
}, |
| 3815 |
Node::list([Row::new("fw13") |
| 3816 |
.secondary("a second line") |
| 3817 |
.meta("2 days ago") |
| 3818 |
.token(Tag::badge("Active")) |
| 3819 |
.meter(Meter::new(3, 10)) |
| 3820 |
.activate(Action::get("/keys/7")) |
| 3821 |
.act(acts())]) |
| 3822 |
.and_more(Rest::more(10, Action::get("/keys?page=2")).of(50)), |
| 3823 |
Node::list([Row::new("astra").toggling(true, Action::post("/keys/8/pin"))]), |
| 3824 |
|
| 3825 |
Node::list([Row::new("fw12")]).and_more( |
| 3826 |
Rest::page(100, 50) |
| 3827 |
.of(400) |
| 3828 |
.back(Action::get("/keys?page=2")) |
| 3829 |
.forward(Action::get("/keys?page=4")) |
| 3830 |
.jumping(Jump::new(2, Action::get("/keys?page=2"))) |
| 3831 |
.jumping(Jump::new(3, Action::get("/keys?page=3")).here()) |
| 3832 |
.jumping(Jump::new(4, Action::get("/keys?page=4"))), |
| 3833 |
), |
| 3834 |
Node::Table { |
| 3835 |
marks: ::quasi_router::stage::Marks::none(), |
| 3836 |
columns: vec![ |
| 3837 |
Column::new("Name") |
| 3838 |
.width(layout::Width::Fill) |
| 3839 |
.priority(layout::Priority::Essential), |
| 3840 |
Column::new("Status") |
| 3841 |
.width(layout::Width::Content) |
| 3842 |
.priority(layout::Priority::Optional), |
| 3843 |
], |
| 3844 |
rows: vec![ |
| 3845 |
Row::cells([ |
| 3846 |
Cell::new("deploy"), |
| 3847 |
Cell::tag(Tag::badge("Paid").tone(layout::Tone::Success)), |
| 3848 |
]) |
| 3849 |
.activate(Action::get("/runs/1")), |
| 3850 |
Row::cells([Cell::new("build"), Cell::acts([acts()])]), |
| 3851 |
], |
| 3852 |
more: None, |
| 3853 |
}, |
| 3854 |
Node::Meter(Meter::new(7, 10).label("7 of 10")), |
| 3855 |
Node::stats([Figure::new("$12.00", "Revenue").change("+3%")]), |
| 3856 |
] { |
| 3857 |
htmls.push(fragment(&node)); |
| 3858 |
} |
| 3859 |
|
| 3860 |
|
| 3861 |
|
| 3862 |
|
| 3863 |
|
| 3864 |
|
| 3865 |
htmls.push(fragment(&Node::Region( |
| 3866 |
Slot::new("tabs", RegionKind::TabGroup) |
| 3867 |
.showing_one(0) |
| 3868 |
.frame( |
| 3869 |
"Open", |
| 3870 |
Node::Region(Slot::new("open", RegionKind::Group).with(Node::text("what is open"))), |
| 3871 |
) |
| 3872 |
.frame( |
| 3873 |
"Done", |
| 3874 |
Node::Region( |
| 3875 |
Slot::new("done", RegionKind::Group).fed_by(Action::get("/tasks/done")), |
| 3876 |
), |
| 3877 |
), |
| 3878 |
))); |
| 3879 |
|
| 3880 |
htmls |
| 3881 |
} |
| 3882 |
|
| 3883 |
|
| 3884 |
|
| 3885 |
|
| 3886 |
|
| 3887 |
|
| 3888 |
fn emitted_classes(htmls: &[String]) -> std::collections::BTreeSet<String> { |
| 3889 |
let mut names = std::collections::BTreeSet::new(); |
| 3890 |
for html in htmls { |
| 3891 |
let mut rest = html.as_str(); |
| 3892 |
while let Some(at) = rest.find("class=\"") { |
| 3893 |
rest = &rest[at + 7..]; |
| 3894 |
let end = rest.find('"').expect("the attribute closes"); |
| 3895 |
for name in rest[..end].split_whitespace() { |
| 3896 |
if !name.starts_with("col-") { |
| 3897 |
names.insert(name.to_string()); |
| 3898 |
} |
| 3899 |
} |
| 3900 |
rest = &rest[end..]; |
| 3901 |
} |
| 3902 |
} |
| 3903 |
names |
| 3904 |
} |
| 3905 |
|
| 3906 |
|
| 3907 |
|
| 3908 |
|
| 3909 |
|
| 3910 |
|
| 3911 |
|
| 3912 |
|
| 3913 |
|
| 3914 |
|
| 3915 |
|
| 3916 |
const BY_DESIGN: &[&str] = &[ |
| 3917 |
|
| 3918 |
"list-detail", |
| 3919 |
"list-detail-tabbed", |
| 3920 |
"sidebar-content", |
| 3921 |
|
| 3922 |
|
| 3923 |
|
| 3924 |
|
| 3925 |
|
| 3926 |
"measure-wide", |
| 3927 |
"measure-contained", |
| 3928 |
"measure-reading", |
| 3929 |
|
| 3930 |
"band", |
| 3931 |
"bespoke", |
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 3936 |
"group", |
| 3937 |
"modal", |
| 3938 |
"pane", |
| 3939 |
"region", |
| 3940 |
"sidebar", |
| 3941 |
"tabgroup", |
| 3942 |
|
| 3943 |
|
| 3944 |
|
| 3945 |
|
| 3946 |
|
| 3947 |
|
| 3948 |
|
| 3949 |
"anchored", |
| 3950 |
"figures", |
| 3951 |
"form", |
| 3952 |
"notices", |
| 3953 |
"rest", |
| 3954 |
"row", |
| 3955 |
"selector", |
| 3956 |
|
| 3957 |
|
| 3958 |
"heading", |
| 3959 |
"rich", |
| 3960 |
"text", |
| 3961 |
]; |
| 3962 |
|
| 3963 |
|
| 3964 |
|
| 3965 |
|
| 3966 |
|
| 3967 |
|
| 3968 |
|
| 3969 |
|
| 3970 |
|
| 3971 |
|
| 3972 |
|
| 3973 |
|
| 3974 |
|
| 3975 |
const MODIFIERS: &[&str] = &[ |
| 3976 |
"act-submit", |
| 3977 |
|
| 3978 |
|
| 3979 |
|
| 3980 |
|
| 3981 |
"rest-next", |
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
|
| 3986 |
|
| 3987 |
"rest-page", |
| 3988 |
"rest-page-here", |
| 3989 |
"rest-pages", |
| 3990 |
"rest-position", |
| 3991 |
"rest-previous", |
| 3992 |
"row-actions", |
| 3993 |
"row-activate", |
| 3994 |
"row-proportion", |
| 3995 |
"row-select", |
| 3996 |
"row-selected", |
| 3997 |
"row-tokens", |
| 3998 |
"cell-actions", |
| 3999 |
"cell-tokens", |
| 4000 |
"field-consults", |
| 4001 |
"field-writes", |
| 4002 |
]; |
| 4003 |
|
| 4004 |
|
| 4005 |
|
| 4006 |
|
| 4007 |
|
| 4008 |
|
| 4009 |
|
| 4010 |
|
| 4011 |
|
| 4012 |
|
| 4013 |
|
| 4014 |
|
| 4015 |
|
| 4016 |
|
| 4017 |
|
| 4018 |
|
| 4019 |
|
| 4020 |
|
| 4021 |
|
| 4022 |
|
| 4023 |
|
| 4024 |
|
| 4025 |
|
| 4026 |
|
| 4027 |
|
| 4028 |
|
| 4029 |
const GAPS: &[&str] = &[ |
| 4030 |
"form-checkbox-label", |
| 4031 |
"form-error", |
| 4032 |
"form-group", |
| 4033 |
"form-label", |
| 4034 |
"form-radio-group", |
| 4035 |
"form-radio-label", |
| 4036 |
"has-error", |
| 4037 |
"visible", |
| 4038 |
"placeholder-action", |
| 4039 |
"cell-fill", |
| 4040 |
"cell-keeps", |
| 4041 |
"banner", |
| 4042 |
"toast", |
| 4043 |
]; |
| 4044 |
|
| 4045 |
#[test] |
| 4046 |
fn every_class_this_renderer_emits_is_one_makeover_defines() { |
| 4047 |
|
| 4048 |
|
| 4049 |
|
| 4050 |
|
| 4051 |
|
| 4052 |
let css = makeover_webview::stylesheet(&Emit::default()); |
| 4053 |
let emitted = emitted_classes(&every_kind_of_screen()); |
| 4054 |
|
| 4055 |
|
| 4056 |
|
| 4057 |
let styled = |name: &str| { |
| 4058 |
css.match_indices(&format!(".{name}")).any(|(at, found)| { |
| 4059 |
css[at + found.len()..] |
| 4060 |
.chars() |
| 4061 |
.next() |
| 4062 |
.is_none_or(|c| !c.is_ascii_alphanumeric() && c != '-' && c != '_') |
| 4063 |
}) |
| 4064 |
}; |
| 4065 |
|
| 4066 |
let unstyled: Vec<&str> = emitted |
| 4067 |
.iter() |
| 4068 |
.map(String::as_str) |
| 4069 |
.filter(|name| !styled(name)) |
| 4070 |
.collect(); |
| 4071 |
|
| 4072 |
let mut accounted: Vec<&str> = [BY_DESIGN, MODIFIERS, GAPS].concat(); |
| 4073 |
accounted.sort_unstable(); |
| 4074 |
assert_eq!( |
| 4075 |
accounted.len(), |
| 4076 |
accounted |
| 4077 |
.iter() |
| 4078 |
.collect::<std::collections::BTreeSet<_>>() |
| 4079 |
.len(), |
| 4080 |
"a name is in two of the three lists, which means two answers to one \ |
| 4081 |
question" |
| 4082 |
); |
| 4083 |
|
| 4084 |
assert_eq!( |
| 4085 |
unstyled, accounted, |
| 4086 |
"a class this renderer emits has no rule and no entry above. If \ |
| 4087 |
makeover spells it differently, use makeover's spelling -- that is the \ |
| 4088 |
whole of the SSH-keys bug. Otherwise put it in BY_DESIGN, MODIFIERS or \ |
| 4089 |
GAPS with the reason, and note that GAPS is work rather than a \ |
| 4090 |
decision." |
| 4091 |
); |
| 4092 |
} |
| 4093 |
|
| 4094 |
#[test] |
| 4095 |
fn a_class_prefix_reaches_the_markup_the_way_it_reaches_the_stylesheet() { |
| 4096 |
|
| 4097 |
|
| 4098 |
|
| 4099 |
|
| 4100 |
let emit = Emit { |
| 4101 |
class_prefix: "mk-", |
| 4102 |
..Emit::default() |
| 4103 |
}; |
| 4104 |
let html = Webview::new() |
| 4105 |
.with_emit(emit) |
| 4106 |
.fragment(&Node::list([Row::new("fw13").token(Tag::badge("Active"))])); |
| 4107 |
|
| 4108 |
assert!(html.contains("class=\"mk-row\""), "{html}"); |
| 4109 |
assert!(html.contains("mk-row-primary"), "{html}"); |
| 4110 |
assert!(html.contains("mk-badge"), "{html}"); |
| 4111 |
assert!(!html.contains("\"row\""), "{html}"); |
| 4112 |
} |
| 4113 |
|
| 4114 |
#[test] |
| 4115 |
fn an_invalidated_slot_is_addressed_by_name_and_swapped_inside_its_region() { |
| 4116 |
|
| 4117 |
|
| 4118 |
|
| 4119 |
|
| 4120 |
let html = Webview::new().invalidated("task-count", &Node::text("4 left")); |
| 4121 |
|
| 4122 |
assert!( |
| 4123 |
html.starts_with("<div hx-swap-oob=\"innerHTML:#task-count\">"), |
| 4124 |
"{html}" |
| 4125 |
); |
| 4126 |
assert!(html.contains("4 left"), "{html}"); |
| 4127 |
assert!(html.ends_with("</div>"), "{html}"); |
| 4128 |
} |
| 4129 |
|
| 4130 |
#[test] |
| 4131 |
fn a_slot_id_cannot_break_out_of_the_out_of_band_selector() { |
| 4132 |
|
| 4133 |
|
| 4134 |
let html = Webview::new().invalidated("a\"><script>x</script>", &Node::text("hi")); |
| 4135 |
assert!(!html.contains("<script>"), "{html}"); |
| 4136 |
} |
| 4137 |
|
| 4138 |
#[test] |
| 4139 |
fn a_tick_carries_the_value_it_contributes_to_the_selection() { |
| 4140 |
|
| 4141 |
|
| 4142 |
let html = fragment(&Node::list([ |
| 4143 |
Row::new("First").ticking("m-1", false), |
| 4144 |
Row::new("Second").ticking("m-2", true), |
| 4145 |
])); |
| 4146 |
|
| 4147 |
assert!(html.contains("name=\"ticked\" value=\"m-1\""), "{html}"); |
| 4148 |
assert!(html.contains("name=\"ticked\" value=\"m-2\""), "{html}"); |
| 4149 |
assert_eq!(html.matches("checked").count(), 1, "{html}"); |
| 4150 |
} |
| 4151 |
|
| 4152 |
#[test] |
| 4153 |
fn a_commit_control_carries_the_hook_the_selection_script_reads() { |
| 4154 |
|
| 4155 |
|
| 4156 |
|
| 4157 |
|
| 4158 |
let act = fragment(&Node::Act( |
| 4159 |
Act::new("Complete", Action::post("/tasks/list/complete")).over("chosen"), |
| 4160 |
)); |
| 4161 |
assert!(act.contains("data-over=\"chosen\""), "{act}"); |
| 4162 |
|
| 4163 |
|
| 4164 |
let plain = fragment(&Node::Act(Act::new("Save", Action::post("/save")))); |
| 4165 |
assert!(!plain.contains("data-over"), "{plain}"); |
| 4166 |
} |
| 4167 |
|
| 4168 |
#[test] |
| 4169 |
fn a_control_that_asks_for_a_value_reveals_the_box_and_sends_it_with_the_ticks() { |
| 4170 |
|
| 4171 |
|
| 4172 |
|
| 4173 |
let html = fragment(&Node::Act( |
| 4174 |
Act::new("Set Price", Action::post("/items/price")) |
| 4175 |
.over("chosen") |
| 4176 |
.asking( |
| 4177 |
Field::new(layout::FieldKind::Number, "price", "New price ($)") |
| 4178 |
.hint("Enter 0 to make items free."), |
| 4179 |
), |
| 4180 |
)); |
| 4181 |
|
| 4182 |
assert!(html.starts_with("<details"), "{html}"); |
| 4183 |
assert!(html.contains("<summary"), "{html}"); |
| 4184 |
assert!(html.contains("Set Price"), "{html}"); |
| 4185 |
assert!(html.contains("name=\"price\""), "{html}"); |
| 4186 |
assert!(html.contains("Enter 0 to make items free."), "{html}"); |
| 4187 |
|
| 4188 |
assert!(html.contains("closest details"), "{html}"); |
| 4189 |
assert!(html.contains("row-select"), "{html}"); |
| 4190 |
|
| 4191 |
|
| 4192 |
assert_eq!(html.matches("data-over=\"chosen\"").count(), 1, "{html}"); |
| 4193 |
} |
| 4194 |
|
| 4195 |
#[test] |
| 4196 |
fn a_box_a_control_asked_for_does_not_also_write_on_its_own() { |
| 4197 |
|
| 4198 |
|
| 4199 |
let html = fragment(&Node::Act( |
| 4200 |
Act::new("Add Tag", Action::post("/items/tag")).asking( |
| 4201 |
Field::new(layout::FieldKind::Text, "tag", "Tag slug") |
| 4202 |
.writes(Action::post("/items/tag/live")), |
| 4203 |
), |
| 4204 |
)); |
| 4205 |
|
| 4206 |
assert!(!html.contains("/items/tag/live"), "{html}"); |
| 4207 |
assert!(!html.contains("data-over"), "{html}"); |
| 4208 |
} |
| 4209 |
|
| 4210 |
#[test] |
| 4211 |
fn the_selection_script_is_linked_after_htmx_and_can_be_left_out() { |
| 4212 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 4213 |
let htmx = with.find("htmx.min.js").expect("htmx is linked"); |
| 4214 |
let selection = with |
| 4215 |
.find("quasi-selection.js") |
| 4216 |
.expect("the selection script is linked"); |
| 4217 |
|
| 4218 |
|
| 4219 |
assert!(htmx < selection, "{with}"); |
| 4220 |
|
| 4221 |
|
| 4222 |
|
| 4223 |
|
| 4224 |
let without = Webview::new() |
| 4225 |
.with_shell(Shell::default().without_selection()) |
| 4226 |
.screen(&Screen::list_detail("A", false)); |
| 4227 |
assert!(!without.contains("quasi-selection.js"), "{without}"); |
| 4228 |
} |
| 4229 |
|
| 4230 |
#[test] |
| 4231 |
fn the_selection_script_reads_only_the_hooks_this_crate_emits() { |
| 4232 |
|
| 4233 |
|
| 4234 |
|
| 4235 |
assert!(crate::SELECTION_JS.contains(".row-select"), "the tick hook"); |
| 4236 |
assert!( |
| 4237 |
crate::SELECTION_JS.contains("[data-over]"), |
| 4238 |
"the commit hook" |
| 4239 |
); |
| 4240 |
|
| 4241 |
|
| 4242 |
|
| 4243 |
|
| 4244 |
for word in ["task", "email", "contact", "goingson"] { |
| 4245 |
assert!( |
| 4246 |
!crate::SELECTION_JS.to_lowercase().contains(word), |
| 4247 |
"the script names {word}" |
| 4248 |
); |
| 4249 |
} |
| 4250 |
} |
| 4251 |
|
| 4252 |
#[test] |
| 4253 |
fn a_table_row_joins_the_same_selection_a_list_row_does() { |
| 4254 |
|
| 4255 |
|
| 4256 |
|
| 4257 |
|
| 4258 |
let html = fragment(&Node::Table { |
| 4259 |
marks: ::quasi_router::stage::Marks::none(), |
| 4260 |
columns: vec![Column::new("title")], |
| 4261 |
rows: vec![ |
| 4262 |
Row::cells(["First"]).ticking("t-1", false), |
| 4263 |
Row::cells(["Second"]).ticking("t-2", true), |
| 4264 |
], |
| 4265 |
more: None, |
| 4266 |
}); |
| 4267 |
|
| 4268 |
assert!(html.contains("name=\"ticked\" value=\"t-1\""), "{html}"); |
| 4269 |
assert!(html.contains("name=\"ticked\" value=\"t-2\""), "{html}"); |
| 4270 |
assert_eq!(html.matches("class=\"row-select\"").count(), 2, "{html}"); |
| 4271 |
assert_eq!(html.matches(" checked").count(), 1, "{html}"); |
| 4272 |
} |
| 4273 |
|
| 4274 |
#[test] |
| 4275 |
fn a_table_row_offers_what_it_does_not_show() { |
| 4276 |
|
| 4277 |
|
| 4278 |
|
| 4279 |
let html = fragment(&Node::Table { |
| 4280 |
marks: ::quasi_router::stage::Marks::none(), |
| 4281 |
columns: vec![Column::new("title"), Column::new("size")], |
| 4282 |
rows: vec![ |
| 4283 |
Row::cells(["kick.wav", "2.1 MB"]) |
| 4284 |
.offers(Act::new("Preview", Action::post("/files/1/preview"))) |
| 4285 |
.offers( |
| 4286 |
Act::new("Delete", Action::post("/files/1/delete")).confirm("Delete kick.wav?"), |
| 4287 |
), |
| 4288 |
], |
| 4289 |
more: None, |
| 4290 |
}); |
| 4291 |
|
| 4292 |
assert!(html.contains(r#"data-menu="row""#), "{html}"); |
| 4293 |
assert!(html.contains("class=\"table-row-menu\""), "{html}"); |
| 4294 |
|
| 4295 |
|
| 4296 |
assert!(html.contains(" hidden>"), "{html}"); |
| 4297 |
assert!(html.contains("Preview"), "{html}"); |
| 4298 |
assert!(html.contains(r#"hx-confirm="Delete kick.wav?""#), "{html}"); |
| 4299 |
|
| 4300 |
|
| 4301 |
|
| 4302 |
|
| 4303 |
|
| 4304 |
assert_eq!(html.matches("role=\"columnheader\"").count(), 2, "{html}"); |
| 4305 |
assert!(!html.contains("table-select"), "{html}"); |
| 4306 |
} |
| 4307 |
|
| 4308 |
#[test] |
| 4309 |
fn a_table_row_that_offers_nothing_emits_no_menu_container() { |
| 4310 |
|
| 4311 |
|
| 4312 |
|
| 4313 |
let html = fragment(&Node::Table { |
| 4314 |
marks: ::quasi_router::stage::Marks::none(), |
| 4315 |
columns: vec![Column::new("title")], |
| 4316 |
rows: vec![Row::cells(["kick.wav"])], |
| 4317 |
more: None, |
| 4318 |
}); |
| 4319 |
assert!(!html.contains("data-menu"), "{html}"); |
| 4320 |
assert!(!html.contains("table-row-menu"), "{html}"); |
| 4321 |
} |
| 4322 |
|
| 4323 |
#[test] |
| 4324 |
fn a_tick_takes_a_gutter_cell_in_the_head_as_well_as_the_body() { |
| 4325 |
|
| 4326 |
|
| 4327 |
let ticked = fragment(&Node::Table { |
| 4328 |
marks: ::quasi_router::stage::Marks::none(), |
| 4329 |
columns: vec![Column::new("title"), Column::new("size")], |
| 4330 |
rows: vec![Row::cells(["kick.wav", "2.1 MB"]).ticking("t-1", false)], |
| 4331 |
more: None, |
| 4332 |
}); |
| 4333 |
assert_eq!( |
| 4334 |
ticked.matches("role=\"columnheader\"").count(), |
| 4335 |
3, |
| 4336 |
"{ticked}" |
| 4337 |
); |
| 4338 |
assert_eq!(ticked.matches("class=\"cell").count(), 3, "{ticked}"); |
| 4339 |
|
| 4340 |
|
| 4341 |
let plain = fragment(&Node::Table { |
| 4342 |
marks: ::quasi_router::stage::Marks::none(), |
| 4343 |
columns: vec![Column::new("title"), Column::new("size")], |
| 4344 |
rows: vec![Row::cells(["kick.wav", "2.1 MB"])], |
| 4345 |
more: None, |
| 4346 |
}); |
| 4347 |
assert_eq!(plain.matches("role=\"columnheader\"").count(), 2, "{plain}"); |
| 4348 |
assert!(!plain.contains("row-select"), "{plain}"); |
| 4349 |
} |
| 4350 |
|
| 4351 |
#[test] |
| 4352 |
fn a_field_that_writes_without_a_selection_gathers_only_itself() { |
| 4353 |
let html = fragment(&Node::field( |
| 4354 |
Field::select("folder", "Folder", vec![Choice::plain("Inbox")]) |
| 4355 |
.writes(Action::get("/emails/list")), |
| 4356 |
)); |
| 4357 |
|
| 4358 |
assert_eq!(html.matches("hx-include").count(), 1, "{html}"); |
| 4359 |
assert!(!html.contains(".row-select"), "{html}"); |
| 4360 |
} |
| 4361 |
|
| 4362 |
#[test] |
| 4363 |
fn a_commit_control_gathers_every_tick_on_the_screen() { |
| 4364 |
|
| 4365 |
|
| 4366 |
|
| 4367 |
let html = fragment(&Node::Act( |
| 4368 |
Act::new("Archive", Action::post("/mail/archive")).over("chosen"), |
| 4369 |
)); |
| 4370 |
|
| 4371 |
assert!(html.contains("hx-include=\".row-select\""), "{html}"); |
| 4372 |
assert!(html.contains("hx-post=\"/mail/archive\""), "{html}"); |
| 4373 |
} |
| 4374 |
|
| 4375 |
#[test] |
| 4376 |
fn a_control_over_nothing_gathers_nothing() { |
| 4377 |
let html = fragment(&Node::Act(Act::new( |
| 4378 |
"Delete", |
| 4379 |
Action::post("/mail/1/delete"), |
| 4380 |
))); |
| 4381 |
assert!(!html.contains("hx-include"), "{html}"); |
| 4382 |
} |
| 4383 |
|
| 4384 |
#[test] |
| 4385 |
fn the_gathering_selector_follows_a_hosts_class_prefix() { |
| 4386 |
|
| 4387 |
|
| 4388 |
let emit = Emit { |
| 4389 |
class_prefix: "q-", |
| 4390 |
..Emit::default() |
| 4391 |
}; |
| 4392 |
let render = Webview::new().with_emit(emit); |
| 4393 |
|
| 4394 |
let boxes = render.fragment(&Node::list([Row::new("First").ticking("m-1", false)])); |
| 4395 |
let button = render.fragment(&Node::Act( |
| 4396 |
Act::new("Archive", Action::post("/mail/archive")).over("chosen"), |
| 4397 |
)); |
| 4398 |
|
| 4399 |
assert!(boxes.contains("q-row-select"), "{boxes}"); |
| 4400 |
assert!(button.contains("hx-include=\".q-row-select\""), "{button}"); |
| 4401 |
} |
| 4402 |
|
| 4403 |
#[test] |
| 4404 |
fn an_overlay_is_the_inside_of_a_container_and_not_a_document() { |
| 4405 |
|
| 4406 |
let screen = Screen::sidebar_content("Palette") |
| 4407 |
.with(Slot::new("results", RegionKind::Pane).with(Node::text("Open task"))); |
| 4408 |
let html = Webview::new().overlay(&screen); |
| 4409 |
assert!(!html.contains("<html"), "{html}"); |
| 4410 |
assert!(!html.contains("<body"), "{html}"); |
| 4411 |
assert!(!html.contains("<title"), "{html}"); |
| 4412 |
assert!(html.contains("Open task"), "{html}"); |
| 4413 |
} |
| 4414 |
|
| 4415 |
#[test] |
| 4416 |
fn an_overlay_names_the_container_it_lands_in() { |
| 4417 |
|
| 4418 |
|
| 4419 |
assert_eq!( |
| 4420 |
Webview::new().overlay_target(), |
| 4421 |
Some(crate::chrome::OVERLAY_ID) |
| 4422 |
); |
| 4423 |
} |
| 4424 |
|
| 4425 |
#[test] |
| 4426 |
fn a_document_carries_the_apps_bindings_and_the_container_they_open_into() { |
| 4427 |
use quasi_router::Chrome; |
| 4428 |
|
| 4429 |
let shell = Shell::default().with_chrome(Chrome::new().bind( |
| 4430 |
"ctrl+k", |
| 4431 |
"Search", |
| 4432 |
Action::get("/palette"), |
| 4433 |
)); |
| 4434 |
let html = Webview::new() |
| 4435 |
.with_shell(shell) |
| 4436 |
.screen(&Screen::list_detail("Tasks", false)); |
| 4437 |
assert!(html.contains("hx-get=\"/palette\""), "{html}"); |
| 4438 |
assert!(html.contains("from:body"), "{html}"); |
| 4439 |
assert!(html.contains("id=\"quasi-overlay\""), "{html}"); |
| 4440 |
|
| 4441 |
|
| 4442 |
let overlay_at = html.find("id=\"quasi-overlay\"").expect("emitted"); |
| 4443 |
assert!( |
| 4444 |
overlay_at > html.find("</main>").expect("main closes"), |
| 4445 |
"{html}" |
| 4446 |
); |
| 4447 |
assert!(html.ends_with("</body></html>"), "{html}"); |
| 4448 |
} |
| 4449 |
|
| 4450 |
#[test] |
| 4451 |
fn an_app_declaring_no_chrome_gets_the_document_it_always_got() { |
| 4452 |
|
| 4453 |
let html = render(&Screen::list_detail("Tasks", false)); |
| 4454 |
assert!(!html.contains("quasi-overlay"), "{html}"); |
| 4455 |
assert!(!html.contains("data-chrome"), "{html}"); |
| 4456 |
} |
| 4457 |
|
| 4458 |
|
| 4459 |
fn gallery() -> Slot { |
| 4460 |
Slot::widget("shots", "carousel") |
| 4461 |
.extend((0..3).map(|n| { |
| 4462 |
Node::Image(quasi_router::screen::Image::new( |
| 4463 |
format!("/shot-{n}.png"), |
| 4464 |
format!("shot {n}"), |
| 4465 |
)) |
| 4466 |
})) |
| 4467 |
.showing_one(1) |
| 4468 |
} |
| 4469 |
|
| 4470 |
#[test] |
| 4471 |
fn a_region_showing_everything_emits_exactly_what_it_always_did() { |
| 4472 |
|
| 4473 |
|
| 4474 |
|
| 4475 |
let screen = Screen::list_detail("Tasks", false) |
| 4476 |
.with(Slot::new("main", RegionKind::Pane).with(Node::text("plain"))); |
| 4477 |
let html = render(&screen); |
| 4478 |
|
| 4479 |
assert!(!html.contains("data-showing")); |
| 4480 |
assert!(!html.contains("showing-frame")); |
| 4481 |
assert!(!html.contains("showing-position")); |
| 4482 |
} |
| 4483 |
|
| 4484 |
#[test] |
| 4485 |
fn a_carousel_gets_a_row_without_this_renderer_knowing_what_a_carousel_is() { |
| 4486 |
|
| 4487 |
|
| 4488 |
let screen = Screen::list_detail("Product", false).with(gallery()); |
| 4489 |
let html = render(&screen); |
| 4490 |
|
| 4491 |
assert!(html.contains("data-showing=\"one\""), "{html}"); |
| 4492 |
assert!(html.contains("data-shows=\"previous\""), "{html}"); |
| 4493 |
assert!(html.contains("data-shows=\"next\""), "{html}"); |
| 4494 |
|
| 4495 |
|
| 4496 |
assert!(html.contains(">2 / 3</span>"), "{html}"); |
| 4497 |
|
| 4498 |
assert!(html.contains("data-widget=\"carousel\"")); |
| 4499 |
} |
| 4500 |
|
| 4501 |
#[test] |
| 4502 |
fn the_row_sits_under_the_frames_and_overlays_nothing() { |
| 4503 |
|
| 4504 |
|
| 4505 |
|
| 4506 |
let html = render(&Screen::list_detail("Product", false).with(gallery())); |
| 4507 |
|
| 4508 |
let last_frame = html.rfind("showing-frame").expect("frames are wrapped"); |
| 4509 |
let row = html.rfind("showing-position").expect("a row is derived"); |
| 4510 |
assert!(row > last_frame, "{html}"); |
| 4511 |
} |
| 4512 |
|
| 4513 |
#[test] |
| 4514 |
fn only_the_current_frame_is_marked_and_the_rest_are_still_in_the_document() { |
| 4515 |
|
| 4516 |
|
| 4517 |
|
| 4518 |
let html = render(&Screen::list_detail("Product", false).with(gallery())); |
| 4519 |
|
| 4520 |
assert_eq!(html.matches("showing-frame").count(), 3, "{html}"); |
| 4521 |
assert_eq!(html.matches("showing-frame current").count(), 1, "{html}"); |
| 4522 |
assert!(html.contains("/shot-0.png") && html.contains("/shot-2.png")); |
| 4523 |
} |
| 4524 |
|
| 4525 |
#[test] |
| 4526 |
fn labelled_children_get_a_strip_and_it_is_makeovers_tab_markup() { |
| 4527 |
|
| 4528 |
|
| 4529 |
let screen = Screen::list_detail("Project", false).with( |
| 4530 |
Slot::new("detail", RegionKind::TabGroup) |
| 4531 |
.frame( |
| 4532 |
"Overview", |
| 4533 |
Node::Region(Slot::new("overview", RegionKind::Pane)), |
| 4534 |
) |
| 4535 |
.frame("Files", Node::Region(Slot::new("files", RegionKind::Pane))) |
| 4536 |
.showing_one(1), |
| 4537 |
); |
| 4538 |
let html = render(&screen); |
| 4539 |
|
| 4540 |
assert!(html.contains("data-selector=\"tab\""), "{html}"); |
| 4541 |
assert!(html.contains("role=\"tablist\""), "{html}"); |
| 4542 |
assert!(html.contains(">Overview</button>"), "{html}"); |
| 4543 |
assert!(html.contains("chosen\" data-shows=\"1\""), "{html}"); |
| 4544 |
assert!(html.contains("aria-selected=\"true\""), "{html}"); |
| 4545 |
assert!(html.contains("aria-selected=\"false\""), "{html}"); |
| 4546 |
|
| 4547 |
|
| 4548 |
|
| 4549 |
assert!(html.contains(">Files</button>"), "{html}"); |
| 4550 |
assert!(html.contains(">Overview</button>"), "{html}"); |
| 4551 |
|
| 4552 |
assert!(!html.contains("showing-position"), "{html}"); |
| 4553 |
} |
| 4554 |
|
| 4555 |
#[test] |
| 4556 |
fn a_tab_whose_panel_is_a_route_carries_the_address_and_the_panel_does_not() { |
| 4557 |
|
| 4558 |
|
| 4559 |
|
| 4560 |
let screen = Screen::list_detail("Library", false).with( |
| 4561 |
Slot::new("tab-content", RegionKind::TabGroup) |
| 4562 |
|
| 4563 |
|
| 4564 |
|
| 4565 |
.frame( |
| 4566 |
"Purchases", |
| 4567 |
Node::Region( |
| 4568 |
Slot::new("purchases", RegionKind::Pane).with(Node::text("what you bought")), |
| 4569 |
), |
| 4570 |
) |
| 4571 |
.frame( |
| 4572 |
"Feed", |
| 4573 |
Node::Region( |
| 4574 |
Slot::new("feed", RegionKind::Pane).fed_by(Action::get("/library/tabs/feed")), |
| 4575 |
), |
| 4576 |
) |
| 4577 |
.showing_one(0), |
| 4578 |
); |
| 4579 |
let html = render(&screen); |
| 4580 |
|
| 4581 |
|
| 4582 |
assert!( |
| 4583 |
html.contains("aria-selected=\"false\" hx-get=\"/library/tabs/feed\""), |
| 4584 |
"{html}" |
| 4585 |
); |
| 4586 |
|
| 4587 |
|
| 4588 |
assert!(!html.contains("hx-trigger=\"load\""), "{html}"); |
| 4589 |
|
| 4590 |
assert!(!html.contains("aria-busy"), "{html}"); |
| 4591 |
|
| 4592 |
|
| 4593 |
assert!(html.contains("data-shows=\"1\""), "{html}"); |
| 4594 |
|
| 4595 |
|
| 4596 |
assert!(html.contains("what you bought"), "{html}"); |
| 4597 |
} |
| 4598 |
|
| 4599 |
#[test] |
| 4600 |
fn a_tab_is_a_button_even_though_its_address_is_a_read() { |
| 4601 |
|
| 4602 |
|
| 4603 |
|
| 4604 |
|
| 4605 |
let screen = Screen::list_detail("Library", false).with( |
| 4606 |
Slot::new("tab-content", RegionKind::TabGroup) |
| 4607 |
.frame( |
| 4608 |
"Purchases", |
| 4609 |
Node::Region( |
| 4610 |
Slot::new("purchases", RegionKind::Pane) |
| 4611 |
.fed_by(Action::get("/library/tabs/purchases")), |
| 4612 |
), |
| 4613 |
) |
| 4614 |
.frame( |
| 4615 |
"Feed", |
| 4616 |
Node::Region( |
| 4617 |
Slot::new("feed", RegionKind::Pane).fed_by(Action::get("/library/tabs/feed")), |
| 4618 |
), |
| 4619 |
) |
| 4620 |
.showing_one(0), |
| 4621 |
); |
| 4622 |
let html = render(&screen); |
| 4623 |
|
| 4624 |
assert!(!html.contains("href="), "{html}"); |
| 4625 |
assert!( |
| 4626 |
html.contains("<button type=\"button\" class=\"tab"), |
| 4627 |
"{html}" |
| 4628 |
); |
| 4629 |
} |
| 4630 |
|
| 4631 |
#[test] |
| 4632 |
fn a_carousel_of_frames_already_here_still_calls_no_route() { |
| 4633 |
|
| 4634 |
|
| 4635 |
|
| 4636 |
let screen = Screen::list_detail("Project", false).with( |
| 4637 |
Slot::new("detail", RegionKind::TabGroup) |
| 4638 |
.frame( |
| 4639 |
"Overview", |
| 4640 |
Node::Region(Slot::new("overview", RegionKind::Pane)), |
| 4641 |
) |
| 4642 |
.frame("Files", Node::Region(Slot::new("files", RegionKind::Pane))) |
| 4643 |
.showing_one(0), |
| 4644 |
); |
| 4645 |
let html = render(&screen); |
| 4646 |
|
| 4647 |
assert!(html.contains("data-shows=\"1\""), "{html}"); |
| 4648 |
assert!(!html.contains("hx-get"), "{html}"); |
| 4649 |
} |
| 4650 |
|
| 4651 |
#[test] |
| 4652 |
fn a_carousel_carries_the_program_that_moves_its_frames() { |
| 4653 |
|
| 4654 |
|
| 4655 |
|
| 4656 |
|
| 4657 |
|
| 4658 |
let html = render(&Screen::list_detail("Product", false).with(gallery())); |
| 4659 |
|
| 4660 |
|
| 4661 |
|
| 4662 |
|
| 4663 |
assert_eq!(html.matches("data-frame=\"shots\"").count(), 3, "{html}"); |
| 4664 |
assert!(html.contains("data-shown=\"2\""), "{html}"); |
| 4665 |
|
| 4666 |
|
| 4667 |
assert!(html.contains("data-position=\"shots\""), "{html}"); |
| 4668 |
|
| 4669 |
|
| 4670 |
assert_eq!(html.matches(" _=\"on click").count(), 2, "{html}"); |
| 4671 |
assert!(html.contains("mod 3"), "{html}"); |
| 4672 |
} |
| 4673 |
|
| 4674 |
#[test] |
| 4675 |
fn a_tab_moves_the_frame_and_carries_the_strips_marks_with_it() { |
| 4676 |
|
| 4677 |
|
| 4678 |
|
| 4679 |
let screen = Screen::list_detail("Project", false).with( |
| 4680 |
Slot::new("detail", RegionKind::TabGroup) |
| 4681 |
.frame( |
| 4682 |
"Overview", |
| 4683 |
Node::Region(Slot::new("overview", RegionKind::Pane)), |
| 4684 |
) |
| 4685 |
.frame("Files", Node::Region(Slot::new("files", RegionKind::Pane))) |
| 4686 |
.showing_one(0), |
| 4687 |
); |
| 4688 |
let html = render(&screen); |
| 4689 |
|
| 4690 |
assert!(html.contains("take .chosen"), "{html}"); |
| 4691 |
assert!( |
| 4692 |
html.contains("set @aria-selected of me to 'true'"), |
| 4693 |
"{html}" |
| 4694 |
); |
| 4695 |
assert!( |
| 4696 |
html.contains("take .current from <[data-frame='detail']/>"), |
| 4697 |
"{html}" |
| 4698 |
); |
| 4699 |
|
| 4700 |
assert!(!html.contains("data-position"), "{html}"); |
| 4701 |
} |
| 4702 |
|
| 4703 |
#[test] |
| 4704 |
fn a_disclosure_opens_and_closes_the_one_child_under_it() { |
| 4705 |
|
| 4706 |
|
| 4707 |
let screen = Screen::list_detail("Item", false).with( |
| 4708 |
Slot::new("more", RegionKind::Group) |
| 4709 |
.frame("Details", Node::Region(Slot::new("body", RegionKind::Pane))) |
| 4710 |
.showing_at_most_one(Some(0)), |
| 4711 |
); |
| 4712 |
let html = render(&screen); |
| 4713 |
|
| 4714 |
assert!(html.contains("toggle .current"), "{html}"); |
| 4715 |
assert!(html.contains("set @aria-expanded of me"), "{html}"); |
| 4716 |
} |
| 4717 |
|
| 4718 |
#[test] |
| 4719 |
fn a_region_showing_everything_carries_no_program_either() { |
| 4720 |
|
| 4721 |
|
| 4722 |
|
| 4723 |
let screen = Screen::list_detail("Tasks", false) |
| 4724 |
.with(Slot::new("main", RegionKind::Pane).with(Node::text("plain"))); |
| 4725 |
let html = render(&screen); |
| 4726 |
|
| 4727 |
assert!(!html.contains(" _=\""), "{html}"); |
| 4728 |
assert!(!html.contains("data-frame"), "{html}"); |
| 4729 |
} |
| 4730 |
|
| 4731 |
#[test] |
| 4732 |
fn a_region_with_no_frames_is_stepped_by_nothing() { |
| 4733 |
|
| 4734 |
|
| 4735 |
|
| 4736 |
let screen = Screen::list_detail("Product", false) |
| 4737 |
.with(Slot::widget("shots", "carousel").showing_one(0)); |
| 4738 |
let html = render(&screen); |
| 4739 |
|
| 4740 |
assert!(html.contains("data-shows=\"next\""), "{html}"); |
| 4741 |
assert!(!html.contains(" _=\""), "{html}"); |
| 4742 |
assert!(html.contains(">0 / 0</span>"), "{html}"); |
| 4743 |
} |
| 4744 |
|
| 4745 |
#[test] |
| 4746 |
fn a_region_id_that_is_not_a_handle_gets_the_mark_and_no_program() { |
| 4747 |
|
| 4748 |
|
| 4749 |
|
| 4750 |
|
| 4751 |
|
| 4752 |
|
| 4753 |
|
| 4754 |
let screen = Screen::list_detail("Product", false).with( |
| 4755 |
Slot::widget("${window.alert(1)}", "carousel") |
| 4756 |
.with(Node::text("a")) |
| 4757 |
.with(Node::text("b")) |
| 4758 |
.showing_one(0), |
| 4759 |
); |
| 4760 |
let html = render(&screen); |
| 4761 |
|
| 4762 |
assert!(html.contains("data-shows=\"next\""), "{html}"); |
| 4763 |
assert!(!html.contains(" _=\""), "{html}"); |
| 4764 |
assert!(!html.contains("alert(1)/>"), "{html}"); |
| 4765 |
} |
| 4766 |
|
| 4767 |
#[test] |
| 4768 |
fn creator_prose_is_fenced_off_from_the_interpreter() { |
| 4769 |
|
| 4770 |
|
| 4771 |
|
| 4772 |
let html = fragment(&Node::rich("**hello**")); |
| 4773 |
|
| 4774 |
assert!(html.contains("data-disable-scripting"), "{html}"); |
| 4775 |
} |
| 4776 |
|
| 4777 |
#[test] |
| 4778 |
fn hyperscript_enters_in_exactly_one_module() { |
| 4779 |
|
| 4780 |
|
| 4781 |
|
| 4782 |
|
| 4783 |
assert!( |
| 4784 |
!include_str!("node.rs").contains(" _=\\\""), |
| 4785 |
"node.rs writes a program" |
| 4786 |
); |
| 4787 |
|
| 4788 |
|
| 4789 |
|
| 4790 |
|
| 4791 |
let source = include_str!("hyperscript.rs"); |
| 4792 |
assert!(!source.contains(" js "), "{source}"); |
| 4793 |
} |
| 4794 |
|
| 4795 |
#[test] |
| 4796 |
fn a_region_that_is_not_in_a_strip_still_fetches_itself() { |
| 4797 |
|
| 4798 |
|
| 4799 |
|
| 4800 |
let screen = Screen::list_detail("Dashboard", false) |
| 4801 |
.with(Slot::new("payouts", RegionKind::Pane).fed_by(Action::get("/dashboard/payouts"))); |
| 4802 |
let html = render(&screen); |
| 4803 |
|
| 4804 |
assert!(html.contains("hx-trigger=\"load\""), "{html}"); |
| 4805 |
assert!(html.contains("aria-busy=\"true\""), "{html}"); |
| 4806 |
} |
| 4807 |
|
| 4808 |
#[test] |
| 4809 |
fn a_strip_sits_above_the_panes_it_opens() { |
| 4810 |
|
| 4811 |
|
| 4812 |
let screen = Screen::list_detail("Project", false).with( |
| 4813 |
Slot::new("detail", RegionKind::TabGroup) |
| 4814 |
.frame( |
| 4815 |
"Overview", |
| 4816 |
Node::Region(Slot::new("overview", RegionKind::Pane)), |
| 4817 |
) |
| 4818 |
.frame("Files", Node::Region(Slot::new("files", RegionKind::Pane))) |
| 4819 |
.showing_one(0), |
| 4820 |
); |
| 4821 |
let html = render(&screen); |
| 4822 |
|
| 4823 |
let strip = html |
| 4824 |
.find("data-selector=\"tab\"") |
| 4825 |
.expect("a strip is derived"); |
| 4826 |
let first_frame = html.find("showing-frame").expect("frames are wrapped"); |
| 4827 |
assert!(strip < first_frame, "{html}"); |
| 4828 |
} |
| 4829 |
|
| 4830 |
#[test] |
| 4831 |
fn a_named_child_that_can_close_is_a_summary_line() { |
| 4832 |
|
| 4833 |
|
| 4834 |
|
| 4835 |
let screen = Screen::list_detail("Item", false).with( |
| 4836 |
Slot::widget("more", "disclosure") |
| 4837 |
.frame( |
| 4838 |
"Technical details", |
| 4839 |
Node::Region(Slot::new("body", RegionKind::Pane).with(Node::text("the rest"))), |
| 4840 |
) |
| 4841 |
.showing_at_most_one(None), |
| 4842 |
); |
| 4843 |
let html = render(&screen); |
| 4844 |
|
| 4845 |
assert!(html.contains("data-showing=\"at-most-one\""), "{html}"); |
| 4846 |
assert!(html.contains("aria-expanded=\"false\""), "{html}"); |
| 4847 |
assert!(html.contains(">Technical details</button>"), "{html}"); |
| 4848 |
assert!(!html.contains("data-shows=\"next\""), "{html}"); |
| 4849 |
} |
| 4850 |
|
| 4851 |
#[test] |
| 4852 |
fn a_derived_control_names_no_route_and_the_transport_stays_in_one_function() { |
| 4853 |
|
| 4854 |
|
| 4855 |
|
| 4856 |
let html = render(&Screen::list_detail("Product", false).with(gallery())); |
| 4857 |
let row = &html[html.find("showing-position").expect("a row is derived") - 200..]; |
| 4858 |
|
| 4859 |
assert!(!row.contains("hx-get"), "{row}"); |
| 4860 |
assert!(!row.contains("hx-post"), "{row}"); |
| 4861 |
} |
| 4862 |
|
| 4863 |
|
| 4864 |
|
| 4865 |
|
| 4866 |
fn placed(at: u16, minutes: u16, text: &str) -> quasi_router::screen::Placed { |
| 4867 |
quasi_router::screen::Placed::new(at, minutes, Row::new(text)) |
| 4868 |
} |
| 4869 |
|
| 4870 |
#[test] |
| 4871 |
fn a_timeline_places_each_entry_as_a_percentage_of_its_span() { |
| 4872 |
|
| 4873 |
|
| 4874 |
|
| 4875 |
let node = Node::Timeline { |
| 4876 |
marks: ::quasi_router::stage::Marks::none(), |
| 4877 |
track: layout::Track::DAY, |
| 4878 |
entries: vec![placed(540, 60, "Standup")], |
| 4879 |
focus: None, |
| 4880 |
}; |
| 4881 |
let html = fragment(&node); |
| 4882 |
|
| 4883 |
assert!(html.contains("--track-at:37.5000%"), "{html}"); |
| 4884 |
assert!(html.contains("--track-for:4.1667%"), "{html}"); |
| 4885 |
|
| 4886 |
|
| 4887 |
assert!(html.contains("--track-lane:0"), "{html}"); |
| 4888 |
assert!(html.contains("--track-lanes:1"), "{html}"); |
| 4889 |
} |
| 4890 |
|
| 4891 |
#[test] |
| 4892 |
fn overlapping_entries_take_lanes_and_the_rest_stay_wide() { |
| 4893 |
|
| 4894 |
|
| 4895 |
|
| 4896 |
|
| 4897 |
let node = Node::Timeline { |
| 4898 |
marks: ::quasi_router::stage::Marks::none(), |
| 4899 |
track: layout::Track::DAY, |
| 4900 |
entries: vec![ |
| 4901 |
placed(540, 60, "Standup"), |
| 4902 |
placed(570, 60, "Interview"), |
| 4903 |
placed(720, 30, "Lunch"), |
| 4904 |
], |
| 4905 |
focus: None, |
| 4906 |
}; |
| 4907 |
let html = fragment(&node); |
| 4908 |
|
| 4909 |
assert!(html.contains("--track-lane:0"), "{html}"); |
| 4910 |
assert!(html.contains("--track-lane:1"), "{html}"); |
| 4911 |
assert!( |
| 4912 |
!html.contains("--track-lane:2"), |
| 4913 |
"two collide, not three: {html}" |
| 4914 |
); |
| 4915 |
assert_eq!(html.matches("--track-lanes:2").count(), 3, "{html}"); |
| 4916 |
} |
| 4917 |
|
| 4918 |
#[test] |
| 4919 |
fn things_that_only_touch_do_not_collide() { |
| 4920 |
|
| 4921 |
|
| 4922 |
|
| 4923 |
let node = Node::Timeline { |
| 4924 |
marks: ::quasi_router::stage::Marks::none(), |
| 4925 |
track: layout::Track::DAY, |
| 4926 |
entries: vec![placed(540, 60, "First"), placed(600, 60, "Second")], |
| 4927 |
focus: None, |
| 4928 |
}; |
| 4929 |
let html = fragment(&node); |
| 4930 |
|
| 4931 |
assert!(html.contains("--track-lanes:1"), "{html}"); |
| 4932 |
assert!(!html.contains("--track-lane:1"), "{html}"); |
| 4933 |
} |
| 4934 |
|
| 4935 |
#[test] |
| 4936 |
fn a_timeline_labels_its_ruler_in_wall_clock_and_wraps_past_midnight() { |
| 4937 |
|
| 4938 |
|
| 4939 |
let node = Node::Timeline { |
| 4940 |
marks: ::quasi_router::stage::Marks::none(), |
| 4941 |
track: layout::Track::over(layout::Span::new(1320, 1560)), |
| 4942 |
entries: vec![], |
| 4943 |
focus: None, |
| 4944 |
}; |
| 4945 |
let html = fragment(&node); |
| 4946 |
|
| 4947 |
assert!(html.contains(">22:00<"), "{html}"); |
| 4948 |
assert!(html.contains(">00:00<"), "{html}"); |
| 4949 |
assert!(html.contains(">01:00<"), "{html}"); |
| 4950 |
assert!(!html.contains(">25:00<"), "the clock wrapped: {html}"); |
| 4951 |
} |
| 4952 |
|
| 4953 |
#[test] |
| 4954 |
fn a_timeline_carries_its_focus_as_a_moment_and_never_as_an_offset() { |
| 4955 |
|
| 4956 |
|
| 4957 |
let node = Node::Timeline { |
| 4958 |
marks: ::quasi_router::stage::Marks::none(), |
| 4959 |
track: layout::Track::DAY, |
| 4960 |
entries: vec![], |
| 4961 |
focus: Some(540), |
| 4962 |
}; |
| 4963 |
let html = fragment(&node); |
| 4964 |
|
| 4965 |
assert!(html.contains("data-focus=\"540\""), "{html}"); |
| 4966 |
assert!(!html.contains("scrollTop"), "{html}"); |
| 4967 |
assert!(!html.contains("px"), "{html}"); |
| 4968 |
} |
| 4969 |
|
| 4970 |
#[test] |
| 4971 |
fn a_placed_rows_text_cannot_become_markup() { |
| 4972 |
|
| 4973 |
|
| 4974 |
let node = Node::Timeline { |
| 4975 |
marks: ::quasi_router::stage::Marks::none(), |
| 4976 |
track: layout::Track::DAY, |
| 4977 |
entries: vec![placed(540, 60, "<script>alert('x')</script>")], |
| 4978 |
focus: None, |
| 4979 |
}; |
| 4980 |
let html = fragment(&node); |
| 4981 |
|
| 4982 |
assert!(!html.contains("<script>"), "{html}"); |
| 4983 |
assert!(html.contains("<script>"), "{html}"); |
| 4984 |
} |
| 4985 |
|
| 4986 |
#[test] |
| 4987 |
fn a_timeline_emits_no_size_of_its_own() { |
| 4988 |
|
| 4989 |
|
| 4990 |
|
| 4991 |
|
| 4992 |
let node = Node::Timeline { |
| 4993 |
marks: ::quasi_router::stage::Marks::none(), |
| 4994 |
track: layout::Track::DAY, |
| 4995 |
entries: vec![placed(540, 60, "Standup")], |
| 4996 |
focus: None, |
| 4997 |
}; |
| 4998 |
let html = fragment(&node); |
| 4999 |
|
| 5000 |
for unit in ["px", "rem", "em;", "vh"] { |
| 5001 |
assert!(!html.contains(unit), "emitted a {unit}: {html}"); |
| 5002 |
} |
| 5003 |
} |
| 5004 |
|
| 5005 |
|
| 5006 |
|
| 5007 |
#[test] |
| 5008 |
fn peer_columns_are_a_row_of_fills_and_carry_no_width() { |
| 5009 |
|
| 5010 |
|
| 5011 |
|
| 5012 |
|
| 5013 |
|
| 5014 |
let column = |id: &str, label: &str| { |
| 5015 |
Node::Region(Slot::new(id, RegionKind::Pane).with(Node::Heading { |
| 5016 |
level: layout::Heading::Section, |
| 5017 |
text: label.into(), |
| 5018 |
})) |
| 5019 |
}; |
| 5020 |
let board = Slot::new("board", RegionKind::Group).across( |
| 5021 |
Run::new(layout::Fallback::Wrap) |
| 5022 |
.spread( |
| 5023 |
column("pending", "Pending"), |
| 5024 |
layout::Priority::Essential, |
| 5025 |
layout::Width::Fill, |
| 5026 |
) |
| 5027 |
.spread( |
| 5028 |
column("started", "Started"), |
| 5029 |
layout::Priority::Essential, |
| 5030 |
layout::Width::Fill, |
| 5031 |
) |
| 5032 |
.spread( |
| 5033 |
column("done", "Completed"), |
| 5034 |
layout::Priority::Essential, |
| 5035 |
layout::Width::Fill, |
| 5036 |
), |
| 5037 |
); |
| 5038 |
let html = render(&Screen::list_detail("Tasks", false).with(board)); |
| 5039 |
|
| 5040 |
assert_eq!(html.matches("data-width=\"fill\"").count(), 3, "{html}"); |
| 5041 |
|
| 5042 |
|
| 5043 |
assert!(!html.contains("data-share"), "{html}"); |
| 5044 |
assert!(!html.contains("data-columns"), "{html}"); |
| 5045 |
for label in ["Pending", "Started", "Completed"] { |
| 5046 |
assert!(html.contains(label), "{html}"); |
| 5047 |
} |
| 5048 |
} |
| 5049 |
|
| 5050 |
#[test] |
| 5051 |
fn a_split_is_a_pane_that_takes_what_it_needs_beside_one_that_fills() { |
| 5052 |
|
| 5053 |
|
| 5054 |
|
| 5055 |
let pane = |id: &str| Node::Region(Slot::new(id, RegionKind::Pane)); |
| 5056 |
let split = Slot::new("review", RegionKind::Group).across( |
| 5057 |
Run::new(layout::Fallback::Stack) |
| 5058 |
.beside(pane("listing"), layout::Priority::Essential) |
| 5059 |
.spread( |
| 5060 |
pane("reading"), |
| 5061 |
layout::Priority::Essential, |
| 5062 |
layout::Width::Fill, |
| 5063 |
), |
| 5064 |
); |
| 5065 |
let html = render(&Screen::list_detail("Review", false).with(split)); |
| 5066 |
|
| 5067 |
|
| 5068 |
|
| 5069 |
assert_eq!(html.matches("data-width=").count(), 1, "{html}"); |
| 5070 |
assert!(html.contains("data-width=\"fill\""), "{html}"); |
| 5071 |
let listing = html.find("id=\"listing\"").expect("the left pane renders"); |
| 5072 |
let reading = html.find("id=\"reading\"").expect("the right pane renders"); |
| 5073 |
assert!(listing < reading, "{html}"); |
| 5074 |
} |
| 5075 |
|
| 5076 |
#[test] |
| 5077 |
fn a_member_that_says_nothing_about_width_is_wrapped_in_nothing() { |
| 5078 |
|
| 5079 |
|
| 5080 |
|
| 5081 |
let bare = Slot::new("bar", RegionKind::Band).across( |
| 5082 |
Run::new(layout::Fallback::Wrap).beside(Node::text("title"), layout::Priority::Essential), |
| 5083 |
); |
| 5084 |
let said = Slot::new("bar", RegionKind::Band).across(Run::new(layout::Fallback::Wrap).spread( |
| 5085 |
Node::text("title"), |
| 5086 |
layout::Priority::Essential, |
| 5087 |
layout::Width::Content, |
| 5088 |
)); |
| 5089 |
|
| 5090 |
assert_eq!( |
| 5091 |
render(&Screen::list_detail("T", false).with(bare)), |
| 5092 |
render(&Screen::list_detail("T", false).with(said)), |
| 5093 |
); |
| 5094 |
} |
| 5095 |
|
| 5096 |
#[test] |
| 5097 |
fn a_day_strip_labels_days_and_not_a_wall_clock() { |
| 5098 |
|
| 5099 |
|
| 5100 |
|
| 5101 |
let march = layout::Track::days(layout::Span::new(0, 31)); |
| 5102 |
let node = Node::Timeline { |
| 5103 |
marks: ::quasi_router::stage::Marks::none(), |
| 5104 |
track: march, |
| 5105 |
entries: vec![quasi_router::screen::Placed::new(2, 15, Row::new("Leave"))], |
| 5106 |
focus: None, |
| 5107 |
}; |
| 5108 |
let html = fragment(&node); |
| 5109 |
|
| 5110 |
|
| 5111 |
|
| 5112 |
assert!(html.contains(">1<"), "{html}"); |
| 5113 |
assert!(html.contains(">8<"), "{html}"); |
| 5114 |
assert!( |
| 5115 |
!html.contains("00:00"), |
| 5116 |
"a month strip under a wall clock: {html}" |
| 5117 |
); |
| 5118 |
|
| 5119 |
|
| 5120 |
assert!(html.contains("--track-at:6.4516%"), "{html}"); |
| 5121 |
assert!(html.contains("--track-for:48.3871%"), "{html}"); |
| 5122 |
} |
| 5123 |
|
| 5124 |
#[test] |
| 5125 |
fn a_day_view_still_labels_a_wall_clock() { |
| 5126 |
|
| 5127 |
|
| 5128 |
let node = Node::Timeline { |
| 5129 |
marks: ::quasi_router::stage::Marks::none(), |
| 5130 |
track: layout::Track::DAY, |
| 5131 |
entries: vec![], |
| 5132 |
focus: None, |
| 5133 |
}; |
| 5134 |
let html = fragment(&node); |
| 5135 |
assert!(html.contains(">09:00<"), "{html}"); |
| 5136 |
} |
| 5137 |
|
| 5138 |
#[test] |
| 5139 |
fn a_field_says_how_much_of_its_row_it_asked_for() { |
| 5140 |
|
| 5141 |
|
| 5142 |
|
| 5143 |
|
| 5144 |
|
| 5145 |
|
| 5146 |
let sized = fragment(&Node::Field(Box::new( |
| 5147 |
Field::new(layout::FieldKind::Text, "query", "Search").width(layout::Width::Content), |
| 5148 |
))); |
| 5149 |
assert!(sized.contains("data-width=\"content\"")); |
| 5150 |
|
| 5151 |
|
| 5152 |
|
| 5153 |
|
| 5154 |
let quiet = fragment(&Node::Field(Box::new(Field::new( |
| 5155 |
layout::FieldKind::Text, |
| 5156 |
"query", |
| 5157 |
"Search", |
| 5158 |
)))); |
| 5159 |
assert!(!quiet.contains("data-width")); |
| 5160 |
assert!(!quiet.contains("field-writes")); |
| 5161 |
} |
| 5162 |
|
| 5163 |
#[test] |
| 5164 |
fn a_field_that_consults_asks_on_the_keystroke_after_the_value_settles() { |
| 5165 |
let html = fragment(&Node::Field(Box::new( |
| 5166 |
Field::new(layout::FieldKind::Text, "username", "Username") |
| 5167 |
.consults(Action::get("/api/validate/username").replacing("username-status")), |
| 5168 |
))); |
| 5169 |
|
| 5170 |
|
| 5171 |
|
| 5172 |
assert!(html.contains("hx-trigger=\"keyup changed delay:500ms\"")); |
| 5173 |
assert!(html.contains("hx-get=\"/api/validate/username\"")); |
| 5174 |
|
| 5175 |
|
| 5176 |
assert!(html.contains("hx-target=\"#username-status\"")); |
| 5177 |
|
| 5178 |
|
| 5179 |
assert!(html.contains("hx-include=\"find input, find select, find textarea\"")); |
| 5180 |
assert!(html.contains("field-consults")); |
| 5181 |
} |
| 5182 |
|
| 5183 |
#[test] |
| 5184 |
fn consulting_and_writing_are_two_routes_and_so_two_wrappers() { |
| 5185 |
|
| 5186 |
|
| 5187 |
|
| 5188 |
let html = fragment(&Node::Field(Box::new( |
| 5189 |
Field::new(layout::FieldKind::Text, "slug", "Slug") |
| 5190 |
.consults(Action::get("/api/validate/slug")) |
| 5191 |
.writes(Action::post("/projects/1/slug")), |
| 5192 |
))); |
| 5193 |
|
| 5194 |
assert_eq!(html.matches("field-consults").count(), 1); |
| 5195 |
assert_eq!(html.matches("field-writes").count(), 1); |
| 5196 |
assert!(html.contains("hx-get=\"/api/validate/slug\"")); |
| 5197 |
assert!(html.contains("hx-post=\"/projects/1/slug\"")); |
| 5198 |
|
| 5199 |
|
| 5200 |
assert!( |
| 5201 |
html.find("field-consults") < html.find("field-writes"), |
| 5202 |
"{html}" |
| 5203 |
); |
| 5204 |
} |
| 5205 |
|
| 5206 |
#[test] |
| 5207 |
fn a_consult_waits_as_long_as_the_description_says() { |
| 5208 |
let html = fragment(&Node::Field(Box::new( |
| 5209 |
Field::new(layout::FieldKind::Text, "q", "Search").consulting( |
| 5210 |
Consult::new(Action::get("/discover/tag-suggest")) |
| 5211 |
.after(std::time::Duration::from_millis(120)), |
| 5212 |
), |
| 5213 |
))); |
| 5214 |
assert!(html.contains("delay:120ms"), "{html}"); |
| 5215 |
} |
| 5216 |
|
| 5217 |
#[test] |
| 5218 |
fn a_box_asking_two_routes_gets_a_wrapper_each() { |
| 5219 |
|
| 5220 |
|
| 5221 |
|
| 5222 |
let html = fragment(&Node::Field(Box::new( |
| 5223 |
Field::new(layout::FieldKind::Text, "q", "Search") |
| 5224 |
.consulting( |
| 5225 |
Consult::new(Action::get("/discover/suggestions").replacing("suggestions")) |
| 5226 |
.after(std::time::Duration::from_millis(200)), |
| 5227 |
) |
| 5228 |
.consulting( |
| 5229 |
Consult::new(Action::get("/discover/results").replacing("results")) |
| 5230 |
.after(std::time::Duration::from_millis(150)), |
| 5231 |
), |
| 5232 |
))); |
| 5233 |
|
| 5234 |
assert_eq!(html.matches("field-consults").count(), 2, "{html}"); |
| 5235 |
assert!(html.contains(r#"hx-get="/discover/suggestions""#), "{html}"); |
| 5236 |
assert!(html.contains(r#"hx-get="/discover/results""#), "{html}"); |
| 5237 |
assert!(html.contains("delay:200ms"), "{html}"); |
| 5238 |
assert!(html.contains("delay:150ms"), "{html}"); |
| 5239 |
} |
| 5240 |
|
| 5241 |
#[test] |
| 5242 |
fn a_question_gathers_the_controls_it_says_it_carries() { |
| 5243 |
|
| 5244 |
|
| 5245 |
|
| 5246 |
|
| 5247 |
|
| 5248 |
let html = fragment(&Node::Field(Box::new( |
| 5249 |
Field::new(layout::FieldKind::Text, "q", "Search") |
| 5250 |
.consulting(Consult::new(Action::get("/discover/results")).sending(["mode", "sort"])), |
| 5251 |
))); |
| 5252 |
|
| 5253 |
assert!( |
| 5254 |
html.contains("[name='mode'], [name='sort']"), |
| 5255 |
"{html}" |
| 5256 |
); |
| 5257 |
|
| 5258 |
|
| 5259 |
|
| 5260 |
assert!(html.contains("find input"), "{html}"); |
| 5261 |
} |
| 5262 |
|
| 5263 |
#[test] |
| 5264 |
fn a_question_that_carries_nothing_gathers_only_its_own_control() { |
| 5265 |
let html = fragment(&Node::Field(Box::new( |
| 5266 |
Field::new(layout::FieldKind::Text, "username", "Username") |
| 5267 |
.consults(Action::get("/api/validate/username")), |
| 5268 |
))); |
| 5269 |
|
| 5270 |
assert!(!html.contains("[name="), "{html}"); |
| 5271 |
} |
| 5272 |
|
| 5273 |
#[test] |
| 5274 |
fn a_consult_with_a_floor_does_not_ask_about_one_letter() { |
| 5275 |
|
| 5276 |
|
| 5277 |
|
| 5278 |
let html = fragment(&Node::Field(Box::new( |
| 5279 |
Field::new(layout::FieldKind::Text, "q", "Find a tag").consulting( |
| 5280 |
Consult::new(Action::get("/discover/tag-suggest")) |
| 5281 |
.after(std::time::Duration::from_millis(150)) |
| 5282 |
.at_least(2), |
| 5283 |
), |
| 5284 |
))); |
| 5285 |
|
| 5286 |
|
| 5287 |
|
| 5288 |
|
| 5289 |
assert!( |
| 5290 |
html.contains("hx-trigger=\"keyup[event.target.value.length>=2] changed delay:150ms\""), |
| 5291 |
"{html}" |
| 5292 |
); |
| 5293 |
} |
| 5294 |
|
| 5295 |
#[test] |
| 5296 |
fn a_select_that_consults_is_heard_on_input_rather_than_on_a_keystroke() { |
| 5297 |
|
| 5298 |
|
| 5299 |
|
| 5300 |
let html = fragment(&Node::Field(Box::new( |
| 5301 |
Field::new(layout::FieldKind::Select, "folder", "Folder").consulting( |
| 5302 |
Consult::new(Action::get("/mail/list").replacing("mail-list")) |
| 5303 |
.after(std::time::Duration::ZERO), |
| 5304 |
), |
| 5305 |
))); |
| 5306 |
assert!( |
| 5307 |
html.contains("hx-trigger=\"input changed delay:0ms\""), |
| 5308 |
"{html}" |
| 5309 |
); |
| 5310 |
assert!(html.contains("hx-get=\"/mail/list\""), "{html}"); |
| 5311 |
assert!(html.contains("field-consults"), "{html}"); |
| 5312 |
} |
| 5313 |
|
| 5314 |
#[test] |
| 5315 |
fn a_dragged_value_that_consults_is_heard_on_input_too() { |
| 5316 |
|
| 5317 |
|
| 5318 |
|
| 5319 |
let html = fragment(&Node::Field(Box::new( |
| 5320 |
Field::new(layout::FieldKind::Range, "cap", "Cap") |
| 5321 |
.consults(Action::get("/price").replacing("price")), |
| 5322 |
))); |
| 5323 |
assert!( |
| 5324 |
html.contains("hx-trigger=\"input changed delay:500ms\""), |
| 5325 |
"{html}" |
| 5326 |
); |
| 5327 |
} |
| 5328 |
|
| 5329 |
#[test] |
| 5330 |
fn a_typed_box_keeps_the_keystroke_it_had() { |
| 5331 |
|
| 5332 |
|
| 5333 |
|
| 5334 |
for kind in [ |
| 5335 |
layout::FieldKind::Text, |
| 5336 |
layout::FieldKind::Secret, |
| 5337 |
layout::FieldKind::Number, |
| 5338 |
layout::FieldKind::Email, |
| 5339 |
layout::FieldKind::Url, |
| 5340 |
layout::FieldKind::Tel, |
| 5341 |
layout::FieldKind::Textarea, |
| 5342 |
] { |
| 5343 |
let html = fragment(&Node::Field(Box::new( |
| 5344 |
Field::new(kind, "q", "Search").consults(Action::get("/find")), |
| 5345 |
))); |
| 5346 |
assert!( |
| 5347 |
html.contains("hx-trigger=\"keyup changed delay:500ms\""), |
| 5348 |
"{kind:?}: {html}" |
| 5349 |
); |
| 5350 |
} |
| 5351 |
} |
| 5352 |
|
| 5353 |
#[test] |
| 5354 |
fn a_consult_without_a_floor_asks_whatever_is_there() { |
| 5355 |
let html = fragment(&Node::Field(Box::new( |
| 5356 |
Field::new(layout::FieldKind::Text, "username", "Username") |
| 5357 |
.consults(Action::get("/api/validate/username")), |
| 5358 |
))); |
| 5359 |
|
| 5360 |
|
| 5361 |
|
| 5362 |
assert!( |
| 5363 |
html.contains("hx-trigger=\"keyup changed delay:500ms\""), |
| 5364 |
"{html}" |
| 5365 |
); |
| 5366 |
assert!(!html.contains("value.length"), "{html}"); |
| 5367 |
} |
| 5368 |
|
| 5369 |
#[test] |
| 5370 |
fn a_field_that_both_writes_and_sizes_itself_says_both_on_one_wrapper() { |
| 5371 |
let both = fragment(&Node::Field(Box::new( |
| 5372 |
Field::new(layout::FieldKind::Text, "query", "Search") |
| 5373 |
.width(layout::Width::Content) |
| 5374 |
.writes(Action::post("/search")), |
| 5375 |
))); |
| 5376 |
assert_eq!(both.matches("field-writes").count(), 1); |
| 5377 |
assert!(both.contains("data-width=\"content\"")); |
| 5378 |
} |
| 5379 |
|
| 5380 |
|
| 5381 |
|
| 5382 |
|
| 5383 |
|
| 5384 |
|
| 5385 |
|
| 5386 |
|
| 5387 |
fn every_shape_that_could_cache() -> Screen { |
| 5388 |
Screen::sidebar_content("Any width").with( |
| 5389 |
Slot::new("main", RegionKind::Pane) |
| 5390 |
.with(Node::Field(Box::new(Field::new( |
| 5391 |
layout::FieldKind::Text, |
| 5392 |
"wide", |
| 5393 |
"Wide", |
| 5394 |
)))) |
| 5395 |
.with(Node::Field(Box::new( |
| 5396 |
Field::new(layout::FieldKind::Text, "tight", "Tight").width(layout::Width::Content), |
| 5397 |
))) |
| 5398 |
.with(Node::Field(Box::new( |
| 5399 |
Field::new(layout::FieldKind::Text, "held", "Held").width(layout::Width::Fixed), |
| 5400 |
))) |
| 5401 |
.with(Node::Table { |
| 5402 |
marks: ::quasi_router::stage::Marks::none(), |
| 5403 |
columns: vec![ |
| 5404 |
Column::new("Name").priority(layout::Priority::Essential), |
| 5405 |
Column::new("Kind").priority(layout::Priority::Secondary), |
| 5406 |
Column::new("Added").priority(layout::Priority::Optional), |
| 5407 |
], |
| 5408 |
rows: vec![Row::cells(["kick.wav", "sample", "2026-08-12"])], |
| 5409 |
more: Some(Rest::more(1, Action::get("/samples?from=1"))), |
| 5410 |
}) |
| 5411 |
.with(Node::Table { |
| 5412 |
marks: ::quasi_router::stage::Marks::none(), |
| 5413 |
columns: Vec::new(), |
| 5414 |
rows: vec![Row::new("one"), Row::new("two")], |
| 5415 |
more: Some(Rest::more(2, Action::get("/rows?from=2"))), |
| 5416 |
}) |
| 5417 |
.with(Node::Region( |
| 5418 |
Slot::group("nested").with(Node::text("inside")), |
| 5419 |
)), |
| 5420 |
) |
| 5421 |
} |
| 5422 |
|
| 5423 |
#[test] |
| 5424 |
fn the_markup_is_not_a_function_of_the_width() { |
| 5425 |
|
| 5426 |
|
| 5427 |
|
| 5428 |
|
| 5429 |
|
| 5430 |
|
| 5431 |
|
| 5432 |
|
| 5433 |
|
| 5434 |
|
| 5435 |
|
| 5436 |
|
| 5437 |
let screen = every_shape_that_could_cache(); |
| 5438 |
|
| 5439 |
let reused = Webview::new(); |
| 5440 |
let first = reused.screen(&screen); |
| 5441 |
for _ in 0..4 { |
| 5442 |
assert_eq!(reused.screen(&screen), first); |
| 5443 |
} |
| 5444 |
assert_eq!(Webview::new().screen(&screen), first); |
| 5445 |
|
| 5446 |
|
| 5447 |
|
| 5448 |
|
| 5449 |
|
| 5450 |
for style in first.split("style=\"").skip(1) { |
| 5451 |
let value = style.split('"').next().expect("an attribute closes"); |
| 5452 |
for unit in ["px", "vw", "vh", "ch", "pt"] { |
| 5453 |
assert!(!value.contains(unit), "{unit} in {value}"); |
| 5454 |
} |
| 5455 |
assert!(value.contains("fr"), "{value}"); |
| 5456 |
} |
| 5457 |
} |
| 5458 |
|
| 5459 |
#[test] |
| 5460 |
fn a_region_member_says_when_it_drops() { |
| 5461 |
|
| 5462 |
|
| 5463 |
|
| 5464 |
|
| 5465 |
let screen = Screen::sidebar_content("Toolbar").with( |
| 5466 |
Slot::new("bar", quasi_router::RegionKind::Band) |
| 5467 |
.with(Node::text("Library")) |
| 5468 |
.with_ranked(Node::text("Filter"), layout::Priority::Secondary) |
| 5469 |
.with_ranked(Node::text("Sort"), layout::Priority::Optional), |
| 5470 |
); |
| 5471 |
let html = render(&screen); |
| 5472 |
|
| 5473 |
|
| 5474 |
|
| 5475 |
|
| 5476 |
assert!(html.contains("cell-drops-next"), "{html}"); |
| 5477 |
assert!(html.contains("cell-drops-first"), "{html}"); |
| 5478 |
|
| 5479 |
|
| 5480 |
|
| 5481 |
assert_eq!(html.matches("<div class=\"cell-drops").count(), 2); |
| 5482 |
assert!(html.contains("<p class=\"text\">Library</p>"), "{html}"); |
| 5483 |
} |
| 5484 |
|
| 5485 |
#[test] |
| 5486 |
fn an_awaiting_control_locks_itself_while_it_waits() { |
| 5487 |
|
| 5488 |
|
| 5489 |
let html = fragment(&Node::Act(Act::new( |
| 5490 |
"Buy", |
| 5491 |
Action::post("/checkout").awaiting(), |
| 5492 |
))); |
| 5493 |
assert!(html.contains(r#"hx-disable="this""#), "{html}"); |
| 5494 |
assert!(html.contains(r#"data-awaiting="indeterminate""#), "{html}"); |
| 5495 |
assert!(!html.contains("data-awaiting-amount"), "{html}"); |
| 5496 |
|
| 5497 |
|
| 5498 |
|
| 5499 |
let plain = fragment(&Node::Act(Act::new("Save", Action::post("/save")))); |
| 5500 |
assert!(!plain.contains("hx-disable"), "{plain}"); |
| 5501 |
assert!(!plain.contains("data-awaiting"), "{plain}"); |
| 5502 |
} |
| 5503 |
|
| 5504 |
#[test] |
| 5505 |
fn an_awaiting_form_locks_its_button_and_not_the_form() { |
| 5506 |
|
| 5507 |
|
| 5508 |
|
| 5509 |
|
| 5510 |
|
| 5511 |
|
| 5512 |
|
| 5513 |
let html = fragment(&Node::Form { |
| 5514 |
marks: ::quasi_router::stage::Marks::none(), |
| 5515 |
action: Action::post("/api/users/me/ssh-keys").awaiting(), |
| 5516 |
submit: "Add SSH Key".into(), |
| 5517 |
fields: vec![Field::new(layout::FieldKind::Text, "label", "Label")], |
| 5518 |
}); |
| 5519 |
assert!( |
| 5520 |
html.contains(r#"hx-disable="find button[type='submit']""#), |
| 5521 |
"{html}" |
| 5522 |
); |
| 5523 |
assert!(!html.contains(r#"hx-disable="this""#), "{html}"); |
| 5524 |
assert!(html.contains(r#"data-awaiting="indeterminate""#), "{html}"); |
| 5525 |
|
| 5526 |
|
| 5527 |
assert!(html.contains(r#"<button type="submit""#), "{html}"); |
| 5528 |
|
| 5529 |
|
| 5530 |
|
| 5531 |
assert!(html.contains(r#"<input type="text""#), "{html}"); |
| 5532 |
assert!(!html.contains("<input disabled"), "{html}"); |
| 5533 |
} |
| 5534 |
|
| 5535 |
#[test] |
| 5536 |
fn a_measured_wait_carries_its_amount_and_never_a_duration() { |
| 5537 |
let html = fragment(&Node::Act(Act::new( |
| 5538 |
"Upload", |
| 5539 |
Action::post("/media").awaiting_amount(41_943_040), |
| 5540 |
))); |
| 5541 |
assert!(html.contains(r#"data-awaiting="determinate""#), "{html}"); |
| 5542 |
assert!( |
| 5543 |
html.contains(r#"data-awaiting-amount="41943040""#), |
| 5544 |
"{html}" |
| 5545 |
); |
| 5546 |
|
| 5547 |
|
| 5548 |
assert!(!html.contains("duration"), "{html}"); |
| 5549 |
assert!(!html.contains("eta"), "{html}"); |
| 5550 |
} |
| 5551 |
|
| 5552 |
#[test] |
| 5553 |
fn a_tab_strip_and_what_shares_its_row_land_in_one_flex_row() { |
| 5554 |
|
| 5555 |
|
| 5556 |
|
| 5557 |
|
| 5558 |
let screen = Screen::sidebar_content("Work").with( |
| 5559 |
Slot::new("work-view", RegionKind::TabGroup) |
| 5560 |
.across(Run::new(layout::Fallback::Menu).beside( |
| 5561 |
Node::Region(Slot::new("work-toolbar", RegionKind::Band)), |
| 5562 |
layout::Priority::Secondary, |
| 5563 |
)) |
| 5564 |
.frame("Tasks", Node::Region(Slot::new("tasks", RegionKind::Pane))) |
| 5565 |
.showing_one(0), |
| 5566 |
); |
| 5567 |
|
| 5568 |
let html = render(&screen); |
| 5569 |
assert!(html.contains("class=\"run run-menu\""), "{html}"); |
| 5570 |
|
| 5571 |
|
| 5572 |
let run = html |
| 5573 |
.split("class=\"run run-menu\"") |
| 5574 |
.nth(1) |
| 5575 |
.expect("the row"); |
| 5576 |
let strip = run.find("Tasks").expect("the strip is in the row"); |
| 5577 |
let toolbar = run.find("work-toolbar").expect("the member is in the row"); |
| 5578 |
assert!(strip < toolbar, "{run}"); |
| 5579 |
assert!(!html.contains("position:"), "{html}"); |
| 5580 |
} |
| 5581 |
|
| 5582 |
#[test] |
| 5583 |
fn a_region_with_no_run_emits_exactly_the_markup_it_always_did() { |
| 5584 |
|
| 5585 |
let bare = Screen::sidebar_content("Work").with( |
| 5586 |
Slot::new("work-view", RegionKind::TabGroup) |
| 5587 |
.frame("Tasks", Node::Region(Slot::new("tasks", RegionKind::Pane))), |
| 5588 |
); |
| 5589 |
|
| 5590 |
assert!(!render(&bare).contains("class=\"run")); |
| 5591 |
} |
| 5592 |
|
| 5593 |
#[test] |
| 5594 |
fn a_region_fed_by_a_call_asks_for_itself() { |
| 5595 |
|
| 5596 |
|
| 5597 |
let screen = Screen::list_detail("Payments", false).with( |
| 5598 |
Slot::new("payouts", RegionKind::Pane).fed_by(Action::get("/dashboard/payouts").awaiting()), |
| 5599 |
); |
| 5600 |
let html = render(&screen); |
| 5601 |
assert!(html.contains(r#"hx-trigger="load""#), "{html}"); |
| 5602 |
assert!(html.contains(r#"hx-get="/dashboard/payouts""#), "{html}"); |
| 5603 |
|
| 5604 |
|
| 5605 |
assert!(html.contains(r#"aria-busy="true""#), "{html}"); |
| 5606 |
|
| 5607 |
assert!(!html.contains(r#"href="/dashboard/payouts""#), "{html}"); |
| 5608 |
|
| 5609 |
|
| 5610 |
assert!(!html.contains("hx-target"), "{html}"); |
| 5611 |
} |
| 5612 |
|
| 5613 |
#[test] |
| 5614 |
fn a_live_region_asks_again_on_the_renderer_cadence() { |
| 5615 |
|
| 5616 |
|
| 5617 |
|
| 5618 |
|
| 5619 |
let screen = Screen::list_detail("Admin", false).with( |
| 5620 |
Slot::new("queue", RegionKind::Pane) |
| 5621 |
.fed_by(Action::get("/admin/queue")) |
| 5622 |
.live(), |
| 5623 |
); |
| 5624 |
let html = render(&screen); |
| 5625 |
let expected = format!(r#"hx-trigger="load, every {}s""#, crate::CADENCE.as_secs()); |
| 5626 |
assert!(html.contains(&expected), "{html}"); |
| 5627 |
assert!(html.contains(r#"hx-get="/admin/queue""#), "{html}"); |
| 5628 |
|
| 5629 |
|
| 5630 |
assert!(!html.contains(r#"hx-trigger="every"#), "{html}"); |
| 5631 |
|
| 5632 |
assert!(!html.contains(r#"href="/admin/queue""#), "{html}"); |
| 5633 |
} |
| 5634 |
|
| 5635 |
#[test] |
| 5636 |
fn a_still_region_asks_once_as_it_always_did() { |
| 5637 |
|
| 5638 |
|
| 5639 |
|
| 5640 |
let screen = Screen::list_detail("Payments", false) |
| 5641 |
.with(Slot::new("payouts", RegionKind::Pane).fed_by(Action::get("/dashboard/payouts"))); |
| 5642 |
let html = render(&screen); |
| 5643 |
assert!(html.contains(r#"hx-trigger="load""#), "{html}"); |
| 5644 |
assert!(!html.contains("every"), "{html}"); |
| 5645 |
} |
| 5646 |
|
| 5647 |
|
| 5648 |
|
| 5649 |
#[test] |
| 5650 |
fn a_mount_that_declares_no_frame_draws_none() { |
| 5651 |
|
| 5652 |
|
| 5653 |
let html = render( |
| 5654 |
&Screen::list_detail("Compose", false) |
| 5655 |
.with(Slot::new("body", RegionKind::Pane).with(Node::text("body"))), |
| 5656 |
); |
| 5657 |
assert!(!html.contains("frame"), "{html}"); |
| 5658 |
} |
| 5659 |
|
| 5660 |
#[test] |
| 5661 |
fn a_framed_mount_offers_its_verbs_outside_the_screen() { |
| 5662 |
|
| 5663 |
|
| 5664 |
|
| 5665 |
|
| 5666 |
let webview = Webview::new().with_frame( |
| 5667 |
Frame::new() |
| 5668 |
.offering(Act::new("Send", Action::post("/compose/send"))) |
| 5669 |
.offering( |
| 5670 |
Act::new("Discard", Action::post("/compose/discard")) |
| 5671 |
.confirm("Discard this draft?"), |
| 5672 |
) |
| 5673 |
.reporting(), |
| 5674 |
); |
| 5675 |
let html = webview.screen( |
| 5676 |
&Screen::list_detail("Compose", false) |
| 5677 |
.with(Slot::new("body", RegionKind::Pane).with(Node::text("body"))), |
| 5678 |
); |
| 5679 |
|
| 5680 |
let main_ends = html.find("</main>").expect("a screen has a main"); |
| 5681 |
let verbs = html.find("Send").expect("the verb is drawn"); |
| 5682 |
assert!(verbs > main_ends, "the frame outlives the screen inside it"); |
| 5683 |
|
| 5684 |
assert!(html.contains("hx-post=\"/compose/send\""), "{html}"); |
| 5685 |
|
| 5686 |
|
| 5687 |
assert!( |
| 5688 |
html.contains("hx-confirm=\"Discard this draft?\""), |
| 5689 |
"{html}" |
| 5690 |
); |
| 5691 |
} |
| 5692 |
|
| 5693 |
#[test] |
| 5694 |
fn a_mount_that_reports_has_somewhere_for_a_banner_to_rest() { |
| 5695 |
let webview = Webview::new().with_frame(Frame::new().reporting()); |
| 5696 |
let html = webview.screen( |
| 5697 |
&Screen::list_detail("Compose", false) |
| 5698 |
.with(Slot::new("body", RegionKind::Pane).with(Node::text("body"))), |
| 5699 |
); |
| 5700 |
|
| 5701 |
assert!(html.contains(crate::frame::STATUS_ID), "{html}"); |
| 5702 |
|
| 5703 |
|
| 5704 |
assert!(html.contains("aria-live=\"polite\""), "{html}"); |
| 5705 |
} |
| 5706 |
|
| 5707 |
#[test] |
| 5708 |
fn a_mount_that_only_offers_verbs_has_no_status_line() { |
| 5709 |
|
| 5710 |
|
| 5711 |
let webview = Webview::new() |
| 5712 |
.with_frame(Frame::new().offering(Act::new("Cancel", Action::post("/close")))); |
| 5713 |
let html = webview.screen( |
| 5714 |
&Screen::list_detail("Compose", false) |
| 5715 |
.with(Slot::new("body", RegionKind::Pane).with(Node::text("body"))), |
| 5716 |
); |
| 5717 |
|
| 5718 |
assert!(html.contains("Cancel"), "{html}"); |
| 5719 |
assert!(!html.contains(crate::frame::STATUS_ID), "{html}"); |
| 5720 |
} |
| 5721 |
|
| 5722 |
#[test] |
| 5723 |
fn a_banner_rests_in_the_frame_and_a_toast_still_floats() { |
| 5724 |
|
| 5725 |
|
| 5726 |
|
| 5727 |
let mut screen = Screen::list_detail("Compose", false) |
| 5728 |
.with(Slot::new("body", RegionKind::Pane).with(Node::text("body"))); |
| 5729 |
screen |
| 5730 |
.notices |
| 5731 |
.push(Node::banner(quasi_router::layout::Tone::Danger, "Not sent")); |
| 5732 |
screen.notices.push(Node::Notice { |
| 5733 |
kind: quasi_router::layout::Notice::Toast, |
| 5734 |
tone: quasi_router::layout::Tone::Info, |
| 5735 |
text: "Saved".into(), |
| 5736 |
act: None, |
| 5737 |
}); |
| 5738 |
|
| 5739 |
let html = Webview::new() |
| 5740 |
.with_frame(Frame::new().reporting()) |
| 5741 |
.screen(&screen); |
| 5742 |
|
| 5743 |
let status = html.find(crate::frame::STATUS_ID).expect("a status line"); |
| 5744 |
let banner = html.find("Not sent").expect("the banner is drawn"); |
| 5745 |
let toast = html.find("Saved").expect("the toast is drawn"); |
| 5746 |
|
| 5747 |
assert!(banner > status, "a banner rests in the frame"); |
| 5748 |
assert!( |
| 5749 |
toast < status, |
| 5750 |
"a toast floats over the screen as it always did" |
| 5751 |
); |
| 5752 |
} |
| 5753 |
|
| 5754 |
#[test] |
| 5755 |
fn an_unreporting_mount_leaves_both_kinds_where_they_were() { |
| 5756 |
|
| 5757 |
|
| 5758 |
let mut screen = Screen::list_detail("Compose", false) |
| 5759 |
.with(Slot::new("body", RegionKind::Pane).with(Node::text("body"))); |
| 5760 |
screen |
| 5761 |
.notices |
| 5762 |
.push(Node::banner(quasi_router::layout::Tone::Danger, "Not sent")); |
| 5763 |
|
| 5764 |
let framed = Webview::new() |
| 5765 |
.with_frame(Frame::new().offering(Act::new("Cancel", Action::post("/close")))) |
| 5766 |
.screen(&screen); |
| 5767 |
let bare = render(&screen); |
| 5768 |
|
| 5769 |
let notices = framed.find("notices").expect("the notices block"); |
| 5770 |
assert!(framed.find("Not sent").expect("drawn") > notices); |
| 5771 |
assert!(bare.contains("Not sent")); |
| 5772 |
} |
| 5773 |
|
| 5774 |
#[test] |
| 5775 |
fn a_readout_carries_the_instant_and_which_way_it_runs() { |
| 5776 |
|
| 5777 |
|
| 5778 |
let started = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000); |
| 5779 |
let html = fragment(&Node::since(started)); |
| 5780 |
|
| 5781 |
assert!(html.contains("data-clock=\"since\""), "{html}"); |
| 5782 |
assert!(html.contains("data-at=\"1000000000\""), "{html}"); |
| 5783 |
assert!(html.contains("class=\"clock\""), "{html}"); |
| 5784 |
assert!(!html.contains("></span>"), "the first words are emitted"); |
| 5785 |
|
| 5786 |
assert!(fragment(&Node::until(started)).contains("data-clock=\"until\"")); |
| 5787 |
assert!(fragment(&Node::age(started)).contains("data-clock=\"age\"")); |
| 5788 |
} |
| 5789 |
|
| 5790 |
#[test] |
| 5791 |
fn a_readout_sits_in_a_row_and_in_a_cell() { |
| 5792 |
|
| 5793 |
|
| 5794 |
let started = std::time::SystemTime::now(); |
| 5795 |
let row = fragment(&Node::list([ |
| 5796 |
Row::new("First").part(layout::RowPart::Meta, Node::since(started)) |
| 5797 |
])); |
| 5798 |
assert!(row.contains("data-clock=\"since\""), "{row}"); |
| 5799 |
|
| 5800 |
let table = fragment(&Node::Table { |
| 5801 |
marks: ::quasi_router::stage::Marks::none(), |
| 5802 |
columns: vec![Column::new("title"), Column::new("elapsed")], |
| 5803 |
rows: vec![Row::cells([ |
| 5804 |
Cell::new("First"), |
| 5805 |
Cell { |
| 5806 |
content: vec![Node::since(started)], |
| 5807 |
..Cell::default() |
| 5808 |
}, |
| 5809 |
])], |
| 5810 |
more: None, |
| 5811 |
}); |
| 5812 |
assert!(table.contains("data-clock=\"since\""), "{table}"); |
| 5813 |
} |
| 5814 |
|
| 5815 |
#[test] |
| 5816 |
fn the_clock_script_is_linked_and_can_be_left_out() { |
| 5817 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 5818 |
assert!(with.contains("quasi-clock.js"), "{with}"); |
| 5819 |
|
| 5820 |
|
| 5821 |
|
| 5822 |
let without = Webview::new() |
| 5823 |
.with_shell(Shell::default().without_clock()) |
| 5824 |
.screen(&Screen::list_detail("A", false)); |
| 5825 |
assert!(!without.contains("quasi-clock.js"), "{without}"); |
| 5826 |
|
| 5827 |
|
| 5828 |
|
| 5829 |
|
| 5830 |
let embed = Webview::new() |
| 5831 |
.with_shell(Shell::default().without_htmx()) |
| 5832 |
.screen(&Screen::list_detail("A", false)); |
| 5833 |
assert!(embed.contains("quasi-clock.js"), "{embed}"); |
| 5834 |
assert!(!embed.contains("quasi-selection.js"), "{embed}"); |
| 5835 |
} |
| 5836 |
|
| 5837 |
#[test] |
| 5838 |
fn the_runtime_for_what_the_emitter_writes_is_linked_and_can_be_left_out() { |
| 5839 |
|
| 5840 |
|
| 5841 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 5842 |
assert!(with.contains("_hyperscript.min.js"), "{with}"); |
| 5843 |
|
| 5844 |
let without = Webview::new() |
| 5845 |
.with_shell(Shell::default().without_hyperscript()) |
| 5846 |
.screen(&Screen::list_detail("A", false)); |
| 5847 |
assert!(!without.contains("_hyperscript.min.js"), "{without}"); |
| 5848 |
|
| 5849 |
|
| 5850 |
|
| 5851 |
|
| 5852 |
let embed = Webview::new() |
| 5853 |
.with_shell(Shell::default().without_htmx()) |
| 5854 |
.screen(&Screen::list_detail("A", false)); |
| 5855 |
assert!(embed.contains("_hyperscript.min.js"), "{embed}"); |
| 5856 |
} |
| 5857 |
|
| 5858 |
#[test] |
| 5859 |
fn a_toast_carries_the_hook_the_script_takes_it_away_by_and_a_banner_does_not() { |
| 5860 |
|
| 5861 |
|
| 5862 |
|
| 5863 |
|
| 5864 |
let toast = fragment(&Node::toast(layout::Tone::Success, "Saved")); |
| 5865 |
assert!(toast.contains(r#"data-notice="toast""#), "{toast}"); |
| 5866 |
|
| 5867 |
let banner = fragment(&Node::banner(layout::Tone::Danger, "Not sent")); |
| 5868 |
assert!(!banner.contains("data-notice"), "{banner}"); |
| 5869 |
|
| 5870 |
|
| 5871 |
|
| 5872 |
let emit = crate::Emit { |
| 5873 |
class_prefix: "qs-", |
| 5874 |
..crate::Emit::default() |
| 5875 |
}; |
| 5876 |
let prefixed = Webview::new() |
| 5877 |
.with_emit(emit) |
| 5878 |
.fragment(&Node::toast(layout::Tone::Success, "Saved")); |
| 5879 |
assert!(prefixed.contains(r#"data-notice="toast""#), "{prefixed}"); |
| 5880 |
assert!(prefixed.contains("qs-toast"), "{prefixed}"); |
| 5881 |
} |
| 5882 |
|
| 5883 |
#[test] |
| 5884 |
fn the_clock_script_takes_a_toast_away_on_this_renderers_own_schedule() { |
| 5885 |
assert!(crate::CLOCK_JS.contains("data-notice"), "the toast hook"); |
| 5886 |
assert!(crate::CLOCK_JS.contains("remove()"), "and it is taken away"); |
| 5887 |
|
| 5888 |
|
| 5889 |
|
| 5890 |
assert!( |
| 5891 |
crate::CLOCK_JS.contains("--timing-dismiss"), |
| 5892 |
"named, not numbered" |
| 5893 |
); |
| 5894 |
} |
| 5895 |
|
| 5896 |
|
| 5897 |
|
| 5898 |
|
| 5899 |
|
| 5900 |
|
| 5901 |
#[test] |
| 5902 |
fn the_clock_scripts_fallback_is_the_duration_the_crate_names() { |
| 5903 |
let named = format!( |
| 5904 |
"LINGER_FALLBACK = {};", |
| 5905 |
makeover_timing::Intent::Dismiss.ms() |
| 5906 |
); |
| 5907 |
assert!(crate::CLOCK_JS.contains(&named), "{named}"); |
| 5908 |
|
| 5909 |
|
| 5910 |
let fade = format!("FADE_FALLBACK = {};", makeover_timing::Motion::Fade.ms()); |
| 5911 |
assert!(crate::CLOCK_JS.contains(&fade), "{fade}"); |
| 5912 |
} |
| 5913 |
|
| 5914 |
|
| 5915 |
|
| 5916 |
|
| 5917 |
|
| 5918 |
|
| 5919 |
#[test] |
| 5920 |
fn a_toast_leaves_before_it_is_removed_rather_than_vanishing_at_dismiss() { |
| 5921 |
assert!( |
| 5922 |
crate::CLOCK_JS.contains("--motion-fade"), |
| 5923 |
"the leaving duration is read, not numbered" |
| 5924 |
); |
| 5925 |
assert!( |
| 5926 |
crate::CLOCK_JS.contains("data-leaving"), |
| 5927 |
"and the attribute makeover-webview's rule transitions on is set" |
| 5928 |
); |
| 5929 |
} |
| 5930 |
|
| 5931 |
|
| 5932 |
|
| 5933 |
|
| 5934 |
|
| 5935 |
|
| 5936 |
|
| 5937 |
#[test] |
| 5938 |
fn the_leaving_step_cannot_strand_a_toast_under_reduced_motion() { |
| 5939 |
|
| 5940 |
|
| 5941 |
|
| 5942 |
assert!( |
| 5943 |
!crate::CLOCK_JS.contains("addEventListener(\"transitionend"), |
| 5944 |
"a transition that never fires is a toast that never leaves" |
| 5945 |
); |
| 5946 |
assert!( |
| 5947 |
!crate::CLOCK_JS.contains("ontransitionend"), |
| 5948 |
"and not by property either" |
| 5949 |
); |
| 5950 |
} |
| 5951 |
|
| 5952 |
#[test] |
| 5953 |
fn the_clock_script_reads_only_the_hooks_this_crate_emits() { |
| 5954 |
|
| 5955 |
|
| 5956 |
|
| 5957 |
assert!(crate::CLOCK_JS.contains("data-clock"), "the kind hook"); |
| 5958 |
assert!(crate::CLOCK_JS.contains("data-at"), "the instant hook"); |
| 5959 |
for kind in ["since", "until", "age"] { |
| 5960 |
assert!(crate::CLOCK_JS.contains(kind), "the script knows {kind}"); |
| 5961 |
} |
| 5962 |
|
| 5963 |
|
| 5964 |
|
| 5965 |
for word in ["task", "email", "contact", "goingson"] { |
| 5966 |
assert!( |
| 5967 |
!crate::CLOCK_JS.to_lowercase().contains(word), |
| 5968 |
"the script names {word}" |
| 5969 |
); |
| 5970 |
} |
| 5971 |
} |
| 5972 |
|
| 5973 |
#[test] |
| 5974 |
fn the_shell_serves_the_clock_script_from_where_the_host_says() { |
| 5975 |
let html = Webview::under("/assets").screen(&Screen::list_detail("A", false)); |
| 5976 |
assert!(html.contains("src=\"/assets/quasi-clock.js\""), "{html}"); |
| 5977 |
} |
| 5978 |
|
| 5979 |
|
| 5980 |
|
| 5981 |
#[test] |
| 5982 |
fn a_field_that_owns_a_suggestion_list_is_a_combobox() { |
| 5983 |
let html = fragment(&Node::Field(Box::new( |
| 5984 |
Field::new(layout::FieldKind::Text, "q", "Search").suggesting( |
| 5985 |
Consult::new(Action::get("/discover/suggestions")) |
| 5986 |
.after(std::time::Duration::from_millis(200)) |
| 5987 |
.at_least(2), |
| 5988 |
), |
| 5989 |
))); |
| 5990 |
|
| 5991 |
assert!(html.contains("field-suggests"), "{html}"); |
| 5992 |
assert!(html.contains(r#"hx-get="/discover/suggestions""#), "{html}"); |
| 5993 |
assert!(html.contains("delay:200ms"), "{html}"); |
| 5994 |
|
| 5995 |
assert!(html.contains(r##"hx-target="#q-suggestions""##), "{html}"); |
| 5996 |
assert!(html.contains(r#"id="q-suggestions""#), "{html}"); |
| 5997 |
assert!(html.contains(r#"role="listbox""#), "{html}"); |
| 5998 |
assert!(html.contains(r#"role="combobox""#), "{html}"); |
| 5999 |
assert!(html.contains(r#"aria-controls="q-suggestions""#), "{html}"); |
| 6000 |
assert!(html.contains(r#"aria-expanded="false""#), "{html}"); |
| 6001 |
|
| 6002 |
assert!(html.contains(r#"aria-label="Search""#), "{html}"); |
| 6003 |
} |
| 6004 |
|
| 6005 |
|
| 6006 |
|
| 6007 |
|
| 6008 |
|
| 6009 |
#[test] |
| 6010 |
fn a_region_recomputes_from_the_dials_inside_it() { |
| 6011 |
let html = render( |
| 6012 |
&Screen::list_detail("Pricing", false).with( |
| 6013 |
Slot::group("pricing-calculator") |
| 6014 |
.with(Node::Field(Box::new(Field::new( |
| 6015 |
layout::FieldKind::Number, |
| 6016 |
"item_price", |
| 6017 |
"Price per item", |
| 6018 |
)))) |
| 6019 |
.with(Node::Region( |
| 6020 |
Slot::group("results-panel").with(Node::text("You keep $9.00")), |
| 6021 |
)) |
| 6022 |
.consulting( |
| 6023 |
Consult::new(Action::get("/pricing/compare").replacing("results-panel")) |
| 6024 |
.after(std::time::Duration::from_millis(300)), |
| 6025 |
), |
| 6026 |
), |
| 6027 |
); |
| 6028 |
|
| 6029 |
assert!(html.contains("region-consults"), "{html}"); |
| 6030 |
assert!(html.contains(r#"hx-get="/pricing/compare""#), "{html}"); |
| 6031 |
assert!(html.contains(r##"hx-target="#results-panel""##), "{html}"); |
| 6032 |
|
| 6033 |
|
| 6034 |
assert!( |
| 6035 |
html.contains(r#"hx-trigger="input changed delay:300ms""#), |
| 6036 |
"{html}" |
| 6037 |
); |
| 6038 |
|
| 6039 |
|
| 6040 |
assert!( |
| 6041 |
html.contains(r#"hx-include="find input, find select, find textarea""#), |
| 6042 |
"{html}" |
| 6043 |
); |
| 6044 |
|
| 6045 |
|
| 6046 |
let wrapper = html.find("region-consults").unwrap(); |
| 6047 |
let region = html.find(r#"id="pricing-calculator""#).unwrap(); |
| 6048 |
assert!(wrapper < region, "{html}"); |
| 6049 |
} |
| 6050 |
|
| 6051 |
|
| 6052 |
|
| 6053 |
#[test] |
| 6054 |
fn a_region_consult_carries_what_it_says_it_carries_from_outside() { |
| 6055 |
let html = render( |
| 6056 |
&Screen::list_detail("Pricing", false).with( |
| 6057 |
Slot::group("calculator") |
| 6058 |
.with(Node::Field(Box::new(Field::new( |
| 6059 |
layout::FieldKind::Number, |
| 6060 |
"sales", |
| 6061 |
"Sales per month", |
| 6062 |
)))) |
| 6063 |
.consulting( |
| 6064 |
Consult::new(Action::get("/pricing/compare").replacing("results")) |
| 6065 |
.sending(["tier"]), |
| 6066 |
), |
| 6067 |
), |
| 6068 |
); |
| 6069 |
|
| 6070 |
|
| 6071 |
|
| 6072 |
|
| 6073 |
assert!( |
| 6074 |
html.contains( |
| 6075 |
r#"hx-include="find input, find select, find textarea, [name='tier']""# |
| 6076 |
), |
| 6077 |
"{html}" |
| 6078 |
); |
| 6079 |
} |
| 6080 |
|
| 6081 |
|
| 6082 |
|
| 6083 |
#[test] |
| 6084 |
fn a_region_asking_twice_is_two_wrappers() { |
| 6085 |
let html = render( |
| 6086 |
&Screen::list_detail("Pricing", false).with( |
| 6087 |
Slot::group("calculator") |
| 6088 |
.with(Node::Field(Box::new(Field::new( |
| 6089 |
layout::FieldKind::Number, |
| 6090 |
"sales", |
| 6091 |
"Sales", |
| 6092 |
)))) |
| 6093 |
.consulting(Consult::new( |
| 6094 |
Action::get("/pricing/compare").replacing("results"), |
| 6095 |
)) |
| 6096 |
.consulting(Consult::new( |
| 6097 |
Action::get("/pricing/chart").replacing("chart"), |
| 6098 |
)), |
| 6099 |
), |
| 6100 |
); |
| 6101 |
|
| 6102 |
assert_eq!(html.matches("region-consults").count(), 2, "{html}"); |
| 6103 |
assert!(html.contains(r##"hx-target="#results""##), "{html}"); |
| 6104 |
assert!(html.contains(r##"hx-target="#chart""##), "{html}"); |
| 6105 |
} |
| 6106 |
|
| 6107 |
|
| 6108 |
|
| 6109 |
|
| 6110 |
#[test] |
| 6111 |
fn owning_a_list_is_not_the_same_as_asking_a_question() { |
| 6112 |
let html = fragment(&Node::Field(Box::new( |
| 6113 |
Field::new(layout::FieldKind::Text, "q", "Search") |
| 6114 |
.suggesting(Consult::new(Action::get("/discover/suggestions")).at_least(2)) |
| 6115 |
.consulting( |
| 6116 |
Consult::new(Action::get("/discover/results").replacing("results")) |
| 6117 |
.sending(["mode", "sort"]), |
| 6118 |
), |
| 6119 |
))); |
| 6120 |
|
| 6121 |
assert_eq!(html.matches("field-suggests").count(), 1, "{html}"); |
| 6122 |
assert_eq!(html.matches("field-consults").count(), 1, "{html}"); |
| 6123 |
assert!(html.contains(r##"hx-target="#q-suggestions""##), "{html}"); |
| 6124 |
assert!(html.contains(r##"hx-target="#results""##), "{html}"); |
| 6125 |
} |
| 6126 |
|
| 6127 |
|
| 6128 |
|
| 6129 |
|
| 6130 |
#[test] |
| 6131 |
fn the_answer_to_a_suggestion_route_is_the_inside_of_the_list() { |
| 6132 |
let html = Webview::new().suggestions( |
| 6133 |
"q", |
| 6134 |
&[ |
| 6135 |
Candidate::new("rust-lang", "Rust"), |
| 6136 |
Candidate::plain("format").detailed("Audio"), |
| 6137 |
], |
| 6138 |
); |
| 6139 |
|
| 6140 |
assert!(html.contains(r#"role="option""#), "{html}"); |
| 6141 |
assert!(html.contains(r#"id="q-suggestions-0""#), "{html}"); |
| 6142 |
assert!(html.contains(r#"data-at="0""#), "{html}"); |
| 6143 |
assert!(html.contains(r#"data-value="rust-lang""#), "{html}"); |
| 6144 |
assert!(html.contains(">Rust<"), "{html}"); |
| 6145 |
|
| 6146 |
|
| 6147 |
assert!(html.contains("form-suggestion-detail"), "{html}"); |
| 6148 |
assert!(html.contains(">Audio<"), "{html}"); |
| 6149 |
|
| 6150 |
|
| 6151 |
assert_eq!(html.matches("on click set box").count(), 2, "{html}"); |
| 6152 |
assert!(!html.contains("hx-get"), "{html}"); |
| 6153 |
} |
| 6154 |
|
| 6155 |
|
| 6156 |
|
| 6157 |
|
| 6158 |
#[test] |
| 6159 |
fn a_candidate_that_carries_an_action_does_not_write_its_value() { |
| 6160 |
let html = Webview::new().suggestions( |
| 6161 |
"q", |
| 6162 |
&[ |
| 6163 |
Candidate::new("rust-lang", "Rust").picking(Action::get("/projects/rust-lang")), |
| 6164 |
Candidate::plain("ruby"), |
| 6165 |
], |
| 6166 |
); |
| 6167 |
|
| 6168 |
|
| 6169 |
|
| 6170 |
assert!(html.contains(r#"hx-get="/projects/rust-lang""#), "{html}"); |
| 6171 |
assert!(!html.contains(r#"data-value="rust-lang""#), "{html}"); |
| 6172 |
|
| 6173 |
|
| 6174 |
|
| 6175 |
assert!(html.contains("set @aria-expanded of box"), "{html}"); |
| 6176 |
assert_eq!(html.matches("set the value of box").count(), 1, "{html}"); |
| 6177 |
|
| 6178 |
|
| 6179 |
|
| 6180 |
assert!(html.contains(r#"data-value="ruby""#), "{html}"); |
| 6181 |
assert_eq!( |
| 6182 |
html.matches("set picked to @data-value").count(), |
| 6183 |
1, |
| 6184 |
"{html}" |
| 6185 |
); |
| 6186 |
} |
| 6187 |
|
| 6188 |
|
| 6189 |
|
| 6190 |
|
| 6191 |
#[test] |
| 6192 |
fn a_candidate_whose_pick_navigates_is_a_link() { |
| 6193 |
let html = Webview::new().suggestions( |
| 6194 |
"q", |
| 6195 |
&[ |
| 6196 |
Candidate::new("/p/rust-lang", "Rust") |
| 6197 |
.picking(Action::get("/p/rust-lang").navigating()), |
| 6198 |
Candidate::new("audio.genre.ambient", "Ambient") |
| 6199 |
.picking(Action::get("/discover/results").replacing("results-container")), |
| 6200 |
], |
| 6201 |
); |
| 6202 |
|
| 6203 |
assert!(html.contains(r#"<a class="form-suggestion""#), "{html}"); |
| 6204 |
assert!(html.contains(r#"href="/p/rust-lang""#), "{html}"); |
| 6205 |
assert!(html.contains("</a>"), "{html}"); |
| 6206 |
|
| 6207 |
|
| 6208 |
assert!(html.contains(r#"<div class="form-suggestion""#), "{html}"); |
| 6209 |
assert_eq!(html.matches("hx-get").count(), 1, "{html}"); |
| 6210 |
assert!(!html.contains("hx-get=\"/p/rust-lang\""), "{html}"); |
| 6211 |
} |
| 6212 |
|
| 6213 |
|
| 6214 |
|
| 6215 |
|
| 6216 |
#[test] |
| 6217 |
fn the_keyboard_presses_a_candidate_that_acts() { |
| 6218 |
let html = fragment(&Node::Field(Box::new( |
| 6219 |
Field::new(layout::FieldKind::Text, "q", "Search") |
| 6220 |
.suggesting(Consult::new(Action::get("/discover/suggestions")).at_least(2)), |
| 6221 |
))); |
| 6222 |
|
| 6223 |
assert!(html.contains("if no (@data-value of cur)"), "{html}"); |
| 6224 |
assert!(html.contains("call cur.click()"), "{html}"); |
| 6225 |
} |
| 6226 |
|
| 6227 |
|
| 6228 |
|
| 6229 |
#[test] |
| 6230 |
fn a_candidate_cannot_write_its_own_program() { |
| 6231 |
let html = Webview::new().suggestions( |
| 6232 |
"q", |
| 6233 |
&[Candidate::new( |
| 6234 |
"' then log 'pwned", |
| 6235 |
"<script>alert(1)</script>", |
| 6236 |
)], |
| 6237 |
); |
| 6238 |
|
| 6239 |
assert!(!html.contains("<script>"), "{html}"); |
| 6240 |
|
| 6241 |
|
| 6242 |
|
| 6243 |
assert_eq!(html.matches("then log").count(), 1, "{html}"); |
| 6244 |
assert!( |
| 6245 |
html.contains(r#"data-value="' then log 'pwned""#), |
| 6246 |
"{html}" |
| 6247 |
); |
| 6248 |
assert!(html.contains("set picked to @data-value"), "{html}"); |
| 6249 |
} |
| 6250 |
|
| 6251 |
#[test] |
| 6252 |
fn a_field_whose_name_is_not_a_handle_draws_no_list() { |
| 6253 |
let html = fragment(&Node::Field(Box::new( |
| 6254 |
Field::new(layout::FieldKind::Text, "q[]", "Search") |
| 6255 |
.suggests(Action::get("/discover/suggestions")), |
| 6256 |
))); |
| 6257 |
|
| 6258 |
assert!(!html.contains("role=\"combobox\""), "{html}"); |
| 6259 |
assert!(!html.contains("role=\"listbox\""), "{html}"); |
| 6260 |
assert!( |
| 6261 |
Webview::new() |
| 6262 |
.suggestions("q[]", &[Candidate::plain("a")]) |
| 6263 |
.is_empty() |
| 6264 |
); |
| 6265 |
assert_eq!(Webview::new().suggestions_target("q[]"), None); |
| 6266 |
} |
| 6267 |
|
| 6268 |
|
| 6269 |
|
| 6270 |
|
| 6271 |
|
| 6272 |
#[test] |
| 6273 |
fn the_download_script_is_linked_and_goes_with_htmx() { |
| 6274 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 6275 |
assert!(with.contains("quasi-download.js"), "{with}"); |
| 6276 |
|
| 6277 |
let without = Webview::new() |
| 6278 |
.with_shell(Shell::default().without_download()) |
| 6279 |
.screen(&Screen::list_detail("A", false)); |
| 6280 |
assert!(!without.contains("quasi-download.js"), "{without}"); |
| 6281 |
|
| 6282 |
let embed = Webview::new() |
| 6283 |
.with_shell(Shell::default().without_htmx()) |
| 6284 |
.screen(&Screen::list_detail("A", false)); |
| 6285 |
assert!(!embed.contains("quasi-download.js"), "{embed}"); |
| 6286 |
} |
| 6287 |
|
| 6288 |
|
| 6289 |
|
| 6290 |
|
| 6291 |
#[test] |
| 6292 |
fn the_download_script_reads_the_header_the_adapter_writes() { |
| 6293 |
assert!(crate::DOWNLOAD_JS.contains("content-disposition")); |
| 6294 |
|
| 6295 |
assert!(crate::DOWNLOAD_JS.contains(r"filename\*=UTF-8''")); |
| 6296 |
assert!(crate::DOWNLOAD_JS.contains("attachment")); |
| 6297 |
} |
| 6298 |
|
| 6299 |
|
| 6300 |
|
| 6301 |
|
| 6302 |
|
| 6303 |
#[test] |
| 6304 |
fn an_act_that_deposits_a_value_names_the_box_and_carries_what_lands_in_it() { |
| 6305 |
let html = Webview::new().screen(&Screen::list_detail("Write", false).with( |
| 6306 |
Slot::new("picker", RegionKind::Modal).with(Node::Act( |
| 6307 |
Act::new("kick.png", Action::local()).filling("body", ""), |
| 6308 |
)), |
| 6309 |
)); |
| 6310 |
|
| 6311 |
assert!(html.contains(r#"data-fills="body""#), "{html}"); |
| 6312 |
assert!( |
| 6313 |
html.contains(r#"data-fill="""#), |
| 6314 |
"{html}" |
| 6315 |
); |
| 6316 |
|
| 6317 |
|
| 6318 |
assert!(html.contains("data-local"), "{html}"); |
| 6319 |
assert!(!html.contains("hx-get"), "{html}"); |
| 6320 |
} |
| 6321 |
|
| 6322 |
|
| 6323 |
|
| 6324 |
#[test] |
| 6325 |
fn a_deposited_value_that_looks_like_markup_stays_a_value() { |
| 6326 |
let html = Webview::new().screen(&Screen::list_detail("Write", false).with( |
| 6327 |
Slot::new("picker", RegionKind::Modal).with(Node::Act( |
| 6328 |
Act::new("odd", Action::local()).filling("body", r#"" onclick="alert(1)"#), |
| 6329 |
)), |
| 6330 |
)); |
| 6331 |
|
| 6332 |
|
| 6333 |
|
| 6334 |
assert!( |
| 6335 |
html.contains(r#"data-fill="" onclick="alert(1)""#), |
| 6336 |
"{html}" |
| 6337 |
); |
| 6338 |
assert!(!html.contains(r#"" onclick=""#), "{html}"); |
| 6339 |
} |
| 6340 |
|
| 6341 |
|
| 6342 |
#[test] |
| 6343 |
fn an_ordinary_control_deposits_nothing() { |
| 6344 |
let html = Webview::new().screen( |
| 6345 |
&Screen::list_detail("Tasks", false).with( |
| 6346 |
Slot::new("body", RegionKind::Pane) |
| 6347 |
.with(Node::Act(Act::new("Delete", Action::post("/delete")))), |
| 6348 |
), |
| 6349 |
); |
| 6350 |
assert!(!html.contains("data-fills"), "{html}"); |
| 6351 |
assert!(!html.contains("data-fill="), "{html}"); |
| 6352 |
} |
| 6353 |
|
| 6354 |
|
| 6355 |
|
| 6356 |
|
| 6357 |
#[test] |
| 6358 |
fn the_fill_script_is_linked_and_outlives_htmx() { |
| 6359 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 6360 |
assert!(with.contains("quasi-fill.js"), "{with}"); |
| 6361 |
|
| 6362 |
let embed = Webview::new() |
| 6363 |
.with_shell(Shell::default().without_htmx()) |
| 6364 |
.screen(&Screen::list_detail("A", false)); |
| 6365 |
assert!(embed.contains("quasi-fill.js"), "{embed}"); |
| 6366 |
|
| 6367 |
let without = Webview::new() |
| 6368 |
.with_shell(Shell::default().without_fill()) |
| 6369 |
.screen(&Screen::list_detail("A", false)); |
| 6370 |
assert!(!without.contains("quasi-fill.js"), "{without}"); |
| 6371 |
} |
| 6372 |
|
| 6373 |
|
| 6374 |
|
| 6375 |
|
| 6376 |
|
| 6377 |
#[test] |
| 6378 |
fn the_fill_script_reads_the_hooks_the_emitter_writes() { |
| 6379 |
assert!( |
| 6380 |
crate::FILL_JS.contains("data-fills"), |
| 6381 |
"the destination hook" |
| 6382 |
); |
| 6383 |
assert!(crate::FILL_JS.contains("data-fill"), "the value hook"); |
| 6384 |
|
| 6385 |
assert!(crate::FILL_JS.contains("selectionStart")); |
| 6386 |
assert!(crate::FILL_JS.contains("setSelectionRange")); |
| 6387 |
|
| 6388 |
assert!(crate::FILL_JS.contains(r#"new Event("input""#)); |
| 6389 |
} |
| 6390 |
|
| 6391 |
|
| 6392 |
|
| 6393 |
|
| 6394 |
|
| 6395 |
fn classes_emitted(html: &str) -> std::collections::BTreeSet<String> { |
| 6396 |
let mut found = std::collections::BTreeSet::new(); |
| 6397 |
let mut rest = html; |
| 6398 |
while let Some(at) = rest.find(" class=\"") { |
| 6399 |
rest = &rest[at + " class=\"".len()..]; |
| 6400 |
let end = rest.find('"').expect("an attribute closes"); |
| 6401 |
for name in rest[..end].split_whitespace() { |
| 6402 |
found.insert(name.to_owned()); |
| 6403 |
} |
| 6404 |
rest = &rest[end..]; |
| 6405 |
} |
| 6406 |
found |
| 6407 |
} |
| 6408 |
|
| 6409 |
|
| 6410 |
|
| 6411 |
|
| 6412 |
|
| 6413 |
|
| 6414 |
|
| 6415 |
fn everything() -> Screen { |
| 6416 |
|
| 6417 |
|
| 6418 |
let sortable_columns = vec![ |
| 6419 |
Column::new("Name") |
| 6420 |
.width(layout::Width::Fill) |
| 6421 |
.reorder(Action::get("/by-name")), |
| 6422 |
Column::new("Size").width(layout::Width::Fill), |
| 6423 |
]; |
| 6424 |
|
| 6425 |
Screen::list_detail("Everything", true) |
| 6426 |
.with( |
| 6427 |
|
| 6428 |
|
| 6429 |
Slot::new("main", RegionKind::Pane) |
| 6430 |
|
| 6431 |
|
| 6432 |
.consulting(Consult::new(Action::get("/recompute").replacing("detail"))) |
| 6433 |
.with(Node::page("A heading")) |
| 6434 |
.with(Node::section("A section")) |
| 6435 |
.with(Node::text("Some text")) |
| 6436 |
.with(Node::rich("**rich**")) |
| 6437 |
.with(Node::Act(Act::new("Press", Action::post("/press")))) |
| 6438 |
.with(Node::Act( |
| 6439 |
Act::new("Ask first", Action::post("/ask")).asking(Field::new( |
| 6440 |
layout::FieldKind::Text, |
| 6441 |
"why", |
| 6442 |
"Why", |
| 6443 |
)), |
| 6444 |
)) |
| 6445 |
.with(Node::Link { |
| 6446 |
text: "A link".into(), |
| 6447 |
action: Action::get("/elsewhere"), |
| 6448 |
}) |
| 6449 |
.with(Node::Act(Act::new( |
| 6450 |
"Open in a window", |
| 6451 |
Action::get("/there").elsewhere(), |
| 6452 |
))) |
| 6453 |
.with(Node::since(instant())) |
| 6454 |
.with(Node::until(instant())) |
| 6455 |
.with(Node::age(instant())) |
| 6456 |
.with(Node::Image( |
| 6457 |
quasi_router::screen::Image::new("/a.png", "A picture").caption("Its caption"), |
| 6458 |
)) |
| 6459 |
.with(Node::Token(Tag::badge("Badge"))) |
| 6460 |
|
| 6461 |
.with(Node::Token(Tag::removable("Chip", Action::post("/x")))) |
| 6462 |
|
| 6463 |
.with(Node::Token( |
| 6464 |
Tag::chip("Latched", Action::post("/t")) |
| 6465 |
.tone(layout::Tone::Info) |
| 6466 |
.latched(), |
| 6467 |
)) |
| 6468 |
.with(Node::banner(layout::Tone::Info, "A banner")) |
| 6469 |
.with(Node::toast(layout::Tone::Success, "A toast")) |
| 6470 |
.with(Node::empty("Nothing here")) |
| 6471 |
.with(Node::failed("It went wrong")) |
| 6472 |
.with(Node::field( |
| 6473 |
Field::new(layout::FieldKind::Text, "name", "Name") |
| 6474 |
.writes(Action::post("/name")), |
| 6475 |
)) |
| 6476 |
.with(Node::field( |
| 6477 |
Field::new(layout::FieldKind::Text, "who", "Who").consulting( |
| 6478 |
Consult::new(Action::get("/people")) |
| 6479 |
.after(std::time::Duration::from_millis(120)), |
| 6480 |
), |
| 6481 |
)) |
| 6482 |
|
| 6483 |
|
| 6484 |
.with(Node::field( |
| 6485 |
Field::new(layout::FieldKind::Text, "person", "Person") |
| 6486 |
.suggesting(Consult::new(Action::get("/people"))), |
| 6487 |
)) |
| 6488 |
.with(Node::Form { |
| 6489 |
marks: ::quasi_router::stage::Marks::none(), |
| 6490 |
action: Action::post("/go"), |
| 6491 |
submit: "Go".into(), |
| 6492 |
fields: vec![Field::new(layout::FieldKind::Text, "q", "Query")], |
| 6493 |
}) |
| 6494 |
.with(Node::list([ |
| 6495 |
|
| 6496 |
Row::new("First").meta("meta").activate(Action::get("/1")), |
| 6497 |
|
| 6498 |
Row { |
| 6499 |
current: true, |
| 6500 |
..Row::new("Second") |
| 6501 |
}, |
| 6502 |
|
| 6503 |
|
| 6504 |
Row { |
| 6505 |
chosen: Some(true), |
| 6506 |
..Row::new("Second and a half") |
| 6507 |
}, |
| 6508 |
|
| 6509 |
|
| 6510 |
|
| 6511 |
Row::new("Third") |
| 6512 |
.ticking("third", true) |
| 6513 |
.offers(Act::new("Delete", Action::post("/del"))) |
| 6514 |
.offers(Act::new("Archive", Action::post("/arch"))) |
| 6515 |
.secondary("underneath") |
| 6516 |
.relaxed(), |
| 6517 |
|
| 6518 |
|
| 6519 |
|
| 6520 |
Row::new("Fourth").disclosing(true), |
| 6521 |
Row::new("Fourth, inside").depth(quasi_router::layout::Nesting::at(1)), |
| 6522 |
Row::new("Fifth").disclosing(false), |
| 6523 |
Row::new("Fifth, inside").depth(quasi_router::layout::Nesting::at(1)), |
| 6524 |
])) |
| 6525 |
.with(Node::Table { |
| 6526 |
marks: ::quasi_router::stage::Marks::none(), |
| 6527 |
columns: sortable_columns, |
| 6528 |
rows: vec![ |
| 6529 |
|
| 6530 |
|
| 6531 |
Row::cells(["kick.wav", "2.1 MB"]).ticking("kick", false), |
| 6532 |
Row::cells(["snare.wav", "1.4 MB"]) |
| 6533 |
.ticking("snare", true) |
| 6534 |
.current(true), |
| 6535 |
|
| 6536 |
Row::cells(["tom.wav", "0.9 MB"]).choosing("tom", true), |
| 6537 |
|
| 6538 |
Row::cells(["hat.wav", "0.3 MB"]) |
| 6539 |
.ticking("hat", false) |
| 6540 |
.offers(Act::new("Delete", Action::post("/del"))) |
| 6541 |
.offers(Act::new("Archive", Action::post("/arch"))), |
| 6542 |
|
| 6543 |
|
| 6544 |
Row::cells(["kit", ""]).disclosing(true), |
| 6545 |
Row::cells(["kit/ride.wav", "1.1 MB"]) |
| 6546 |
.depth(quasi_router::layout::Nesting::at(1)), |
| 6547 |
], |
| 6548 |
|
| 6549 |
|
| 6550 |
more: Some( |
| 6551 |
Rest::page(100, 50) |
| 6552 |
.of(400) |
| 6553 |
.back(Action::get("/t?page=2")) |
| 6554 |
.forward(Action::get("/t?page=4")), |
| 6555 |
), |
| 6556 |
}) |
| 6557 |
|
| 6558 |
|
| 6559 |
|
| 6560 |
|
| 6561 |
.with( |
| 6562 |
Node::list([Row::new("One")]).and_more( |
| 6563 |
Rest::page(100, 50) |
| 6564 |
.of(400) |
| 6565 |
.back(Action::get("/t?page=2")) |
| 6566 |
.forward(Action::get("/t?page=4")) |
| 6567 |
.jumping(Jump::new(2, Action::get("/t?page=2"))) |
| 6568 |
.jumping(Jump::new(3, Action::get("/t?page=3")).here()) |
| 6569 |
.jumping(Jump::new(4, Action::get("/t?page=4"))), |
| 6570 |
), |
| 6571 |
) |
| 6572 |
|
| 6573 |
|
| 6574 |
|
| 6575 |
.with(Node::Region( |
| 6576 |
Slot::new("tabs", RegionKind::TabGroup) |
| 6577 |
.showing_one(0) |
| 6578 |
.frame( |
| 6579 |
"Open", |
| 6580 |
Node::Region( |
| 6581 |
Slot::new("open", RegionKind::Group) |
| 6582 |
.with(Node::text("what is open")), |
| 6583 |
), |
| 6584 |
) |
| 6585 |
.frame("Done", Node::Region(Slot::new("done", RegionKind::Group))), |
| 6586 |
)) |
| 6587 |
.with(Node::Meter(Meter::new(3, 10))) |
| 6588 |
|
| 6589 |
.with(Node::Stats { |
| 6590 |
marks: ::quasi_router::stage::Marks::none(), |
| 6591 |
figures: vec![ |
| 6592 |
(Figure::new("12", "Open"), None), |
| 6593 |
( |
| 6594 |
Figure::new("3", "Overdue"), |
| 6595 |
Some(Action::get("/tasks?due=past")), |
| 6596 |
), |
| 6597 |
], |
| 6598 |
}) |
| 6599 |
.with(Node::Region( |
| 6600 |
Slot::new("nested", RegionKind::Pane).with(Node::text("inside")), |
| 6601 |
)), |
| 6602 |
) |
| 6603 |
.with(Slot::new("aside", RegionKind::Pane).with(Node::text("beside"))) |
| 6604 |
|
| 6605 |
|
| 6606 |
|
| 6607 |
.saying(Node::banner(layout::Tone::Warning, "Careful")) |
| 6608 |
.saying(Node::toast(layout::Tone::Success, "Saved")) |
| 6609 |
} |
| 6610 |
|
| 6611 |
|
| 6612 |
|
| 6613 |
|
| 6614 |
|
| 6615 |
|
| 6616 |
fn every_shape() -> Vec<Screen> { |
| 6617 |
let kinds = [ |
| 6618 |
RegionKind::Band, |
| 6619 |
RegionKind::Sidebar, |
| 6620 |
RegionKind::Pane, |
| 6621 |
RegionKind::Group, |
| 6622 |
RegionKind::TabGroup, |
| 6623 |
RegionKind::Modal, |
| 6624 |
RegionKind::Handover { |
| 6625 |
name: "graph".into(), |
| 6626 |
}, |
| 6627 |
RegionKind::Ceded { |
| 6628 |
name: "revenue-chart".into(), |
| 6629 |
}, |
| 6630 |
RegionKind::Widget { |
| 6631 |
name: "timer".into(), |
| 6632 |
}, |
| 6633 |
]; |
| 6634 |
|
| 6635 |
let mut screens: Vec<Screen> = kinds |
| 6636 |
.into_iter() |
| 6637 |
.map(|kind| { |
| 6638 |
Screen::list_detail("Shapes", false) |
| 6639 |
.with(Slot::new("r", kind).with(Node::text("in it"))) |
| 6640 |
}) |
| 6641 |
.collect(); |
| 6642 |
|
| 6643 |
screens.push(Screen::list_detail("Tabbed", true)); |
| 6644 |
screens.push(Screen::sidebar_content("Sidebar")); |
| 6645 |
for measure in [ |
| 6646 |
layout::Measure::Wide, |
| 6647 |
layout::Measure::Contained, |
| 6648 |
layout::Measure::Reading, |
| 6649 |
] { |
| 6650 |
screens.push(Screen::list_detail("Measured", false).measured(measure)); |
| 6651 |
} |
| 6652 |
screens |
| 6653 |
} |
| 6654 |
|
| 6655 |
|
| 6656 |
fn instant() -> std::time::SystemTime { |
| 6657 |
std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000) |
| 6658 |
} |
| 6659 |
|
| 6660 |
#[test] |
| 6661 |
fn every_class_this_renderer_emits_is_one_it_wrote_down() { |
| 6662 |
|
| 6663 |
|
| 6664 |
|
| 6665 |
|
| 6666 |
|
| 6667 |
|
| 6668 |
|
| 6669 |
|
| 6670 |
|
| 6671 |
let opts = Emit::default(); |
| 6672 |
|
| 6673 |
let document = Webview::new() |
| 6674 |
.with_frame( |
| 6675 |
Frame::new() |
| 6676 |
.reporting() |
| 6677 |
.offering(Act::new("Save", Action::post("/save"))), |
| 6678 |
) |
| 6679 |
.with_shell( |
| 6680 |
Shell::under("/static").with_chrome( |
| 6681 |
quasi_router::Chrome::new() |
| 6682 |
.offering(quasi_router::Place::new( |
| 6683 |
"tasks", |
| 6684 |
"Tasks", |
| 6685 |
Action::get("/tasks"), |
| 6686 |
)) |
| 6687 |
.banded(banded()) |
| 6688 |
.presenting( |
| 6689 |
"timer", |
| 6690 |
quasi_router::Role::Activity, |
| 6691 |
Node::text("00:12:04"), |
| 6692 |
), |
| 6693 |
), |
| 6694 |
) |
| 6695 |
.screen(&everything()); |
| 6696 |
|
| 6697 |
let mut emitted = classes_emitted(&document); |
| 6698 |
for screen in every_shape() { |
| 6699 |
emitted.extend(classes_emitted(&render(&screen))); |
| 6700 |
} |
| 6701 |
assert!( |
| 6702 |
!emitted.is_empty(), |
| 6703 |
"the corpus rendered no classes at all, so this test proves nothing" |
| 6704 |
); |
| 6705 |
|
| 6706 |
let stranger: Vec<&String> = emitted |
| 6707 |
.iter() |
| 6708 |
.filter(|class| !crate::vocabulary::covers(class, &opts)) |
| 6709 |
.collect(); |
| 6710 |
assert!( |
| 6711 |
stranger.is_empty(), |
| 6712 |
"{} class(es) emitted that `vocabulary` does not name. Add them to \ |
| 6713 |
`vocabulary::OWN`, or to makeover if makeover should be styling them:\n{}", |
| 6714 |
stranger.len(), |
| 6715 |
stranger |
| 6716 |
.iter() |
| 6717 |
.map(|c| format!(" .{c}")) |
| 6718 |
.collect::<Vec<_>>() |
| 6719 |
.join("\n") |
| 6720 |
); |
| 6721 |
} |
| 6722 |
|
| 6723 |
#[test] |
| 6724 |
fn every_name_this_renderer_wrote_down_is_one_it_still_emits() { |
| 6725 |
|
| 6726 |
|
| 6727 |
|
| 6728 |
|
| 6729 |
|
| 6730 |
|
| 6731 |
|
| 6732 |
|
| 6733 |
let opts = Emit::default(); |
| 6734 |
|
| 6735 |
let mut emitted = classes_emitted( |
| 6736 |
&Webview::new() |
| 6737 |
.with_frame( |
| 6738 |
Frame::new() |
| 6739 |
.reporting() |
| 6740 |
.offering(Act::new("Save", Action::post("/save"))), |
| 6741 |
) |
| 6742 |
.with_shell( |
| 6743 |
Shell::under("/static").with_chrome( |
| 6744 |
quasi_router::Chrome::new() |
| 6745 |
.offering(quasi_router::Place::new( |
| 6746 |
"tasks", |
| 6747 |
"Tasks", |
| 6748 |
Action::get("/tasks"), |
| 6749 |
)) |
| 6750 |
.banded(banded()) |
| 6751 |
.presenting( |
| 6752 |
"timer", |
| 6753 |
quasi_router::Role::Activity, |
| 6754 |
Node::text("00:12:04"), |
| 6755 |
), |
| 6756 |
), |
| 6757 |
) |
| 6758 |
.screen(&everything()), |
| 6759 |
); |
| 6760 |
for screen in every_shape() { |
| 6761 |
emitted.extend(classes_emitted(&render(&screen))); |
| 6762 |
} |
| 6763 |
|
| 6764 |
let stale: Vec<&&str> = crate::vocabulary::OWN |
| 6765 |
.iter() |
| 6766 |
.filter(|name| !emitted.contains(&makeover_webview::class(name, &opts))) |
| 6767 |
.collect(); |
| 6768 |
|
| 6769 |
assert!( |
| 6770 |
stale.is_empty(), |
| 6771 |
"{} name(s) in `vocabulary::OWN` that the corpus never emits. Either an \ |
| 6772 |
emitter stopped writing them -- delete them, and the app rules that \ |
| 6773 |
match them -- or this corpus stopped covering them:\n{}", |
| 6774 |
stale.len(), |
| 6775 |
stale |
| 6776 |
.iter() |
| 6777 |
.map(|c| format!(" .{c}")) |
| 6778 |
.collect::<Vec<_>>() |
| 6779 |
.join("\n") |
| 6780 |
); |
| 6781 |
} |
| 6782 |
|
| 6783 |
|
| 6784 |
|
| 6785 |
|
| 6786 |
#[test] |
| 6787 |
fn a_conditional_region_emits_its_condition_and_no_transport() { |
| 6788 |
let html = render( |
| 6789 |
&Screen::list_detail("Pricing", false).with( |
| 6790 |
Slot::new("body", RegionKind::Pane) |
| 6791 |
.with(Node::Field(Box::new(Field::new( |
| 6792 |
layout::FieldKind::Checkbox, |
| 6793 |
"pwyw", |
| 6794 |
"Pay what you want", |
| 6795 |
)))) |
| 6796 |
.with(Node::Region( |
| 6797 |
Slot::group("pwyw-settings") |
| 6798 |
.revealed_by(Reveal::ticked("pwyw")) |
| 6799 |
.with(Node::text("Suggested price")), |
| 6800 |
)), |
| 6801 |
), |
| 6802 |
); |
| 6803 |
|
| 6804 |
assert!(html.contains(r#"data-reveal="pwyw""#), "{html}"); |
| 6805 |
assert!(html.contains(r#"data-reveal-when="any""#), "{html}"); |
| 6806 |
|
| 6807 |
|
| 6808 |
assert!(!html.contains("hx-get=\"/pwyw"), "{html}"); |
| 6809 |
assert!(!html.contains("data-reveal-values"), "{html}"); |
| 6810 |
} |
| 6811 |
|
| 6812 |
|
| 6813 |
|
| 6814 |
#[test] |
| 6815 |
fn a_condition_on_a_value_carries_the_values_it_accepts() { |
| 6816 |
let one = render( |
| 6817 |
&Screen::list_detail("Licensing", false).with( |
| 6818 |
Slot::new("body", RegionKind::Pane).with(Node::Region( |
| 6819 |
Slot::group("dash-custom-license") |
| 6820 |
.revealed_by(Reveal::holding("license_kind", "custom")), |
| 6821 |
)), |
| 6822 |
), |
| 6823 |
); |
| 6824 |
assert!(one.contains(r#"data-reveal="license_kind""#), "{one}"); |
| 6825 |
assert!(one.contains(r#"data-reveal-when="value""#), "{one}"); |
| 6826 |
assert!( |
| 6827 |
one.contains(r#"data-reveal-values="["custom"]""#), |
| 6828 |
"{one}" |
| 6829 |
); |
| 6830 |
|
| 6831 |
let several = render( |
| 6832 |
&Screen::list_detail("Placement", false).with( |
| 6833 |
Slot::new("body", RegionKind::Pane) |
| 6834 |
.with(Node::Region(Slot::group("offset-input").revealed_by( |
| 6835 |
Reveal::holding_one_of("position", ["before", "after"]), |
| 6836 |
))), |
| 6837 |
), |
| 6838 |
); |
| 6839 |
assert!( |
| 6840 |
several.contains(r#"data-reveal-values="["before","after"]""#), |
| 6841 |
"{several}" |
| 6842 |
); |
| 6843 |
|
| 6844 |
let unticked = render(&Screen::list_detail("Defaults", false).with( |
| 6845 |
Slot::new("body", RegionKind::Pane).with(Node::Region( |
| 6846 |
Slot::group("advanced").revealed_by(Reveal::unticked("use_defaults")), |
| 6847 |
)), |
| 6848 |
)); |
| 6849 |
assert!( |
| 6850 |
unticked.contains(r#"data-reveal-when="none""#), |
| 6851 |
"{unticked}" |
| 6852 |
); |
| 6853 |
} |
| 6854 |
|
| 6855 |
|
| 6856 |
|
| 6857 |
#[test] |
| 6858 |
fn a_watched_value_cannot_close_the_attribute_it_sits_in() { |
| 6859 |
let html = render(&Screen::list_detail("Odd", false).with( |
| 6860 |
Slot::new("body", RegionKind::Pane).with(Node::Region( |
| 6861 |
Slot::group("odd").revealed_by(Reveal::holding("kind", r#"" onclick="alert(1)"#)), |
| 6862 |
)), |
| 6863 |
)); |
| 6864 |
|
| 6865 |
assert!( |
| 6866 |
html.contains(r#"data-reveal-values="["\" onclick=\"alert(1)"]""#), |
| 6867 |
"{html}" |
| 6868 |
); |
| 6869 |
assert!(!html.contains(r#"" onclick=""#), "{html}"); |
| 6870 |
} |
| 6871 |
|
| 6872 |
|
| 6873 |
|
| 6874 |
#[test] |
| 6875 |
fn an_ordinary_region_names_no_condition() { |
| 6876 |
let html = render( |
| 6877 |
&Screen::list_detail("Tasks", false) |
| 6878 |
.with(Slot::new("body", RegionKind::Pane).with(Node::text("plain"))), |
| 6879 |
); |
| 6880 |
assert!(!html.contains("data-reveal"), "{html}"); |
| 6881 |
} |
| 6882 |
|
| 6883 |
|
| 6884 |
|
| 6885 |
#[test] |
| 6886 |
fn the_reveal_script_is_linked_and_outlives_htmx() { |
| 6887 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 6888 |
assert!(with.contains("quasi-reveal.js"), "{with}"); |
| 6889 |
|
| 6890 |
let embed = Webview::new() |
| 6891 |
.with_shell(Shell::default().without_htmx()) |
| 6892 |
.screen(&Screen::list_detail("A", false)); |
| 6893 |
assert!(embed.contains("quasi-reveal.js"), "{embed}"); |
| 6894 |
|
| 6895 |
let without = Webview::new() |
| 6896 |
.with_shell(Shell::default().without_reveal()) |
| 6897 |
.screen(&Screen::list_detail("A", false)); |
| 6898 |
assert!(!without.contains("quasi-reveal.js"), "{without}"); |
| 6899 |
} |
| 6900 |
|
| 6901 |
|
| 6902 |
|
| 6903 |
|
| 6904 |
#[test] |
| 6905 |
fn the_reveal_script_reads_the_marks_the_emitter_writes() { |
| 6906 |
for mark in ["data-reveal", "data-reveal-when", "data-reveal-values"] { |
| 6907 |
assert!(crate::REVEAL_JS.contains(mark), "{mark} is not read"); |
| 6908 |
} |
| 6909 |
|
| 6910 |
|
| 6911 |
assert!( |
| 6912 |
!crate::REVEAL_JS.contains("fetch("), |
| 6913 |
"the reveal makes no request" |
| 6914 |
); |
| 6915 |
assert!(!crate::REVEAL_JS.contains("new Function"), "no eval"); |
| 6916 |
} |
| 6917 |
|
| 6918 |
|
| 6919 |
|
| 6920 |
|
| 6921 |
|
| 6922 |
|
| 6923 |
#[test] |
| 6924 |
fn a_conditional_question_carries_its_condition_and_stays_in_its_form() { |
| 6925 |
let html = render(&Screen::list_detail("Event", false).with( |
| 6926 |
Slot::new("body", RegionKind::Pane).with(Node::Form { |
| 6927 |
marks: ::quasi_router::stage::Marks::none(), |
| 6928 |
action: Action::post("/events"), |
| 6929 |
submit: "Save".into(), |
| 6930 |
fields: vec![ |
| 6931 |
Field::select( |
| 6932 |
"tz_kind", |
| 6933 |
"Time zone", |
| 6934 |
vec![Choice::new("local", "Anchored to a place")], |
| 6935 |
), |
| 6936 |
Field::new(layout::FieldKind::Text, "timezone", "Anchored to") |
| 6937 |
.revealed_by(Reveal::holding("tz_kind", "local")), |
| 6938 |
], |
| 6939 |
}), |
| 6940 |
)); |
| 6941 |
|
| 6942 |
assert!(html.contains(r#"data-reveal="tz_kind""#), "{html}"); |
| 6943 |
assert!(html.contains(r#"data-reveal-when="value""#), "{html}"); |
| 6944 |
assert!( |
| 6945 |
html.contains(r#"data-reveal-values="["local"]""#), |
| 6946 |
"{html}" |
| 6947 |
); |
| 6948 |
|
| 6949 |
|
| 6950 |
assert_eq!(html.matches("<form").count(), 1, "{html}"); |
| 6951 |
let form = &html[html.find("<form").expect("a form")..]; |
| 6952 |
let box_at = form.find(r#"name="timezone""#).expect("the box"); |
| 6953 |
assert!(box_at < form.find("</form>").expect("the end"), "{form}"); |
| 6954 |
|
| 6955 |
assert!(!html.contains("hx-get=\"/events/timezone"), "{html}"); |
| 6956 |
} |
| 6957 |
|
| 6958 |
|
| 6959 |
|
| 6960 |
#[test] |
| 6961 |
fn an_ordinary_question_names_no_condition() { |
| 6962 |
let html = render(&form_with(Field::new( |
| 6963 |
layout::FieldKind::Text, |
| 6964 |
"title", |
| 6965 |
"Title", |
| 6966 |
))); |
| 6967 |
assert!(!html.contains("data-reveal"), "{html}"); |
| 6968 |
} |
| 6969 |
|
| 6970 |
|
| 6971 |
|
| 6972 |
|
| 6973 |
|
| 6974 |
fn reminders() -> Field { |
| 6975 |
Field::new(layout::FieldKind::Number, "reminder", "Reminder").repeating( |
| 6976 |
Repeat::answered(["300", "900", "3600"]) |
| 6977 |
.most(8) |
| 6978 |
.adding("Add reminder") |
| 6979 |
.removing("Remove"), |
| 6980 |
) |
| 6981 |
} |
| 6982 |
|
| 6983 |
fn form_with(field: Field) -> Screen { |
| 6984 |
Screen::list_detail("Event", false).with(Slot::new("body", RegionKind::Pane).with(Node::Form { |
| 6985 |
marks: ::quasi_router::stage::Marks::none(), |
| 6986 |
action: Action::post("/events"), |
| 6987 |
submit: "Save".into(), |
| 6988 |
fields: vec![field], |
| 6989 |
})) |
| 6990 |
} |
| 6991 |
|
| 6992 |
|
| 6993 |
|
| 6994 |
|
| 6995 |
#[test] |
| 6996 |
fn every_slot_of_a_repeating_question_submits_under_its_own_indexed_name() { |
| 6997 |
let html = render(&form_with(reminders())); |
| 6998 |
|
| 6999 |
let standing = &html[..html.find("<template").expect("a blank slot")]; |
| 7000 |
for at in 0..3 { |
| 7001 |
assert!( |
| 7002 |
standing.contains(&format!(r#"name="reminder[{at}]""#)), |
| 7003 |
"{standing}" |
| 7004 |
); |
| 7005 |
} |
| 7006 |
|
| 7007 |
|
| 7008 |
assert!(!standing.contains(r#"name="reminder[3]""#), "{standing}"); |
| 7009 |
assert_eq!(html.matches(r#"name="reminder[3]""#).count(), 1, "{html}"); |
| 7010 |
|
| 7011 |
assert_eq!(html.matches("<form").count(), 1, "{html}"); |
| 7012 |
assert_eq!(html.matches("act-submit").count(), 1, "{html}"); |
| 7013 |
|
| 7014 |
assert!(html.contains(r#"value="300""#), "{html}"); |
| 7015 |
assert!(html.contains(r#"value="3600""#), "{html}"); |
| 7016 |
} |
| 7017 |
|
| 7018 |
|
| 7019 |
|
| 7020 |
#[test] |
| 7021 |
fn an_error_can_belong_to_one_slot_rather_than_to_the_question() { |
| 7022 |
let field = Field::new(layout::FieldKind::Number, "reminder", "Reminder") |
| 7023 |
.repeating(Repeat::answered(["300", "-1", "3600"]).wrong(1, "Must not be negative")); |
| 7024 |
let html = render(&form_with(field)); |
| 7025 |
|
| 7026 |
|
| 7027 |
|
| 7028 |
assert!(html.contains(r#"id="reminder[1]-error""#), "{html}"); |
| 7029 |
assert!(html.contains("Must not be negative"), "{html}"); |
| 7030 |
assert!(!html.contains(r#"id="reminder[0]-error""#), "{html}"); |
| 7031 |
assert!(!html.contains(r#"id="reminder[2]-error""#), "{html}"); |
| 7032 |
} |
| 7033 |
|
| 7034 |
|
| 7035 |
|
| 7036 |
#[test] |
| 7037 |
fn an_error_about_the_set_sits_on_the_question() { |
| 7038 |
let mut field = reminders(); |
| 7039 |
field.error = Some("At most eight reminders".into()); |
| 7040 |
let html = render(&form_with(field)); |
| 7041 |
|
| 7042 |
assert!(html.contains("At most eight reminders"), "{html}"); |
| 7043 |
assert!(!html.contains(r#"id="reminder[0]-error""#), "{html}"); |
| 7044 |
} |
| 7045 |
|
| 7046 |
|
| 7047 |
|
| 7048 |
#[test] |
| 7049 |
fn adding_and_removing_a_slot_asks_no_route() { |
| 7050 |
let html = render(&form_with(reminders())); |
| 7051 |
|
| 7052 |
assert!(html.contains("<template"), "{html}"); |
| 7053 |
assert!(html.contains("data-repeat-add"), "{html}"); |
| 7054 |
assert!(html.contains("data-repeat-remove"), "{html}"); |
| 7055 |
assert!(html.contains("Add reminder"), "{html}"); |
| 7056 |
|
| 7057 |
|
| 7058 |
let group = &html[html.find("field-repeat").expect("a group")..]; |
| 7059 |
let controls = group.match_indices("data-repeat-add").count() |
| 7060 |
+ group.match_indices("data-repeat-remove").count(); |
| 7061 |
assert_eq!(controls, 5, "three slots, a blank, and the add: {group}"); |
| 7062 |
assert_eq!( |
| 7063 |
group.matches("type=\"button\"").count(), |
| 7064 |
5, |
| 7065 |
"no control here submits: {group}" |
| 7066 |
); |
| 7067 |
assert!(!group.contains("hx-get"), "{group}"); |
| 7068 |
assert!(!group.contains("hx-post"), "{group}"); |
| 7069 |
} |
| 7070 |
|
| 7071 |
|
| 7072 |
|
| 7073 |
|
| 7074 |
#[test] |
| 7075 |
fn the_floor_and_the_ceiling_reach_the_controls() { |
| 7076 |
let full = render(&form_with( |
| 7077 |
Field::new(layout::FieldKind::Number, "reminder", "Reminder") |
| 7078 |
.repeating(Repeat::answered(["300", "900"]).most(2)), |
| 7079 |
)); |
| 7080 |
assert!(full.contains(r#"data-repeat-most="2""#), "{full}"); |
| 7081 |
assert!(full.contains("data-repeat-add disabled"), "{full}"); |
| 7082 |
|
| 7083 |
let floored = render(&form_with( |
| 7084 |
Field::new(layout::FieldKind::Text, "guest", "Guest") |
| 7085 |
.repeating(Repeat::answered(["ana"]).least(1)), |
| 7086 |
)); |
| 7087 |
assert!(floored.contains(r#"data-repeat-least="1""#), "{floored}"); |
| 7088 |
assert!(floored.contains("data-repeat-remove disabled"), "{floored}"); |
| 7089 |
} |
| 7090 |
|
| 7091 |
|
| 7092 |
|
| 7093 |
#[test] |
| 7094 |
fn a_question_nobody_has_answered_still_offers_a_slot_to_add() { |
| 7095 |
let html = render(&form_with( |
| 7096 |
Field::new(layout::FieldKind::Number, "reminder", "Reminder") |
| 7097 |
.repeating(Repeat::new().adding("Add reminder")), |
| 7098 |
)); |
| 7099 |
|
| 7100 |
|
| 7101 |
assert!(html.contains("data-repeat-slots></div>"), "{html}"); |
| 7102 |
assert_eq!(html.matches(r#"name="reminder[0]""#).count(), 1, "{html}"); |
| 7103 |
assert!(html.contains("data-repeat-add"), "{html}"); |
| 7104 |
assert!(html.contains("Add reminder"), "{html}"); |
| 7105 |
} |
| 7106 |
|
| 7107 |
|
| 7108 |
|
| 7109 |
#[test] |
| 7110 |
fn a_slot_keeps_everything_the_question_said_about_itself() { |
| 7111 |
let mut field = reminders(); |
| 7112 |
field.min = Some("0".into()); |
| 7113 |
field.unit = Some("s".into()); |
| 7114 |
let html = render(&form_with(field)); |
| 7115 |
|
| 7116 |
assert_eq!(html.matches(r#"type="number""#).count(), 4, "{html}"); |
| 7117 |
assert_eq!(html.matches(r#"min="0""#).count(), 4, "{html}"); |
| 7118 |
assert!(html.contains(r#"id="reminder[0]-unit""#), "{html}"); |
| 7119 |
} |
| 7120 |
|
| 7121 |
|
| 7122 |
|
| 7123 |
#[test] |
| 7124 |
fn a_repeating_question_cannot_close_the_attribute_it_sits_in() { |
| 7125 |
let html = render(&form_with( |
| 7126 |
Field::new(layout::FieldKind::Text, "odd", r#"" onclick="alert(1)"#) |
| 7127 |
.repeating(Repeat::answered(["one"])), |
| 7128 |
)); |
| 7129 |
assert!(!html.contains(r#"" onclick=""#), "{html}"); |
| 7130 |
assert!(html.contains("" onclick="alert(1)"), "{html}"); |
| 7131 |
} |
| 7132 |
|
| 7133 |
|
| 7134 |
|
| 7135 |
#[test] |
| 7136 |
fn an_ordinary_field_repeats_nothing() { |
| 7137 |
let html = render(&form_with(Field::new( |
| 7138 |
layout::FieldKind::Text, |
| 7139 |
"title", |
| 7140 |
"Title", |
| 7141 |
))); |
| 7142 |
assert!(!html.contains("data-repeat"), "{html}"); |
| 7143 |
assert!(!html.contains("<fieldset"), "{html}"); |
| 7144 |
assert!(html.contains(r#"name="title""#), "{html}"); |
| 7145 |
} |
| 7146 |
|
| 7147 |
|
| 7148 |
|
| 7149 |
|
| 7150 |
|
| 7151 |
|
| 7152 |
|
| 7153 |
#[test] |
| 7154 |
fn the_focus_script_is_linked_by_default_and_goes_with_htmx() { |
| 7155 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 7156 |
assert!(with.contains("quasi-focus.js"), "{with}"); |
| 7157 |
|
| 7158 |
let without = Webview::new() |
| 7159 |
.with_shell(Shell::default().without_htmx()) |
| 7160 |
.screen(&Screen::list_detail("A", false)); |
| 7161 |
assert!(!without.contains("quasi-focus.js"), "{without}"); |
| 7162 |
} |
| 7163 |
|
| 7164 |
|
| 7165 |
|
| 7166 |
|
| 7167 |
#[test] |
| 7168 |
fn the_focus_script_is_served_under_the_name_the_shell_asks_for() { |
| 7169 |
let shell = Shell::default(); |
| 7170 |
assert_eq!(shell.focus_src.as_deref(), Some("/static/quasi-focus.js")); |
| 7171 |
assert!( |
| 7172 |
crate::FOCUS_JS.contains("htmx:after:settle"), |
| 7173 |
"the settle hook" |
| 7174 |
); |
| 7175 |
assert!( |
| 7176 |
crate::FOCUS_JS.contains("htmx:before:request"), |
| 7177 |
"and the record taken before it" |
| 7178 |
); |
| 7179 |
} |
| 7180 |
|
| 7181 |
|
| 7182 |
|
| 7183 |
|
| 7184 |
#[test] |
| 7185 |
fn the_outline_script_is_linked_and_outlives_htmx() { |
| 7186 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 7187 |
assert!(with.contains("quasi-outline.js"), "{with}"); |
| 7188 |
|
| 7189 |
let embed = Webview::new() |
| 7190 |
.with_shell(Shell::default().without_htmx()) |
| 7191 |
.screen(&Screen::list_detail("A", false)); |
| 7192 |
assert!(embed.contains("quasi-outline.js"), "{embed}"); |
| 7193 |
|
| 7194 |
let without = Webview::new() |
| 7195 |
.with_shell(Shell::default().without_outline()) |
| 7196 |
.screen(&Screen::list_detail("A", false)); |
| 7197 |
assert!(!without.contains("quasi-outline.js"), "{without}"); |
| 7198 |
} |
| 7199 |
|
| 7200 |
|
| 7201 |
|
| 7202 |
|
| 7203 |
#[test] |
| 7204 |
fn the_repeat_script_is_linked_and_outlives_htmx() { |
| 7205 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 7206 |
assert!(with.contains("quasi-repeat.js"), "{with}"); |
| 7207 |
|
| 7208 |
let embed = Webview::new() |
| 7209 |
.with_shell(Shell::default().without_htmx()) |
| 7210 |
.screen(&Screen::list_detail("A", false)); |
| 7211 |
assert!(embed.contains("quasi-repeat.js"), "{embed}"); |
| 7212 |
|
| 7213 |
let without = Webview::new() |
| 7214 |
.with_shell(Shell::default().without_repeat()) |
| 7215 |
.screen(&Screen::list_detail("A", false)); |
| 7216 |
assert!(!without.contains("quasi-repeat.js"), "{without}"); |
| 7217 |
} |
| 7218 |
|
| 7219 |
|
| 7220 |
|
| 7221 |
|
| 7222 |
#[test] |
| 7223 |
fn the_repeat_script_reads_the_marks_the_emitter_writes() { |
| 7224 |
for mark in [ |
| 7225 |
"data-repeat", |
| 7226 |
"data-repeat-label", |
| 7227 |
"data-repeat-least", |
| 7228 |
"data-repeat-most", |
| 7229 |
"data-repeat-slots", |
| 7230 |
"data-repeat-at", |
| 7231 |
"data-repeat-add", |
| 7232 |
"data-repeat-remove", |
| 7233 |
] { |
| 7234 |
assert!(crate::REPEAT_JS.contains(mark), "{mark} is not read"); |
| 7235 |
} |
| 7236 |
|
| 7237 |
|
| 7238 |
assert!( |
| 7239 |
!crate::REPEAT_JS.contains("fetch("), |
| 7240 |
"the repeat makes no request" |
| 7241 |
); |
| 7242 |
assert!(!crate::REPEAT_JS.contains("new Function"), "no eval"); |
| 7243 |
} |
| 7244 |
|
| 7245 |
#[test] |
| 7246 |
fn the_shell_serves_the_binder_that_makes_a_wait_visible() { |
| 7247 |
|
| 7248 |
|
| 7249 |
|
| 7250 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 7251 |
assert!(with.contains("quasi-awaiting.js"), "{with}"); |
| 7252 |
|
| 7253 |
|
| 7254 |
|
| 7255 |
|
| 7256 |
let embed = Webview::new() |
| 7257 |
.with_shell(Shell::default().without_htmx()) |
| 7258 |
.screen(&Screen::list_detail("A", false)); |
| 7259 |
assert!(!embed.contains("quasi-awaiting.js"), "{embed}"); |
| 7260 |
|
| 7261 |
let without = Webview::new() |
| 7262 |
.with_shell(Shell::default().without_awaiting()) |
| 7263 |
.screen(&Screen::list_detail("A", false)); |
| 7264 |
assert!(!without.contains("quasi-awaiting.js"), "{without}"); |
| 7265 |
} |
| 7266 |
|
| 7267 |
#[test] |
| 7268 |
fn the_shell_serves_the_script_that_submits_a_moment_rather_than_the_words() { |
| 7269 |
|
| 7270 |
|
| 7271 |
|
| 7272 |
|
| 7273 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 7274 |
assert!(with.contains("quasi-instant.js"), "{with}"); |
| 7275 |
|
| 7276 |
|
| 7277 |
let embed = Webview::new() |
| 7278 |
.with_shell(Shell::default().without_htmx()) |
| 7279 |
.screen(&Screen::list_detail("A", false)); |
| 7280 |
assert!(!embed.contains("quasi-instant.js"), "{embed}"); |
| 7281 |
|
| 7282 |
let without = Webview::new() |
| 7283 |
.with_shell(Shell::default().without_instant()) |
| 7284 |
.screen(&Screen::list_detail("A", false)); |
| 7285 |
assert!(!without.contains("quasi-instant.js"), "{without}"); |
| 7286 |
} |
| 7287 |
|
| 7288 |
#[test] |
| 7289 |
fn the_shell_serves_the_script_that_folds_a_run_that_ran_out_of_room() { |
| 7290 |
|
| 7291 |
|
| 7292 |
|
| 7293 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 7294 |
assert!(with.contains("quasi-menu.js"), "{with}"); |
| 7295 |
|
| 7296 |
|
| 7297 |
let embed = Webview::new() |
| 7298 |
.with_shell(Shell::default().without_htmx()) |
| 7299 |
.screen(&Screen::list_detail("A", false)); |
| 7300 |
assert!(embed.contains("quasi-menu.js"), "{embed}"); |
| 7301 |
|
| 7302 |
let without = Webview::new() |
| 7303 |
.with_shell(Shell::default().without_menu()) |
| 7304 |
.screen(&Screen::list_detail("A", false)); |
| 7305 |
assert!(!without.contains("quasi-menu.js"), "{without}"); |
| 7306 |
} |
| 7307 |
|
| 7308 |
#[test] |
| 7309 |
fn the_menu_script_marks_the_run_before_it_measures_it() { |
| 7310 |
|
| 7311 |
|
| 7312 |
|
| 7313 |
|
| 7314 |
let at_mark = crate::MENU_JS.find("data-menu").expect("writes the mark"); |
| 7315 |
let at_measure = crate::MENU_JS |
| 7316 |
.find("scrollWidth") |
| 7317 |
.expect("measures the overflow"); |
| 7318 |
assert!( |
| 7319 |
at_mark < at_measure, |
| 7320 |
"the mark is written before the measure" |
| 7321 |
); |
| 7322 |
|
| 7323 |
|
| 7324 |
|
| 7325 |
assert!( |
| 7326 |
crate::MENU_JS.contains("cell-drops-first"), |
| 7327 |
"reads priority" |
| 7328 |
); |
| 7329 |
assert!(crate::MENU_JS.contains("cell-drops-next"), "reads priority"); |
| 7330 |
|
| 7331 |
|
| 7332 |
|
| 7333 |
|
| 7334 |
assert!(crate::MENU_JS.contains("tablist"), "folds a strip's tabs"); |
| 7335 |
|
| 7336 |
assert!(!crate::MENU_JS.contains("new Function"), "no eval"); |
| 7337 |
} |
| 7338 |
|
| 7339 |
#[test] |
| 7340 |
fn the_instant_script_leaves_an_unparseable_value_for_the_route_to_answer() { |
| 7341 |
|
| 7342 |
assert!( |
| 7343 |
crate::INSTANT_JS.contains("Number.isNaN"), |
| 7344 |
"an unparseable value has to be left alone" |
| 7345 |
); |
| 7346 |
|
| 7347 |
|
| 7348 |
assert!( |
| 7349 |
!crate::INSTANT_JS.contains(".value ="), |
| 7350 |
"the control is not rewritten" |
| 7351 |
); |
| 7352 |
assert!(!crate::INSTANT_JS.contains("new Function"), "no eval"); |
| 7353 |
} |
| 7354 |
|
| 7355 |
#[test] |
| 7356 |
fn the_binder_reads_delivery_and_never_a_clock() { |
| 7357 |
|
| 7358 |
|
| 7359 |
|
| 7360 |
|
| 7361 |
assert!(crate::AWAITING_JS.contains("loaded"), "the numerator"); |
| 7362 |
assert!(crate::AWAITING_JS.contains("aria-busy"), "the running flag"); |
| 7363 |
for banned in [ |
| 7364 |
"setInterval", |
| 7365 |
"setTimeout", |
| 7366 |
"requestAnimationFrame", |
| 7367 |
"Date.now", |
| 7368 |
] { |
| 7369 |
assert!( |
| 7370 |
!crate::AWAITING_JS.contains(banned), |
| 7371 |
"{banned} would make the share a prediction" |
| 7372 |
); |
| 7373 |
} |
| 7374 |
} |
| 7375 |
|
| 7376 |
#[test] |
| 7377 |
fn an_act_hint_is_a_title_and_an_accessible_description() { |
| 7378 |
|
| 7379 |
|
| 7380 |
|
| 7381 |
let html = fragment(&Node::Act( |
| 7382 |
Act::new("Verify library integrity", Action::post("/verify")) |
| 7383 |
.hint("The result appears in the status line."), |
| 7384 |
)); |
| 7385 |
|
| 7386 |
assert!( |
| 7387 |
html.contains(r#"title="The result appears in the status line.""#), |
| 7388 |
"{html}" |
| 7389 |
); |
| 7390 |
assert!( |
| 7391 |
html.contains(r#"aria-description="The result appears in the status line.""#), |
| 7392 |
"{html}" |
| 7393 |
); |
| 7394 |
} |
| 7395 |
|
| 7396 |
#[test] |
| 7397 |
fn an_act_with_no_hint_says_neither() { |
| 7398 |
|
| 7399 |
|
| 7400 |
let html = fragment(&Node::Act(Act::new("Save", Action::post("/save")))); |
| 7401 |
|
| 7402 |
assert!(!html.contains("title="), "{html}"); |
| 7403 |
assert!(!html.contains("aria-description="), "{html}"); |
| 7404 |
} |
| 7405 |
|
| 7406 |
#[test] |
| 7407 |
fn a_regions_own_name_lands_on_the_region() { |
| 7408 |
|
| 7409 |
|
| 7410 |
|
| 7411 |
let html = render( |
| 7412 |
&Screen::list_detail("Library", false).with( |
| 7413 |
Slot::new("chrome", RegionKind::Pane) |
| 7414 |
.named("Tag breadcrumbs") |
| 7415 |
.with(Node::text("in it")), |
| 7416 |
), |
| 7417 |
); |
| 7418 |
|
| 7419 |
assert!(html.contains(r#"aria-label="Tag breadcrumbs""#), "{html}"); |
| 7420 |
} |
| 7421 |
|
| 7422 |
#[test] |
| 7423 |
fn a_tab_strips_name_lands_on_the_tablist_and_not_twice() { |
| 7424 |
|
| 7425 |
|
| 7426 |
|
| 7427 |
|
| 7428 |
let html = render( |
| 7429 |
&Screen::list_detail("Library", false).with( |
| 7430 |
Slot::new("sections", RegionKind::TabGroup) |
| 7431 |
.named("Library sections") |
| 7432 |
.showing_one(0) |
| 7433 |
.frame("Feed", Node::Region(Slot::new("feed", RegionKind::Pane))) |
| 7434 |
.frame( |
| 7435 |
"Purchases", |
| 7436 |
Node::Region(Slot::new("purchases", RegionKind::Pane)), |
| 7437 |
), |
| 7438 |
), |
| 7439 |
); |
| 7440 |
|
| 7441 |
assert_eq!( |
| 7442 |
html.matches(r#"aria-label="Library sections""#).count(), |
| 7443 |
1, |
| 7444 |
"{html}" |
| 7445 |
); |
| 7446 |
let strip = html |
| 7447 |
.find(r#"role="tablist""#) |
| 7448 |
.expect("a labelled tab group emits a strip"); |
| 7449 |
let name = html |
| 7450 |
.find(r#"aria-label="Library sections""#) |
| 7451 |
.expect("the name is emitted"); |
| 7452 |
|
| 7453 |
assert!(name > strip, "{html}"); |
| 7454 |
} |
| 7455 |
|
| 7456 |
#[test] |
| 7457 |
fn a_region_with_no_name_of_its_own_says_nothing() { |
| 7458 |
let html = render( |
| 7459 |
&Screen::list_detail("Library", false) |
| 7460 |
.with(Slot::new("chrome", RegionKind::Pane).with(Node::text("in it"))), |
| 7461 |
); |
| 7462 |
|
| 7463 |
assert!(!html.contains("aria-label="), "{html}"); |
| 7464 |
} |
| 7465 |
|
| 7466 |
#[test] |
| 7467 |
fn a_named_region_is_still_an_id_target() { |
| 7468 |
|
| 7469 |
|
| 7470 |
|
| 7471 |
let html = fragment(&Node::act( |
| 7472 |
"Remove", |
| 7473 |
Action::post("/api/links/7/delete").replacing("link-list"), |
| 7474 |
)); |
| 7475 |
assert!(html.contains("hx-target=\"#link-list\""), "{html}"); |
| 7476 |
} |
| 7477 |
|
| 7478 |
#[test] |
| 7479 |
fn an_enclosing_target_is_the_row_and_not_an_id() { |
| 7480 |
|
| 7481 |
|
| 7482 |
|
| 7483 |
let html = fragment(&Node::act( |
| 7484 |
"Remove", |
| 7485 |
Action::post("/api/links/7/delete").replacing_enclosing(), |
| 7486 |
)); |
| 7487 |
assert!(html.contains("hx-target=\"closest [data-row]\""), "{html}"); |
| 7488 |
|
| 7489 |
|
| 7490 |
assert!(html.contains("hx-swap=\"outerHTML\""), "{html}"); |
| 7491 |
assert!(!html.contains("hx-target=\"#"), "{html}"); |
| 7492 |
} |
| 7493 |
|
| 7494 |
#[test] |
| 7495 |
fn both_kinds_of_row_carry_the_hook_an_enclosing_target_finds() { |
| 7496 |
|
| 7497 |
|
| 7498 |
let list = fragment(&Node::list([Row::new("One")])); |
| 7499 |
assert!(list.contains("<li"), "{list}"); |
| 7500 |
assert!(list.contains("data-row"), "{list}"); |
| 7501 |
|
| 7502 |
let table = fragment(&Node::Table { |
| 7503 |
marks: ::quasi_router::stage::Marks::none(), |
| 7504 |
columns: vec![Column::new("Name")], |
| 7505 |
rows: vec![Row::cells([Cell::new("One")])], |
| 7506 |
more: None, |
| 7507 |
}); |
| 7508 |
assert!(table.contains("role=\"row\""), "{table}"); |
| 7509 |
assert!(table.contains("data-row"), "{table}"); |
| 7510 |
} |
| 7511 |
|
| 7512 |
#[test] |
| 7513 |
fn the_row_hook_survives_a_class_prefix() { |
| 7514 |
|
| 7515 |
|
| 7516 |
let emit = crate::Emit { |
| 7517 |
class_prefix: "qs-", |
| 7518 |
..crate::Emit::default() |
| 7519 |
}; |
| 7520 |
let html = Webview::new() |
| 7521 |
.with_emit(emit) |
| 7522 |
.fragment(&Node::list([Row::new("One")])); |
| 7523 |
assert!(html.contains("data-row"), "{html}"); |
| 7524 |
assert!(!html.contains("qs-data-row"), "{html}"); |
| 7525 |
} |
| 7526 |
|
| 7527 |
#[test] |
| 7528 |
fn an_act_contained_by_nothing_names_no_target_at_all() { |
| 7529 |
|
| 7530 |
|
| 7531 |
|
| 7532 |
let html = fragment(&Node::act( |
| 7533 |
"Disconnect Stripe", |
| 7534 |
Action::post("/api/stripe/disconnect").invalidating(), |
| 7535 |
)); |
| 7536 |
assert!(html.contains("data-replaces=\"everything\""), "{html}"); |
| 7537 |
assert!(!html.contains("hx-target"), "{html}"); |
| 7538 |
} |
| 7539 |
|
| 7540 |
#[test] |
| 7541 |
fn a_copying_act_carries_its_value_and_asks_nothing() { |
| 7542 |
|
| 7543 |
|
| 7544 |
|
| 7545 |
let html = fragment(&Node::act("Copy", quasi_router::Action::local())); |
| 7546 |
assert!(!html.contains("data-copies"), "{html}"); |
| 7547 |
|
| 7548 |
let act = |
| 7549 |
quasi_router::Act::new("Copy", quasi_router::Action::local()).copying("mnw_live_abc123"); |
| 7550 |
let html = fragment(&Node::Act(act)); |
| 7551 |
assert!(html.contains("data-copies=\"mnw_live_abc123\""), "{html}"); |
| 7552 |
assert!(!html.contains("hx-get"), "{html}"); |
| 7553 |
assert!(!html.contains("hx-post"), "{html}"); |
| 7554 |
} |
| 7555 |
|
| 7556 |
#[test] |
| 7557 |
fn a_copied_value_is_escaped_and_never_a_program() { |
| 7558 |
|
| 7559 |
|
| 7560 |
let act = |
| 7561 |
quasi_router::Act::new("Copy", quasi_router::Action::local()).copying(r#""; alert(1); ""#); |
| 7562 |
let html = fragment(&Node::Act(act)); |
| 7563 |
assert!(!html.contains("alert(1);\""), "{html}"); |
| 7564 |
assert!(html.contains("""), "{html}"); |
| 7565 |
} |
| 7566 |
|
| 7567 |
#[test] |
| 7568 |
fn the_copy_script_reads_the_hook_the_emitter_writes() { |
| 7569 |
assert!(crate::COPY_JS.contains("data-copies"), "the value hook"); |
| 7570 |
|
| 7571 |
assert!(crate::COPY_JS.contains("addEventListener")); |
| 7572 |
|
| 7573 |
assert!(!crate::COPY_JS.contains("new Function"), "no eval"); |
| 7574 |
assert!( |
| 7575 |
!crate::COPY_JS.contains("innerHTML"), |
| 7576 |
"no markup from a value" |
| 7577 |
); |
| 7578 |
|
| 7579 |
|
| 7580 |
|
| 7581 |
assert!( |
| 7582 |
!crate::COPY_JS.contains("textContent"), |
| 7583 |
"no temporary label" |
| 7584 |
); |
| 7585 |
assert!(!crate::COPY_JS.contains("setTimeout"), "no revert timer"); |
| 7586 |
} |
| 7587 |
|
| 7588 |
#[test] |
| 7589 |
fn the_shell_links_the_copy_script_and_can_drop_it() { |
| 7590 |
let with = Webview::new().screen(&Screen::list_detail("A", false)); |
| 7591 |
assert!(with.contains("/static/quasi-copy.js"), "{with}"); |
| 7592 |
|
| 7593 |
let under = Webview::under("/assets").screen(&Screen::list_detail("A", false)); |
| 7594 |
assert!(under.contains("/assets/quasi-copy.js"), "{under}"); |
| 7595 |
|
| 7596 |
|
| 7597 |
let bare = Webview::new() |
| 7598 |
.with_shell(Shell::default().without_htmx()) |
| 7599 |
.screen(&Screen::list_detail("A", false)); |
| 7600 |
assert!(bare.contains("quasi-copy.js"), "{bare}"); |
| 7601 |
|
| 7602 |
let without = Webview::new() |
| 7603 |
.with_shell(Shell::default().without_copy()) |
| 7604 |
.screen(&Screen::list_detail("A", false)); |
| 7605 |
assert!(!without.contains("quasi-copy.js"), "{without}"); |
| 7606 |
} |
| 7607 |
|
| 7608 |
#[test] |
| 7609 |
fn a_control_can_show_a_picture_and_still_say_its_name() { |
| 7610 |
|
| 7611 |
|
| 7612 |
|
| 7613 |
|
| 7614 |
use quasi_router::screen::Image; |
| 7615 |
let act = quasi_router::Act::new("kick.wav", quasi_router::Action::get("/media/1")) |
| 7616 |
.showing(Image::new("/m/1.png", "kick.wav")); |
| 7617 |
let html = fragment(&Node::Act(act)); |
| 7618 |
|
| 7619 |
let control = html |
| 7620 |
.split_once("<a") |
| 7621 |
.map(|(_, rest)| rest.split_once("</a>").map_or(rest, |(inner, _)| inner)) |
| 7622 |
.unwrap_or_default(); |
| 7623 |
assert!(control.contains("<img"), "the picture is inside it: {html}"); |
| 7624 |
assert!(control.contains("kick.wav"), "and so is the name: {html}"); |
| 7625 |
} |
| 7626 |
|
| 7627 |
#[test] |
| 7628 |
fn a_shown_picture_is_the_same_element_a_region_body_writes() { |
| 7629 |
|
| 7630 |
|
| 7631 |
|
| 7632 |
use quasi_router::screen::Image; |
| 7633 |
let picture = Image::new("/m/1.png", "kick.wav").lazy(); |
| 7634 |
let in_body = fragment(&Node::Image(picture.clone())); |
| 7635 |
let in_control = fragment(&Node::Act( |
| 7636 |
quasi_router::Act::new("kick.wav", quasi_router::Action::get("/media/1")).showing(picture), |
| 7637 |
)); |
| 7638 |
let tag = |html: &str| { |
| 7639 |
let at = html.find("<img").expect("an img"); |
| 7640 |
html[at..].split_once('>').expect("it closes").0.to_owned() |
| 7641 |
}; |
| 7642 |
assert_eq!(tag(&in_body), tag(&in_control), "{in_body} / {in_control}"); |
| 7643 |
assert!(tag(&in_body).contains("loading=\"lazy\"")); |
| 7644 |
} |
| 7645 |
|
| 7646 |
#[test] |
| 7647 |
fn a_control_showing_nothing_is_exactly_what_it_was() { |
| 7648 |
|
| 7649 |
let plain = fragment(&Node::act("Save", quasi_router::Action::post("/save"))); |
| 7650 |
assert!(!plain.contains("<img"), "{plain}"); |
| 7651 |
} |
| 7652 |
|
| 7653 |
|
| 7654 |
|
| 7655 |
|
| 7656 |
#[test] |
| 7657 |
fn an_anchored_menu_is_aimed_at_the_region_it_names() { |
| 7658 |
use quasi_http::Serves; |
| 7659 |
|
| 7660 |
let screen = Screen::list_detail("Files", false) |
| 7661 |
.with(Slot::new("browser", RegionKind::Pane).with(Node::text("rows"))); |
| 7662 |
let html = render(&screen); |
| 7663 |
|
| 7664 |
assert!( |
| 7665 |
html.contains("id=\"browser-anchored\" data-menu=\"anchored\" hidden></div>"), |
| 7666 |
"{html}" |
| 7667 |
); |
| 7668 |
assert_eq!( |
| 7669 |
Webview::new().anchored_target(&quasi_router::Anchor::Region("browser".into())), |
| 7670 |
Some("browser-anchored".to_owned()) |
| 7671 |
); |
| 7672 |
} |
| 7673 |
|
| 7674 |
|
| 7675 |
|
| 7676 |
#[test] |
| 7677 |
fn only_a_named_control_carries_an_anchor_container() { |
| 7678 |
use quasi_http::Serves; |
| 7679 |
|
| 7680 |
let named = render( |
| 7681 |
&Screen::list_detail("Files", false).with( |
| 7682 |
Slot::new("bar", RegionKind::Band) |
| 7683 |
.with(Node::Act(Act::new("Sort", Action::get("/sort")).id("sort"))), |
| 7684 |
), |
| 7685 |
); |
| 7686 |
assert!(named.contains("id=\"sort\""), "{named}"); |
| 7687 |
assert!( |
| 7688 |
named.contains("id=\"sort-anchored\" data-menu=\"anchored\" hidden></div>"), |
| 7689 |
"{named}" |
| 7690 |
); |
| 7691 |
|
| 7692 |
let bare = render(&Screen::list_detail("Files", false).with( |
| 7693 |
Slot::new("bar", RegionKind::Band).with(Node::Act(Act::new("Sort", Action::get("/sort")))), |
| 7694 |
)); |
| 7695 |
assert!(!bare.contains("sort-anchored"), "{bare}"); |
| 7696 |
assert_eq!( |
| 7697 |
Webview::new().anchored_target(&quasi_router::Anchor::Control("sort".into())), |
| 7698 |
Some("sort-anchored".to_owned()) |
| 7699 |
); |
| 7700 |
} |
| 7701 |
|
| 7702 |
|
| 7703 |
|
| 7704 |
#[test] |
| 7705 |
fn the_selection_container_arrives_with_the_selection() { |
| 7706 |
use quasi_http::Serves; |
| 7707 |
|
| 7708 |
let plain = render(&Screen::list_detail("Files", false)); |
| 7709 |
assert!(!plain.contains("quasi-selection-anchored"), "{plain}"); |
| 7710 |
|
| 7711 |
let selecting = render(&Screen::list_detail("Files", false).selecting("chosen")); |
| 7712 |
assert!( |
| 7713 |
selecting.contains("id=\"quasi-selection-anchored\""), |
| 7714 |
"{selecting}" |
| 7715 |
); |
| 7716 |
assert_eq!( |
| 7717 |
Webview::new().anchored_target(&quasi_router::Anchor::Selection), |
| 7718 |
Some("quasi-selection-anchored".to_owned()) |
| 7719 |
); |
| 7720 |
} |
| 7721 |
|
| 7722 |
|
| 7723 |
|
| 7724 |
|
| 7725 |
|
| 7726 |
#[test] |
| 7727 |
fn an_anchor_that_cannot_be_an_id_is_aimed_at_nothing() { |
| 7728 |
use quasi_http::Serves; |
| 7729 |
|
| 7730 |
assert_eq!( |
| 7731 |
Webview::new().anchored_target(&quasi_router::Anchor::Region("has space".into())), |
| 7732 |
None |
| 7733 |
); |
| 7734 |
|
| 7735 |
|
| 7736 |
let html = |
| 7737 |
render(&Screen::list_detail("Files", false).with(Slot::new("has space", RegionKind::Pane))); |
| 7738 |
assert!(!html.contains("data-menu=\"anchored\""), "{html}"); |
| 7739 |
} |
| 7740 |
|
| 7741 |
|
| 7742 |
|
| 7743 |
|
| 7744 |
#[test] |
| 7745 |
fn a_bespoke_regions_container_sits_outside_it() { |
| 7746 |
let html = |
| 7747 |
render(&Screen::list_detail("Files", false).with(Slot::handover("player", "media-player"))); |
| 7748 |
assert!( |
| 7749 |
html.contains("data-bespoke=\"media-player\"></div>"), |
| 7750 |
"{html}" |
| 7751 |
); |
| 7752 |
assert!( |
| 7753 |
html.contains("</div><div class=\"anchored\" id=\"player-anchored\""), |
| 7754 |
"{html}" |
| 7755 |
); |
| 7756 |
} |
| 7757 |
|
| 7758 |
|
| 7759 |
|
| 7760 |
|
| 7761 |
#[test] |
| 7762 |
fn a_code_block_writes_one_span_per_classified_run_and_none_for_plain() { |
| 7763 |
use quasi_router::screen::Lexeme; |
| 7764 |
|
| 7765 |
let html = Webview::new().fragment(&Node::Code { |
| 7766 |
runs: vec![ |
| 7767 |
Lexeme::new("fn", layout::Syntax::Keyword), |
| 7768 |
Lexeme::plain(" main() { "), |
| 7769 |
Lexeme::new("\"hi\"", layout::Syntax::String), |
| 7770 |
Lexeme::plain(" }"), |
| 7771 |
], |
| 7772 |
language: Some("rust".to_owned()), |
| 7773 |
inline: false, |
| 7774 |
}); |
| 7775 |
|
| 7776 |
assert!(html.contains("<pre"), "{html}"); |
| 7777 |
assert!(html.contains("data-language=\"rust\""), "{html}"); |
| 7778 |
assert!( |
| 7779 |
html.contains("<span class=\"lex-keyword\">fn</span>"), |
| 7780 |
"{html}" |
| 7781 |
); |
| 7782 |
assert!( |
| 7783 |
html.contains("<span class=\"lex-string\">"hi"</span>") |
| 7784 |
|| html.contains("<span class=\"lex-string\">\"hi\"</span>"), |
| 7785 |
"{html}" |
| 7786 |
); |
| 7787 |
|
| 7788 |
assert_eq!(html.matches("<span class=\"lex-").count(), 2, "{html}"); |
| 7789 |
assert!(html.contains(" main() { "), "{html}"); |
| 7790 |
} |
| 7791 |
|
| 7792 |
|
| 7793 |
|
| 7794 |
#[test] |
| 7795 |
fn a_code_block_loses_no_character_of_its_source() { |
| 7796 |
use quasi_router::screen::Lexeme; |
| 7797 |
|
| 7798 |
let source = " let x = 1;\n // done\n"; |
| 7799 |
let html = Webview::new().fragment(&Node::Code { |
| 7800 |
runs: vec![ |
| 7801 |
Lexeme::plain(" let x = "), |
| 7802 |
Lexeme::new("1", layout::Syntax::Constant), |
| 7803 |
Lexeme::plain(";\n "), |
| 7804 |
Lexeme::new("// done", layout::Syntax::Comment), |
| 7805 |
Lexeme::plain("\n"), |
| 7806 |
], |
| 7807 |
language: None, |
| 7808 |
inline: false, |
| 7809 |
}); |
| 7810 |
let stripped = html |
| 7811 |
.replace("<span class=\"lex-constant\">", "") |
| 7812 |
.replace("<span class=\"lex-comment\">", "") |
| 7813 |
.replace("</span>", ""); |
| 7814 |
let body = stripped |
| 7815 |
.split_once('>') |
| 7816 |
.expect("an opening tag") |
| 7817 |
.1 |
| 7818 |
.rsplit_once("</pre>") |
| 7819 |
.expect("a closing tag") |
| 7820 |
.0; |
| 7821 |
assert_eq!(body, source, "{html}"); |
| 7822 |
} |
| 7823 |
|
| 7824 |
|
| 7825 |
|
| 7826 |
#[test] |
| 7827 |
fn an_inline_literal_is_a_code_element_and_not_a_block() { |
| 7828 |
use quasi_router::screen::Lexeme; |
| 7829 |
|
| 7830 |
let html = Webview::new().fragment(&Node::Code { |
| 7831 |
runs: vec![Lexeme::plain("git clone https://example.com/r.git")], |
| 7832 |
language: None, |
| 7833 |
inline: true, |
| 7834 |
}); |
| 7835 |
assert!(html.contains("<code"), "{html}"); |
| 7836 |
assert!(!html.contains("<pre"), "{html}"); |
| 7837 |
} |
| 7838 |
|
| 7839 |
|
| 7840 |
|
| 7841 |
#[test] |
| 7842 |
fn only_a_diff_row_carries_a_change_and_an_ordinary_row_carries_none() { |
| 7843 |
use quasi_router::screen::Column; |
| 7844 |
|
| 7845 |
let diff = Webview::new().fragment(&Node::Table { |
| 7846 |
marks: ::quasi_router::stage::Marks::none(), |
| 7847 |
columns: vec![Column::new("line")], |
| 7848 |
rows: vec![ |
| 7849 |
Row::cells(["+ added"]).changed(layout::Change::Added), |
| 7850 |
Row::cells(["- gone"]).changed(layout::Change::Removed), |
| 7851 |
Row::cells([" same"]).changed(layout::Change::Context), |
| 7852 |
], |
| 7853 |
more: None, |
| 7854 |
}); |
| 7855 |
assert!(diff.contains("data-change=\"added\""), "{diff}"); |
| 7856 |
assert!(diff.contains("data-change=\"removed\""), "{diff}"); |
| 7857 |
assert!(diff.contains("data-change=\"context\""), "{diff}"); |
| 7858 |
|
| 7859 |
|
| 7860 |
assert!(diff.contains("data-tone=\"success\""), "{diff}"); |
| 7861 |
assert!(diff.contains("data-tone=\"danger\""), "{diff}"); |
| 7862 |
|
| 7863 |
let plain = Webview::new().fragment(&Node::Table { |
| 7864 |
marks: ::quasi_router::stage::Marks::none(), |
| 7865 |
columns: vec![Column::new("name")], |
| 7866 |
rows: vec![Row::cells(["kick.wav"])], |
| 7867 |
more: None, |
| 7868 |
}); |
| 7869 |
assert!(!plain.contains("data-change"), "{plain}"); |
| 7870 |
} |
| 7871 |
|
| 7872 |
|
| 7873 |
|
| 7874 |
|
| 7875 |
|
| 7876 |
|
| 7877 |
#[test] |
| 7878 |
fn a_row_that_names_itself_says_so() { |
| 7879 |
let html = fragment(&Node::Table { |
| 7880 |
marks: ::quasi_router::stage::Marks::none(), |
| 7881 |
columns: vec![Column::new("Line").width(layout::Width::Fill)], |
| 7882 |
rows: vec![Row::cells([Cell::new("fn main() {}")]).identified("L1")], |
| 7883 |
more: None, |
| 7884 |
}); |
| 7885 |
|
| 7886 |
assert!(html.contains("data-value=\"L1\""), "{html}"); |
| 7887 |
|
| 7888 |
|
| 7889 |
assert!(!html.contains("id=\"L1\""), "{html}"); |
| 7890 |
} |
| 7891 |
|
| 7892 |
|
| 7893 |
|
| 7894 |
|
| 7895 |
|
| 7896 |
|
| 7897 |
|
| 7898 |
|
| 7899 |
#[test] |
| 7900 |
fn a_row_that_carries_an_address_is_reachable_at_it() { |
| 7901 |
let html = fragment(&Node::Table { |
| 7902 |
marks: ::quasi_router::stage::Marks::none(), |
| 7903 |
columns: vec![Column::new("Line").width(layout::Width::Fill)], |
| 7904 |
rows: vec![Row::cells([Cell::new("fn main() {}")]).addressed("L1")], |
| 7905 |
more: None, |
| 7906 |
}); |
| 7907 |
|
| 7908 |
assert!(html.contains("id=\"L1\""), "{html}"); |
| 7909 |
|
| 7910 |
|
| 7911 |
assert!(!html.contains("data-value"), "{html}"); |
| 7912 |
|
| 7913 |
|
| 7914 |
let list = fragment(&Node::Table { |
| 7915 |
marks: ::quasi_router::stage::Marks::none(), |
| 7916 |
columns: Vec::new(), |
| 7917 |
rows: vec![Row::new("fn main() {}").addressed("L1")], |
| 7918 |
more: None, |
| 7919 |
}); |
| 7920 |
assert!(list.contains("id=\"L1\""), "{list}"); |
| 7921 |
|
| 7922 |
|
| 7923 |
let hostile = fragment(&Node::Table { |
| 7924 |
marks: ::quasi_router::stage::Marks::none(), |
| 7925 |
columns: Vec::new(), |
| 7926 |
rows: vec![Row::new("x").addressed("a\"onload=\"x")], |
| 7927 |
more: None, |
| 7928 |
}); |
| 7929 |
assert!(!hostile.contains("onload=\"x\""), "{hostile}"); |
| 7930 |
} |
| 7931 |
|