123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- #![allow(unused, non_upper_case_globals)]
- //! Prove that the dom works normally through virtualdom methods.
- //!
- //! This methods all use "rebuild" which completely bypasses the scheduler.
- //! Hard rebuilds don't consume any events from the event queue.
- use dioxus::{prelude::*, DomEdit};
- use dioxus_core as dioxus;
- use dioxus_core_macro::*;
- use dioxus_html as dioxus_elements;
- mod test_logging;
- use DomEdit::*;
- fn new_dom<P: 'static + Send>(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)
- }
- #[test]
- fn test_original_diff() {
- static APP: FC<()> = |(cx, props)| {
- cx.render(rsx! {
- div {
- div {
- "Hello, world!"
- }
- }
- })
- };
- let mut dom = new_dom(APP, ());
- let mutations = dom.rebuild();
- assert_eq!(
- mutations.edits,
- [
- CreateElement {
- root: 0,
- tag: "div"
- },
- CreateElement {
- root: 1,
- tag: "div"
- },
- CreateTextNode {
- root: 2,
- text: "Hello, world!"
- },
- AppendChildren { many: 1 },
- AppendChildren { many: 1 },
- AppendChildren { many: 1 },
- ]
- );
- }
- #[test]
- fn create() {
- static APP: FC<()> = |(cx, props)| {
- cx.render(rsx! {
- div {
- div {
- "Hello, world!"
- div {
- div {
- // Fragment {
- // "hello"
- // "world"
- // }
- }
- }
- }
- }
- })
- };
- let mut dom = new_dom(APP, ());
- let mutations = dom.rebuild();
- assert_eq!(
- mutations.edits,
- [
- CreateElement {
- root: 0,
- tag: "div"
- },
- CreateElement {
- root: 1,
- tag: "div"
- },
- CreateTextNode {
- root: 2,
- text: "Hello, world!"
- },
- CreateElement {
- root: 3,
- tag: "div"
- },
- CreateElement {
- root: 4,
- tag: "div"
- },
- CreateTextNode {
- root: 5,
- text: "hello"
- },
- CreateTextNode {
- root: 6,
- text: "world"
- },
- AppendChildren { many: 2 },
- AppendChildren { many: 1 },
- AppendChildren { many: 2 },
- AppendChildren { many: 1 },
- AppendChildren { many: 1 },
- ]
- );
- }
- #[test]
- fn create_list() {
- static APP: FC<()> = |(cx, props)| {
- cx.render(rsx! {
- {(0..3).map(|f| rsx!{ div {
- "hello"
- }})}
- })
- };
- let mut dom = new_dom(APP, ());
- let mutations = dom.rebuild();
- // copilot wrote this test :P
- assert_eq!(
- mutations.edits,
- [
- CreateElement {
- root: 0,
- tag: "div"
- },
- CreateTextNode {
- root: 1,
- text: "hello"
- },
- AppendChildren { many: 1 },
- CreateElement {
- root: 2,
- tag: "div"
- },
- CreateTextNode {
- root: 3,
- text: "hello"
- },
- AppendChildren { many: 1 },
- CreateElement {
- root: 4,
- tag: "div"
- },
- CreateTextNode {
- root: 5,
- text: "hello"
- },
- AppendChildren { many: 1 },
- AppendChildren { many: 3 },
- ]
- );
- }
- #[test]
- fn create_simple() {
- static APP: FC<()> = |(cx, props)| {
- cx.render(rsx! {
- div {}
- div {}
- div {}
- div {}
- })
- };
- let mut dom = new_dom(APP, ());
- let mutations = dom.rebuild();
- // copilot wrote this test :P
- assert_eq!(
- mutations.edits,
- [
- CreateElement {
- root: 0,
- tag: "div"
- },
- CreateElement {
- root: 1,
- tag: "div"
- },
- CreateElement {
- root: 2,
- tag: "div"
- },
- CreateElement {
- root: 3,
- tag: "div"
- },
- AppendChildren { many: 4 },
- ]
- );
- }
- #[test]
- fn create_components() {
- static App: FC<()> = |(cx, props)| {
- cx.render(rsx! {
- Child { "abc1" }
- Child { "abc2" }
- Child { "abc3" }
- })
- };
- #[derive(Props)]
- struct ChildProps<'a> {
- children: ScopeChildren<'a>,
- }
- fn Child<'a>((cx, props): Scope<'a, ChildProps<'a>>) -> Element {
- cx.render(rsx! {
- h1 {}
- div { {&props.children} }
- p {}
- })
- }
- let mut dom = new_dom(App, ());
- let mutations = dom.rebuild();
- assert_eq!(
- mutations.edits,
- [
- CreateElement { root: 0, tag: "h1" },
- CreateElement {
- root: 1,
- tag: "div"
- },
- CreateTextNode {
- root: 2,
- text: "abc1"
- },
- AppendChildren { many: 1 },
- CreateElement { root: 3, tag: "p" },
- CreateElement { root: 4, tag: "h1" },
- CreateElement {
- root: 5,
- tag: "div"
- },
- CreateTextNode {
- root: 6,
- text: "abc2"
- },
- AppendChildren { many: 1 },
- CreateElement { root: 7, tag: "p" },
- CreateElement { root: 8, tag: "h1" },
- CreateElement {
- root: 9,
- tag: "div"
- },
- CreateTextNode {
- root: 10,
- text: "abc3"
- },
- AppendChildren { many: 1 },
- CreateElement { root: 11, tag: "p" },
- AppendChildren { many: 9 },
- ]
- );
- }
- #[test]
- fn anchors() {
- static App: FC<()> = |(cx, props)| {
- cx.render(rsx! {
- {true.then(|| rsx!{ div { "hello" } })}
- {false.then(|| rsx!{ div { "goodbye" } })}
- })
- };
- let mut dom = new_dom(App, ());
- let mutations = dom.rebuild();
- assert_eq!(
- mutations.edits,
- [
- CreateElement {
- root: 0,
- tag: "div"
- },
- CreateTextNode {
- root: 1,
- text: "hello"
- },
- AppendChildren { many: 1 },
- CreatePlaceholder { root: 2 },
- AppendChildren { many: 2 },
- ]
- );
- }
- #[test]
- fn suspended() {
- static App: FC<()> = |(cx, props)| {
- let val = use_suspense(cx, || async {}, |p| todo!());
- cx.render(rsx! { {val} })
- };
- let mut dom = new_dom(App, ());
- let mutations = dom.rebuild();
- assert_eq!(
- mutations.edits,
- [CreatePlaceholder { root: 0 }, AppendChildren { many: 1 },]
- );
- }
|