use pretty_assertions::assert_eq;
use tide::StatusCode;
use tide_jsx::{html, rsx, view, component, Render, raw};
use tide_jsx::html::HTML5Doctype;
use std::borrow::Cow;
#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/fail/*.rs");
}
#[test]
fn works_with_dashes() {
use pretty_assertions::assert_eq;
let value = html! {
};
assert_eq!(value, r#""#);
}
#[test]
fn works_with_raw() {
let actual = html! {
{raw!("")}
};
assert_eq!(actual, "
");
}
#[test]
fn works_with_raw_ident() {
let actual = html! {
};
assert_eq!(actual, r#""#);
}
#[test]
fn works_with_keywords() {
assert_eq!(html! { }, r#""#);
assert_eq!(html! { }, r#""#);
}
#[test]
fn element_ordering() {
let actual = html! {
"#
);
}
#[test]
fn cow_str() {
let owned1 = "Borrowed from owned".to_owned();
let owned2 = "Owned".to_owned();
assert_eq!(
html! {
{Cow::Borrowed("Static")}
{Cow::<'_, str>::Borrowed(&owned1)}
{Cow::<'_, str>::Owned(owned2)}
},
r#"
Static
Borrowed from owned
Owned
"#,
);
}
#[test]
fn number() {
let num = 42;
assert_eq!(html! {
{num}
}, "
42
")
}
#[test]
fn vec() {
let list = vec!["Mouse", "Rat", "Hamster"];
assert_eq!(
html! {
{
list
.into_iter()
.map(|text| rsx! {
{text}
})
.collect::>()
}
},
"
Mouse
Rat
Hamster
"
)
}
#[async_std::test]
async fn render_view() -> std::io::Result<()> {
let result = view! {
{"hello"}
} as tide::Result;
let mut res = result.unwrap();
assert_eq!(res.status(), StatusCode::Ok);
assert_eq!(res.header("content-type").unwrap().as_str(), tide::http::mime::HTML.to_string());
assert_eq!(res.take_body().into_string().await.unwrap(), "
hello
");
Ok(())
}
mod kaki {
use crate::{html, rsx, component, HTML5Doctype, Render};
use crate::other::ExternalPage;
// This can be any layout we want
#[component]
fn Page<'a, Children: Render>(title: &'a str, children: Children) {
rsx! {
<>
{title}
{children}
>
}
}
#[test]
fn test() {
let actual = html! {
{format!("Welcome, {}", "Gal")}
};
let expected = concat!(
"",
"",
"Home",
"",
"Welcome, Gal",
"",
""
);
assert_eq!(actual, expected);
}
#[test]
fn externals_test() {
let actual = html! {
{format!("Welcome, {}", "Gal")}
};
let expected = concat!(
"",
"",
"Home",
"",
"
Foo
",
"Welcome, Gal",
"",
""
);
assert_eq!(actual, expected);
}
}
/// ## Other
///
/// Module for testing component visibility when imported from other modules.
mod other {
use crate::{component, rsx, HTML5Doctype, Render};
#[component]
pub fn ExternalPage<'title, 'subtitle, Children: Render>(
title: &'title str,
subtitle: &'subtitle str,
children: Children,
) {
rsx! {
<>
{title}