|
| 1 | +#![cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] |
| 2 | + |
| 3 | +use std::time::Duration; |
| 4 | + |
| 5 | +use function_router::App; |
| 6 | +use gloo::utils::document; |
| 7 | +use ssr_e2e_harness::{fetch_ssr_html, output_element, push_route, wait_for}; |
| 8 | +use wasm_bindgen_test::*; |
| 9 | +use yew::platform::time::sleep; |
| 10 | + |
| 11 | +wasm_bindgen_test_configure!(run_in_browser); |
| 12 | + |
| 13 | +const SERVER_BASE: &str = "http://127.0.0.1:8080"; |
| 14 | + |
| 15 | +fn get_title_text() -> Option<String> { |
| 16 | + document() |
| 17 | + .query_selector("h1.title") |
| 18 | + .ok() |
| 19 | + .flatten() |
| 20 | + .map(|el| el.text_content().unwrap_or_default()) |
| 21 | +} |
| 22 | + |
| 23 | +#[wasm_bindgen_test] |
| 24 | +async fn hydrate_post_page() { |
| 25 | + let body_html = fetch_ssr_html(SERVER_BASE, "/posts/0").await; |
| 26 | + |
| 27 | + output_element().set_inner_html(&body_html); |
| 28 | + push_route("/posts/0"); |
| 29 | + yew::Renderer::<App>::with_root(output_element()).hydrate(); |
| 30 | + |
| 31 | + wait_for( |
| 32 | + || { |
| 33 | + let html = output_element().inner_html(); |
| 34 | + html.contains("<h1 class=\"title\">") && !html.contains("Loading post...") |
| 35 | + }, |
| 36 | + 5000, |
| 37 | + "post page content", |
| 38 | + ) |
| 39 | + .await; |
| 40 | + |
| 41 | + let title = get_title_text().expect("h1.title should be present on the post page"); |
| 42 | + assert!(!title.is_empty(), "post title should not be empty"); |
| 43 | +} |
| 44 | + |
| 45 | +#[wasm_bindgen_test] |
| 46 | +async fn hydrate_posts_list() { |
| 47 | + let body_html = fetch_ssr_html(SERVER_BASE, "/posts").await; |
| 48 | + |
| 49 | + output_element().set_inner_html(&body_html); |
| 50 | + push_route("/posts"); |
| 51 | + yew::Renderer::<App>::with_root(output_element()).hydrate(); |
| 52 | + |
| 53 | + wait_for( |
| 54 | + || { |
| 55 | + document() |
| 56 | + .query_selector("a.title.is-block") |
| 57 | + .ok() |
| 58 | + .flatten() |
| 59 | + .is_some() |
| 60 | + }, |
| 61 | + 10000, |
| 62 | + "post links to appear on /posts", |
| 63 | + ) |
| 64 | + .await; |
| 65 | +} |
| 66 | + |
| 67 | +#[wasm_bindgen_test] |
| 68 | +async fn hydrate_home() { |
| 69 | + let body_html = fetch_ssr_html(SERVER_BASE, "/").await; |
| 70 | + |
| 71 | + output_element().set_inner_html(&body_html); |
| 72 | + push_route("/"); |
| 73 | + yew::Renderer::<App>::with_root(output_element()).hydrate(); |
| 74 | + |
| 75 | + sleep(Duration::from_secs(2)).await; |
| 76 | + let html = output_element().inner_html(); |
| 77 | + assert!( |
| 78 | + html.contains("Welcome"), |
| 79 | + "home page should have content after hydration" |
| 80 | + ); |
| 81 | +} |
0 commit comments