🎨 Convierte el ciclo de renderizado en async
`prepare()` de `Component`, `render()` de `Region` y `Template`, y la implementación de `ComponentRender` pasan a ser funciones async. Se actualizan todos los componentes base, helpers, tests y ejemplos.
This commit is contained in:
parent
9acd8cc51a
commit
9354894b3a
48 changed files with 436 additions and 365 deletions
|
|
@ -11,6 +11,7 @@ struct TestComp {
|
|||
text: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Component for TestComp {
|
||||
fn new() -> Self {
|
||||
Self::default()
|
||||
|
|
@ -20,7 +21,7 @@ impl Component for TestComp {
|
|||
self.props.get_id()
|
||||
}
|
||||
|
||||
fn prepare(&self, _cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
async fn prepare(&self, _cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
Ok(html! { (self.text) })
|
||||
}
|
||||
}
|
||||
|
|
@ -48,27 +49,30 @@ impl TestComp {
|
|||
async fn child_default_is_empty() {
|
||||
let child = Child::default();
|
||||
assert!(child.id().is_none());
|
||||
assert!(child.render(&mut Context::default()).is_empty());
|
||||
assert!(child.render(&mut Context::default()).await.is_empty());
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn child_with_stores_component_and_renders_it() {
|
||||
let child = Child::with(TestComp::text("hola"));
|
||||
assert_eq!(child.render(&mut Context::default()).into_string(), "hola");
|
||||
let child = Child::with(TestComp::text("hello"));
|
||||
assert_eq!(
|
||||
child.render(&mut Context::default()).await.into_string(),
|
||||
"hello"
|
||||
);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn child_id_returns_component_id() {
|
||||
let child = Child::with(TestComp::tagged("my-id", "texto"));
|
||||
let child = Child::with(TestComp::tagged("my-id", "text"));
|
||||
assert_eq!(child.id(), Some("my-id".to_string()));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn child_from_component_is_equivalent_to_with() {
|
||||
let child: Child = TestComp::text("desde from").into();
|
||||
let child: Child = TestComp::text("from-trait").into();
|
||||
assert_eq!(
|
||||
child.render(&mut Context::default()).into_string(),
|
||||
"desde from"
|
||||
child.render(&mut Context::default()).await.into_string(),
|
||||
"from-trait"
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -78,11 +82,11 @@ async fn child_clone_is_deep() {
|
|||
let original = Child::with(TestComp::text("original"));
|
||||
let clone = original.clone();
|
||||
assert_eq!(
|
||||
original.render(&mut Context::default()).into_string(),
|
||||
original.render(&mut Context::default()).await.into_string(),
|
||||
"original"
|
||||
);
|
||||
assert_eq!(
|
||||
clone.render(&mut Context::default()).into_string(),
|
||||
clone.render(&mut Context::default()).await.into_string(),
|
||||
"original"
|
||||
);
|
||||
}
|
||||
|
|
@ -103,7 +107,7 @@ async fn children_add_appends_in_order() {
|
|||
.with_child(TestComp::text("b"))
|
||||
.with_child(TestComp::text("c"));
|
||||
assert_eq!(c.len(), 3);
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "abc");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "abc");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -111,13 +115,13 @@ async fn children_add_if_empty_only_adds_when_list_is_empty() {
|
|||
let mut cx = Context::default();
|
||||
|
||||
// Se añade porque la lista está vacía.
|
||||
let c = Children::new().with_child(ChildOp::AddIfEmpty(TestComp::text("primero").into()));
|
||||
let c = Children::new().with_child(ChildOp::AddIfEmpty(TestComp::text("first").into()));
|
||||
assert_eq!(c.len(), 1);
|
||||
|
||||
// No se añade porque ya hay un elemento.
|
||||
let c = c.with_child(ChildOp::AddIfEmpty(TestComp::text("segundo").into()));
|
||||
let c = c.with_child(ChildOp::AddIfEmpty(TestComp::text("second").into()));
|
||||
assert_eq!(c.len(), 1);
|
||||
assert_eq!(c.render(&mut cx).into_string(), "primero");
|
||||
assert_eq!(c.render(&mut cx).await.into_string(), "first");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -128,7 +132,7 @@ async fn children_add_many_appends_all_in_order() {
|
|||
TestComp::text("z").into(),
|
||||
]));
|
||||
assert_eq!(c.len(), 3);
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "xyz");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "xyz");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -136,7 +140,7 @@ async fn children_prepend_inserts_at_start() {
|
|||
let c = Children::new()
|
||||
.with_child(TestComp::text("b"))
|
||||
.with_child(ChildOp::Prepend(TestComp::text("a").into()));
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "ab");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "ab");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -147,7 +151,7 @@ async fn children_prepend_many_inserts_all_at_start_maintaining_order() {
|
|||
TestComp::text("a").into(),
|
||||
TestComp::text("b").into(),
|
||||
]));
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "abc");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "abc");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -156,7 +160,7 @@ async fn children_insert_after_id_inserts_after_matching_element() {
|
|||
.with_child(TestComp::tagged("first", "a"))
|
||||
.with_child(TestComp::text("c"))
|
||||
.with_child(ChildOp::InsertAfterId("first", TestComp::text("b").into()));
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "abc");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "abc");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -164,10 +168,10 @@ async fn children_insert_after_id_appends_when_id_not_found() {
|
|||
let c = Children::new()
|
||||
.with_child(TestComp::text("a"))
|
||||
.with_child(ChildOp::InsertAfterId(
|
||||
"no-existe",
|
||||
"not-found",
|
||||
TestComp::text("b").into(),
|
||||
));
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "ab");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "ab");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -176,7 +180,7 @@ async fn children_insert_before_id_inserts_before_matching_element() {
|
|||
.with_child(TestComp::text("a"))
|
||||
.with_child(TestComp::tagged("last", "c"))
|
||||
.with_child(ChildOp::InsertBeforeId("last", TestComp::text("b").into()));
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "abc");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "abc");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -184,10 +188,10 @@ async fn children_insert_before_id_prepends_when_id_not_found() {
|
|||
let c = Children::new()
|
||||
.with_child(TestComp::text("b"))
|
||||
.with_child(ChildOp::InsertBeforeId(
|
||||
"no-existe",
|
||||
"not-found",
|
||||
TestComp::text("a").into(),
|
||||
));
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "ab");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "ab");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -198,28 +202,28 @@ async fn children_remove_by_id_removes_first_matching_element() {
|
|||
.with_child(TestComp::text("c"))
|
||||
.with_child(ChildOp::RemoveById("drop"));
|
||||
assert_eq!(c.len(), 2);
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "ac");
|
||||
assert_eq!(c.render(&mut Context::default()).await.into_string(), "ac");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn children_remove_by_id_does_nothing_when_id_not_found() {
|
||||
let c = Children::new()
|
||||
.with_child(TestComp::text("a"))
|
||||
.with_child(ChildOp::RemoveById("no-existe"));
|
||||
.with_child(ChildOp::RemoveById("not-found"));
|
||||
assert_eq!(c.len(), 1);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn children_replace_by_id_replaces_first_matching_element() {
|
||||
let c = Children::new()
|
||||
.with_child(TestComp::tagged("target", "viejo"))
|
||||
.with_child(TestComp::tagged("target", "old"))
|
||||
.with_child(TestComp::text("b"))
|
||||
.with_child(ChildOp::ReplaceById(
|
||||
"target",
|
||||
TestComp::text("nuevo").into(),
|
||||
));
|
||||
.with_child(ChildOp::ReplaceById("target", TestComp::text("new").into()));
|
||||
assert_eq!(c.len(), 2);
|
||||
assert_eq!(c.render(&mut Context::default()).into_string(), "nuevob");
|
||||
assert_eq!(
|
||||
c.render(&mut Context::default()).await.into_string(),
|
||||
"newb"
|
||||
);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -234,11 +238,11 @@ async fn children_reset_clears_all_elements() {
|
|||
#[pagetop::test]
|
||||
async fn children_get_by_id_returns_first_matching_child() {
|
||||
let c = Children::new()
|
||||
.with_child(TestComp::tagged("uno", "a"))
|
||||
.with_child(TestComp::tagged("dos", "b"));
|
||||
assert!(c.get_by_id("uno").is_some());
|
||||
assert!(c.get_by_id("dos").is_some());
|
||||
assert!(c.get_by_id("tres").is_none());
|
||||
.with_child(TestComp::tagged("one", "a"))
|
||||
.with_child(TestComp::tagged("two", "b"));
|
||||
assert!(c.get_by_id("one").is_some());
|
||||
assert!(c.get_by_id("two").is_some());
|
||||
assert!(c.get_by_id("three").is_none());
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
|
|
@ -246,21 +250,21 @@ async fn children_iter_by_id_yields_all_matching_children() {
|
|||
let c = Children::new()
|
||||
.with_child(TestComp::tagged("rep", "a"))
|
||||
.with_child(TestComp::tagged("rep", "b"))
|
||||
.with_child(TestComp::tagged("otro", "c"));
|
||||
.with_child(TestComp::tagged("other", "c"));
|
||||
assert_eq!(c.iter_by_id("rep").count(), 2);
|
||||
assert_eq!(c.iter_by_id("otro").count(), 1);
|
||||
assert_eq!(c.iter_by_id("ninguno").count(), 0);
|
||||
assert_eq!(c.iter_by_id("other").count(), 1);
|
||||
assert_eq!(c.iter_by_id("none").count(), 0);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn children_render_concatenates_all_outputs_in_order() {
|
||||
let c = Children::new()
|
||||
.with_child(TestComp::text("uno "))
|
||||
.with_child(TestComp::text("dos "))
|
||||
.with_child(TestComp::text("tres"));
|
||||
.with_child(TestComp::text("one "))
|
||||
.with_child(TestComp::text("two "))
|
||||
.with_child(TestComp::text("three"));
|
||||
assert_eq!(
|
||||
c.render(&mut Context::default()).into_string(),
|
||||
"uno dos tres"
|
||||
c.render(&mut Context::default()).await.into_string(),
|
||||
"one two three"
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -270,29 +274,29 @@ async fn children_render_concatenates_all_outputs_in_order() {
|
|||
async fn embed_default_is_empty() {
|
||||
let embed: Embed<TestComp> = Embed::default();
|
||||
assert!(embed.id().is_none());
|
||||
assert!(embed.render(&mut Context::default()).is_empty());
|
||||
assert!(embed.render(&mut Context::default()).await.is_empty());
|
||||
assert!(embed.get().is_none());
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn embed_with_stores_component() {
|
||||
let embed = Embed::with(TestComp::text("contenido"));
|
||||
let embed = Embed::with(TestComp::text("content"));
|
||||
assert!(embed.get().is_some());
|
||||
assert_eq!(
|
||||
embed.render(&mut Context::default()).into_string(),
|
||||
"contenido"
|
||||
embed.render(&mut Context::default()).await.into_string(),
|
||||
"content"
|
||||
);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn embed_id_returns_component_id() {
|
||||
let embed = Embed::with(TestComp::tagged("embed-id", "texto"));
|
||||
let embed = Embed::with(TestComp::tagged("embed-id", "text"));
|
||||
assert_eq!(embed.id(), Some("embed-id".to_string()));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn embed_get_is_some_when_component_present() {
|
||||
let embed = Embed::with(TestComp::tagged("abc", "hola"));
|
||||
let embed = Embed::with(TestComp::tagged("abc", "hello"));
|
||||
// `get()` devuelve Some; la lectura del id verifica que accede al componente correctamente.
|
||||
assert!(embed.get().is_some());
|
||||
assert_eq!(embed.id(), Some("abc".to_string()));
|
||||
|
|
@ -300,38 +304,36 @@ async fn embed_get_is_some_when_component_present() {
|
|||
|
||||
#[pagetop::test]
|
||||
async fn embed_get_allows_mutating_component() {
|
||||
let embed = Embed::with(TestComp::tagged("orig", "texto"));
|
||||
// El `;` final convierte el `if let` en sentencia y libera el guard antes que `embed`.
|
||||
if let Some(mut comp) = embed.get() {
|
||||
let mut embed = Embed::with(TestComp::tagged("orig", "text"));
|
||||
if let Some(comp) = embed.get_mut() {
|
||||
comp.props
|
||||
.alter_prop(PropsOp::set_id("modificado".to_string()));
|
||||
.alter_prop(PropsOp::set_id("modified".to_string()));
|
||||
};
|
||||
assert_eq!(embed.id(), Some("modificado".to_string()));
|
||||
assert_eq!(embed.id(), Some("modified".to_string()));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn embed_with_component_replaces_content() {
|
||||
let embed =
|
||||
Embed::with(TestComp::text("primero")).with_component(Some(TestComp::text("segundo")));
|
||||
let embed = Embed::with(TestComp::text("first")).with_component(Some(TestComp::text("second")));
|
||||
assert_eq!(
|
||||
embed.render(&mut Context::default()).into_string(),
|
||||
"segundo"
|
||||
embed.render(&mut Context::default()).await.into_string(),
|
||||
"second"
|
||||
);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn embed_with_component_none_empties_embed() {
|
||||
let embed = Embed::with(TestComp::text("algo")).with_component(None);
|
||||
let embed = Embed::with(TestComp::text("something")).with_component(None);
|
||||
assert!(embed.get().is_none());
|
||||
assert!(embed.render(&mut Context::default()).is_empty());
|
||||
assert!(embed.render(&mut Context::default()).await.is_empty());
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn embed_clone_is_deep() {
|
||||
let original = Embed::with(TestComp::tagged("orig", "texto"));
|
||||
let clone = original.clone();
|
||||
let original = Embed::with(TestComp::tagged("orig", "text"));
|
||||
let mut clone = original.clone();
|
||||
// Mutar el clon no debe afectar al original.
|
||||
if let Some(mut comp) = clone.get() {
|
||||
if let Some(comp) = clone.get_mut() {
|
||||
comp.props
|
||||
.alter_prop(PropsOp::set_id("clone-id".to_string()));
|
||||
}
|
||||
|
|
@ -341,10 +343,10 @@ async fn embed_clone_is_deep() {
|
|||
|
||||
#[pagetop::test]
|
||||
async fn embed_converts_into_child() {
|
||||
let embed = Embed::with(TestComp::text("desde embed"));
|
||||
let embed = Embed::with(TestComp::text("from embed"));
|
||||
let child = Child::from(embed);
|
||||
assert_eq!(
|
||||
child.render(&mut Context::default()).into_string(),
|
||||
"desde embed"
|
||||
child.render(&mut Context::default()).await.into_string(),
|
||||
"from embed"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue