2021-12-10 02:19:31 +00:00
|
|
|
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
2021-10-22 05:16:39 +00:00
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
//! Prove that the dom works normally through virtualdom methods.
|
2021-09-13 04:59:08 +00:00
|
|
|
//!
|
2021-09-10 00:58:48 +00: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 06:50:35 +00:00
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
use dioxus::{prelude::*, DomEdit};
|
2021-08-19 06:50:35 +00:00
|
|
|
use dioxus_core as dioxus;
|
2021-09-25 01:46:23 +00:00
|
|
|
use dioxus_core_macro::*;
|
2021-08-19 06:50:35 +00:00
|
|
|
use dioxus_html as dioxus_elements;
|
2021-09-10 00:58:48 +00:00
|
|
|
|
|
|
|
mod test_logging;
|
2021-08-23 16:11:45 +00:00
|
|
|
use DomEdit::*;
|
2021-08-19 06:50:35 +00:00
|
|
|
|
2021-12-10 02:19:31 +00:00
|
|
|
fn new_dom<P: 'static + Send>(app: Component<P>, props: P) -> VirtualDom {
|
2021-09-10 00:58:48 +00:00
|
|
|
const IS_LOGGING_ENABLED: bool = false;
|
|
|
|
test_logging::set_up_logging(IS_LOGGING_ENABLED);
|
|
|
|
VirtualDom::new_with_props(app, props)
|
|
|
|
}
|
2021-08-24 16:43:46 +00:00
|
|
|
|
2021-08-19 06:50:35 +00:00
|
|
|
#[test]
|
|
|
|
fn test_original_diff() {
|
2021-12-25 22:18:05 +00:00
|
|
|
static APP: Component = |cx| {
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx! {
|
2021-08-19 06:50:35 +00:00
|
|
|
div {
|
|
|
|
div {
|
|
|
|
"Hello, world!"
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 06:22:08 +00:00
|
|
|
})
|
2021-08-19 06:50:35 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let mut dom = new_dom(APP, ());
|
2021-08-24 19:12:20 +00:00
|
|
|
let mutations = dom.rebuild();
|
2021-08-23 16:11:45 +00:00
|
|
|
assert_eq!(
|
|
|
|
mutations.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 2,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 3,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "Hello, world!"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
]
|
|
|
|
);
|
2021-08-19 06:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 04:59:08 +00:00
|
|
|
#[test]
|
|
|
|
fn create() {
|
2021-12-25 22:18:05 +00:00
|
|
|
static APP: Component = |cx| {
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx! {
|
2021-08-19 06:50:35 +00:00
|
|
|
div {
|
|
|
|
div {
|
|
|
|
"Hello, world!"
|
2021-08-21 17:24:47 +00:00
|
|
|
div {
|
|
|
|
div {
|
2021-11-08 03:45:41 +00:00
|
|
|
Fragment {
|
|
|
|
"hello"
|
|
|
|
"world"
|
|
|
|
}
|
2021-08-21 17:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-19 06:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 06:22:08 +00:00
|
|
|
})
|
2021-08-19 06:50:35 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let mut dom = new_dom(APP, ());
|
|
|
|
let mutations = dom.rebuild();
|
|
|
|
|
2021-08-23 16:11:45 +00:00
|
|
|
assert_eq!(
|
|
|
|
mutations.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 2,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 3,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "Hello, world!"
|
|
|
|
},
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 4,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 5,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 6,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "hello"
|
|
|
|
},
|
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 7,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "world"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 2 },
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 2 },
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
]
|
|
|
|
);
|
2021-08-19 06:50:35 +00:00
|
|
|
}
|
2021-08-20 15:39:13 +00:00
|
|
|
|
2021-09-13 04:59:08 +00:00
|
|
|
#[test]
|
|
|
|
fn create_list() {
|
2021-12-25 22:18:05 +00:00
|
|
|
static APP: Component = |cx| {
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx! {
|
2021-08-22 03:04:34 +00:00
|
|
|
{(0..3).map(|f| rsx!{ div {
|
2021-08-21 17:24:47 +00:00
|
|
|
"hello"
|
|
|
|
}})}
|
2021-11-01 06:22:08 +00:00
|
|
|
})
|
2021-08-21 17:24:47 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let mut dom = new_dom(APP, ());
|
|
|
|
let mutations = dom.rebuild();
|
2021-08-23 16:11:45 +00:00
|
|
|
|
|
|
|
// copilot wrote this test :P
|
|
|
|
assert_eq!(
|
|
|
|
mutations.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 2,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "hello"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 3,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 4,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "hello"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 5,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 6,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "hello"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 3 },
|
|
|
|
]
|
|
|
|
);
|
2021-08-20 15:39:13 +00:00
|
|
|
}
|
2021-08-21 17:24:47 +00:00
|
|
|
|
2021-09-13 04:59:08 +00:00
|
|
|
#[test]
|
|
|
|
fn create_simple() {
|
2021-12-25 22:18:05 +00:00
|
|
|
static APP: Component = |cx| {
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx! {
|
2021-08-22 03:04:34 +00:00
|
|
|
div {}
|
|
|
|
div {}
|
|
|
|
div {}
|
|
|
|
div {}
|
2021-11-01 06:22:08 +00:00
|
|
|
})
|
2021-08-22 03:04:34 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let mut dom = new_dom(APP, ());
|
|
|
|
let mutations = dom.rebuild();
|
2021-08-23 16:11:45 +00:00
|
|
|
|
|
|
|
// copilot wrote this test :P
|
|
|
|
assert_eq!(
|
|
|
|
mutations.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
|
|
|
root: 1,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 2,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 3,
|
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:34:20 +00:00
|
|
|
CreateElement {
|
|
|
|
root: 4,
|
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
AppendChildren { many: 4 },
|
|
|
|
]
|
|
|
|
);
|
2021-08-22 03:04:34 +00:00
|
|
|
}
|
2021-09-13 04:59:08 +00:00
|
|
|
#[test]
|
|
|
|
fn create_components() {
|
2021-12-25 22:18:05 +00:00
|
|
|
static App: Component = |cx| {
|
2021-11-01 17:32:01 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
Child { "abc1" }
|
|
|
|
Child { "abc2" }
|
|
|
|
Child { "abc3" }
|
|
|
|
})
|
2021-08-22 03:04:34 +00:00
|
|
|
};
|
|
|
|
|
2021-12-15 02:46:19 +00:00
|
|
|
#[derive(Props)]
|
|
|
|
struct ChildProps<'a> {
|
|
|
|
children: Element<'a>,
|
2021-11-01 07:49:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 02:46:19 +00:00
|
|
|
fn Child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx! {
|
2021-08-22 03:04:34 +00:00
|
|
|
h1 {}
|
2021-12-15 03:48:20 +00:00
|
|
|
div { {&cx.props.children} }
|
2021-08-22 03:04:34 +00:00
|
|
|
p {}
|
2021-11-01 06:22:08 +00:00
|
|
|
})
|
2021-11-01 07:49:32 +00:00
|
|
|
}
|
2021-08-22 03:04:34 +00:00
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let mut dom = new_dom(App, ());
|
|
|
|
let mutations = dom.rebuild();
|
2021-08-23 16:11:45 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
mutations.edits,
|
|
|
|
[
|
2021-11-12 02:34:20 +00:00
|
|
|
CreateElement { root: 1, tag: "h1" },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 2,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 3,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "abc1"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-11-12 02:34:20 +00:00
|
|
|
CreateElement { root: 4, tag: "p" },
|
|
|
|
CreateElement { root: 5, tag: "h1" },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 6,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 7,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "abc2"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-11-12 02:34:20 +00:00
|
|
|
CreateElement { root: 8, tag: "p" },
|
|
|
|
CreateElement { root: 9, tag: "h1" },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 10,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 11,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "abc3"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-11-12 02:34:20 +00:00
|
|
|
CreateElement { root: 12, tag: "p" },
|
2021-08-23 16:11:45 +00:00
|
|
|
AppendChildren { many: 9 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2021-09-13 04:59:08 +00:00
|
|
|
#[test]
|
|
|
|
fn anchors() {
|
2021-12-25 22:18:05 +00:00
|
|
|
static App: Component = |cx| {
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx! {
|
2021-08-23 16:11:45 +00:00
|
|
|
{true.then(|| rsx!{ div { "hello" } })}
|
|
|
|
{false.then(|| rsx!{ div { "goodbye" } })}
|
2021-11-01 06:22:08 +00:00
|
|
|
})
|
2021-08-23 16:11:45 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let mut dom = new_dom(App, ());
|
|
|
|
let mutations = dom.rebuild();
|
2021-08-23 16:11:45 +00:00
|
|
|
assert_eq!(
|
|
|
|
mutations.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-23 16:11:45 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:34:20 +00:00
|
|
|
root: 2,
|
2021-08-23 16:11:45 +00:00
|
|
|
text: "hello"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-11-12 02:34:20 +00:00
|
|
|
CreatePlaceholder { root: 3 },
|
2021-08-23 16:11:45 +00:00
|
|
|
AppendChildren { many: 2 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|