🧑💻 Mejora funcionalidad del componente Html
- Amplía la documentación del componente. - Aplica la nueva funcionalidad en la página de bienvenida usando el nuevo renderizado dinámico con contexto. - Añade pruebas unitarias para el componente.
This commit is contained in:
parent
3a3e3b810f
commit
ef8d16f41f
7 changed files with 155 additions and 27 deletions
74
tests/component_html.rs
Normal file
74
tests/component_html.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
#[pagetop::test]
|
||||
async fn component_html_renders_static_markup() {
|
||||
let component = Html::with(|_| {
|
||||
html! {
|
||||
p { "Test" }
|
||||
}
|
||||
});
|
||||
|
||||
let markup = component
|
||||
.prepare_component(&mut Context::new(None))
|
||||
.render();
|
||||
|
||||
assert_eq!(markup.0, "<p>Test</p>");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn component_html_renders_using_context_param() {
|
||||
let mut cx = Context::new(None).with_param("username", String::from("Alice"));
|
||||
|
||||
let component = Html::with(|cx| {
|
||||
let name = cx.get_param::<String>("username").unwrap_or_default();
|
||||
html! {
|
||||
span { (name) }
|
||||
}
|
||||
});
|
||||
|
||||
let markup = component.prepare_component(&mut cx).render();
|
||||
|
||||
assert_eq!(markup.0, "<span>Alice</span>");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn component_html_allows_replacing_render_function() {
|
||||
let mut component = Html::with(|_| html! { div { "Original" } });
|
||||
|
||||
component.alter_html(|_| html! { div { "Modified" } });
|
||||
|
||||
let markup = component
|
||||
.prepare_component(&mut Context::new(None))
|
||||
.render();
|
||||
|
||||
assert_eq!(markup.0, "<div>Modified</div>");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn component_html_default_renders_empty_markup() {
|
||||
let component = Html::default();
|
||||
|
||||
let markup = component
|
||||
.prepare_component(&mut Context::new(None))
|
||||
.render();
|
||||
|
||||
assert_eq!(markup.0, "");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn component_html_can_access_http_method() {
|
||||
let req = service::test::TestRequest::with_uri("/").to_http_request();
|
||||
let mut cx = Context::new(Some(req));
|
||||
|
||||
let component = Html::with(|cx| {
|
||||
let method = cx
|
||||
.request()
|
||||
.map(|r| r.method().to_string())
|
||||
.unwrap_or_default();
|
||||
html! { span { (method) } }
|
||||
});
|
||||
|
||||
let markup = component.prepare_component(&mut cx).render();
|
||||
|
||||
assert_eq!(markup.0, "<span>GET</span>");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue