| 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 |
Act, Cell, Cells, Choice, Column, Field, Figure, Meter, Prose, Rest, Row, Tag, |
| 14 |
}; |
| 15 |
use quasi_router::{Action, Node, RegionKind, Screen, Slot}; |
| 16 |
|
| 17 |
use crate::{Emit, Shell, Webview}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
fn strip_discovery(head: &str) -> String { |
| 25 |
let mut out = String::with_capacity(head.len()); |
| 26 |
let mut rest = head; |
| 27 |
while let Some(start) = rest.find("<meta property=\"og:").or_else(|| { |
| 28 |
rest.find("<meta name=\"twitter:") |
| 29 |
.or_else(|| rest.find("<meta name=\"robots\"")) |
| 30 |
.or_else(|| rest.find("<link rel=\"canonical\"")) |
| 31 |
}) { |
| 32 |
out.push_str(&rest[..start]); |
| 33 |
let end = rest[start..].find('>').expect("a tag closes") + start + 1; |
| 34 |
rest = &rest[end..]; |
| 35 |
} |
| 36 |
out.push_str(rest); |
| 37 |
out |
| 38 |
} |
| 39 |
|
| 40 |
fn render(screen: &Screen) -> String { |
| 41 |
Webview::new().screen(screen) |
| 42 |
} |
| 43 |
|
| 44 |
fn fragment(node: &Node) -> String { |
| 45 |
Webview::new().fragment(node) |
| 46 |
} |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn a_screen_is_a_whole_document() { |
| 50 |
let html = render(&Screen::list_detail("Tasks", false)); |
| 51 |
assert!(html.starts_with("<!doctype html><html lang=\"en\">")); |
| 52 |
assert!(html.contains("<title>Tasks</title>")); |
| 53 |
assert!(html.ends_with("</body></html>")); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn a_fragment_is_not() { |
| 58 |
let html = fragment(&Node::text("hello")); |
| 59 |
assert!(!html.contains("<html")); |
| 60 |
assert!(!html.contains("<body")); |
| 61 |
assert_eq!(html, "<p class=\"text\">hello</p>"); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn the_document_carries_the_response_handling_config() { |
| 66 |
|
| 67 |
|
| 68 |
let html = render(&Screen::list_detail("Tasks", false)); |
| 69 |
assert!(html.contains(quasi_http::htmx::CONFIG_META)); |
| 70 |
assert!(html.contains(r#"{"code":"[45]..","swap":true,"error":true}"#)); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn the_title_is_escaped_into_the_head() { |
| 75 |
let html = render(&Screen::list_detail("</title><script>x()</script>", false)); |
| 76 |
assert!(!html.contains("<script>x()")); |
| 77 |
assert!(html.contains("</title>")); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn morph_is_emitted_only_when_the_extension_is_loaded() { |
| 82 |
let with = render(&Screen::list_detail("Tasks", false)); |
| 83 |
assert!(with.contains("hx-ext=\"morph\"")); |
| 84 |
|
| 85 |
let without = Webview::new() |
| 86 |
.with_shell(Shell::default().without_morph()) |
| 87 |
.screen(&Screen::list_detail("Tasks", false)); |
| 88 |
assert!(!without.contains("hx-ext=\"morph\"")); |
| 89 |
assert!(!without.contains("idiomorph")); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
let screen = Screen::list_detail("Tasks", false) |
| 95 |
.with(Slot::new("main", RegionKind::Pane).with(Node::act("Go", Action::get("/go")))); |
| 96 |
let quiet = Webview::new() |
| 97 |
.with_shell(Shell::default().without_morph()) |
| 98 |
.screen(&screen); |
| 99 |
assert!(!quiet.contains("hx-swap")); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn an_action_becomes_the_verb_it_names() { |
| 104 |
let get = fragment(&Node::act("Open", Action::get("/tasks/1"))); |
| 105 |
assert!(get.contains("hx-get=\"/tasks/1\"")); |
| 106 |
assert!(!get.contains("hx-post")); |
| 107 |
|
| 108 |
let post = fragment(&Node::act("Delete", Action::post("/tasks/1/delete"))); |
| 109 |
assert!(post.contains("hx-post=\"/tasks/1/delete\"")); |
| 110 |
assert!(!post.contains("hx-get")); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn a_delete_and_a_put_reach_the_route_the_server_actually_answers() { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
let removed = fragment(&Node::act("Delete", Action::delete("/api/blog/7"))); |
| 120 |
assert!(removed.contains("hx-delete=\"/api/blog/7\""), "{removed}"); |
| 121 |
|
| 122 |
let replaced = fragment(&Node::act("Move", Action::put("/api/items/7/move"))); |
| 123 |
assert!( |
| 124 |
replaced.contains("hx-put=\"/api/items/7/move\""), |
| 125 |
"{replaced}" |
| 126 |
); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
for html in [&removed, &replaced] { |
| 131 |
assert!(html.contains("<button"), "{html}"); |
| 132 |
assert!(!html.contains("href"), "{html}"); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn a_read_of_a_route_is_a_link_and_a_write_is_not() { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
let read = fragment(&Node::act("Open", Action::get("/tasks/1"))); |
| 144 |
assert!(read.contains("<a ")); |
| 145 |
assert!(read.contains("href=\"/tasks/1\"")); |
| 146 |
assert!(read.contains("hx-get=\"/tasks/1\"")); |
| 147 |
assert!(!read.contains("<button")); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
let write = fragment(&Node::act("Delete", Action::post("/tasks/1/delete"))); |
| 154 |
assert!(write.contains("<button")); |
| 155 |
assert!(!write.contains("href")); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
let away = fragment(&Node::act("Docs", Action::external("https://example.com"))); |
| 160 |
assert!(away.contains("href=\"https://example.com\"")); |
| 161 |
assert!(away.contains("rel=\"noopener noreferrer\"")); |
| 162 |
assert!(!away.contains("hx-get")); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn a_reads_values_are_its_address_and_nothing_is_sent_beside_it() { |
| 167 |
let action = Action::get("/tasks") |
| 168 |
.with("filter", "open") |
| 169 |
.with("sort", "due"); |
| 170 |
let html = fragment(&Node::act("Filter", action)); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
assert!(html.contains("hx-get=\"/tasks?filter=open&sort=due\"")); |
| 175 |
assert!(!html.contains("hx-vals")); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
assert!(html.contains("href=\"/tasks?filter=open&sort=due\"")); |
| 181 |
} |
| 182 |
|
| 183 |
#[test] |
| 184 |
fn a_write_from_a_filtered_list_keeps_the_two_status_values_apart() { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
let action = Action::post("/problems/7/status") |
| 191 |
.with("status", "Dismissed") |
| 192 |
.carrying("status", "Open"); |
| 193 |
let html = fragment(&Node::act("Dismiss", action)); |
| 194 |
|
| 195 |
assert!(html.contains("hx-post=\"/problems/7/status?status=Open\"")); |
| 196 |
assert!(html.contains("hx-vals=\"{"status":"Dismissed"}\"")); |
| 197 |
|
| 198 |
|
| 199 |
assert!(!html.contains("href=")); |
| 200 |
} |
| 201 |
|
| 202 |
#[test] |
| 203 |
fn a_repeated_name_survives_the_wire_rather_than_collapsing() { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
let action = Action::get("/tags") |
| 209 |
.with("tag", "rust") |
| 210 |
.with("tag", "router"); |
| 211 |
let html = fragment(&Node::act("Both", action)); |
| 212 |
|
| 213 |
assert!(html.contains("hx-get=\"/tags?tag=rust&tag=router\"")); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn a_param_value_cannot_escape_the_attribute_or_the_json() { |
| 218 |
let action = Action::post("/search").with("q", "\" onload=\"steal()"); |
| 219 |
let html = fragment(&Node::act("Search", action)); |
| 220 |
|
| 221 |
assert!(!html.contains("onload=\"steal()")); |
| 222 |
assert!(html.contains("\\"")); |
| 223 |
|
| 224 |
let action = Action::post("/search").with("q", "a\\b\nc"); |
| 225 |
let html = fragment(&Node::act("Search", action)); |
| 226 |
assert!(html.contains("a\\\\b\\nc")); |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn no_control_ever_names_its_own_target() { |
| 231 |
|
| 232 |
|
| 233 |
let screen = Screen::list_detail("Tasks", false) |
| 234 |
.with( |
| 235 |
Slot::new("list", RegionKind::Pane) |
| 236 |
.with(Node::list([ |
| 237 |
Row::new("One").activate(Action::get("/tasks/1")) |
| 238 |
])) |
| 239 |
.with(Node::act("New", Action::post("/tasks"))), |
| 240 |
) |
| 241 |
.with(Slot::new("detail", RegionKind::Pane).with(Node::text("Nothing selected"))); |
| 242 |
|
| 243 |
let html = render(&screen); |
| 244 |
assert!(!html.contains("hx-target")); |
| 245 |
assert!(!html.contains("hx-retarget")); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
fn htmx_lines(source: &str) -> usize { |
| 254 |
source |
| 255 |
.lines() |
| 256 |
.filter(|line| !line.trim_start().starts_with("//")) |
| 257 |
.filter(|line| line.contains("hx-")) |
| 258 |
.count() |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
fn body_of<'a>(source: &'a str, signature: &str) -> &'a str { |
| 263 |
source |
| 264 |
.split(signature) |
| 265 |
.nth(1) |
| 266 |
.unwrap_or_else(|| panic!("{signature} exists")) |
| 267 |
.split("\n/// ") |
| 268 |
.next() |
| 269 |
.expect("the function has a body") |
| 270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn htmx_enters_in_exactly_two_functions() { |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
let source = include_str!("node.rs"); |
| 289 |
let seam = ["pub(crate) fn action_attrs", "pub(crate) fn oob_html"] |
| 290 |
.iter() |
| 291 |
.map(|signature| htmx_lines(body_of(source, signature))) |
| 292 |
.sum::<usize>(); |
| 293 |
|
| 294 |
assert_eq!( |
| 295 |
htmx_lines(source), |
| 296 |
seam, |
| 297 |
"every emitted hx- attribute should come from action_attrs or oob_html" |
| 298 |
); |
| 299 |
assert!(seam >= 4, "verb, vals, swap and the out-of-band address"); |
| 300 |
} |
| 301 |
|
| 302 |
#[test] |
| 303 |
fn a_disabled_control_carries_no_address() { |
| 304 |
let act = Act::new("Delete", Action::post("/tasks/1/delete")).disabled(); |
| 305 |
let html = fragment(&Node::Act(act)); |
| 306 |
|
| 307 |
assert!(html.contains("disabled")); |
| 308 |
assert!(!html.contains("hx-post")); |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn a_row_is_a_control_only_when_selecting_it_does_something() { |
| 313 |
|
| 314 |
let live = fragment(&Node::list([Row::new("One").activate(Action::get("/1"))])); |
| 315 |
assert!(live.contains("<a class=\"row-activate\"")); |
| 316 |
assert!(live.contains("href=\"/1\"")); |
| 317 |
|
| 318 |
let inert = fragment(&Node::list([Row::new("One")])); |
| 319 |
assert!(!inert.contains("<button")); |
| 320 |
assert!(!inert.contains("<a ")); |
| 321 |
assert!(inert.contains("<span class=\"row-primary\">One</span>")); |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn the_current_row_says_so_to_a_screen_reader() { |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
let html = fragment(&Node::list([Row { |
| 330 |
current: true, |
| 331 |
..Row::new("One") |
| 332 |
}])); |
| 333 |
assert!(html.contains("aria-current=\"true\"")); |
| 334 |
assert!(html.contains("row-current")); |
| 335 |
|
| 336 |
assert!(!html.contains("type=\"checkbox\"")); |
| 337 |
} |
| 338 |
|
| 339 |
#[test] |
| 340 |
fn a_selectable_row_gets_a_real_checkbox_and_an_unselectable_one_gets_nothing() { |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
let untickable = fragment(&Node::list([Row::new("One")])); |
| 345 |
assert!(!untickable.contains("type=\"checkbox\"")); |
| 346 |
|
| 347 |
let unticked = fragment(&Node::list([Row::new("One").selectable(false)])); |
| 348 |
assert!(unticked.contains("type=\"checkbox\"")); |
| 349 |
assert!(!unticked.contains(" checked")); |
| 350 |
assert!(!unticked.contains("row-selected")); |
| 351 |
|
| 352 |
let ticked = fragment(&Node::list([Row::new("One").selectable(true)])); |
| 353 |
assert!(ticked.contains("type=\"checkbox\"")); |
| 354 |
assert!(ticked.contains(" checked")); |
| 355 |
assert!(ticked.contains("row-selected")); |
| 356 |
|
| 357 |
assert!(!ticked.contains("aria-current")); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn a_row_carries_its_tokens_as_tokens_rather_than_as_joined_text() { |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
let html = fragment(&Node::list([Row::new("Mine") |
| 366 |
.meta("3 files") |
| 367 |
.token(Tag::badge("Side Project")) |
| 368 |
.token(Tag::badge("On Hold").tone(layout::Tone::Warning))])); |
| 369 |
|
| 370 |
assert!(html.contains("class=\"row-tokens\"")); |
| 371 |
assert!(html.contains("On Hold")); |
| 372 |
|
| 373 |
assert!(html.contains("warning")); |
| 374 |
|
| 375 |
assert!(html.contains("class=\"row-meta\">3 files</span>")); |
| 376 |
} |
| 377 |
|
| 378 |
#[test] |
| 379 |
fn an_external_destination_is_an_anchor_and_never_a_route() { |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
let html = fragment(&Node::act( |
| 384 |
"Profile", |
| 385 |
Action::external("https://example.com/@ada"), |
| 386 |
)); |
| 387 |
|
| 388 |
assert!(html.contains("<a ")); |
| 389 |
assert!(html.contains("href=\"https://example.com/@ada\"")); |
| 390 |
assert!(html.contains("rel=\"noopener noreferrer\"")); |
| 391 |
|
| 392 |
assert!(!html.contains("hx-get")); |
| 393 |
assert!(!html.contains("hx-post")); |
| 394 |
assert!(!html.contains("<button")); |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
fn an_external_url_cannot_break_out_of_its_own_attribute() { |
| 399 |
let html = fragment(&Node::act( |
| 400 |
"Profile", |
| 401 |
Action::external("\"><script>alert(1)</script>"), |
| 402 |
)); |
| 403 |
assert!(!html.contains("<script>")); |
| 404 |
} |
| 405 |
|
| 406 |
#[test] |
| 407 |
fn a_meter_renders_through_makeover_and_not_through_a_second_emitter() { |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
let html = fragment(&Node::Meter( |
| 412 |
Meter::new(3, 7) |
| 413 |
.tone(layout::Tone::Success) |
| 414 |
.label("subtasks"), |
| 415 |
)); |
| 416 |
assert_eq!( |
| 417 |
html, |
| 418 |
makeover_webview::meter::meter_html( |
| 419 |
&layout::Meter::new(3, 7) |
| 420 |
.tone(layout::Tone::Success) |
| 421 |
.label("subtasks"), |
| 422 |
&Emit::default(), |
| 423 |
) |
| 424 |
); |
| 425 |
assert!(html.contains(r#"aria-label="3 of 7 subtasks""#)); |
| 426 |
} |
| 427 |
|
| 428 |
#[test] |
| 429 |
fn a_link_and_a_figure_in_a_row_cost_no_release() { |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
let html = fragment(&Node::list([Row::new("Invoice 41") |
| 436 |
.part( |
| 437 |
layout::RowPart::Meta, |
| 438 |
Node::Link { |
| 439 |
text: "makenot.work".into(), |
| 440 |
action: Action::get("/sites/1"), |
| 441 |
}, |
| 442 |
) |
| 443 |
.part( |
| 444 |
layout::RowPart::Tokens, |
| 445 |
Node::Figure(Figure::new("41", "days")), |
| 446 |
)])); |
| 447 |
|
| 448 |
assert!(html.contains("makenot.work"), "{html}"); |
| 449 |
assert!(html.contains("/sites/1"), "{html}"); |
| 450 |
assert!(html.contains("41"), "{html}"); |
| 451 |
|
| 452 |
|
| 453 |
assert!(html.contains("class=\"row-meta\""), "{html}"); |
| 454 |
assert!(html.contains("class=\"row-tokens\""), "{html}"); |
| 455 |
} |
| 456 |
|
| 457 |
#[test] |
| 458 |
#[should_panic(expected = "a row is an inline run and holds leaves")] |
| 459 |
fn a_row_refuses_a_block() { |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
let _ = Row::new("Ship it").part( |
| 464 |
layout::RowPart::Meta, |
| 465 |
Node::List { |
| 466 |
rows: Vec::new(), |
| 467 |
more: None, |
| 468 |
}, |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn a_rows_parts_draw_in_the_order_the_description_says_them() { |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
let html = fragment(&Node::list([Row::new("Ship it") |
| 478 |
.token(Tag::badge("beta")) |
| 479 |
.meta("2 files")])); |
| 480 |
|
| 481 |
let tokens = html.find("row-tokens").expect("a tokens strip"); |
| 482 |
let meta = html.find("row-meta").expect("a meta part"); |
| 483 |
assert!(tokens < meta, "{html}"); |
| 484 |
} |
| 485 |
|
| 486 |
#[test] |
| 487 |
fn consecutive_parts_of_one_role_share_a_strip() { |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
let html = fragment(&Node::list([Row::new("Ship it") |
| 493 |
.token(Tag::badge("beta")) |
| 494 |
.token(Tag::badge("draft"))])); |
| 495 |
|
| 496 |
assert_eq!(html.matches("row-tokens").count(), 1, "{html}"); |
| 497 |
assert!(html.contains("beta"), "{html}"); |
| 498 |
assert!(html.contains("draft"), "{html}"); |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn a_proportion_in_a_row_is_a_bar_and_not_flattened_text() { |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
let html = fragment(&Node::list([ |
| 508 |
Row::new("Ship it").meter(Meter::new(3, 7).label("subtasks")) |
| 509 |
])); |
| 510 |
|
| 511 |
assert!(html.contains("class=\"row-proportion\"")); |
| 512 |
assert!(html.contains(r#"aria-label="3 of 7 subtasks""#)); |
| 513 |
|
| 514 |
|
| 515 |
assert!(html.contains(&makeover_webview::meter::meter_html( |
| 516 |
&layout::Meter::new(3, 7).label("subtasks"), |
| 517 |
&Emit::default(), |
| 518 |
))); |
| 519 |
} |
| 520 |
|
| 521 |
#[test] |
| 522 |
fn a_strip_of_figures_is_one_node_because_a_renderer_cannot_infer_a_set() { |
| 523 |
|
| 524 |
|
| 525 |
let html = fragment(&Node::stats([ |
| 526 |
Figure::new("17", "Current Streak"), |
| 527 |
Figure::new("84%", "Completion Rate").tone(layout::Tone::Success), |
| 528 |
])); |
| 529 |
|
| 530 |
assert!(html.starts_with("<div class=\"figures\">")); |
| 531 |
assert!(html.contains(r#"aria-label="Current Streak: 17""#)); |
| 532 |
assert!(html.contains(r#"data-tone="success""#)); |
| 533 |
|
| 534 |
assert!(!html.contains("<button")); |
| 535 |
} |
| 536 |
|
| 537 |
#[test] |
| 538 |
fn a_figure_whose_value_is_a_control_becomes_one_without_a_second_emitter() { |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
let html = fragment(&Node::Stats { |
| 543 |
figures: vec![( |
| 544 |
Figure::new("3", "Not Applied").tone(layout::Tone::Warning), |
| 545 |
Some(Action::get("/settings/held")), |
| 546 |
)], |
| 547 |
}); |
| 548 |
|
| 549 |
assert!(html.contains("<a class=\"figure-act\"")); |
| 550 |
assert!(html.contains("href=\"/settings/held\"")); |
| 551 |
assert!(html.contains("hx-get=\"/settings/held\"")); |
| 552 |
|
| 553 |
assert!(html.contains(&makeover_webview::figure::figure_html( |
| 554 |
&layout::Figure::new("3", "Not Applied").tone(layout::Tone::Warning), |
| 555 |
&Emit::default(), |
| 556 |
))); |
| 557 |
} |
| 558 |
|
| 559 |
#[test] |
| 560 |
fn a_meter_that_ran_over_says_so_after_the_width_is_clamped() { |
| 561 |
|
| 562 |
|
| 563 |
let html = fragment(&Node::Meter( |
| 564 |
Meter::new(45, 30) |
| 565 |
.tone(layout::Tone::Danger) |
| 566 |
.label("minutes"), |
| 567 |
)); |
| 568 |
assert!(html.contains("width: 100%")); |
| 569 |
assert!(html.contains(r#"data-over="true""#)); |
| 570 |
assert!(html.contains(r#"aria-label="45 of 30 minutes""#)); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn a_badge_is_not_a_button_and_a_chip_is() { |
| 575 |
let badge = fragment(&Node::Token(Tag { |
| 576 |
kind: layout::Token::Badge, |
| 577 |
label: "3".into(), |
| 578 |
tone: layout::Tone::Info, |
| 579 |
latched: false, |
| 580 |
action: Some(Action::get("/x")), |
| 581 |
})); |
| 582 |
|
| 583 |
|
| 584 |
assert!(!badge.contains("<button")); |
| 585 |
assert!(!badge.contains("<a ")); |
| 586 |
assert!(!badge.contains("hx-get")); |
| 587 |
assert!(!badge.contains("href")); |
| 588 |
|
| 589 |
let chip = fragment(&Node::Token(Tag { |
| 590 |
kind: layout::Token::Chip { removable: false }, |
| 591 |
label: "open".into(), |
| 592 |
tone: layout::Tone::Neutral, |
| 593 |
latched: true, |
| 594 |
action: Some(Action::get("/x")), |
| 595 |
})); |
| 596 |
|
| 597 |
|
| 598 |
assert!(chip.contains("<a ")); |
| 599 |
assert!(chip.contains("aria-current=\"true\"")); |
| 600 |
assert!(!chip.contains("aria-pressed")); |
| 601 |
assert!(chip.contains("href=\"/x\"")); |
| 602 |
assert!(chip.contains("hx-get=\"/x\"")); |
| 603 |
|
| 604 |
|
| 605 |
let toggle = fragment(&Node::Token(Tag { |
| 606 |
kind: layout::Token::Chip { removable: false }, |
| 607 |
label: "open".into(), |
| 608 |
tone: layout::Tone::Neutral, |
| 609 |
latched: true, |
| 610 |
action: Some(Action::post("/x/toggle")), |
| 611 |
})); |
| 612 |
assert!(toggle.contains("<button")); |
| 613 |
assert!(toggle.contains("aria-pressed=\"true\"")); |
| 614 |
assert!(!toggle.contains("href")); |
| 615 |
} |
| 616 |
|
| 617 |
#[test] |
| 618 |
fn a_select_sends_its_value_under_the_one_agreed_name() { |
| 619 |
let html = fragment(&Node::Select { |
| 620 |
kind: layout::Selector::Tabs, |
| 621 |
options: vec![ |
| 622 |
( |
| 623 |
Choice { |
| 624 |
value: "open".into(), |
| 625 |
label: "Open".into(), |
| 626 |
}, |
| 627 |
None, |
| 628 |
), |
| 629 |
( |
| 630 |
Choice { |
| 631 |
value: "done".into(), |
| 632 |
label: "Done".into(), |
| 633 |
}, |
| 634 |
None, |
| 635 |
), |
| 636 |
], |
| 637 |
chosen: Some("open".into()), |
| 638 |
action: Some(Action::get("/tasks")), |
| 639 |
}); |
| 640 |
|
| 641 |
assert!(html.contains("role=\"tablist\"")); |
| 642 |
assert!(html.contains("aria-selected=\"true\"")); |
| 643 |
assert!(html.contains("aria-selected=\"false\"")); |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
assert!(html.contains(&format!("/tasks?{}=open", Node::SELECTED))); |
| 648 |
assert!(html.contains(&format!("/tasks?{}=done", Node::SELECTED))); |
| 649 |
} |
| 650 |
|
| 651 |
#[test] |
| 652 |
fn a_pending_region_says_it_is_waiting() { |
| 653 |
let screen = |
| 654 |
Screen::list_detail("Tasks", false).with(Slot::new("detail", RegionKind::Pane).pending()); |
| 655 |
assert!(render(&screen).contains("aria-busy=\"true\"")); |
| 656 |
} |
| 657 |
|
| 658 |
#[test] |
| 659 |
fn a_bespoke_region_is_a_place_and_nothing_else() { |
| 660 |
|
| 661 |
|
| 662 |
let screen = Screen::list_detail("Tasks", false).with(Slot::bespoke("player", "media-player")); |
| 663 |
let html = render(&screen); |
| 664 |
|
| 665 |
assert!(html.contains("id=\"player\"")); |
| 666 |
assert!(html.contains("data-bespoke=\"media-player\"")); |
| 667 |
|
| 668 |
assert!(html.contains("data-bespoke=\"media-player\"></div>")); |
| 669 |
} |
| 670 |
|
| 671 |
#[test] |
| 672 |
fn a_bespoke_region_the_host_filled_carries_its_markup() { |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
let screen = Screen::list_detail("Files", false).with(Slot::bespoke("file", "git-file")); |
| 677 |
let html = Webview::new() |
| 678 |
.with_fill("file", "<pre class=\"hl\">fn main() {}</pre>") |
| 679 |
.screen(&screen); |
| 680 |
|
| 681 |
assert!( |
| 682 |
html.contains("<pre class=\"hl\">fn main() {}</pre></div>"), |
| 683 |
"{html}" |
| 684 |
); |
| 685 |
} |
| 686 |
|
| 687 |
#[test] |
| 688 |
fn a_bespoke_region_with_no_fill_is_still_empty() { |
| 689 |
|
| 690 |
|
| 691 |
let screen = Screen::list_detail("Files", false) |
| 692 |
.with(Slot::bespoke("file", "git-file")) |
| 693 |
.with(Slot::bespoke("player", "media-player")); |
| 694 |
let html = Webview::new() |
| 695 |
.with_fill("file", "<pre></pre>") |
| 696 |
.screen(&screen); |
| 697 |
|
| 698 |
assert!( |
| 699 |
html.contains("data-bespoke=\"media-player\"></div>"), |
| 700 |
"{html}" |
| 701 |
); |
| 702 |
} |
| 703 |
|
| 704 |
#[test] |
| 705 |
fn two_regions_sharing_a_name_are_filled_by_id() { |
| 706 |
|
| 707 |
|
| 708 |
let screen = Screen::list_detail("Files", false) |
| 709 |
.with(Slot::bespoke("row-1", "diff")) |
| 710 |
.with(Slot::bespoke("row-2", "diff")); |
| 711 |
let html = Webview::new() |
| 712 |
.with_fill("row-1", "<i>one</i>") |
| 713 |
.with_fill("row-2", "<i>two</i>") |
| 714 |
.screen(&screen); |
| 715 |
|
| 716 |
assert!(html.contains("id=\"row-1\""), "{html}"); |
| 717 |
let one = html.find("<i>one</i>").expect("the first row is filled"); |
| 718 |
let two = html.find("<i>two</i>").expect("the second row is filled"); |
| 719 |
assert!(one < two); |
| 720 |
assert!(html.find("id=\"row-2\"").expect("the second row exists") < two); |
| 721 |
} |
| 722 |
|
| 723 |
#[test] |
| 724 |
fn a_fill_is_markup_and_is_not_escaped() { |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
let screen = Screen::list_detail("Files", false).with(Slot::bespoke("file", "git-file")); |
| 730 |
let html = Webview::new() |
| 731 |
.with_fill("file", "<span data-x=\"1\">&</span>") |
| 732 |
.screen(&screen); |
| 733 |
|
| 734 |
assert!(html.contains("<span data-x=\"1\">&</span>"), "{html}"); |
| 735 |
assert!(!html.contains("<span"), "{html}"); |
| 736 |
} |
| 737 |
|
| 738 |
#[test] |
| 739 |
fn a_fill_for_a_region_the_screen_lacks_goes_nowhere() { |
| 740 |
|
| 741 |
|
| 742 |
let screen = Screen::list_detail("Files", false).with(Slot::bespoke("file", "git-file")); |
| 743 |
let plain = render(&screen); |
| 744 |
let html = Webview::new() |
| 745 |
.with_fill("absent", "<b>stray</b>") |
| 746 |
.screen(&screen); |
| 747 |
|
| 748 |
assert!(!html.contains("stray"), "{html}"); |
| 749 |
assert_eq!(html, plain); |
| 750 |
} |
| 751 |
|
| 752 |
#[test] |
| 753 |
fn a_fill_is_only_for_a_bespoke_region() { |
| 754 |
|
| 755 |
|
| 756 |
let screen = Screen::list_detail("Files", false).with(Slot::new("detail", RegionKind::Pane)); |
| 757 |
let html = Webview::new() |
| 758 |
.with_fill("detail", "<b>stray</b>") |
| 759 |
.screen(&screen); |
| 760 |
|
| 761 |
assert!(!html.contains("stray"), "{html}"); |
| 762 |
} |
| 763 |
|
| 764 |
#[test] |
| 765 |
fn a_slot_id_survives_intact_because_a_fragment_is_aimed_at_it() { |
| 766 |
let screen = Screen::sidebar_content("Feeds").with(Slot::new("feed-list", RegionKind::Sidebar)); |
| 767 |
assert!(render(&screen).contains("id=\"feed-list\"")); |
| 768 |
} |
| 769 |
|
| 770 |
#[test] |
| 771 |
fn a_modal_says_it_takes_input_until_dismissed() { |
| 772 |
let screen = Screen::list_detail("Tasks", false).with(Slot::new("confirm", RegionKind::Modal)); |
| 773 |
let html = render(&screen); |
| 774 |
assert!(html.contains("role=\"dialog\"")); |
| 775 |
assert!(html.contains("aria-modal=\"true\"")); |
| 776 |
} |
| 777 |
|
| 778 |
#[test] |
| 779 |
fn a_table_addresses_its_cells_by_column_never_by_position() { |
| 780 |
let columns = vec![ |
| 781 |
Column::new("Name").width(layout::Width::Fill), |
| 782 |
Column::new("Size").width(layout::Width::Content), |
| 783 |
]; |
| 784 |
let html = fragment(&Node::Table { |
| 785 |
columns, |
| 786 |
rows: vec![Cells::new(["kick.wav", "2.1 MB"])], |
| 787 |
}); |
| 788 |
|
| 789 |
assert!(html.contains("role=\"table\"")); |
| 790 |
assert!(html.contains("role=\"columnheader\"")); |
| 791 |
assert!(html.contains("kick.wav")); |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
assert!(!html.contains("grid-template-columns")); |
| 797 |
assert!(!html.contains("style=")); |
| 798 |
} |
| 799 |
|
| 800 |
#[test] |
| 801 |
fn a_table_row_says_current_the_same_way_a_list_row_does() { |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
let html = fragment(&Node::Table { |
| 808 |
columns: vec![Column::new("Name").width(layout::Width::Fill)], |
| 809 |
rows: vec![Cells { |
| 810 |
current: true, |
| 811 |
..Cells::new(["kick.wav"]) |
| 812 |
}], |
| 813 |
}); |
| 814 |
assert!(html.contains("aria-current=\"true\"")); |
| 815 |
assert!(html.contains("table-row-current")); |
| 816 |
assert!(!html.contains("table-row-selected")); |
| 817 |
|
| 818 |
|
| 819 |
assert!(!html.contains("type=\"checkbox\"")); |
| 820 |
} |
| 821 |
|
| 822 |
#[test] |
| 823 |
fn a_cell_value_is_text_and_cannot_become_markup() { |
| 824 |
let html = fragment(&Node::Table { |
| 825 |
columns: vec![Column::new("Name").width(layout::Width::Fill)], |
| 826 |
rows: vec![Cells::new(["<img src=x onerror=alert(1)>"])], |
| 827 |
}); |
| 828 |
assert!(!html.contains("<img")); |
| 829 |
assert!(html.contains("<img")); |
| 830 |
} |
| 831 |
|
| 832 |
#[test] |
| 833 |
fn a_table_row_carries_its_controls_in_the_cell_they_belong_to() { |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
let html = fragment(&Node::Table { |
| 838 |
columns: vec![ |
| 839 |
Column::new("Fingerprint").width(layout::Width::Fill), |
| 840 |
Column::new("Label").width(layout::Width::Content), |
| 841 |
Column::new("").width(layout::Width::Content), |
| 842 |
], |
| 843 |
rows: vec![Cells::new([ |
| 844 |
Cell::new("SHA256:abc"), |
| 845 |
Cell::new("fw13"), |
| 846 |
Cell::acts([ |
| 847 |
Act::new("Remove", Action::post("/keys/7/delete")).tone(layout::Tone::Danger) |
| 848 |
]), |
| 849 |
])], |
| 850 |
}); |
| 851 |
|
| 852 |
assert!(html.contains("role=\"table\"")); |
| 853 |
assert!(html.contains("SHA256:abc")); |
| 854 |
assert!(html.contains("hx-post=\"/keys/7/delete\"")); |
| 855 |
assert!(html.contains("cell-actions")); |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
assert!(!html.contains("row-actions")); |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
assert!(html.contains("cell-value"), "{html}"); |
| 865 |
assert!(!html.contains("<span class=\"cell-value\""), "{html}"); |
| 866 |
} |
| 867 |
|
| 868 |
#[test] |
| 869 |
fn a_cell_that_mixes_parts_names_them_inside_rather_than_on_itself() { |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
let html = fragment(&Node::Table { |
| 875 |
columns: vec![Column::new("Item").width(layout::Width::Fill)], |
| 876 |
rows: vec![Cells::new([Cell::new("Release notes") |
| 877 |
.token(Tag::badge("Published")) |
| 878 |
.act(Act::new("Remove", Action::post("/blog/7/delete")))])], |
| 879 |
}); |
| 880 |
|
| 881 |
|
| 882 |
assert!(html.contains("<span class=\"cell-value\">"), "{html}"); |
| 883 |
assert!(html.contains("cell-tokens"), "{html}"); |
| 884 |
assert!(html.contains("cell-actions"), "{html}"); |
| 885 |
|
| 886 |
|
| 887 |
assert!(!html.contains("cell-keeps cell-value"), "{html}"); |
| 888 |
} |
| 889 |
|
| 890 |
#[test] |
| 891 |
fn a_figure_carries_its_delta_through_to_the_strip() { |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
let html = fragment(&Node::Stats { |
| 896 |
figures: vec![( |
| 897 |
Figure::new("1,204", "Views") |
| 898 |
.change("+12.5%") |
| 899 |
.tone(layout::Tone::Success), |
| 900 |
None, |
| 901 |
)], |
| 902 |
}); |
| 903 |
|
| 904 |
assert!(html.contains("+12.5%"), "{html}"); |
| 905 |
assert!(html.contains("figure-change"), "{html}"); |
| 906 |
assert!(html.contains("data-tone=\"success\""), "{html}"); |
| 907 |
|
| 908 |
|
| 909 |
assert!(html.contains("Views: 1,204, +12.5%"), "{html}"); |
| 910 |
|
| 911 |
|
| 912 |
let plain = fragment(&Node::Stats { |
| 913 |
figures: vec![(Figure::new("3.1%", "Conversion"), None)], |
| 914 |
}); |
| 915 |
assert!(!plain.contains("figure-change"), "{plain}"); |
| 916 |
} |
| 917 |
|
| 918 |
#[test] |
| 919 |
fn a_control_calling_an_undescribed_route_can_say_where_the_answer_goes() { |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
let html = fragment(&Node::list([Row::new("fw13").act(Act::new( |
| 927 |
"Remove", |
| 928 |
Action::delete("/api/users/me/ssh-keys/7").replacing("ssh-keys-list"), |
| 929 |
))])); |
| 930 |
|
| 931 |
assert!(html.contains("hx-target=\"#ssh-keys-list\""), "{html}"); |
| 932 |
assert!( |
| 933 |
html.contains("hx-delete=\"/api/users/me/ssh-keys/7\""), |
| 934 |
"{html}" |
| 935 |
); |
| 936 |
} |
| 937 |
|
| 938 |
#[test] |
| 939 |
fn nothing_else_ever_emits_a_target() { |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
let html = fragment(&Node::list([Row::new("fw13") |
| 944 |
.act(Act::new("Open", Action::get("/keys/7"))) |
| 945 |
.activate(Action::get("/keys/7"))])); |
| 946 |
assert!(!html.contains("hx-target"), "{html}"); |
| 947 |
|
| 948 |
let form = fragment(&Node::Form { |
| 949 |
action: Action::post("/keys"), |
| 950 |
submit: "Add".into(), |
| 951 |
fields: vec![Field::new(layout::FieldKind::Text, "label", "Label")], |
| 952 |
}); |
| 953 |
assert!(!form.contains("hx-target"), "{form}"); |
| 954 |
} |
| 955 |
|
| 956 |
#[test] |
| 957 |
fn a_region_id_cannot_break_out_of_the_target_attribute() { |
| 958 |
let html = fragment(&Node::act( |
| 959 |
"Remove", |
| 960 |
Action::delete("/api/x").replacing("a\" onload=\"x()"), |
| 961 |
)); |
| 962 |
assert!(!html.contains("\" onload="), "{html}"); |
| 963 |
} |
| 964 |
|
| 965 |
#[test] |
| 966 |
fn a_control_whose_answer_is_a_file_says_so() { |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
let read = fragment(&Node::act( |
| 974 |
"Download LICENSE.txt", |
| 975 |
Action::get("/api/items/7/license.txt").saving("LICENSE.txt"), |
| 976 |
)); |
| 977 |
assert!(read.contains("download=\"LICENSE.txt\""), "{read}"); |
| 978 |
assert!( |
| 979 |
read.contains("<a "), |
| 980 |
"a read that saves is still a link: {read}" |
| 981 |
); |
| 982 |
|
| 983 |
|
| 984 |
let write = fragment(&Node::act( |
| 985 |
"Export CSV", |
| 986 |
Action::post("/api/export/contacts").saving("contacts.csv"), |
| 987 |
)); |
| 988 |
assert!(write.contains("data-saves=\"contacts.csv\""), "{write}"); |
| 989 |
assert!( |
| 990 |
write.contains("hx-post=\"/api/export/contacts\""), |
| 991 |
"{write}" |
| 992 |
); |
| 993 |
assert!( |
| 994 |
!write.contains("download="), |
| 995 |
"a button is not a link: {write}" |
| 996 |
); |
| 997 |
} |
| 998 |
|
| 999 |
#[test] |
| 1000 |
fn a_control_that_saves_nothing_says_nothing() { |
| 1001 |
let plain = fragment(&Node::act("Export", Action::post("/api/export/contacts"))); |
| 1002 |
assert!(!plain.contains("data-saves"), "{plain}"); |
| 1003 |
assert!(!plain.contains("download="), "{plain}"); |
| 1004 |
} |
| 1005 |
|
| 1006 |
#[test] |
| 1007 |
fn a_filename_cannot_break_out_of_its_attribute() { |
| 1008 |
|
| 1009 |
|
| 1010 |
let html = fragment(&Node::act( |
| 1011 |
"Export", |
| 1012 |
Action::post("/api/export").saving("\" onload=\"x()"), |
| 1013 |
)); |
| 1014 |
assert!(!html.contains("\" onload="), "{html}"); |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[test] |
| 1018 |
fn a_cell_value_that_is_a_link_is_the_link() { |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
let html = fragment(&Node::Table { |
| 1023 |
columns: vec![ |
| 1024 |
Column::new("Title").width(layout::Width::Fill), |
| 1025 |
Column::new("Status").width(layout::Width::Content), |
| 1026 |
], |
| 1027 |
rows: vec![Cells::new([ |
| 1028 |
Cell::new("Release notes").activate(Action::get("/blog/7")), |
| 1029 |
Cell::tag(Tag::badge("Published")), |
| 1030 |
])], |
| 1031 |
}); |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
assert!(html.contains("href=\"/blog/7\""), "{html}"); |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
assert!(html.contains("cell-link"), "{html}"); |
| 1040 |
assert!(html.contains(">Release notes</a>"), "{html}"); |
| 1041 |
|
| 1042 |
|
| 1043 |
let hostile = fragment(&Node::Table { |
| 1044 |
columns: vec![Column::new("Title").width(layout::Width::Fill)], |
| 1045 |
rows: vec![Cells::new([ |
| 1046 |
Cell::new("<img src=x onerror=alert(1)>").activate(Action::get("/blog/7")) |
| 1047 |
])], |
| 1048 |
}); |
| 1049 |
assert!(!hostile.contains("<img"), "{hostile}"); |
| 1050 |
} |
| 1051 |
|
| 1052 |
#[test] |
| 1053 |
fn a_linked_value_does_not_also_open_the_row() { |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
let html = fragment(&Node::Table { |
| 1058 |
columns: vec![Column::new("Title").width(layout::Width::Fill)], |
| 1059 |
rows: vec![ |
| 1060 |
Cells::new([Cell::new("Release notes").activate(Action::get("/blog/7"))]) |
| 1061 |
.activate(Action::get("/blog/7/edit")), |
| 1062 |
], |
| 1063 |
}); |
| 1064 |
|
| 1065 |
assert!( |
| 1066 |
html.contains("closest("), |
| 1067 |
"the row filters the link out: {html}" |
| 1068 |
); |
| 1069 |
assert!(html.contains("hx-get=\"/blog/7/edit\""), "{html}"); |
| 1070 |
assert!(html.contains("hx-get=\"/blog/7\""), "{html}"); |
| 1071 |
} |
| 1072 |
|
| 1073 |
#[test] |
| 1074 |
fn a_control_beside_a_value_does_not_also_open_the_row() { |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
let html = fragment(&Node::Table { |
| 1080 |
columns: vec![Column::new("Slug").width(layout::Width::Fill)], |
| 1081 |
rows: vec![ |
| 1082 |
Cells::new([ |
| 1083 |
Cell::new("my-app").act(Act::new("Set slug", Action::post("/apps/3/slug"))) |
| 1084 |
]) |
| 1085 |
.activate(Action::get("/apps/3")), |
| 1086 |
], |
| 1087 |
}); |
| 1088 |
|
| 1089 |
assert!(html.contains("hx-get=\"/apps/3\"")); |
| 1090 |
assert!(html.contains("hx-post=\"/apps/3/slug\"")); |
| 1091 |
assert!(html.contains("data-act")); |
| 1092 |
assert!(html.contains("closest(")); |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
let plain = fragment(&Node::Table { |
| 1097 |
columns: vec![Column::new("Slug").width(layout::Width::Fill)], |
| 1098 |
rows: vec![Cells::new(["my-app"]).activate(Action::get("/apps/3"))], |
| 1099 |
}); |
| 1100 |
assert!(!plain.contains("hx-trigger")); |
| 1101 |
} |
| 1102 |
|
| 1103 |
#[test] |
| 1104 |
fn the_ssh_keys_tab_gaps_stay_shut() { |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
let html = fragment(&Node::list([Row::new("fw13").act( |
| 1110 |
Act::new("Remove", Action::post("/keys/7/delete")).tone(layout::Tone::Danger), |
| 1111 |
)])); |
| 1112 |
|
| 1113 |
assert!(html.contains("class=\"button\""), "{html}"); |
| 1114 |
assert!(html.contains("data-tone=\"danger\""), "{html}"); |
| 1115 |
assert!(!html.contains("\"act\""), "{html}"); |
| 1116 |
assert!(!html.contains("tone-danger"), "{html}"); |
| 1117 |
|
| 1118 |
let chip = fragment(&Node::Token( |
| 1119 |
Tag::chip("Open", Action::get("/tasks?open=1")).latched(true), |
| 1120 |
)); |
| 1121 |
assert!(chip.contains("latched"), "{chip}"); |
| 1122 |
assert!(!chip.contains("chip-latched"), "{chip}"); |
| 1123 |
} |
| 1124 |
|
| 1125 |
#[test] |
| 1126 |
fn a_table_heading_drops_with_the_cells_below_it() { |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
let html = fragment(&Node::Table { |
| 1132 |
columns: vec![ |
| 1133 |
Column::new("Name") |
| 1134 |
.width(layout::Width::Fill) |
| 1135 |
.priority(layout::Priority::Essential), |
| 1136 |
Column::new("Used") |
| 1137 |
.width(layout::Width::Content) |
| 1138 |
.priority(layout::Priority::Optional), |
| 1139 |
], |
| 1140 |
rows: vec![Cells::new(["deploy", "Aug 9"])], |
| 1141 |
}); |
| 1142 |
|
| 1143 |
for classes in [ |
| 1144 |
"col-Name cell-fill cell-keeps", |
| 1145 |
"col-Used cell-content cell-drops-first", |
| 1146 |
] { |
| 1147 |
assert_eq!( |
| 1148 |
html.matches(classes).count(), |
| 1149 |
2, |
| 1150 |
"the heading and its cell must carry the same classes: {html}" |
| 1151 |
); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
#[test] |
| 1156 |
fn a_status_column_keeps_its_tone_instead_of_flattening_to_text() { |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
let html = fragment(&Node::Table { |
| 1162 |
columns: vec![ |
| 1163 |
Column::new("Amount").width(layout::Width::Content), |
| 1164 |
Column::new("Status").width(layout::Width::Content), |
| 1165 |
], |
| 1166 |
rows: vec![Cells::new([ |
| 1167 |
Cell::new("$12.00"), |
| 1168 |
Cell::tag(Tag::badge("Refunded").tone(layout::Tone::Warning)), |
| 1169 |
])], |
| 1170 |
}); |
| 1171 |
|
| 1172 |
assert!(html.contains("cell-tokens"), "{html}"); |
| 1173 |
assert!( |
| 1174 |
html.contains("class=\"badge\" data-tone=\"warning\""), |
| 1175 |
"{html}" |
| 1176 |
); |
| 1177 |
assert!(html.contains("Refunded"), "{html}"); |
| 1178 |
} |
| 1179 |
|
| 1180 |
#[test] |
| 1181 |
fn a_meter_and_a_figure_in_a_cell_were_the_two_gaps_the_enumeration_left_open() { |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
let html = fragment(&Node::Table { |
| 1188 |
columns: vec![ |
| 1189 |
Column::new("Task").width(layout::Width::Fill), |
| 1190 |
Column::new("Done").width(layout::Width::Content), |
| 1191 |
Column::new("Revenue").width(layout::Width::Content), |
| 1192 |
], |
| 1193 |
rows: vec![Cells::new([ |
| 1194 |
Cell::new("Write the renderer"), |
| 1195 |
Cell::new("").part(Node::Meter(Meter::new(3, 7))), |
| 1196 |
Cell::new("").part(Node::Figure(Figure::new("$12.00", "this month"))), |
| 1197 |
])], |
| 1198 |
}); |
| 1199 |
|
| 1200 |
assert!(html.contains("progress"), "the meter is drawn: {html}"); |
| 1201 |
assert!(html.contains("figure"), "the figure is drawn: {html}"); |
| 1202 |
assert!(html.contains("$12.00"), "{html}"); |
| 1203 |
} |
| 1204 |
|
| 1205 |
#[test] |
| 1206 |
fn a_run_says_its_own_order_rather_than_hoisting_the_tags_to_the_end() { |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
let html = fragment(&Node::Table { |
| 1212 |
columns: vec![Column::new("Note").width(layout::Width::Fill)], |
| 1213 |
rows: vec![Cells::new([Cell::new("shipped") |
| 1214 |
.token(Tag::badge("beta")) |
| 1215 |
.part(Node::text("to staging"))])], |
| 1216 |
}); |
| 1217 |
|
| 1218 |
let badge = html.find("beta").expect("the tag is drawn"); |
| 1219 |
let before = html.find("shipped").expect("the first text"); |
| 1220 |
let after = html.find("to staging").expect("the second text"); |
| 1221 |
assert!(before < badge && badge < after, "{html}"); |
| 1222 |
} |
| 1223 |
|
| 1224 |
#[test] |
| 1225 |
#[should_panic(expected = "a cell is an inline run and holds leaves")] |
| 1226 |
fn a_cell_refuses_a_block() { |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
let _ = Cell::new("x").part(Node::List { |
| 1231 |
rows: Vec::new(), |
| 1232 |
more: None, |
| 1233 |
}); |
| 1234 |
} |
| 1235 |
|
| 1236 |
#[test] |
| 1237 |
fn a_chip_in_a_cell_is_a_control_and_a_badge_is_not() { |
| 1238 |
|
| 1239 |
|
| 1240 |
let chip = fragment(&Node::Table { |
| 1241 |
columns: vec![Column::new("Tag").width(layout::Width::Content)], |
| 1242 |
rows: vec![ |
| 1243 |
Cells::new([Cell::tag(Tag::chip("Open", Action::get("/tasks?open=1")))]) |
| 1244 |
.activate(Action::get("/tasks/1")), |
| 1245 |
], |
| 1246 |
}); |
| 1247 |
assert!(chip.contains("data-act"), "{chip}"); |
| 1248 |
assert!(chip.contains("closest("), "{chip}"); |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
let badge = fragment(&Node::Table { |
| 1253 |
columns: vec![Column::new("Status").width(layout::Width::Content)], |
| 1254 |
rows: vec![Cells::new([Cell::tag(Tag::badge("Paid"))]).activate(Action::get("/sales/1"))], |
| 1255 |
}); |
| 1256 |
assert!(!badge.contains("data-act"), "{badge}"); |
| 1257 |
assert!(!badge.contains("hx-trigger"), "{badge}"); |
| 1258 |
} |
| 1259 |
|
| 1260 |
#[test] |
| 1261 |
fn a_cell_of_plain_text_is_still_a_string() { |
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
let cells = Cells::new(["kick.wav", "2.1 MB"]); |
| 1266 |
assert_eq!( |
| 1267 |
cells.values, |
| 1268 |
vec![Cell::new("kick.wav"), Cell::new("2.1 MB")] |
| 1269 |
); |
| 1270 |
|
| 1271 |
|
| 1272 |
assert!( |
| 1273 |
cells |
| 1274 |
.values |
| 1275 |
.iter() |
| 1276 |
.all(|cell| matches!(cell.parts.as_slice(), [Node::Text { .. }])) |
| 1277 |
); |
| 1278 |
} |
| 1279 |
|
| 1280 |
#[test] |
| 1281 |
fn a_form_borrows_its_fields_rather_than_emitting_them_twice() { |
| 1282 |
let html = fragment(&Node::Form { |
| 1283 |
action: Action::post("/tasks"), |
| 1284 |
submit: "Save".into(), |
| 1285 |
fields: vec![Field::new(layout::FieldKind::Text, "title", "Title").required()], |
| 1286 |
}); |
| 1287 |
|
| 1288 |
assert!(html.contains("hx-post=\"/tasks\"")); |
| 1289 |
assert!(html.contains("name=\"title\"")); |
| 1290 |
assert!(html.contains("required")); |
| 1291 |
assert!(html.contains("<button type=\"submit\"")); |
| 1292 |
|
| 1293 |
|
| 1294 |
assert!(html.contains("<label")); |
| 1295 |
} |
| 1296 |
|
| 1297 |
#[test] |
| 1298 |
fn a_control_that_writes_on_change_says_so_without_a_form_around_it() { |
| 1299 |
|
| 1300 |
|
| 1301 |
let html = fragment(&Node::field( |
| 1302 |
Field::select( |
| 1303 |
"theme", |
| 1304 |
"Theme", |
| 1305 |
vec![Choice::new("dark", "Dark"), Choice::new("light", "Light")], |
| 1306 |
) |
| 1307 |
.changes(Action::post("/settings/theme")), |
| 1308 |
)); |
| 1309 |
|
| 1310 |
assert!(html.contains("hx-post=\"/settings/theme\"")); |
| 1311 |
assert!(html.contains("hx-trigger=\"change\"")); |
| 1312 |
|
| 1313 |
|
| 1314 |
assert!(html.contains("hx-include=")); |
| 1315 |
assert!(html.contains("name=\"theme\"")); |
| 1316 |
|
| 1317 |
assert!(!html.contains("<form")); |
| 1318 |
assert!(!html.contains("type=\"submit\"")); |
| 1319 |
} |
| 1320 |
|
| 1321 |
#[test] |
| 1322 |
fn a_field_with_no_route_is_the_plain_group_it_always_was() { |
| 1323 |
let html = fragment(&Node::field(Field::new( |
| 1324 |
layout::FieldKind::Text, |
| 1325 |
"title", |
| 1326 |
"Title", |
| 1327 |
))); |
| 1328 |
assert!(html.contains("name=\"title\"")); |
| 1329 |
assert!(!html.contains("hx-")); |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[test] |
| 1333 |
fn a_tick_that_is_the_write_calls_a_route_and_a_tick_that_is_not_does_not() { |
| 1334 |
|
| 1335 |
|
| 1336 |
let checklist = fragment(&Node::list(vec![ |
| 1337 |
Row::new("Buy milk").toggling(false, Action::post("/subtasks/1/toggle")), |
| 1338 |
])); |
| 1339 |
assert!(checklist.contains("type=\"checkbox\"")); |
| 1340 |
assert!(checklist.contains("hx-post=\"/subtasks/1/toggle\"")); |
| 1341 |
assert!(checklist.contains("hx-trigger=\"change\"")); |
| 1342 |
|
| 1343 |
let mut bulk = Row::new("Ada Lovelace"); |
| 1344 |
bulk.selected = Some(false); |
| 1345 |
let bulk = fragment(&Node::list(vec![bulk])); |
| 1346 |
assert!(bulk.contains("type=\"checkbox\"")); |
| 1347 |
assert!(!bulk.contains("hx-")); |
| 1348 |
} |
| 1349 |
|
| 1350 |
#[test] |
| 1351 |
fn rich_text_is_rendered_from_source_and_still_cannot_smuggle_markup() { |
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
let html = fragment(&Node::rich("A **bold** claim\n\n<script>alert(1)</script>")); |
| 1356 |
|
| 1357 |
assert!(html.contains("<strong>bold</strong>")); |
| 1358 |
assert!(!html.contains("<script")); |
| 1359 |
|
| 1360 |
|
| 1361 |
assert!(html.contains("class=\"rich\"")); |
| 1362 |
} |
| 1363 |
|
| 1364 |
#[test] |
| 1365 |
fn a_refused_form_comes_back_with_what_was_typed_in_it() { |
| 1366 |
|
| 1367 |
|
| 1368 |
let params = quasi_router::Params::new() |
| 1369 |
.with("title", "a name with <angles> in it") |
| 1370 |
.with("done", "on"); |
| 1371 |
let html = fragment(&Node::Form { |
| 1372 |
action: Action::post("/tasks"), |
| 1373 |
submit: "Save".into(), |
| 1374 |
fields: vec![ |
| 1375 |
Field::new(layout::FieldKind::Text, "title", "Title").refilled(¶ms), |
| 1376 |
Field::new(layout::FieldKind::Checkbox, "done", "Done").refilled(¶ms), |
| 1377 |
|
| 1378 |
|
| 1379 |
Field::new(layout::FieldKind::Text, "notes", "Notes").refilled(¶ms), |
| 1380 |
], |
| 1381 |
}); |
| 1382 |
|
| 1383 |
assert!(html.contains("value=\"a name with <angles> in it\"")); |
| 1384 |
assert!(html.contains("checked")); |
| 1385 |
|
| 1386 |
assert!(!html.contains("<angles>")); |
| 1387 |
|
| 1388 |
|
| 1389 |
assert!(html.contains("name=\"notes\" value=\"\"")); |
| 1390 |
} |
| 1391 |
|
| 1392 |
#[test] |
| 1393 |
fn a_secret_is_never_offered_back_however_it_was_set() { |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
let params = quasi_router::Params::new().with("password", "hunter2"); |
| 1398 |
|
| 1399 |
let refused = Field::new(layout::FieldKind::Secret, "password", "Password").refilled(¶ms); |
| 1400 |
assert_eq!(refused.value, None); |
| 1401 |
|
| 1402 |
let mut forced = Field::new(layout::FieldKind::Secret, "password", "Password"); |
| 1403 |
forced.value = Some("hunter2".to_owned()); |
| 1404 |
let html = fragment(&Node::Form { |
| 1405 |
action: Action::post("/login"), |
| 1406 |
submit: "Sign in".into(), |
| 1407 |
fields: vec![forced], |
| 1408 |
}); |
| 1409 |
assert!(!html.contains("hunter2")); |
| 1410 |
} |
| 1411 |
|
| 1412 |
#[test] |
| 1413 |
fn an_empty_region_says_so_instead_of_rendering_an_empty_box() { |
| 1414 |
|
| 1415 |
|
| 1416 |
let empty = render( |
| 1417 |
&Screen::list_detail("Projects", false).with( |
| 1418 |
Slot::new("list", RegionKind::Pane) |
| 1419 |
.with(Node::section("Projects")) |
| 1420 |
.with(Node::empty("No projects yet")), |
| 1421 |
), |
| 1422 |
); |
| 1423 |
|
| 1424 |
assert!(empty.contains("No projects yet")); |
| 1425 |
assert!(empty.contains(r#"data-state="empty""#)); |
| 1426 |
|
| 1427 |
|
| 1428 |
assert!(empty.contains(">Projects<")); |
| 1429 |
|
| 1430 |
|
| 1431 |
assert!(!empty.contains(r#"role="alert""#)); |
| 1432 |
} |
| 1433 |
|
| 1434 |
#[test] |
| 1435 |
fn a_failed_region_is_a_different_state_from_an_empty_one_and_offers_a_way_out() { |
| 1436 |
let failed = render( |
| 1437 |
&Screen::list_detail("Events", false).with( |
| 1438 |
Slot::new("list", RegionKind::Pane).with( |
| 1439 |
Node::failed("Failed to load events") |
| 1440 |
.offering(Act::new("Try again", Action::get("/events"))), |
| 1441 |
), |
| 1442 |
), |
| 1443 |
); |
| 1444 |
|
| 1445 |
assert!(failed.contains(r#"data-state="failed""#)); |
| 1446 |
assert!(failed.contains(r#"data-tone="danger""#)); |
| 1447 |
assert!(failed.contains(r#"role="alert""#)); |
| 1448 |
|
| 1449 |
assert!(failed.contains("hx-get=\"/events\"")); |
| 1450 |
} |
| 1451 |
|
| 1452 |
#[test] |
| 1453 |
fn a_destructive_act_asks_first_and_a_shortcut_reaches_it() { |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
let html = fragment(&Node::Act( |
| 1458 |
Act::new("Delete", Action::post("/tasks/1/delete")) |
| 1459 |
.tone(layout::Tone::Danger) |
| 1460 |
.confirm("Delete this task? This cannot be undone.") |
| 1461 |
.key("d"), |
| 1462 |
)); |
| 1463 |
|
| 1464 |
assert!(html.contains(r#"hx-confirm="Delete this task? This cannot be undone.""#)); |
| 1465 |
assert!(html.contains(r#"accesskey="d""#)); |
| 1466 |
assert!(html.contains("hx-post=\"/tasks/1/delete\"")); |
| 1467 |
} |
| 1468 |
|
| 1469 |
#[test] |
| 1470 |
fn a_row_offers_what_it_does_not_show() { |
| 1471 |
|
| 1472 |
|
| 1473 |
let html = fragment(&Node::list([Row::new("Buy milk") |
| 1474 |
.act(Act::new("Done", Action::post("/tasks/1/complete"))) |
| 1475 |
.offers(Act::new("Duplicate", Action::post("/tasks/1/copy"))) |
| 1476 |
.offers( |
| 1477 |
Act::new("Delete", Action::post("/tasks/1/delete")).confirm("Delete this task?"), |
| 1478 |
)])); |
| 1479 |
|
| 1480 |
assert!(html.contains(r#"data-menu="row""#)); |
| 1481 |
|
| 1482 |
|
| 1483 |
assert!(html.contains(" hidden>")); |
| 1484 |
assert!(html.contains("Duplicate")); |
| 1485 |
assert!(html.contains(r#"hx-confirm="Delete this task?""#)); |
| 1486 |
|
| 1487 |
assert!(html.contains("Done")); |
| 1488 |
} |
| 1489 |
|
| 1490 |
#[test] |
| 1491 |
fn a_list_says_how_much_more_there_is_and_how_to_ask() { |
| 1492 |
|
| 1493 |
|
| 1494 |
let counted = fragment( |
| 1495 |
&Node::list([Row::new("One")]) |
| 1496 |
.and_more(Rest::more(Action::get("/tasks?page=2")).remaining(350)), |
| 1497 |
); |
| 1498 |
assert!(counted.contains("350 remaining")); |
| 1499 |
assert!(counted.contains("hx-get=\"/tasks?page=2\"")); |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
let uncounted = |
| 1504 |
fragment(&Node::list([Row::new("One")]).and_more(Rest::more(Action::get("/more")))); |
| 1505 |
assert!(uncounted.contains("Show more")); |
| 1506 |
assert!(!uncounted.contains("remaining")); |
| 1507 |
|
| 1508 |
let plain = fragment(&Node::list([Row::new("One")])); |
| 1509 |
assert!(!plain.contains("rest")); |
| 1510 |
} |
| 1511 |
|
| 1512 |
#[test] |
| 1513 |
fn a_sortable_column_says_which_way_and_offers_the_press() { |
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
let html = fragment(&Node::Table { |
| 1518 |
columns: vec![ |
| 1519 |
Column::new("Title") |
| 1520 |
.reorder(Action::get("/tasks?sort=title")) |
| 1521 |
.sorted(layout::Sort::Ascending), |
| 1522 |
Column::new("Due").reorder(Action::get("/tasks?sort=due")), |
| 1523 |
Column::new("Notes"), |
| 1524 |
], |
| 1525 |
rows: vec![Cells::new(["Ship it", "Tomorrow", "None"])], |
| 1526 |
}); |
| 1527 |
|
| 1528 |
assert!(html.contains(r#"aria-sort="ascending""#)); |
| 1529 |
assert_eq!(html.matches("data-sortable").count(), 2); |
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
assert!(html.contains(r#"<a class="table-sort""#)); |
| 1534 |
assert!(html.contains("href=\"/tasks?sort=due\"")); |
| 1535 |
assert!(html.contains("hx-get=\"/tasks?sort=due\"")); |
| 1536 |
} |
| 1537 |
|
| 1538 |
#[test] |
| 1539 |
fn a_notice_interrupts_only_when_its_tone_says_to() { |
| 1540 |
let danger = fragment(&Node::banner(layout::Tone::Danger, "Disk full")); |
| 1541 |
assert!(danger.contains("role=\"alert\"")); |
| 1542 |
|
| 1543 |
let info = fragment(&Node::toast(layout::Tone::Info, "Saved")); |
| 1544 |
assert!(info.contains("role=\"status\"")); |
| 1545 |
assert!(info.contains("aria-live=\"polite\"")); |
| 1546 |
} |
| 1547 |
|
| 1548 |
#[test] |
| 1549 |
fn a_screens_notices_come_before_its_regions() { |
| 1550 |
let screen = Screen::list_detail("Tasks", false) |
| 1551 |
.with(Slot::new("list", RegionKind::Pane)) |
| 1552 |
.saying(Node::toast(layout::Tone::Success, "Saved")); |
| 1553 |
let html = render(&screen); |
| 1554 |
|
| 1555 |
let notice = html.find("Saved").expect("the notice is rendered"); |
| 1556 |
let region = html.find("id=\"list\"").expect("the region is rendered"); |
| 1557 |
assert!(notice < region); |
| 1558 |
} |
| 1559 |
|
| 1560 |
#[test] |
| 1561 |
fn every_arrangement_has_a_class_and_they_differ() { |
| 1562 |
let plain = render(&Screen::list_detail("A", false)); |
| 1563 |
let tabbed = render(&Screen::list_detail("A", true)); |
| 1564 |
let sidebar = render(&Screen::sidebar_content("A")); |
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
assert!(plain.contains("class=\"list-detail measure-wide\"")); |
| 1570 |
assert!(tabbed.contains("class=\"list-detail-tabbed measure-wide\"")); |
| 1571 |
assert!(sidebar.contains("class=\"sidebar-content measure-wide\"")); |
| 1572 |
|
| 1573 |
let reading = render(&Screen::list_detail("A", false).measured(layout::Measure::Reading)); |
| 1574 |
assert!(reading.contains("class=\"list-detail measure-reading\"")); |
| 1575 |
} |
| 1576 |
|
| 1577 |
#[test] |
| 1578 |
fn a_screen_carries_the_share_as_a_number_rather_than_a_class() { |
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
let wide = render(&Screen::sidebar_content("A")); |
| 1584 |
assert!( |
| 1585 |
wide.contains("--region-share:25fr;--region-rest:75fr"), |
| 1586 |
"{wide}" |
| 1587 |
); |
| 1588 |
|
| 1589 |
let narrow = render(&Screen::new( |
| 1590 |
"A", |
| 1591 |
layout::Arrangement::sidebar_content().with_share(layout::Share::percent(20)), |
| 1592 |
)); |
| 1593 |
assert!( |
| 1594 |
narrow.contains("--region-share:20fr;--region-rest:80fr"), |
| 1595 |
"{narrow}" |
| 1596 |
); |
| 1597 |
} |
| 1598 |
|
| 1599 |
#[test] |
| 1600 |
fn a_class_prefix_reaches_every_emitted_name() { |
| 1601 |
let emit = crate::Emit { |
| 1602 |
class_prefix: "qs-", |
| 1603 |
..crate::Emit::default() |
| 1604 |
}; |
| 1605 |
|
| 1606 |
let screen = Screen::list_detail("Tasks", false).with( |
| 1607 |
Slot::new("list", RegionKind::Pane) |
| 1608 |
.with(Node::page("Tasks")) |
| 1609 |
.with(Node::list([Row::new("One")])), |
| 1610 |
); |
| 1611 |
let html = Webview::new().with_emit(emit).screen(&screen); |
| 1612 |
|
| 1613 |
assert!(html.contains("class=\"qs-list-detail qs-measure-wide\"")); |
| 1614 |
assert!(html.contains("qs-region qs-pane")); |
| 1615 |
assert!(html.contains("qs-heading")); |
| 1616 |
assert!(html.contains("qs-list")); |
| 1617 |
|
| 1618 |
|
| 1619 |
assert!(!html.contains("class=\"list\"")); |
| 1620 |
assert!(!html.contains("class=\"heading\"")); |
| 1621 |
} |
| 1622 |
|
| 1623 |
#[test] |
| 1624 |
fn the_shell_serves_its_assets_from_where_the_host_says() { |
| 1625 |
let html = Webview::under("/assets").screen(&Screen::list_detail("A", false)); |
| 1626 |
assert!(html.contains("src=\"/assets/htmx.min.js\"")); |
| 1627 |
assert!(html.contains("src=\"/assets/idiomorph-ext.min.js\"")); |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
let tauri = Webview::under("quasi://localhost/assets").screen(&Screen::list_detail("A", false)); |
| 1632 |
assert!(tauri.contains("src=\"quasi://localhost/assets/htmx.min.js\"")); |
| 1633 |
} |
| 1634 |
|
| 1635 |
#[test] |
| 1636 |
fn stylesheets_link_in_the_order_they_were_added() { |
| 1637 |
let shell = Shell::default().styled("/a.css").styled("/b.css"); |
| 1638 |
let html = Webview::new() |
| 1639 |
.with_shell(shell) |
| 1640 |
.screen(&Screen::list_detail("A", false)); |
| 1641 |
|
| 1642 |
let a = html.find("/a.css").expect("a is linked"); |
| 1643 |
let b = html.find("/b.css").expect("b is linked"); |
| 1644 |
assert!(a < b); |
| 1645 |
} |
| 1646 |
|
| 1647 |
#[test] |
| 1648 |
fn injected_head_markup_lands_last_so_it_can_override() { |
| 1649 |
let shell = Shell::default().with_head("<link rel=\"icon\" href=\"/f.png\">"); |
| 1650 |
let html = Webview::new() |
| 1651 |
.with_shell(shell) |
| 1652 |
.screen(&Screen::list_detail("A", false)); |
| 1653 |
|
| 1654 |
let icon = html.find("/f.png").expect("the icon is linked"); |
| 1655 |
let htmx = html.find("htmx.min.js").expect("htmx is linked"); |
| 1656 |
assert!(htmx < icon); |
| 1657 |
assert!(icon < html.find("</head>").expect("the head closes")); |
| 1658 |
} |
| 1659 |
|
| 1660 |
#[test] |
| 1661 |
fn the_layer_statement_precedes_every_stylesheet() { |
| 1662 |
|
| 1663 |
|
| 1664 |
let shell = Shell::default() |
| 1665 |
.layered(["base", "components", "responsive"]) |
| 1666 |
.styled("/geometry.css") |
| 1667 |
.styled("/style.css"); |
| 1668 |
let html = Webview::new() |
| 1669 |
.with_shell(shell) |
| 1670 |
.screen(&Screen::list_detail("A", false)); |
| 1671 |
|
| 1672 |
let stmt = html |
| 1673 |
.find("@layer makeover, base, components, responsive;") |
| 1674 |
.expect("the order is stated"); |
| 1675 |
let first_sheet = html.find("/geometry.css").expect("the sheet is linked"); |
| 1676 |
assert!(stmt < first_sheet); |
| 1677 |
} |
| 1678 |
|
| 1679 |
#[test] |
| 1680 |
fn the_layer_statement_is_emitted_even_with_no_app_layers() { |
| 1681 |
|
| 1682 |
|
| 1683 |
let html = Webview::new().screen(&Screen::list_detail("A", false)); |
| 1684 |
assert!(html.contains("@layer makeover;")); |
| 1685 |
} |
| 1686 |
|
| 1687 |
#[test] |
| 1688 |
fn a_layer_name_cannot_escape_the_style_element() { |
| 1689 |
|
| 1690 |
|
| 1691 |
let shell = Shell::default().layered(["base</style><script>alert(1)</script>"]); |
| 1692 |
let html = Webview::new() |
| 1693 |
.with_shell(shell) |
| 1694 |
.screen(&Screen::list_detail("A", false)); |
| 1695 |
|
| 1696 |
assert!(!html.contains("<script>alert(1)")); |
| 1697 |
assert!(html.contains("@layer makeover, basestylescriptalert1script;")); |
| 1698 |
} |
| 1699 |
|
| 1700 |
#[test] |
| 1701 |
fn head_first_markup_lands_before_the_layer_statement_and_the_sheets() { |
| 1702 |
|
| 1703 |
let shell = Shell::default() |
| 1704 |
.with_head_first("<link rel=\"preload\" href=\"/f.woff2\" as=\"font\">") |
| 1705 |
.styled("/style.css"); |
| 1706 |
let html = Webview::new() |
| 1707 |
.with_shell(shell) |
| 1708 |
.screen(&Screen::list_detail("A", false)); |
| 1709 |
|
| 1710 |
let preload = html.find("/f.woff2").expect("the font is preloaded"); |
| 1711 |
let stmt = html.find("@layer makeover").expect("the order is stated"); |
| 1712 |
let sheet = html.find("/style.css").expect("the sheet is linked"); |
| 1713 |
assert!(preload < stmt); |
| 1714 |
assert!(stmt < sheet); |
| 1715 |
} |
| 1716 |
|
| 1717 |
#[test] |
| 1718 |
fn head_first_and_head_are_different_ends_of_the_same_head() { |
| 1719 |
let shell = Shell::default() |
| 1720 |
.with_head_first("<meta name=\"first\">") |
| 1721 |
.with_head("<meta name=\"last\">"); |
| 1722 |
let html = Webview::new() |
| 1723 |
.with_shell(shell) |
| 1724 |
.screen(&Screen::list_detail("A", false)); |
| 1725 |
|
| 1726 |
let first = html.find("name=\"first\"").expect("first is emitted"); |
| 1727 |
let last = html.find("name=\"last\"").expect("last is emitted"); |
| 1728 |
let htmx = html.find("htmx.min.js").expect("htmx is linked"); |
| 1729 |
assert!(first < htmx); |
| 1730 |
assert!(htmx < last); |
| 1731 |
} |
| 1732 |
|
| 1733 |
#[test] |
| 1734 |
fn repeated_head_first_calls_keep_their_call_order() { |
| 1735 |
let shell = Shell::default() |
| 1736 |
.with_head_first("<meta name=\"a\">") |
| 1737 |
.with_head_first("<meta name=\"b\">"); |
| 1738 |
let html = Webview::new() |
| 1739 |
.with_shell(shell) |
| 1740 |
.screen(&Screen::list_detail("A", false)); |
| 1741 |
|
| 1742 |
assert!(html.find("name=\"a\"") < html.find("name=\"b\"")); |
| 1743 |
} |
| 1744 |
|
| 1745 |
#[test] |
| 1746 |
fn a_document_wraps_markup_the_renderer_did_not_write() { |
| 1747 |
|
| 1748 |
|
| 1749 |
let shell = Shell::default().layered(["base"]).styled("/style.css"); |
| 1750 |
let html = shell.document("Console", "<main>hand-written</main>"); |
| 1751 |
|
| 1752 |
assert!(html.starts_with("<!doctype html><html lang=\"en\">")); |
| 1753 |
assert!(html.contains("<title>Console</title>")); |
| 1754 |
assert!(html.contains("<main>hand-written</main>")); |
| 1755 |
assert!(html.ends_with("</body></html>")); |
| 1756 |
} |
| 1757 |
|
| 1758 |
#[test] |
| 1759 |
fn a_document_states_the_layers_before_the_sheets_like_a_screen_does() { |
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
let shell = Shell::default().layered(["base"]).styled("/style.css"); |
| 1764 |
let html = shell.document("Console", "<main>x</main>"); |
| 1765 |
|
| 1766 |
let stmt = html |
| 1767 |
.find("@layer makeover, base;") |
| 1768 |
.expect("the order is stated"); |
| 1769 |
let sheet = html.find("/style.css").expect("the sheet is linked"); |
| 1770 |
let body = html.find("<main>").expect("the body is placed"); |
| 1771 |
assert!(stmt < sheet); |
| 1772 |
assert!(sheet < body); |
| 1773 |
} |
| 1774 |
|
| 1775 |
#[test] |
| 1776 |
fn a_documents_body_tag_is_the_shells_own() { |
| 1777 |
|
| 1778 |
|
| 1779 |
let shell = Shell { |
| 1780 |
body_class: Some("console".into()), |
| 1781 |
..Shell::default() |
| 1782 |
}; |
| 1783 |
let html = shell.document("Console", "x"); |
| 1784 |
|
| 1785 |
assert!(html.contains("<body hx-ext=\"morph\" class=\"console\">x</body>")); |
| 1786 |
} |
| 1787 |
|
| 1788 |
#[test] |
| 1789 |
fn a_host_assembling_its_own_document_gets_the_same_head_as_a_screen() { |
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
let shell = Shell::default() |
| 1794 |
.layered(["base", "components"]) |
| 1795 |
.styled("/style.css") |
| 1796 |
.with_head_first("<link rel=\"preload\" href=\"/f.woff2\" as=\"font\">"); |
| 1797 |
let parts = shell.parts(); |
| 1798 |
let screen = Webview::new() |
| 1799 |
.with_shell(shell) |
| 1800 |
.screen(&Screen::list_detail("Console", false)); |
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
let head = screen |
| 1809 |
.split("</head>") |
| 1810 |
.next() |
| 1811 |
.expect("the screen has a head") |
| 1812 |
.replace("<title>Console</title>", ""); |
| 1813 |
let head = strip_discovery(&head); |
| 1814 |
assert_eq!(parts.head, head); |
| 1815 |
assert!(screen.contains(&format!("<body{}>", parts.body_attrs))); |
| 1816 |
} |
| 1817 |
|
| 1818 |
#[test] |
| 1819 |
fn the_body_attributes_compose_with_the_hosts_own() { |
| 1820 |
|
| 1821 |
|
| 1822 |
let parts = Shell::default().parts(); |
| 1823 |
assert_eq!(parts.body_attrs, " hx-ext=\"morph\""); |
| 1824 |
assert!(!parts.head.contains("</head>")); |
| 1825 |
assert!(!parts.head.contains("<title>")); |
| 1826 |
|
| 1827 |
let quiet = Shell::default().without_morph().parts(); |
| 1828 |
assert_eq!(quiet.body_attrs, ""); |
| 1829 |
} |
| 1830 |
|
| 1831 |
#[test] |
| 1832 |
fn a_nested_region_renders_inside_its_parent() { |
| 1833 |
let screen = |
| 1834 |
Screen::list_detail("Tasks", false).with(Slot::new("outer", RegionKind::Split).with( |
| 1835 |
Node::Region(Slot::new("inner", RegionKind::Pane).with(Node::text("in"))), |
| 1836 |
)); |
| 1837 |
let html = render(&screen); |
| 1838 |
|
| 1839 |
let outer = html.find("id=\"outer\"").expect("outer renders"); |
| 1840 |
let inner = html.find("id=\"inner\"").expect("inner renders"); |
| 1841 |
assert!(outer < inner); |
| 1842 |
assert!(html.contains("in</p></div></div>")); |
| 1843 |
} |
| 1844 |
|
| 1845 |
#[test] |
| 1846 |
fn text_from_a_description_can_never_become_markup() { |
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
let hostile = "<script>alert(1)</script>"; |
| 1852 |
let screen = Screen::list_detail(hostile, false) |
| 1853 |
.saying(Node::banner(layout::Tone::Danger, hostile)) |
| 1854 |
.with( |
| 1855 |
Slot::new("s", RegionKind::Pane) |
| 1856 |
.with(Node::page(hostile)) |
| 1857 |
.with(Node::text(hostile)) |
| 1858 |
.with(Node::act(hostile, Action::get("/x"))) |
| 1859 |
.with(Node::list([Row::new(hostile) |
| 1860 |
.secondary(hostile) |
| 1861 |
.meta(hostile) |
| 1862 |
.act(Act::new(hostile, Action::post("/y")))])) |
| 1863 |
.with(Node::Token(Tag { |
| 1864 |
kind: layout::Token::Chip { removable: true }, |
| 1865 |
label: hostile.into(), |
| 1866 |
tone: layout::Tone::Neutral, |
| 1867 |
latched: false, |
| 1868 |
action: Some(Action::get("/z")), |
| 1869 |
})), |
| 1870 |
); |
| 1871 |
|
| 1872 |
let html = render(&screen); |
| 1873 |
assert!(!html.contains("<script>")); |
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
assert_eq!(html.matches("<script>").count(), 12); |
| 1881 |
} |
| 1882 |
|
| 1883 |
#[test] |
| 1884 |
fn a_rows_plain_prose_is_escaped_exactly_as_it_always_was() { |
| 1885 |
|
| 1886 |
|
| 1887 |
let row = Row::new("Atlas").secondary("**not bold** <b>not bold either</b>"); |
| 1888 |
let node = Node::list(vec![row]); |
| 1889 |
|
| 1890 |
let html = Webview::new().fragment(&node); |
| 1891 |
|
| 1892 |
assert!(html.contains("**not bold**"), "got: {html}"); |
| 1893 |
assert!(!html.contains("<strong>"), "got: {html}"); |
| 1894 |
assert!(!html.contains("<b>"), "got: {html}"); |
| 1895 |
assert!(html.contains("<b>"), "got: {html}"); |
| 1896 |
} |
| 1897 |
|
| 1898 |
#[test] |
| 1899 |
fn a_rows_rich_prose_is_rendered_inline() { |
| 1900 |
|
| 1901 |
|
| 1902 |
let row = Row::new("Atlas").secondary(Prose::rich("**Ships Q3.** `soon`")); |
| 1903 |
let node = Node::list(vec![row]); |
| 1904 |
|
| 1905 |
let html = Webview::new().fragment(&node); |
| 1906 |
|
| 1907 |
assert!(html.contains("<strong>Ships Q3.</strong>"), "got: {html}"); |
| 1908 |
assert!(html.contains("<code>soon</code>"), "got: {html}"); |
| 1909 |
} |
| 1910 |
|
| 1911 |
#[test] |
| 1912 |
fn a_rows_rich_prose_keeps_no_blocks() { |
| 1913 |
|
| 1914 |
|
| 1915 |
let row = Row::new("Borealis").secondary(Prose::rich("# Goal\n\n> ship it\n\n- one\n- two")); |
| 1916 |
let node = Node::list(vec![row]); |
| 1917 |
|
| 1918 |
let html = Webview::new().fragment(&node); |
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
let secondary = html |
| 1923 |
.split_once(r#"<span class="row-secondary">"#) |
| 1924 |
.expect("the row draws its secondary") |
| 1925 |
.1 |
| 1926 |
.split_once("</span>") |
| 1927 |
.expect("the part closes") |
| 1928 |
.0; |
| 1929 |
for block in ["<h1", "<blockquote", "<ul", "<li", "<p"] { |
| 1930 |
assert!(!secondary.contains(block), "no {block} in a row: {html}"); |
| 1931 |
} |
| 1932 |
for word in ["Goal", "ship it", "one", "two"] { |
| 1933 |
assert!(html.contains(word), "{word} survives: {html}"); |
| 1934 |
} |
| 1935 |
} |
| 1936 |
|
| 1937 |
#[test] |
| 1938 |
fn a_rows_rich_prose_carries_no_second_click_target() { |
| 1939 |
|
| 1940 |
|
| 1941 |
let row = Row::new("Atlas") |
| 1942 |
.secondary(Prose::rich( |
| 1943 |
"see [the brief](https://example.com/a/long/path)", |
| 1944 |
)) |
| 1945 |
.activate(Action::get("/projects/1")); |
| 1946 |
let node = Node::list(vec![row]); |
| 1947 |
|
| 1948 |
let html = Webview::new().fragment(&node); |
| 1949 |
|
| 1950 |
assert!(html.contains("see the brief"), "the text survives: {html}"); |
| 1951 |
assert!(!html.contains("example.com"), "no href: {html}"); |
| 1952 |
|
| 1953 |
|
| 1954 |
assert_eq!( |
| 1955 |
html.matches("<a ").count(), |
| 1956 |
1, |
| 1957 |
"the row is the only target: {html}" |
| 1958 |
); |
| 1959 |
} |
| 1960 |
|
| 1961 |
#[test] |
| 1962 |
fn a_rows_rich_prose_cannot_smuggle_markup() { |
| 1963 |
|
| 1964 |
|
| 1965 |
let row = Row::new("Atlas").secondary(Prose::rich( |
| 1966 |
"hi <script>alert(1)</script> <img src=x onerror=alert(1)> [x](javascript:alert(1))", |
| 1967 |
)); |
| 1968 |
let node = Node::list(vec![row]); |
| 1969 |
|
| 1970 |
let html = Webview::new().fragment(&node); |
| 1971 |
|
| 1972 |
assert!(!html.contains("<script"), "got: {html}"); |
| 1973 |
assert!(!html.contains("onerror"), "got: {html}"); |
| 1974 |
assert!(!html.contains("javascript:"), "got: {html}"); |
| 1975 |
} |
| 1976 |
|
| 1977 |
#[test] |
| 1978 |
fn no_control_ever_says_whether_its_answer_is_a_place() { |
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
let html = fragment(&Node::Table { |
| 1985 |
columns: vec![Column::new("Title").width(layout::Width::Fill)], |
| 1986 |
rows: vec![ |
| 1987 |
Cells::new([Cell::new("Release notes").activate(Action::get("/blog/7"))]) |
| 1988 |
.activate(Action::get("/blog/7/edit")), |
| 1989 |
], |
| 1990 |
}); |
| 1991 |
assert!(!html.contains("push-url"), "{html}"); |
| 1992 |
assert!(!html.contains("replace-url"), "{html}"); |
| 1993 |
} |
| 1994 |
|
| 1995 |
#[test] |
| 1996 |
fn an_option_naming_its_own_route_calls_that_and_not_the_strips() { |
| 1997 |
|
| 1998 |
|
| 1999 |
let html = fragment(&Node::Select { |
| 2000 |
kind: layout::Selector::Tabs, |
| 2001 |
options: vec![ |
| 2002 |
( |
| 2003 |
Choice { |
| 2004 |
value: "projects".into(), |
| 2005 |
label: "Projects".into(), |
| 2006 |
}, |
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
Some(Action::get("/dashboard/tabs/projects")), |
| 2011 |
), |
| 2012 |
( |
| 2013 |
Choice { |
| 2014 |
value: "keys".into(), |
| 2015 |
label: "SSH keys".into(), |
| 2016 |
}, |
| 2017 |
Some(Action::get("/dashboard/tabs/ssh-keys")), |
| 2018 |
), |
| 2019 |
], |
| 2020 |
chosen: Some("projects".into()), |
| 2021 |
action: None, |
| 2022 |
}); |
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
assert!(html.contains("/dashboard/tabs/projects"), "{html}"); |
| 2027 |
assert!(html.contains("/dashboard/tabs/ssh-keys"), "{html}"); |
| 2028 |
assert!( |
| 2029 |
!html.contains(&format!("{}=projects", Node::SELECTED)), |
| 2030 |
"{html}" |
| 2031 |
); |
| 2032 |
|
| 2033 |
|
| 2034 |
assert!(html.contains("<a"), "{html}"); |
| 2035 |
assert!(html.contains("role=\"tablist\""), "{html}"); |
| 2036 |
assert!(html.contains("aria-selected=\"true\""), "{html}"); |
| 2037 |
} |
| 2038 |
|
| 2039 |
#[test] |
| 2040 |
fn an_option_with_no_route_of_its_own_is_unchanged() { |
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
let with_tuple = fragment(&Node::Select { |
| 2045 |
kind: layout::Selector::Segmented, |
| 2046 |
options: vec![ |
| 2047 |
( |
| 2048 |
Choice { |
| 2049 |
value: "0".into(), |
| 2050 |
label: "Active".into(), |
| 2051 |
}, |
| 2052 |
None, |
| 2053 |
), |
| 2054 |
( |
| 2055 |
Choice { |
| 2056 |
value: "1".into(), |
| 2057 |
label: "Archived".into(), |
| 2058 |
}, |
| 2059 |
None, |
| 2060 |
), |
| 2061 |
], |
| 2062 |
chosen: Some("0".into()), |
| 2063 |
action: Some(Action::get("/notes")), |
| 2064 |
}); |
| 2065 |
|
| 2066 |
assert!( |
| 2067 |
with_tuple.contains(&format!("/notes?{}=0", Node::SELECTED)), |
| 2068 |
"{with_tuple}" |
| 2069 |
); |
| 2070 |
assert!( |
| 2071 |
with_tuple.contains(&format!("/notes?{}=1", Node::SELECTED)), |
| 2072 |
"{with_tuple}" |
| 2073 |
); |
| 2074 |
|
| 2075 |
assert!( |
| 2076 |
with_tuple.contains("<button type=\"button\""), |
| 2077 |
"{with_tuple}" |
| 2078 |
); |
| 2079 |
} |
| 2080 |
|
| 2081 |
#[test] |
| 2082 |
fn a_screen_that_says_nothing_is_still_a_findable_page() { |
| 2083 |
|
| 2084 |
|
| 2085 |
let html = render(&Screen::sidebar_content("Projects")); |
| 2086 |
|
| 2087 |
assert!( |
| 2088 |
html.contains("<meta property=\"og:title\" content=\"Projects\">"), |
| 2089 |
"{html}" |
| 2090 |
); |
| 2091 |
assert!( |
| 2092 |
html.contains("<meta property=\"og:type\" content=\"website\">"), |
| 2093 |
"{html}" |
| 2094 |
); |
| 2095 |
assert!(!html.contains("robots"), "{html}"); |
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
assert!(!html.contains("og:description"), "{html}"); |
| 2100 |
assert!(!html.contains("og:image"), "{html}"); |
| 2101 |
assert!(!html.contains("canonical"), "{html}"); |
| 2102 |
} |
| 2103 |
|
| 2104 |
#[test] |
| 2105 |
fn a_purchased_content_screen_can_say_it_is_not_for_crawlers() { |
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
let html = render(&Screen::sidebar_content("Downloads").indexed(false)); |
| 2110 |
assert!( |
| 2111 |
html.contains("<meta name=\"robots\" content=\"noindex\">"), |
| 2112 |
"{html}" |
| 2113 |
); |
| 2114 |
} |
| 2115 |
|
| 2116 |
#[test] |
| 2117 |
fn a_screen_names_what_it_is_about_and_how_it_previews() { |
| 2118 |
let html = render( |
| 2119 |
&Screen::sidebar_content("Blue Hour") |
| 2120 |
.summarised("Nine tracks recorded in one night.") |
| 2121 |
.illustrated("https://makenot.work/media/cover.png") |
| 2122 |
.about(quasi_router::SocialKind::Song) |
| 2123 |
.canonical_at("https://makenot.work/i/7"), |
| 2124 |
); |
| 2125 |
|
| 2126 |
assert!( |
| 2127 |
html.contains("content=\"Nine tracks recorded in one night.\""), |
| 2128 |
"{html}" |
| 2129 |
); |
| 2130 |
assert!( |
| 2131 |
html.contains("content=\"https://makenot.work/media/cover.png\""), |
| 2132 |
"{html}" |
| 2133 |
); |
| 2134 |
assert!( |
| 2135 |
html.contains("<meta property=\"og:type\" content=\"music.song\">"), |
| 2136 |
"{html}" |
| 2137 |
); |
| 2138 |
assert!( |
| 2139 |
html.contains("<link rel=\"canonical\" href=\"https://makenot.work/i/7\">"), |
| 2140 |
"{html}" |
| 2141 |
); |
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
assert!( |
| 2147 |
html.contains("<meta name=\"twitter:card\" content=\"summary_large_image\">"), |
| 2148 |
"{html}" |
| 2149 |
); |
| 2150 |
assert!(!html.contains("property=\"twitter:"), "{html}"); |
| 2151 |
} |
| 2152 |
|
| 2153 |
#[test] |
| 2154 |
fn a_summary_is_escaped_because_a_person_wrote_it() { |
| 2155 |
|
| 2156 |
|
| 2157 |
let html = |
| 2158 |
render(&Screen::sidebar_content("Item").summarised("She said \"hi\" & <b>waved</b>")); |
| 2159 |
|
| 2160 |
assert!(!html.contains("<b>waved"), "{html}"); |
| 2161 |
assert!(html.contains(""hi""), "{html}"); |
| 2162 |
assert!(html.contains("&"), "{html}"); |
| 2163 |
} |
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
fn every_kind_of_screen() -> Vec<String> { |
| 2173 |
let mut htmls = Vec::new(); |
| 2174 |
|
| 2175 |
for kind in [ |
| 2176 |
RegionKind::Band, |
| 2177 |
RegionKind::Sidebar, |
| 2178 |
RegionKind::Pane, |
| 2179 |
RegionKind::Split, |
| 2180 |
RegionKind::TabGroup, |
| 2181 |
RegionKind::Modal, |
| 2182 |
] { |
| 2183 |
htmls.push(render( |
| 2184 |
&Screen::list_detail("Everything", false) |
| 2185 |
.saying(Node::banner(layout::Tone::Warning, "heads up")) |
| 2186 |
.saying(Node::toast(layout::Tone::Success, "saved")) |
| 2187 |
.with(Slot::new("region", kind).with(Node::text("in a region"))), |
| 2188 |
)); |
| 2189 |
} |
| 2190 |
htmls.push(render(&Screen::sidebar_content("Everything").with( |
| 2191 |
Slot::bespoke("bespoke", "map").with(Node::text("beside a fill")), |
| 2192 |
))); |
| 2193 |
htmls.push(render(&Screen::list_detail("Tabbed", true))); |
| 2194 |
|
| 2195 |
|
| 2196 |
for measure in [ |
| 2197 |
layout::Measure::Wide, |
| 2198 |
layout::Measure::Contained, |
| 2199 |
layout::Measure::Reading, |
| 2200 |
] { |
| 2201 |
htmls.push(render( |
| 2202 |
&Screen::list_detail("Measured", false).measured(measure), |
| 2203 |
)); |
| 2204 |
} |
| 2205 |
|
| 2206 |
let acts = || Act::new("Remove", Action::post("/keys/7/delete")).tone(layout::Tone::Danger); |
| 2207 |
for node in [ |
| 2208 |
Node::page("A page"), |
| 2209 |
Node::section("A section"), |
| 2210 |
Node::text("plain"), |
| 2211 |
Node::rich("**bold** and a [link](https://example.com)"), |
| 2212 |
Node::act("Save", Action::post("/save")), |
| 2213 |
Node::Token(Tag::badge("Paid").tone(layout::Tone::Success)), |
| 2214 |
Node::Token(Tag::chip("Open", Action::get("/tasks?open=1")).latched(true)), |
| 2215 |
Node::banner(layout::Tone::Danger, "it broke"), |
| 2216 |
Node::toast(layout::Tone::Info, "it saved"), |
| 2217 |
Node::empty("nothing here").offering(acts()), |
| 2218 |
Node::failed("it broke").offering(acts()), |
| 2219 |
Node::field(Field::new(layout::FieldKind::Text, "name", "Name").required()), |
| 2220 |
Node::field(Field::new(layout::FieldKind::Secret, "pw", "Password").error("too short")), |
| 2221 |
Node::field( |
| 2222 |
Field::new(layout::FieldKind::Checkbox, "live", "Live").changes(Action::post("/live")), |
| 2223 |
), |
| 2224 |
Node::field(Field::select( |
| 2225 |
"size", |
| 2226 |
"Size", |
| 2227 |
vec![Choice::plain("small"), Choice::new("l", "large")], |
| 2228 |
)), |
| 2229 |
Node::field(Field::radio( |
| 2230 |
"mode", |
| 2231 |
"Mode", |
| 2232 |
vec![Choice::plain("one"), Choice::plain("two")], |
| 2233 |
)), |
| 2234 |
Node::Form { |
| 2235 |
action: Action::post("/new"), |
| 2236 |
submit: "Create".into(), |
| 2237 |
fields: vec![Field::new(layout::FieldKind::Text, "title", "Title")], |
| 2238 |
}, |
| 2239 |
Node::list([Row::new("fw13") |
| 2240 |
.secondary("a second line") |
| 2241 |
.meta("2 days ago") |
| 2242 |
.token(Tag::badge("Active")) |
| 2243 |
.meter(Meter::new(3, 10)) |
| 2244 |
.activate(Action::get("/keys/7")) |
| 2245 |
.act(acts())]) |
| 2246 |
.and_more(Rest::more(Action::get("/keys?page=2")).remaining(40)), |
| 2247 |
Node::list([Row::new("astra").toggling(true, Action::post("/keys/8/pin"))]), |
| 2248 |
Node::Table { |
| 2249 |
columns: vec![ |
| 2250 |
Column::new("Name") |
| 2251 |
.width(layout::Width::Fill) |
| 2252 |
.priority(layout::Priority::Essential), |
| 2253 |
Column::new("Status") |
| 2254 |
.width(layout::Width::Content) |
| 2255 |
.priority(layout::Priority::Optional), |
| 2256 |
], |
| 2257 |
rows: vec![ |
| 2258 |
Cells::new([ |
| 2259 |
Cell::new("deploy"), |
| 2260 |
Cell::tag(Tag::badge("Paid").tone(layout::Tone::Success)), |
| 2261 |
]) |
| 2262 |
.activate(Action::get("/runs/1")), |
| 2263 |
Cells::new([Cell::new("build"), Cell::acts([acts()])]), |
| 2264 |
], |
| 2265 |
}, |
| 2266 |
Node::Meter(Meter::new(7, 10).label("7 of 10")), |
| 2267 |
Node::stats([Figure::new("$12.00", "Revenue").change("+3%")]), |
| 2268 |
] { |
| 2269 |
htmls.push(fragment(&node)); |
| 2270 |
} |
| 2271 |
|
| 2272 |
for kind in [ |
| 2273 |
layout::Selector::Tabs, |
| 2274 |
layout::Selector::Segmented, |
| 2275 |
layout::Selector::Toggle, |
| 2276 |
] { |
| 2277 |
htmls.push(fragment(&Node::Select { |
| 2278 |
kind, |
| 2279 |
options: vec![ |
| 2280 |
(Choice::plain("open"), Some(Action::get("/tasks?open=1"))), |
| 2281 |
(Choice::new("done", "Done"), None), |
| 2282 |
], |
| 2283 |
chosen: Some("open".into()), |
| 2284 |
action: Some(Action::get("/tasks")), |
| 2285 |
})); |
| 2286 |
} |
| 2287 |
|
| 2288 |
htmls |
| 2289 |
} |
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
fn emitted_classes(htmls: &[String]) -> std::collections::BTreeSet<String> { |
| 2297 |
let mut names = std::collections::BTreeSet::new(); |
| 2298 |
for html in htmls { |
| 2299 |
let mut rest = html.as_str(); |
| 2300 |
while let Some(at) = rest.find("class=\"") { |
| 2301 |
rest = &rest[at + 7..]; |
| 2302 |
let end = rest.find('"').expect("the attribute closes"); |
| 2303 |
for name in rest[..end].split_whitespace() { |
| 2304 |
if !name.starts_with("col-") { |
| 2305 |
names.insert(name.to_string()); |
| 2306 |
} |
| 2307 |
} |
| 2308 |
rest = &rest[end..]; |
| 2309 |
} |
| 2310 |
} |
| 2311 |
names |
| 2312 |
} |
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
const BY_DESIGN: &[&str] = &[ |
| 2325 |
|
| 2326 |
"list-detail", |
| 2327 |
"list-detail-tabbed", |
| 2328 |
"sidebar-content", |
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
"measure-wide", |
| 2335 |
"measure-contained", |
| 2336 |
"measure-reading", |
| 2337 |
|
| 2338 |
"band", |
| 2339 |
"bespoke", |
| 2340 |
"modal", |
| 2341 |
"pane", |
| 2342 |
"region", |
| 2343 |
"sidebar", |
| 2344 |
"split", |
| 2345 |
"tabgroup", |
| 2346 |
|
| 2347 |
"figures", |
| 2348 |
"form", |
| 2349 |
"notices", |
| 2350 |
"rest", |
| 2351 |
"row", |
| 2352 |
"selector", |
| 2353 |
|
| 2354 |
|
| 2355 |
"heading", |
| 2356 |
"rich", |
| 2357 |
"text", |
| 2358 |
]; |
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
const MODIFIERS: &[&str] = &[ |
| 2373 |
"act-submit", |
| 2374 |
"rest-more", |
| 2375 |
"row-actions", |
| 2376 |
"row-activate", |
| 2377 |
"row-proportion", |
| 2378 |
"row-select", |
| 2379 |
"row-selected", |
| 2380 |
"row-tokens", |
| 2381 |
"cell-actions", |
| 2382 |
"cell-tokens", |
| 2383 |
"field-writes", |
| 2384 |
]; |
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
const GAPS: &[&str] = &[ |
| 2412 |
"form-checkbox-label", |
| 2413 |
"form-error", |
| 2414 |
"form-group", |
| 2415 |
"form-label", |
| 2416 |
"form-radio-group", |
| 2417 |
"form-radio-label", |
| 2418 |
"has-error", |
| 2419 |
"visible", |
| 2420 |
"placeholder-action", |
| 2421 |
"cell-fill", |
| 2422 |
"cell-keeps", |
| 2423 |
"banner", |
| 2424 |
"toast", |
| 2425 |
]; |
| 2426 |
|
| 2427 |
#[test] |
| 2428 |
fn every_class_this_renderer_emits_is_one_makeover_defines() { |
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
let css = makeover_webview::stylesheet(&Emit::default()); |
| 2435 |
let emitted = emitted_classes(&every_kind_of_screen()); |
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
let styled = |name: &str| { |
| 2440 |
css.match_indices(&format!(".{name}")).any(|(at, found)| { |
| 2441 |
css[at + found.len()..] |
| 2442 |
.chars() |
| 2443 |
.next() |
| 2444 |
.is_none_or(|c| !c.is_ascii_alphanumeric() && c != '-' && c != '_') |
| 2445 |
}) |
| 2446 |
}; |
| 2447 |
|
| 2448 |
let unstyled: Vec<&str> = emitted |
| 2449 |
.iter() |
| 2450 |
.map(String::as_str) |
| 2451 |
.filter(|name| !styled(name)) |
| 2452 |
.collect(); |
| 2453 |
|
| 2454 |
let mut accounted: Vec<&str> = [BY_DESIGN, MODIFIERS, GAPS].concat(); |
| 2455 |
accounted.sort_unstable(); |
| 2456 |
assert_eq!( |
| 2457 |
accounted.len(), |
| 2458 |
accounted |
| 2459 |
.iter() |
| 2460 |
.collect::<std::collections::BTreeSet<_>>() |
| 2461 |
.len(), |
| 2462 |
"a name is in two of the three lists, which means two answers to one \ |
| 2463 |
question" |
| 2464 |
); |
| 2465 |
|
| 2466 |
assert_eq!( |
| 2467 |
unstyled, accounted, |
| 2468 |
"a class this renderer emits has no rule and no entry above. If \ |
| 2469 |
makeover spells it differently, use makeover's spelling -- that is the \ |
| 2470 |
whole of the SSH-keys bug. Otherwise put it in BY_DESIGN, MODIFIERS or \ |
| 2471 |
GAPS with the reason, and note that GAPS is work rather than a \ |
| 2472 |
decision." |
| 2473 |
); |
| 2474 |
} |
| 2475 |
|
| 2476 |
#[test] |
| 2477 |
fn a_class_prefix_reaches_the_markup_the_way_it_reaches_the_stylesheet() { |
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
let emit = Emit { |
| 2483 |
class_prefix: "mk-", |
| 2484 |
..Emit::default() |
| 2485 |
}; |
| 2486 |
let html = Webview::new() |
| 2487 |
.with_emit(emit) |
| 2488 |
.fragment(&Node::list([Row::new("fw13").token(Tag::badge("Active"))])); |
| 2489 |
|
| 2490 |
assert!(html.contains("class=\"mk-row\""), "{html}"); |
| 2491 |
assert!(html.contains("mk-row-primary"), "{html}"); |
| 2492 |
assert!(html.contains("mk-badge"), "{html}"); |
| 2493 |
assert!(!html.contains("\"row\""), "{html}"); |
| 2494 |
} |
| 2495 |
|
| 2496 |
#[test] |
| 2497 |
fn an_invalidated_slot_is_addressed_by_name_and_swapped_inside_its_region() { |
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
let html = Webview::new().invalidated("task-count", &Node::text("4 left")); |
| 2503 |
|
| 2504 |
assert!( |
| 2505 |
html.starts_with("<div hx-swap-oob=\"innerHTML:#task-count\">"), |
| 2506 |
"{html}" |
| 2507 |
); |
| 2508 |
assert!(html.contains("4 left"), "{html}"); |
| 2509 |
assert!(html.ends_with("</div>"), "{html}"); |
| 2510 |
} |
| 2511 |
|
| 2512 |
#[test] |
| 2513 |
fn a_slot_id_cannot_break_out_of_the_out_of_band_selector() { |
| 2514 |
|
| 2515 |
|
| 2516 |
let html = Webview::new().invalidated("a\"><script>x</script>", &Node::text("hi")); |
| 2517 |
assert!(!html.contains("<script>"), "{html}"); |
| 2518 |
} |
| 2519 |
|
| 2520 |
#[test] |
| 2521 |
fn a_tick_carries_the_value_it_contributes_to_the_selection() { |
| 2522 |
|
| 2523 |
|
| 2524 |
let html = fragment(&Node::list([ |
| 2525 |
Row::new("First").ticking("m-1", false), |
| 2526 |
Row::new("Second").ticking("m-2", true), |
| 2527 |
])); |
| 2528 |
|
| 2529 |
assert!(html.contains("name=\"ticked\" value=\"m-1\""), "{html}"); |
| 2530 |
assert!(html.contains("name=\"ticked\" value=\"m-2\""), "{html}"); |
| 2531 |
assert_eq!(html.matches("checked").count(), 1, "{html}"); |
| 2532 |
} |
| 2533 |
|
| 2534 |
#[test] |
| 2535 |
fn a_commit_control_gathers_every_tick_on_the_screen() { |
| 2536 |
|
| 2537 |
|
| 2538 |
|
| 2539 |
let html = fragment(&Node::Act( |
| 2540 |
Act::new("Archive", Action::post("/mail/archive")).over("chosen"), |
| 2541 |
)); |
| 2542 |
|
| 2543 |
assert!(html.contains("hx-include=\".row-select\""), "{html}"); |
| 2544 |
assert!(html.contains("hx-post=\"/mail/archive\""), "{html}"); |
| 2545 |
} |
| 2546 |
|
| 2547 |
#[test] |
| 2548 |
fn a_control_over_nothing_gathers_nothing() { |
| 2549 |
let html = fragment(&Node::Act(Act::new( |
| 2550 |
"Delete", |
| 2551 |
Action::post("/mail/1/delete"), |
| 2552 |
))); |
| 2553 |
assert!(!html.contains("hx-include"), "{html}"); |
| 2554 |
} |
| 2555 |
|
| 2556 |
#[test] |
| 2557 |
fn the_gathering_selector_follows_a_hosts_class_prefix() { |
| 2558 |
|
| 2559 |
|
| 2560 |
let emit = Emit { |
| 2561 |
class_prefix: "q-", |
| 2562 |
..Emit::default() |
| 2563 |
}; |
| 2564 |
let render = Webview::new().with_emit(emit); |
| 2565 |
|
| 2566 |
let boxes = render.fragment(&Node::list([Row::new("First").ticking("m-1", false)])); |
| 2567 |
let button = render.fragment(&Node::Act( |
| 2568 |
Act::new("Archive", Action::post("/mail/archive")).over("chosen"), |
| 2569 |
)); |
| 2570 |
|
| 2571 |
assert!(boxes.contains("q-row-select"), "{boxes}"); |
| 2572 |
assert!(button.contains("hx-include=\".q-row-select\""), "{button}"); |
| 2573 |
} |
| 2574 |
|
| 2575 |
#[test] |
| 2576 |
fn an_overlay_is_the_inside_of_a_container_and_not_a_document() { |
| 2577 |
|
| 2578 |
let screen = Screen::sidebar_content("Palette") |
| 2579 |
.with(Slot::new("results", RegionKind::Pane).with(Node::text("Open task"))); |
| 2580 |
let html = Webview::new().overlay(&screen); |
| 2581 |
assert!(!html.contains("<html"), "{html}"); |
| 2582 |
assert!(!html.contains("<body"), "{html}"); |
| 2583 |
assert!(!html.contains("<title"), "{html}"); |
| 2584 |
assert!(html.contains("Open task"), "{html}"); |
| 2585 |
} |
| 2586 |
|
| 2587 |
#[test] |
| 2588 |
fn an_overlay_names_the_container_it_lands_in() { |
| 2589 |
|
| 2590 |
|
| 2591 |
assert_eq!( |
| 2592 |
Webview::new().overlay_target(), |
| 2593 |
Some(crate::chrome::OVERLAY_ID) |
| 2594 |
); |
| 2595 |
} |
| 2596 |
|
| 2597 |
#[test] |
| 2598 |
fn a_document_carries_the_apps_bindings_and_the_container_they_open_into() { |
| 2599 |
use quasi_router::Chrome; |
| 2600 |
|
| 2601 |
let shell = Shell::default().with_chrome(Chrome::new().bind( |
| 2602 |
"ctrl+k", |
| 2603 |
"Search", |
| 2604 |
Action::get("/palette"), |
| 2605 |
)); |
| 2606 |
let html = Webview::new() |
| 2607 |
.with_shell(shell) |
| 2608 |
.screen(&Screen::list_detail("Tasks", false)); |
| 2609 |
assert!(html.contains("hx-get=\"/palette\""), "{html}"); |
| 2610 |
assert!(html.contains("from:body"), "{html}"); |
| 2611 |
assert!(html.contains("id=\"quasi-overlay\""), "{html}"); |
| 2612 |
|
| 2613 |
|
| 2614 |
let overlay_at = html.find("id=\"quasi-overlay\"").expect("emitted"); |
| 2615 |
assert!( |
| 2616 |
overlay_at > html.find("</main>").expect("main closes"), |
| 2617 |
"{html}" |
| 2618 |
); |
| 2619 |
assert!(html.ends_with("</body></html>"), "{html}"); |
| 2620 |
} |
| 2621 |
|
| 2622 |
#[test] |
| 2623 |
fn an_app_declaring_no_chrome_gets_the_document_it_always_got() { |
| 2624 |
|
| 2625 |
let html = render(&Screen::list_detail("Tasks", false)); |
| 2626 |
assert!(!html.contains("quasi-overlay"), "{html}"); |
| 2627 |
assert!(!html.contains("data-chrome"), "{html}"); |
| 2628 |
} |
| 2629 |
|
| 2630 |
|
| 2631 |
fn gallery() -> Slot { |
| 2632 |
Slot::widget("shots", "carousel") |
| 2633 |
.extend((0..3).map(|n| { |
| 2634 |
Node::Image(quasi_router::screen::Picture::new( |
| 2635 |
format!("/shot-{n}.png"), |
| 2636 |
format!("shot {n}"), |
| 2637 |
)) |
| 2638 |
})) |
| 2639 |
.showing_one(1) |
| 2640 |
} |
| 2641 |
|
| 2642 |
#[test] |
| 2643 |
fn a_region_showing_everything_emits_exactly_what_it_always_did() { |
| 2644 |
|
| 2645 |
|
| 2646 |
|
| 2647 |
let screen = Screen::list_detail("Tasks", false) |
| 2648 |
.with(Slot::new("main", RegionKind::Pane).with(Node::text("plain"))); |
| 2649 |
let html = render(&screen); |
| 2650 |
|
| 2651 |
assert!(!html.contains("data-showing")); |
| 2652 |
assert!(!html.contains("showing-frame")); |
| 2653 |
assert!(!html.contains("showing-position")); |
| 2654 |
} |
| 2655 |
|
| 2656 |
#[test] |
| 2657 |
fn a_carousel_gets_a_row_without_this_renderer_knowing_what_a_carousel_is() { |
| 2658 |
|
| 2659 |
|
| 2660 |
let screen = Screen::list_detail("Product", false).with(gallery()); |
| 2661 |
let html = render(&screen); |
| 2662 |
|
| 2663 |
assert!(html.contains("data-showing=\"one\""), "{html}"); |
| 2664 |
assert!(html.contains("data-shows=\"previous\""), "{html}"); |
| 2665 |
assert!(html.contains("data-shows=\"next\""), "{html}"); |
| 2666 |
|
| 2667 |
|
| 2668 |
assert!(html.contains(">2 / 3</span>"), "{html}"); |
| 2669 |
|
| 2670 |
assert!(html.contains("data-widget=\"carousel\"")); |
| 2671 |
} |
| 2672 |
|
| 2673 |
#[test] |
| 2674 |
fn the_row_sits_under_the_frames_and_overlays_nothing() { |
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
let html = render(&Screen::list_detail("Product", false).with(gallery())); |
| 2679 |
|
| 2680 |
let last_frame = html.rfind("showing-frame").expect("frames are wrapped"); |
| 2681 |
let row = html.rfind("showing-position").expect("a row is derived"); |
| 2682 |
assert!(row > last_frame, "{html}"); |
| 2683 |
} |
| 2684 |
|
| 2685 |
#[test] |
| 2686 |
fn only_the_current_frame_is_marked_and_the_rest_are_still_in_the_document() { |
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
let html = render(&Screen::list_detail("Product", false).with(gallery())); |
| 2691 |
|
| 2692 |
assert_eq!(html.matches("showing-frame").count(), 3, "{html}"); |
| 2693 |
assert_eq!(html.matches("showing-frame current").count(), 1, "{html}"); |
| 2694 |
assert!(html.contains("/shot-0.png") && html.contains("/shot-2.png")); |
| 2695 |
} |
| 2696 |
|
| 2697 |
#[test] |
| 2698 |
fn labelled_children_get_a_strip_and_it_is_makeovers_tab_markup() { |
| 2699 |
|
| 2700 |
|
| 2701 |
let screen = Screen::list_detail("Project", false).with( |
| 2702 |
Slot::new("detail", RegionKind::TabGroup) |
| 2703 |
.with(Node::Region( |
| 2704 |
Slot::new("overview", RegionKind::Pane).label("Overview"), |
| 2705 |
)) |
| 2706 |
.with(Node::Region( |
| 2707 |
Slot::new("files", RegionKind::Pane).label("Files"), |
| 2708 |
)) |
| 2709 |
.showing_one(1), |
| 2710 |
); |
| 2711 |
let html = render(&screen); |
| 2712 |
|
| 2713 |
assert!(html.contains("data-selector=\"tab\""), "{html}"); |
| 2714 |
assert!(html.contains("role=\"tablist\""), "{html}"); |
| 2715 |
assert!(html.contains(">Overview</button>"), "{html}"); |
| 2716 |
assert!(html.contains("chosen\" data-shows=\"1\""), "{html}"); |
| 2717 |
assert!( |
| 2718 |
html.contains("aria-selected=\"true\">Files</button>"), |
| 2719 |
"{html}" |
| 2720 |
); |
| 2721 |
assert!( |
| 2722 |
html.contains("aria-selected=\"false\">Overview</button>"), |
| 2723 |
"{html}" |
| 2724 |
); |
| 2725 |
|
| 2726 |
assert!(!html.contains("showing-position"), "{html}"); |
| 2727 |
} |
| 2728 |
|
| 2729 |
#[test] |
| 2730 |
fn a_strip_sits_above_the_panes_it_opens() { |
| 2731 |
|
| 2732 |
|
| 2733 |
let screen = Screen::list_detail("Project", false).with( |
| 2734 |
Slot::new("detail", RegionKind::TabGroup) |
| 2735 |
.with(Node::Region( |
| 2736 |
Slot::new("overview", RegionKind::Pane).label("Overview"), |
| 2737 |
)) |
| 2738 |
.with(Node::Region( |
| 2739 |
Slot::new("files", RegionKind::Pane).label("Files"), |
| 2740 |
)) |
| 2741 |
.showing_one(0), |
| 2742 |
); |
| 2743 |
let html = render(&screen); |
| 2744 |
|
| 2745 |
let strip = html |
| 2746 |
.find("data-selector=\"tab\"") |
| 2747 |
.expect("a strip is derived"); |
| 2748 |
let first_frame = html.find("showing-frame").expect("frames are wrapped"); |
| 2749 |
assert!(strip < first_frame, "{html}"); |
| 2750 |
} |
| 2751 |
|
| 2752 |
#[test] |
| 2753 |
fn a_named_child_that_can_close_is_a_summary_line() { |
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
let screen = Screen::list_detail("Item", false).with( |
| 2758 |
Slot::widget("more", "disclosure") |
| 2759 |
.with(Node::Region( |
| 2760 |
Slot::new("body", RegionKind::Pane) |
| 2761 |
.label("Technical details") |
| 2762 |
.with(Node::text("the rest")), |
| 2763 |
)) |
| 2764 |
.showing_at_most_one(None), |
| 2765 |
); |
| 2766 |
let html = render(&screen); |
| 2767 |
|
| 2768 |
assert!(html.contains("data-showing=\"at-most-one\""), "{html}"); |
| 2769 |
assert!(html.contains("aria-expanded=\"false\""), "{html}"); |
| 2770 |
assert!(html.contains(">Technical details</button>"), "{html}"); |
| 2771 |
assert!(!html.contains("data-shows=\"next\""), "{html}"); |
| 2772 |
} |
| 2773 |
|
| 2774 |
#[test] |
| 2775 |
fn a_derived_control_names_no_route_and_the_transport_stays_in_one_function() { |
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
let html = render(&Screen::list_detail("Product", false).with(gallery())); |
| 2780 |
let row = &html[html.find("showing-position").expect("a row is derived") - 200..]; |
| 2781 |
|
| 2782 |
assert!(!row.contains("hx-get"), "{row}"); |
| 2783 |
assert!(!row.contains("hx-post"), "{row}"); |
| 2784 |
} |
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 2789 |
fn placed(at: u16, minutes: u16, text: &str) -> quasi_router::screen::Placed { |
| 2790 |
quasi_router::screen::Placed::new(at, minutes, Row::new(text)) |
| 2791 |
} |
| 2792 |
|
| 2793 |
#[test] |
| 2794 |
fn a_timeline_places_each_entry_as_a_percentage_of_its_span() { |
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
let node = Node::Timeline { |
| 2799 |
track: layout::Track::DAY, |
| 2800 |
entries: vec![placed(540, 60, "Standup")], |
| 2801 |
focus: None, |
| 2802 |
}; |
| 2803 |
let html = fragment(&node); |
| 2804 |
|
| 2805 |
assert!(html.contains("--track-at:37.5000%"), "{html}"); |
| 2806 |
assert!(html.contains("--track-for:4.1667%"), "{html}"); |
| 2807 |
|
| 2808 |
|
| 2809 |
assert!(html.contains("--track-lane:0"), "{html}"); |
| 2810 |
assert!(html.contains("--track-lanes:1"), "{html}"); |
| 2811 |
} |
| 2812 |
|
| 2813 |
#[test] |
| 2814 |
fn overlapping_entries_take_lanes_and_the_rest_stay_wide() { |
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
let node = Node::Timeline { |
| 2820 |
track: layout::Track::DAY, |
| 2821 |
entries: vec![ |
| 2822 |
placed(540, 60, "Standup"), |
| 2823 |
placed(570, 60, "Interview"), |
| 2824 |
placed(720, 30, "Lunch"), |
| 2825 |
], |
| 2826 |
focus: None, |
| 2827 |
}; |
| 2828 |
let html = fragment(&node); |
| 2829 |
|
| 2830 |
assert!(html.contains("--track-lane:0"), "{html}"); |
| 2831 |
assert!(html.contains("--track-lane:1"), "{html}"); |
| 2832 |
assert!( |
| 2833 |
!html.contains("--track-lane:2"), |
| 2834 |
"two collide, not three: {html}" |
| 2835 |
); |
| 2836 |
assert_eq!(html.matches("--track-lanes:2").count(), 3, "{html}"); |
| 2837 |
} |
| 2838 |
|
| 2839 |
#[test] |
| 2840 |
fn things_that_only_touch_do_not_collide() { |
| 2841 |
|
| 2842 |
|
| 2843 |
|
| 2844 |
let node = Node::Timeline { |
| 2845 |
track: layout::Track::DAY, |
| 2846 |
entries: vec![placed(540, 60, "First"), placed(600, 60, "Second")], |
| 2847 |
focus: None, |
| 2848 |
}; |
| 2849 |
let html = fragment(&node); |
| 2850 |
|
| 2851 |
assert!(html.contains("--track-lanes:1"), "{html}"); |
| 2852 |
assert!(!html.contains("--track-lane:1"), "{html}"); |
| 2853 |
} |
| 2854 |
|
| 2855 |
#[test] |
| 2856 |
fn a_timeline_labels_its_ruler_in_wall_clock_and_wraps_past_midnight() { |
| 2857 |
|
| 2858 |
|
| 2859 |
let node = Node::Timeline { |
| 2860 |
track: layout::Track::over(layout::Span::new(1320, 1560)), |
| 2861 |
entries: vec![], |
| 2862 |
focus: None, |
| 2863 |
}; |
| 2864 |
let html = fragment(&node); |
| 2865 |
|
| 2866 |
assert!(html.contains(">22:00<"), "{html}"); |
| 2867 |
assert!(html.contains(">00:00<"), "{html}"); |
| 2868 |
assert!(html.contains(">01:00<"), "{html}"); |
| 2869 |
assert!(!html.contains(">25:00<"), "the clock wrapped: {html}"); |
| 2870 |
} |
| 2871 |
|
| 2872 |
#[test] |
| 2873 |
fn a_timeline_carries_its_focus_as_a_moment_and_never_as_an_offset() { |
| 2874 |
|
| 2875 |
|
| 2876 |
let node = Node::Timeline { |
| 2877 |
track: layout::Track::DAY, |
| 2878 |
entries: vec![], |
| 2879 |
focus: Some(540), |
| 2880 |
}; |
| 2881 |
let html = fragment(&node); |
| 2882 |
|
| 2883 |
assert!(html.contains("data-focus=\"540\""), "{html}"); |
| 2884 |
assert!(!html.contains("scrollTop"), "{html}"); |
| 2885 |
assert!(!html.contains("px"), "{html}"); |
| 2886 |
} |
| 2887 |
|
| 2888 |
#[test] |
| 2889 |
fn a_placed_rows_text_cannot_become_markup() { |
| 2890 |
|
| 2891 |
|
| 2892 |
let node = Node::Timeline { |
| 2893 |
track: layout::Track::DAY, |
| 2894 |
entries: vec![placed(540, 60, "<script>alert('x')</script>")], |
| 2895 |
focus: None, |
| 2896 |
}; |
| 2897 |
let html = fragment(&node); |
| 2898 |
|
| 2899 |
assert!(!html.contains("<script>"), "{html}"); |
| 2900 |
assert!(html.contains("<script>"), "{html}"); |
| 2901 |
} |
| 2902 |
|
| 2903 |
#[test] |
| 2904 |
fn a_timeline_emits_no_size_of_its_own() { |
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
let node = Node::Timeline { |
| 2910 |
track: layout::Track::DAY, |
| 2911 |
entries: vec![placed(540, 60, "Standup")], |
| 2912 |
focus: None, |
| 2913 |
}; |
| 2914 |
let html = fragment(&node); |
| 2915 |
|
| 2916 |
for unit in ["px", "rem", "em;", "vh"] { |
| 2917 |
assert!(!html.contains(unit), "emitted a {unit}: {html}"); |
| 2918 |
} |
| 2919 |
} |
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
#[test] |
| 2924 |
fn peer_columns_name_themselves_and_carry_no_width() { |
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
let column = |id: &str, label: &str| { |
| 2929 |
Slot::new(id, RegionKind::Pane).with(Node::Heading { |
| 2930 |
level: layout::Heading::Section, |
| 2931 |
text: label.into(), |
| 2932 |
}) |
| 2933 |
}; |
| 2934 |
let screen = Screen::list_detail("Tasks", false).with( |
| 2935 |
Slot::new("board", RegionKind::Columns) |
| 2936 |
.with(Node::Region(column("pending", "Pending"))) |
| 2937 |
.with(Node::Region(column("started", "Started"))) |
| 2938 |
.with(Node::Region(column("done", "Completed"))), |
| 2939 |
); |
| 2940 |
|
| 2941 |
let html = render(&screen); |
| 2942 |
|
| 2943 |
assert!(html.contains("columns"), "{html}"); |
| 2944 |
|
| 2945 |
|
| 2946 |
assert!(!html.contains("data-share"), "{html}"); |
| 2947 |
assert!(!html.contains("data-columns"), "{html}"); |
| 2948 |
for label in ["Pending", "Started", "Completed"] { |
| 2949 |
assert!(html.contains(label), "{html}"); |
| 2950 |
} |
| 2951 |
} |
| 2952 |
|
| 2953 |
#[test] |
| 2954 |
fn a_board_is_not_a_split() { |
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
let board = render(&Screen::list_detail("B", false).with(Slot::new("r", RegionKind::Columns))); |
| 2959 |
let split = render(&Screen::list_detail("S", false).with(Slot::new("r", RegionKind::Split))); |
| 2960 |
|
| 2961 |
assert!(board.contains("columns"), "{board}"); |
| 2962 |
assert!(!board.contains("split"), "{board}"); |
| 2963 |
assert!(split.contains("split"), "{split}"); |
| 2964 |
} |
| 2965 |
|
| 2966 |
#[test] |
| 2967 |
fn a_day_strip_labels_days_and_not_a_wall_clock() { |
| 2968 |
|
| 2969 |
|
| 2970 |
|
| 2971 |
let march = layout::Track::days(layout::Span::new(0, 31)); |
| 2972 |
let node = Node::Timeline { |
| 2973 |
track: march, |
| 2974 |
entries: vec![quasi_router::screen::Placed::new(2, 15, Row::new("Leave"))], |
| 2975 |
focus: None, |
| 2976 |
}; |
| 2977 |
let html = fragment(&node); |
| 2978 |
|
| 2979 |
|
| 2980 |
|
| 2981 |
assert!(html.contains(">1<"), "{html}"); |
| 2982 |
assert!(html.contains(">8<"), "{html}"); |
| 2983 |
assert!( |
| 2984 |
!html.contains("00:00"), |
| 2985 |
"a month strip under a wall clock: {html}" |
| 2986 |
); |
| 2987 |
|
| 2988 |
|
| 2989 |
assert!(html.contains("--track-at:6.4516%"), "{html}"); |
| 2990 |
assert!(html.contains("--track-for:48.3871%"), "{html}"); |
| 2991 |
} |
| 2992 |
|
| 2993 |
#[test] |
| 2994 |
fn a_day_view_still_labels_a_wall_clock() { |
| 2995 |
|
| 2996 |
|
| 2997 |
let node = Node::Timeline { |
| 2998 |
track: layout::Track::DAY, |
| 2999 |
entries: vec![], |
| 3000 |
focus: None, |
| 3001 |
}; |
| 3002 |
let html = fragment(&node); |
| 3003 |
assert!(html.contains(">09:00<"), "{html}"); |
| 3004 |
} |
| 3005 |
|