🎨 Protege el uso de render en PrepareMarkup

This commit is contained in:
Manuel Cillero 2025-11-17 22:47:47 +01:00
parent 6091f451ac
commit 682ed7cc45
4 changed files with 112 additions and 90 deletions

View file

@ -2,32 +2,28 @@ use pagetop::prelude::*;
#[pagetop::test]
async fn component_html_renders_static_markup() {
let component = Html::with(|_| {
let mut component = Html::with(|_| {
html! {
p { "Test" }
}
});
let markup = component
.prepare_component(&mut Context::new(None))
.render();
let markup = component.render(&mut Context::default());
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", "Alice".to_string());
let mut cx = Context::default().with_param("username", "Alice".to_string());
let component = Html::with(|cx| {
let mut component = Html::with(|cx| {
let name = cx.param::<String>("username").cloned().unwrap_or_default();
html! {
span { (name) }
}
});
let markup = component.prepare_component(&mut cx).render();
let markup = component.render(&mut cx);
assert_eq!(markup.0, "<span>Alice</span>");
}
@ -37,21 +33,15 @@ async fn component_html_allows_replacing_render_function() {
component.alter_fn(|_| html! { div { "Modified" } });
let markup = component
.prepare_component(&mut Context::new(None))
.render();
let markup = component.render(&mut Context::default());
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();
let mut component = Html::default();
let markup = component.render(&mut Context::default());
assert_eq!(markup.0, "");
}
@ -60,7 +50,7 @@ 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 mut component = Html::with(|cx| {
let method = cx
.request()
.map(|r| r.method().to_string())
@ -68,7 +58,6 @@ async fn component_html_can_access_http_method() {
html! { span { (method) } }
});
let markup = component.prepare_component(&mut cx).render();
let markup = component.render(&mut cx);
assert_eq!(markup.0, "<span>GET</span>");
}