dioxus/packages/core/tests/create_dom.rs

266 lines
6.5 KiB
Rust
Raw Normal View History

2021-09-09 20:58:48 -04:00
//! Prove that the dom works normally through virtualdom methods.
//!
2021-09-09 20:58:48 -04:00
//! This methods all use "rebuild" which completely bypasses the scheduler.
//! Hard rebuilds don't consume any events from the event queue.
2021-08-19 02:50:35 -04:00
2021-09-09 20:58:48 -04:00
use dioxus::{prelude::*, DomEdit};
2021-08-19 02:50:35 -04:00
use dioxus_core as dioxus;
2021-09-24 21:46:23 -04:00
use dioxus_core_macro::*;
2021-08-19 02:50:35 -04:00
use dioxus_html as dioxus_elements;
2021-09-09 20:58:48 -04:00
mod test_logging;
use DomEdit::*;
2021-08-19 02:50:35 -04:00
2021-09-09 20:58:48 -04:00
fn new_dom<P: Properties + 'static>(app: FC<P>, props: P) -> VirtualDom {
const IS_LOGGING_ENABLED: bool = false;
test_logging::set_up_logging(IS_LOGGING_ENABLED);
VirtualDom::new_with_props(app, props)
}
2021-08-19 02:50:35 -04:00
#[test]
fn test_original_diff() {
2021-09-21 13:13:15 -04:00
static APP: FC<()> = |cx, props| {
2021-08-19 02:50:35 -04:00
cx.render(rsx! {
div {
div {
"Hello, world!"
}
}
})
};
2021-09-09 20:58:48 -04:00
let mut dom = new_dom(APP, ());
2021-08-24 15:12:20 -04:00
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[
CreateElement { id: 0, tag: "div" },
CreateElement { id: 1, tag: "div" },
CreateTextNode {
id: 2,
text: "Hello, world!"
},
AppendChildren { many: 1 },
AppendChildren { many: 1 },
AppendChildren { many: 1 },
]
);
2021-08-19 02:50:35 -04:00
}
#[test]
fn create() {
2021-09-21 13:13:15 -04:00
static APP: FC<()> = |cx, props| {
2021-08-19 02:50:35 -04:00
cx.render(rsx! {
div {
div {
"Hello, world!"
2021-08-21 13:24:47 -04:00
div {
div {
Fragment {
"hello"
"world"
}
}
}
2021-08-19 02:50:35 -04:00
}
}
})
};
2021-09-09 20:58:48 -04:00
let mut dom = new_dom(APP, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[
CreateElement { id: 0, tag: "div" },
CreateElement { id: 1, tag: "div" },
CreateTextNode {
id: 2,
text: "Hello, world!"
},
CreateElement { id: 3, tag: "div" },
CreateElement { id: 4, tag: "div" },
CreateTextNode {
id: 5,
text: "hello"
},
CreateTextNode {
id: 6,
text: "world"
},
AppendChildren { many: 2 },
AppendChildren { many: 1 },
AppendChildren { many: 2 },
AppendChildren { many: 1 },
AppendChildren { many: 1 },
]
);
2021-08-19 02:50:35 -04:00
}
2021-08-20 11:39:13 -04:00
#[test]
fn create_list() {
2021-09-21 13:13:15 -04:00
static APP: FC<()> = |cx, props| {
2021-08-21 13:24:47 -04:00
cx.render(rsx! {
2021-08-21 23:04:34 -04:00
{(0..3).map(|f| rsx!{ div {
2021-08-21 13:24:47 -04:00
"hello"
}})}
})
};
2021-09-09 20:58:48 -04:00
let mut dom = new_dom(APP, ());
let mutations = dom.rebuild();
// copilot wrote this test :P
assert_eq!(
mutations.edits,
[
CreateElement { id: 0, tag: "div" },
CreateTextNode {
id: 1,
text: "hello"
},
AppendChildren { many: 1 },
CreateElement { id: 2, tag: "div" },
CreateTextNode {
id: 3,
text: "hello"
},
AppendChildren { many: 1 },
CreateElement { id: 4, tag: "div" },
CreateTextNode {
id: 5,
text: "hello"
},
AppendChildren { many: 1 },
AppendChildren { many: 3 },
]
);
2021-08-20 11:39:13 -04:00
}
2021-08-21 13:24:47 -04:00
#[test]
fn create_simple() {
2021-09-21 13:13:15 -04:00
static APP: FC<()> = |cx, props| {
2021-08-21 23:04:34 -04:00
cx.render(rsx! {
div {}
div {}
div {}
div {}
})
};
2021-09-09 20:58:48 -04:00
let mut dom = new_dom(APP, ());
let mutations = dom.rebuild();
// copilot wrote this test :P
assert_eq!(
mutations.edits,
[
CreateElement { id: 0, tag: "div" },
CreateElement { id: 1, tag: "div" },
CreateElement { id: 2, tag: "div" },
CreateElement { id: 3, tag: "div" },
AppendChildren { many: 4 },
]
);
2021-08-21 23:04:34 -04:00
}
#[test]
fn create_components() {
2021-09-21 13:13:15 -04:00
static App: FC<()> = |cx, props| {
2021-08-21 23:04:34 -04:00
cx.render(rsx! {
Child { "abc1" }
Child { "abc2" }
Child { "abc3" }
})
};
2021-09-21 13:13:15 -04:00
static Child: FC<()> = |cx, props| {
2021-08-21 23:04:34 -04:00
cx.render(rsx! {
h1 {}
2021-08-22 17:08:25 -04:00
div { {cx.children()} }
2021-08-21 23:04:34 -04:00
p {}
})
};
2021-09-09 20:58:48 -04:00
let mut dom = new_dom(App, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[
CreateElement { id: 0, tag: "h1" },
CreateElement { id: 1, tag: "div" },
CreateTextNode {
id: 2,
text: "abc1"
},
AppendChildren { many: 1 },
CreateElement { id: 3, tag: "p" },
CreateElement { id: 4, tag: "h1" },
CreateElement { id: 5, tag: "div" },
CreateTextNode {
id: 6,
text: "abc2"
},
AppendChildren { many: 1 },
CreateElement { id: 7, tag: "p" },
CreateElement { id: 8, tag: "h1" },
CreateElement { id: 9, tag: "div" },
CreateTextNode {
id: 10,
text: "abc3"
},
AppendChildren { many: 1 },
CreateElement { id: 11, tag: "p" },
AppendChildren { many: 9 },
]
);
}
#[test]
fn anchors() {
2021-09-21 13:13:15 -04:00
static App: FC<()> = |cx, props| {
cx.render(rsx! {
{true.then(|| rsx!{ div { "hello" } })}
{false.then(|| rsx!{ div { "goodbye" } })}
})
};
2021-09-09 20:58:48 -04:00
let mut dom = new_dom(App, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[
CreateElement { id: 0, tag: "div" },
CreateTextNode {
id: 1,
text: "hello"
},
AppendChildren { many: 1 },
CreatePlaceholder { id: 2 },
AppendChildren { many: 2 },
]
);
}
#[test]
fn suspended() {
2021-09-21 13:13:15 -04:00
static App: FC<()> = |cx, props| {
2021-09-09 20:58:48 -04:00
let val = use_suspense(
cx,
|| async {
//
},
|cx, _| cx.render(rsx! { "hi "}),
);
cx.render(rsx! { {val} })
};
2021-09-09 20:58:48 -04:00
let mut dom = new_dom(App, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[CreatePlaceholder { id: 0 }, AppendChildren { many: 1 },]
);
2021-08-21 23:04:34 -04:00
}