Skip to content

Commit c461123

Browse files
committed
fix: flush
1 parent b3684d8 commit c461123

File tree

5 files changed

+40
-31
lines changed

5 files changed

+40
-31
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ssr_router/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ jemallocator = "0.5"
4242

4343
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
4444
wasm-bindgen-test.workspace = true
45+
wasm-bindgen.workspace = true
46+
web-sys = { workspace = true }
4547
gloo = { workspace = true }
4648
ssr-e2e-harness = { path = "../../tools/ssr-e2e-harness" }
4749

4850
[dev-dependencies]
49-
yew = { path = "../../packages/yew", features = ["hydration"] }
51+
yew = { path = "../../packages/yew", features = ["hydration", "test"] }
5052
yew-link = { path = "../../packages/yew-link", features = ["hydration"] }
5153

5254
[features]

examples/ssr_router/tests/e2e.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
use gloo::utils::document;
44
use ssr_e2e_harness::{
5-
clear_resource_timings, fetch_ssr_html, navigate, output_element, resource_request_count,
6-
setup_ssr_page, wait_for,
5+
clear_resource_timings, fetch_ssr_html, output_element, resource_request_count, setup_ssr_page,
6+
wait_for,
77
};
88
use ssr_router::{App, AppProps, LINK_ENDPOINT};
9+
use wasm_bindgen::JsCast;
910
use wasm_bindgen_test::*;
1011
use yew::Renderer;
1112

@@ -90,13 +91,18 @@ async fn ssr_hydration_and_client_navigation() {
9091

9192
// -- Part 2: Navigate to /posts within the same app, then to /posts/0 --
9293

93-
// Yield to ensure effects (router history listener) are registered.
94-
gloo::timers::future::sleep(std::time::Duration::from_millis(500)).await;
94+
yew::scheduler::flush().await;
9595

9696
clear_resource_timings();
9797

98-
// Navigate to /posts first, then to /posts/0 to trigger a client-side fetch.
99-
navigate("/posts");
98+
let posts_link: web_sys::HtmlElement = output_element()
99+
.query_selector("a.navbar-item[href='/posts']")
100+
.unwrap()
101+
.expect("Posts navbar link should exist")
102+
.dyn_into()
103+
.unwrap();
104+
posts_link.click();
105+
yew::scheduler::flush().await;
100106

101107
wait_for(
102108
|| {
@@ -114,7 +120,27 @@ async fn ssr_hydration_and_client_navigation() {
114120

115121
clear_resource_timings();
116122

117-
navigate("/posts/0");
123+
wait_for(
124+
|| {
125+
output_element()
126+
.query_selector("a.title.is-block[href='/posts/0']")
127+
.ok()
128+
.flatten()
129+
.is_some()
130+
},
131+
15000,
132+
"post 0 card link on posts list",
133+
)
134+
.await;
135+
136+
let post_link: web_sys::HtmlElement = output_element()
137+
.query_selector("a.title.is-block[href='/posts/0']")
138+
.unwrap()
139+
.unwrap()
140+
.dyn_into()
141+
.unwrap();
142+
post_link.click();
143+
yew::scheduler::flush().await;
118144

119145
wait_for(
120146
|| {
@@ -153,7 +179,7 @@ async fn ssr_hydration_and_client_navigation() {
153179
);
154180

155181
app.destroy();
156-
output_element().set_inner_html("");
182+
yew::scheduler::flush().await;
157183
}
158184

159185
#[wasm_bindgen_test]
@@ -169,5 +195,5 @@ async fn hydrate_home() {
169195
.await;
170196

171197
app.destroy();
172-
output_element().set_inner_html("");
198+
yew::scheduler::flush().await;
173199
}

tools/ssr-e2e-harness/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
gloo = { workspace = true, features = ["futures"] }
8-
gloo-history = "0.2"
98
web-sys = { workspace = true }
109
wasm-bindgen = { workspace = true }
1110
js-sys = { workspace = true }

tools/ssr-e2e-harness/src/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,6 @@ pub fn push_route(path: &str) {
5858
.unwrap();
5959
}
6060

61-
/// Triggers in-app navigation by pushing a route onto gloo-history's
62-
/// `BrowserHistory` singleton, which calls `pushState` and then directly
63-
/// invokes all registered history callbacks.
64-
///
65-
/// HtmlElement.click() on `<a>` in headless Firefox triggers real browser
66-
/// navigation, crashing the test runner. Yew's capture-phase event delegation
67-
/// cannot prevent it in the wasm-bindgen-test context.
68-
///
69-
/// Dispatching a synthetic `PopStateEvent` on `window` also does not work
70-
/// reliably: `pushState` does not fire `popstate` natively, and while
71-
/// gloo-history registers a popstate listener, tests have shown it does not
72-
/// pick up synthetic events consistently across browsers. The reliable approach
73-
/// is to use `BrowserHistory::push()` directly, which calls `notify_callbacks()`
74-
/// internally and is the same codepath as yew-router's `<Link>` component.
75-
pub fn navigate(path: &str) {
76-
use gloo_history::History;
77-
gloo_history::BrowserHistory::new().push(path);
78-
}
79-
8061
fn window_js() -> JsValue {
8162
web_sys::window().unwrap().into()
8263
}

0 commit comments

Comments
 (0)