dioxus/packages/core/tests/vdom_rebuild.rs

106 lines
2.6 KiB
Rust
Raw Normal View History

2021-10-22 05:16:39 +00:00
#![allow(unused, non_upper_case_globals)]
2021-07-30 20:07:42 +00:00
//! Rebuilding tests
//! ----------------
//!
//! This tests module ensures that the initial build of the virtualdom is correct.
//! This does not include dynamic tests or the diffing algorithm itself.
//!
//! It does prove that mounting works properly and the correct edit streams are generated.
//!
//! Don't have a good way to validate, everything is done manually ATM
use dioxus::prelude::*;
2021-11-15 14:49:01 +00:00
use dioxus::DomEdit;
2021-07-30 20:07:42 +00:00
use dioxus_core as dioxus;
2021-09-25 01:46:23 +00:00
use dioxus_core_macro::*;
2021-07-30 20:07:42 +00:00
use dioxus_html as dioxus_elements;
2021-11-15 14:49:01 +00:00
use DomEdit::*;
2021-07-30 20:07:42 +00:00
#[test]
fn app_runs() {
2021-12-10 02:19:31 +00:00
static App: Component<()> = |cx, props| rsx!(cx, div{"hello"} );
2021-11-15 14:49:01 +00:00
2021-07-30 20:07:42 +00:00
let mut vdom = VirtualDom::new(App);
2021-08-24 19:12:20 +00:00
let edits = vdom.rebuild();
2021-07-30 20:07:42 +00:00
}
#[test]
fn fragments_work() {
2021-12-10 02:19:31 +00:00
static App: Component<()> = |cx, props| {
cx.render(rsx!(
2021-07-30 20:07:42 +00:00
div{"hello"}
div{"goodbye"}
))
2021-07-30 20:07:42 +00:00
};
let mut vdom = VirtualDom::new(App);
2021-08-24 19:12:20 +00:00
let edits = vdom.rebuild();
2021-07-30 20:07:42 +00:00
// should result in a final "appendchildren n=2"
dbg!(edits);
}
#[test]
fn lists_work() {
2021-12-10 02:19:31 +00:00
static App: Component<()> = |cx, props| {
cx.render(rsx!(
2021-07-30 20:07:42 +00:00
h1 {"hello"}
{(0..6).map(|f| rsx!(span{ "{f}" }))}
))
2021-07-30 20:07:42 +00:00
};
let mut vdom = VirtualDom::new(App);
2021-08-24 19:12:20 +00:00
let edits = vdom.rebuild();
2021-07-30 20:07:42 +00:00
dbg!(edits);
}
#[test]
fn conditional_rendering() {
2021-12-10 02:19:31 +00:00
static App: Component<()> = |cx, props| {
cx.render(rsx!(
2021-07-30 20:07:42 +00:00
h1 {"hello"}
{true.then(|| rsx!(span{ "a" }))}
{false.then(|| rsx!(span{ "b" }))}
))
2021-07-30 20:07:42 +00:00
};
let mut vdom = VirtualDom::new(App);
2021-08-24 19:12:20 +00:00
let mutations = vdom.rebuild();
2021-11-15 14:49:01 +00:00
assert_eq!(
mutations.edits,
[
CreateElement { root: 1, tag: "h1" },
CreateTextNode {
root: 2,
text: "hello"
},
AppendChildren { many: 1 },
CreateElement {
root: 3,
tag: "span"
},
CreateTextNode { root: 4, text: "a" },
AppendChildren { many: 1 },
CreatePlaceholder { root: 5 },
AppendChildren { many: 3 },
]
)
2021-07-30 20:07:42 +00:00
}
#[test]
fn child_components() {
2021-12-10 02:19:31 +00:00
static App: Component<()> = |cx, props| {
cx.render(rsx!(
2021-07-30 20:07:42 +00:00
{true.then(|| rsx!(Child { }))}
{false.then(|| rsx!(Child { }))}
))
2021-07-30 20:07:42 +00:00
};
2021-12-10 02:19:31 +00:00
static Child: Component<()> = |cx, props| {
cx.render(rsx!(
2021-07-30 20:07:42 +00:00
h1 {"hello"}
h1 {"goodbye"}
))
2021-07-30 20:07:42 +00:00
};
let mut vdom = VirtualDom::new(App);
2021-08-24 19:12:20 +00:00
let edits = vdom.rebuild();
2021-07-30 20:07:42 +00:00
dbg!(edits);
}