use serde::Serialize;
use crate::error::Error;
use crate::exec::exec;
#[derive(Serialize)]
struct NavArgs<'a> {
href: &'a str,
replace: bool,
}
pub fn navigate(href: &str, replace: bool) -> Result<(), Error> {
exec("navigate", &NavArgs { href, replace })
}
pub fn patch(href: &str, replace: bool) -> Result<(), Error> {
exec("patch", &NavArgs { href, replace })
}
#[cfg(test)]
mod tests {
use super::*;
use crate::exec::encode_command;
use serde_json::Value;
#[test]
fn navigate_encodes_href_and_replace() {
let args = NavArgs {
href: "/room/42",
replace: true,
};
let parsed: Value =
serde_json::from_str(&encode_command("navigate", &args).unwrap()).unwrap();
assert_eq!(parsed[0][0], "navigate");
assert_eq!(parsed[0][1]["href"], "/room/42");
assert_eq!(parsed[0][1]["replace"], true);
}
#[test]
fn patch_defaults_to_push_history() {
let args = NavArgs {
href: "/room/42?tab=chat",
replace: false,
};
let parsed: Value =
serde_json::from_str(&encode_command("patch", &args).unwrap()).unwrap();
assert_eq!(parsed[0][0], "patch");
assert_eq!(parsed[0][1]["replace"], false);
}
#[test]
fn non_wasm_stubs_to_ok() {
assert!(navigate("/", false).is_ok());
}
}