Skip to content

Commit

Permalink
Cleanup some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Sep 5, 2024
1 parent 06f493c commit 6e92250
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 150 deletions.
10 changes: 6 additions & 4 deletions packages/sycamore-reactive/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::mem;

use crate::*;

/// Function that maps a `Vec` to another `Vec` via a map function. The mapped `Vec` is lazy
/// computed, meaning that it's value will only be updated when requested. Modifications to the
/// Function that maps a `Vec` to another `Vec` via a map function and a key.
///
/// The mapped `Vec` is lazily computed, meaning that it's value will only be updated when requested. Modifications to the
/// input `Vec` are diffed using keys to prevent recomputing values that have not changed.
///
/// This function is the underlying utility behind `Keyed`.
Expand Down Expand Up @@ -173,8 +174,9 @@ where
create_memo(move || scope.run_in(&mut update))
}

/// Function that maps a `Vec` to another `Vec` via a map function. The mapped `Vec` is lazy
/// computed, meaning that it's value will only be updated when requested. Modifications to the
/// Function that maps a `Vec` to another `Vec` via a map function.
///
/// The mapped `Vec` is lazily computed, meaning that it's value will only be updated when requested. Modifications to the
/// input `Vec` are diffed by index to prevent recomputing values that have not changed.
///
/// Generally, it is preferred to use [`map_keyed`] instead when a key function
Expand Down
58 changes: 32 additions & 26 deletions packages/sycamore-web/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,22 @@ where
.collect(),
)
});
(Box::new(move || {
// Get all nodes between start and end and reconcile with new nodes.
let mut new = flattened.get_clone();
let mut old = get_nodes_between(&start_node, &end_node);
// We must include the end node in case `old` is empty (precondition for
// reconcile_fragments).
new.push(end_node.clone());
old.push(end_node.clone());

if let Some(parent) = start_node.parent_node() {
reconcile_fragments(&parent, &mut old, &new);
}
}), (start, view, end).into())
(
Box::new(move || {
// Get all nodes between start and end and reconcile with new nodes.
let mut new = flattened.get_clone();
let mut old = get_nodes_between(&start_node, &end_node);
// We must include the end node in case `old` is empty (precondition for
// reconcile_fragments).
new.push(end_node.clone());
old.push(end_node.clone());

if let Some(parent) = start_node.parent_node() {
reconcile_fragments(&parent, &mut old, &new);
}
}),
(start, view, end).into(),
)
})
}
}
Expand Down Expand Up @@ -197,19 +200,22 @@ where
.collect(),
)
});
(Box::new(move || {
// Get all nodes between start and end and reconcile with new nodes.
let mut new = flattened.get_clone();
let mut old = get_nodes_between(&start_node, &end_node);
// We must include the end node in case `old` is empty (precondition for
// reconcile_fragments).
new.push(end_node.clone());
old.push(end_node.clone());

if let Some(parent) = start_node.parent_node() {
reconcile_fragments(&parent, &mut old, &new);
}
}), (start, view, end).into())
(
Box::new(move || {
// Get all nodes between start and end and reconcile with new nodes.
let mut new = flattened.get_clone();
let mut old = get_nodes_between(&start_node, &end_node);
// We must include the end node in case `old` is empty (precondition for
// reconcile_fragments).
new.push(end_node.clone());
old.push(end_node.clone());

if let Some(parent) = start_node.parent_node() {
reconcile_fragments(&parent, &mut old, &new);
}
}),
(start, view, end).into(),
)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod portal;
mod view;

use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::cell::Cell;
use std::rc::Rc;

