-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathhash_history.rs
More file actions
76 lines (61 loc) · 2.69 KB
/
Copy pathhash_history.rs
File metadata and controls
76 lines (61 loc) · 2.69 KB
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
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use gloo_history::{HashHistory, History};
use gloo_utils::window;
wasm_bindgen_test_configure!(run_in_browser);
mod utils;
use utils::delayed_assert_eq;
// All assertions live in a single test because HashHistory is a thread-local
// singleton backed by a shared browser URL, so separate tests would leak
// state into each other depending on execution order.
#[test]
async fn history_works() {
let history = HashHistory::new();
{
let history = history.clone();
delayed_assert_eq(|| history.location().path().to_owned(), || "/").await;
}
delayed_assert_eq(|| window().location().pathname().unwrap(), || "/").await;
delayed_assert_eq(|| window().location().hash().unwrap(), || "#/").await;
history.push("/path-a");
{
let history = history.clone();
delayed_assert_eq(|| history.location().path().to_owned(), || "/path-a").await;
}
delayed_assert_eq(|| window().location().pathname().unwrap(), || "/").await;
delayed_assert_eq(|| window().location().hash().unwrap(), || "#/path-a").await;
history.replace("/path-b");
{
let history = history.clone();
delayed_assert_eq(|| history.location().path().to_owned(), || "/path-b").await;
}
delayed_assert_eq(|| window().location().pathname().unwrap(), || "/").await;
delayed_assert_eq(|| window().location().hash().unwrap(), || "#/path-b").await;
history.back();
{
let history = history.clone();
delayed_assert_eq(|| history.location().path().to_owned(), || "/").await;
}
delayed_assert_eq(|| window().location().pathname().unwrap(), || "/").await;
delayed_assert_eq(|| window().location().hash().unwrap(), || "#/").await;
history.forward();
{
let history = history.clone();
delayed_assert_eq(|| history.location().path().to_owned(), || "/path-b").await;
}
delayed_assert_eq(|| window().location().pathname().unwrap(), || "/").await;
delayed_assert_eq(|| window().location().hash().unwrap(), || "#/path-b").await;
// Malformed hash: simulate user editing the URL bar to a hash without '/'
window().location().set_hash("no-leading-slash").unwrap();
let location = history.location();
assert_eq!(location.path(), "/no-leading-slash");
delayed_assert_eq(
|| window().location().hash().unwrap(),
|| "#/no-leading-slash",
)
.await;
// Empty hash: simulate user clearing the hash entirely
window().location().set_hash("").unwrap();
let location = history.location();
assert_eq!(location.path(), "/");
delayed_assert_eq(|| window().location().hash().unwrap(), || "#/").await;
}