leptos_hydrated 0.7.0

A component to hydrate and manage interactive hydration state in Leptos 0.8
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use super::*;
use leptos::reactive::owner::Owner;
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Shared fixture
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Shared fixture
// ---------------------------------------------------------------------------

static INIT: std::sync::Once = std::sync::Once::new();

fn init_test_env() {
    INIT.call_once(|| {
        let _ = any_spawner::Executor::init_tokio();
    });
}

#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Debug)]
pub struct DefaultState {
    pub value: i32,
}
impl Hydratable for DefaultState {
    fn initial() -> Self { Self::default() }
}

#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Debug)]
pub struct ThemeState {
    pub theme: String,
}

impl Hydratable for ThemeState {
    fn initial() -> Self {
        // Use isomorphic helpers to read from cookies/query params on both sides.
        let theme = get_cookie("theme").unwrap_or_else(|| "dark".into());
        ThemeState { theme }
    }

    fn fetch() -> impl std::future::Future<Output = Option<Self>> + Send + 'static {
        // Re-read from the same client-side source after hydration.
        async {
            let theme = get_cookie("theme").unwrap_or_else(|| "dark".into());
            Some(ThemeState { theme })
        }
    }
}

// ---------------------------------------------------------------------------
// Mechanism tests: use_hydrate_signal
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_signal_initialises_from_ssr_value() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        owner.with(|| {
            let (signal, _) = use_hydrate_signal(
                || 42,
                || async { Some(100) },
            );
            assert_eq!(signal.get(), 42);
        });
    }).await;
}

#[tokio::test]
async fn test_fetch_none_keeps_initial_value() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        owner.with(|| {
            let (signal, _) = use_hydrate_signal(
                || 42,
                || async { None::<i32> },
            );
            assert_eq!(signal.get(), 42);
        });
    }).await;
}

// CSR-only: resource actually resolves
#[cfg(not(feature = "ssr"))]
#[tokio::test]
async fn test_fetch_some_ok_updates_signal() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        let signal = owner.with(|| {
            let (signal, _) = use_hydrate_signal(
                || 42,
                || async { Some(100) },
            );
            signal
        });
        assert_eq!(signal.get(), 42);
        for _ in 0..20 { tokio::task::yield_now().await; }
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        assert_eq!(signal.get(), 100);
    }).await;
}

#[cfg(not(feature = "ssr"))]
#[tokio::test]
async fn test_fetch_some_err_keeps_initial_value() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        let signal = owner.with(|| {
            let (signal, _) = use_hydrate_signal(
                || 42,
                || async { None::<i32> },
            );
            signal
        });
        assert_eq!(signal.get(), 42);
        for _ in 0..20 { tokio::task::yield_now().await; }
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        assert_eq!(signal.get(), 42);
    }).await;
}
#[cfg(not(feature = "ssr"))]
#[tokio::test]
async fn test_two_way_binding_sync_flow() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        let (signal, resource) = owner.with(|| {
            use_hydrate_signal(
                || 1,
                || async { 
                    tokio::time::sleep(std::time::Duration::from_millis(10)).await;
                    Some(2) 
                },
            )
        });

        // 1. Initial SSR state
        assert_eq!(signal.get(), 1);
        // LocalResource is always None on the server or initially on the client before task runs
        assert!(resource.get().is_none());

        // 2. Wait for hydration to finish
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        assert_eq!(signal.get(), 2);
        assert_eq!(resource.get(), Some(Some(2)));

        // 3. Simulated user update (2-way binding concept)
        signal.set(3);
        assert_eq!(signal.get(), 3);
        // Wait for resource to re-evaluate (it tracks the signal now)
        for _ in 0..10 { tokio::task::yield_now().await; }
        assert_eq!(resource.get(), Some(Some(3)));
    }).await;
}

// ---------------------------------------------------------------------------
// SSR isolation
// ---------------------------------------------------------------------------

#[cfg(feature = "ssr")]
#[tokio::test]
async fn test_ssr_resource_is_muted() {
    init_test_env();
    let owner = Owner::new_root(None);
    owner.with(|| {
        let (signal, resource) = use_hydrate_signal(
            || 42,
            || async { panic!("Fetcher should not be called on SSR") },
        );
        assert_eq!(signal.get(), 42);
        // LocalResource should not resolve on the server
        assert!(resource.get().is_none());
    });
}