pub use elements::*;
Expand Down
6 changes: 3 additions & 3 deletions packages/sycamore-web/src/node/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ fn render_recursive(node: SsrNode, buf: &mut String) {
buf.push_str(&tag);
for (name, value) in &attributes {
buf.push(' ');
buf.push_str(&name);
buf.push_str(name);
buf.push_str("=\"");
html_escape::encode_double_quoted_attribute_to_string(&value, buf);
html_escape::encode_double_quoted_attribute_to_string(value, buf);
buf.push('"');
}
for (name, value) in &bool_attributes {
if *value {
buf.push(' ');
buf.push_str(&name);
buf.push_str(name);
}
}

Expand Down
69 changes: 15 additions & 54 deletions packages/sycamore/tests/web/builder_hydrate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use expect_test::{expect, Expect};
use sycamore::builder::prelude::*;
use sycamore::web::tags::*;

use super::*;

Expand All @@ -9,8 +9,8 @@ fn check(actual: &str, expect: Expect) {

mod hello_world {
use super::*;
fn v<G: Html>() -> View<G> {
p().t("Hello World!").view()
fn v() -> View {
p().children("Hello World!").into()
}
#[test]
fn ssr() {
Expand All @@ -34,8 +34,8 @@ mod hello_world {

mod hydrate_recursive {
use super::*;
fn v<G: Html>() -> View<G> {
div().c(p().t("Nested")).view()
fn v() -> View {
div().children(p().children("Nested")).into()
}
#[test]
fn ssr() {
Expand All @@ -59,8 +59,8 @@ mod hydrate_recursive {

mod multiple_nodes_at_same_depth {
use super::*;
fn v<G: Html>() -> View<G> {
div().c(p().t("First")).c(p().t("Second")).view()
fn v() -> View {
div().children((p().children("First"), p().children("Second"))).into()
}
#[test]
fn ssr() {
Expand All @@ -86,8 +86,8 @@ mod multiple_nodes_at_same_depth {

mod top_level_fragment {
use super::*;
fn v<G: Html>() -> View<G> {
fragment([p().t("First").view(), p().t("Second").view()])
fn v() -> View {
(p().children("First"), p().children("Second")).into()
}
#[test]
fn ssr() {
Expand All @@ -111,8 +111,8 @@ mod top_level_fragment {

mod dynamic {
use super::*;
fn v<G: Html>(state: ReadSignal<i32>) -> View<G> {
p().dyn_t(move || state.get().to_string()).view()
fn v(state: ReadSignal<i32>) -> View {
p().children(move || state.get()).into()
}
#[test]
fn ssr() {
Expand Down Expand Up @@ -146,11 +146,8 @@ mod dynamic {

mod dynamic_with_siblings {
use super::*;
fn v<G: Html>(state: ReadSignal<i32>) -> View<G> {
p().t("Value: ")
.dyn_t(move || state.get().to_string())
.t("!")
.view()
fn v(state: ReadSignal<i32>) -> View {
p().children(("Value: ", move || state.get(), "!")).into()
}
#[test]
fn ssr() {
Expand Down Expand Up @@ -180,46 +177,10 @@ mod dynamic_with_siblings {
}
}

mod dynamic_template {
use super::*;
fn v<G: Html>(state: ReadSignal<View<G>>) -> View<G> {
p().t("before")
.dyn_c(move || state.get_clone())
.t("after")
.view()
}
#[test]
fn ssr() {
check(
&sycamore::render_to_string(|| v(*create_signal(view! { "text" }))),
expect![[r#"<p data-hk="0.0">before<!--#-->text<!--/-->after</p>"#]],
);
}
#[wasm_bindgen_test]
fn test() {
let html_str = sycamore::render_to_string(|| v(*create_signal(view! { "text" })));
let c = test_container();
c.set_inner_html(&html_str);

let _ = create_root(|| {
let state = create_signal(view! { "text" });

sycamore::hydrate_in_scope(|| v(*state), &c);

// Reactivity should work normally.
state.set(view! { span { "nested node" } });
assert_text_content!(query("p"), "beforenested nodeafter");

// P tag should still be the SSR-ed node, not a new node.
assert_eq!(query("p").get_attribute("data-hk").as_deref(), Some("0.0"));
});
}
}

mod top_level_dynamic_with_siblings {
use super::*;
fn v<G: Html>(state: ReadSignal<i32>) -> View<G> {
fragment([t("Value: "), dyn_t(move || state.get().to_string()), t("!")])
fn v(state: ReadSignal<i32>) -> View {
("Value: ", move || state.get(), t("!")).into()
}
#[test]
fn ssr() {
Expand Down
33 changes: 0 additions & 33 deletions packages/sycamore/tests/web/hydrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,39 +174,6 @@ mod dynamic_with_siblings {
}
}

mod dynamic_template {
use super::*;
fn v(state: ReadSignal<View>) -> View<G> {
view! { p { "before" (state.get_clone()) "after" } }
}
#[test]
fn ssr() {
check(
&sycamore::render_to_string(|| v(*create_signal(view! { "text" }))),
expect![[r##"<p data-hk="0.0">before<!--#-->text<!--/-->after</p>"##]],
);
}
#[wasm_bindgen_test]
fn test() {
let html = sycamore::render_to_string(|| v(*create_signal(view! { "text" })));
let c = test_container();
c.set_inner_html(&html);

let _ = create_root(|| {
let state = create_signal(view! { "text" });

sycamore::hydrate_in_scope(|| v(*state), &c);

// Reactivity should work normally.
state.set(view! { span { "nested node" } });
assert_text_content!(query("p"), "beforenested nodeafter");

// P tag should still be the SSR-ed node, not a new node.
assert_eq!(query("p").get_attribute("data-hk").as_deref(), Some("0.0"));
});
}
}

mod top_level_dynamic_with_siblings {
use super::*;
fn v(state: ReadSignal<i32>) -> View {
Expand Down
41 changes: 12 additions & 29 deletions packages/sycamore/tests/web/reconcile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,27 @@
#![allow(clippy::redundant_clone)] // Borrow checking error

use sycamore::utils::render::{append_nodes, insert, reconcile_fragments};
use sycamore_web::tags::*;
use sycamore_web::IntoHtmlNode;
use sycamore_web::ViewNode;
use wasm_bindgen_test::*;

use super::*;

#[wasm_bindgen_test]
fn insert_create_nodes() {
let _ = create_root(|| {
let nodes = [
DomNode::text_node("1".into()),
DomNode::text_node("2".into()),
DomNode::text_node("3".into()),
];
let parent = DomNode::element::<html::div>();

insert(
&parent,
View::new_fragment(nodes.iter().cloned().map(View::new_node).collect()),
None,
None,
true,
);
assert_text_content!(parent.to_web_sys(), "123");
});
macro_rules! nodes {
($($node:expr),*) => {
[$(
HtmlNode::create_text_node(stringify!($node).into()),
)*]
};
}

#[wasm_bindgen_test]
fn reconcile_pop_nodes() {
let nodes = [
DomNode::text_node("1".into()),
DomNode::text_node("2".into()),
DomNode::text_node("3".into()),
];
let parent = DomNode::element::<html::div>();
let child_nodes = nodes.to_vec();
let nodes = nodes![1, 2, 3];
let parent = div().into_html_node();

for node in &child_nodes {
parent.append_child(node);
}
parent.append_view(View::from_nodes(nodes));
assert_text_content!(parent.to_web_sys(), "123");

reconcile_fragments(&parent, &mut child_nodes.clone(), &nodes[..2]);
Expand Down

0 comments on commit 6e92250

Please sign in to comment.