// ---------------------------------------------------------------------------
// Component + context tests
// ---------------------------------------------------------------------------

#[component]
fn MainContent() -> impl IntoView {
    let state = use_hydrated::<ThemeState>();
    view! { <p>"Theme: " {move || state.get().theme}</p> }
}

#[tokio::test]
async fn test_hydrate_state_provides_context() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        owner.with(|| {
            let _ = view! {
                <HydrateState<ThemeState> />
                <MainContent />
            };
        });
    }).await;
}

#[component]
fn ScopedDisplay() -> impl IntoView {
    let state = use_hydrated::<ThemeState>();
    view! { <p>"Scoped: " {move || state.get().theme}</p> }
}

#[tokio::test]
async fn test_hydrate_context_provides_context_to_children() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        owner.with(|| {
            let _ = view! {
                <HydrateContext<ThemeState>>
                    <ScopedDisplay />
                </HydrateContext<ThemeState>>
            };
        });
    }).await;
}

#[tokio::test]
async fn test_hydrate_state_with_provides_signal_and_resource() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        owner.with(|| {
            let _ = view! {
                <HydrateStateWith
                    ssr_value=|| ThemeState { theme: "dark".into() }
                    fetcher=|| async { None::<ThemeState> }
                />
                {move || {
                    let signal = use_hydrated::<ThemeState>();
                    let _resource = use_hydrated_resource::<ThemeState>();
                    assert_eq!(signal.get_untracked().theme, "dark");
                    "".into_view()
                }}
            };
        });
    }).await;
}

#[tokio::test]
async fn test_hydrate_context_with_provides_signal_and_resource() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        owner.with(|| {
            let _ = view! {
                <HydrateContextWith
                    ssr_value=|| ThemeState { theme: "dark".into() }
                    fetcher=|| async { None::<ThemeState> }
                >
                    {move || {
                        let signal = use_hydrated::<ThemeState>();
                        let _resource = use_hydrated_resource::<ThemeState>();
                        assert_eq!(signal.get_untracked().theme, "dark");
                        "".into_view()
                    }}
                </HydrateContextWith>
            };
        });
    }).await;
}

// ---------------------------------------------------------------------------
// HydratedSignal wrapper
// ---------------------------------------------------------------------------

#[test]
fn test_hydrated_signal_wrapper_eq_and_debug() {
    let owner = Owner::new_root(None);
    owner.with(|| {
        let s = RwSignal::new(42);
        let h1 = HydratedSignal(s);
        let h2 = h1;
        assert_eq!(h1, h2);
        assert!(format!("{:?}", h1).contains("HydratedSignal"));
    });
}

// ---------------------------------------------------------------------------
// try_ accessors
// ---------------------------------------------------------------------------

#[test]
fn test_try_use_hydrated_returns_some_when_context_exists() {
    let owner = Owner::new_root(None);
    owner.with(|| {
        provide_context(HydratedSignal(RwSignal::new(ThemeState { theme: "dark".into() })));
        let result = try_use_hydrated::<ThemeState>();
        assert!(result.is_some());
        assert_eq!(result.unwrap().get().theme, "dark");
    });
}

#[test]
fn test_try_use_hydrated_returns_none_when_no_context() {
    let owner = Owner::new_root(None);
    owner.with(|| {
        assert!(try_use_hydrated::<ThemeState>().is_none());
    });
}

#[tokio::test]
async fn test_try_use_hydrated_resource_returns_some_when_context_exists() {
    init_test_env();
    let local = tokio::task::LocalSet::new();
    local.run_until(async {
        let owner = Owner::new_root(None);
        owner.with(|| {
            let _ = view! {
                <HydrateContextWith
                    ssr_value=|| ThemeState { theme: "dark".into() }
                    fetcher=|| async { None::<ThemeState> }
                >
                    {move || {
                        assert!(try_use_hydrated_resource::<ThemeState>().is_some());
                        "".into_view()
                    }}
                </HydrateContextWith>
            };
        });
    }).await;
}

#[test]
fn test_try_use_hydrated_resource_returns_none_when_no_context() {
    let owner = Owner::new_root(None);
    owner.with(|| {
        assert!(try_use_hydrated_resource::<ThemeState>().is_none());
    });
}

// ---------------------------------------------------------------------------
// Isomorphic Helpers
// ---------------------------------------------------------------------------

#[cfg(feature = "ssr")]
#[tokio::test]
async fn test_get_cookie_ssr() {
    use http::Request;
    use http::header::COOKIE;
    
    let (parts, _) = Request::builder()
        .header(COOKIE, "test=value; other=foo")
        .body(())
        .unwrap()
        .into_parts();
        
    let owner = Owner::new_root(None);
    owner.with(|| {
        provide_context(parts);
        assert_eq!(get_cookie("test"), Some("value".into()));
        assert_eq!(get_cookie("other"), Some("foo".into()));
        assert_eq!(get_cookie("missing"), None);
    });
}

#[cfg(feature = "ssr")]
#[tokio::test]
async fn test_get_query_param_ssr() {
    use http::Request;
    
    let (parts, _) = Request::builder()
        .uri("http://example.com/?foo=bar&baz=qux")
        .body(())
        .unwrap()
        .into_parts();
        
    let owner = Owner::new_root(None);
    owner.with(|| {
        provide_context(parts);
        assert_eq!(get_query_param("foo"), Some("bar".into()));
        assert_eq!(get_query_param("baz"), Some("qux".into()));
        assert_eq!(get_query_param("missing"), None);
    });
}

#[cfg(feature = "ssr")]
#[tokio::test]
async fn test_get_header_ssr() {
    use http::Request;
    
    let (parts, _) = Request::builder()
        .header("X-Test", "value")
        .body(())
        .unwrap()
        .into_parts();
        
    let owner = Owner::new_root(None);
    owner.with(|| {
        provide_context(parts);
        assert_eq!(get_header("X-Test"), Some("value".into()));
        assert_eq!(get_header("Missing"), None);
    });
}

#[cfg(feature = "ssr")]
#[tokio::test]
async fn test_set_header_ssr() {
    use leptos_axum::ResponseOptions;
    
    let owner = Owner::new_root(None);
    owner.with(|| {
        let res = ResponseOptions::default();
        provide_context(res.clone());
        
        // These should not panic
        set_header("X-Response", "isomorphic");
        set_cookie("session", "abc", "; path=/");
    });
}

#[cfg(feature = "ssr")]
#[tokio::test]
async fn test_get_referer_query_param_ssr() {
    use http::Request;
    use http::header::REFERER;
    
    let (parts, _) = Request::builder()
        .header(REFERER, "http://site.com/page?ref=123")
        .body(())
        .unwrap()
        .into_parts();
        
    let owner = Owner::new_root(None);
    owner.with(|| {
        provide_context(parts);
        assert_eq!(get_referer_query_param("ref"), Some("123".into()));
        assert_eq!(get_referer_query_param("missing"), None);
    });
}

#[cfg(not(feature = "ssr"))]
#[test]
fn test_helpers_return_none_on_client() {
    // On client (without actual browser globals mocked) these should return None
    assert_eq!(get_cookie("any"), None);
    assert_eq!(get_query_param("any"), None);
    assert_eq!(get_header("any"), None);
    assert_eq!(get_referer_query_param("any"), None);
}

// ---------------------------------------------------------------------------
// Internal Mechanisms
// ---------------------------------------------------------------------------

#[cfg(feature = "ssr")]
#[test]
fn test_serialize_for_injection_internal() {
    let state = ThemeState { theme: "dark".into() };
    let json = serialize_for_injection(&state);
    assert_eq!(json, r#"{"theme":"dark"}"#);
    
    let id = type_hydration_id::<ThemeState>();
    assert!(id.contains("ThemeState"));
}

#[tokio::test]
async fn test_hydratable_default_fetch() {
    let result = DefaultState::fetch().await;
    assert!(result.is_none());
}

#[tokio::test]
async fn test_hydratable_custom_fetch() {
    let result = ThemeState::fetch().await;
    assert!(result.is_some());
}

// ---------------------------------------------------------------------------
// Panic tests
// ---------------------------------------------------------------------------

#[test]
#[should_panic(expected = "HydratedSignal<i32> not found")]
fn test_use_hydrated_panics_without_context() {
    let owner = Owner::new_root(None);
    owner.with(|| {
        let _ = use_hydrated::<i32>();
    });
}

#[test]
#[should_panic(expected = "Hydrated LocalResource<i32> not found")]
fn test_use_hydrated_resource_panics_without_context() {
    let owner = Owner::new_root(None);
    owner.with(|| {
        let _ = use_hydrated_resource::<i32>();
    });
}