123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- //! tests to prove that the iterative implementation works
- use anyhow::{Context, Result};
- use dioxus::{arena::SharedResources, diff::DiffMachine, prelude::*, DomEdit, Mutations};
- mod test_logging;
- use dioxus_core as dioxus;
- use dioxus_html as dioxus_elements;
- use DomEdit::*;
- const LOGGING_ENABLED: bool = false;
- #[test]
- fn test_original_diff() {
- static App: FC<()> = |cx| {
- cx.render(rsx! {
- div {
- div {
- "Hello, world!"
- }
- }
- })
- };
- let mut dom = VirtualDom::new(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!"
- },
- AppendChildren { many: 1 },
- AppendChildren { many: 1 },
- AppendChildren { many: 1 },
- ]
- );
- }
- #[async_std::test]
- async fn create() {
- static App: FC<()> = |cx| {
- cx.render(rsx! {
- div {
- div {
- "Hello, world!"
- div {
- div {
- Fragment {
- "hello"
- "world"
- }
- }
- }
- }
- }
- })
- };
- test_logging::set_up_logging(LOGGING_ENABLED);
- let mut dom = VirtualDom::new(App);
- let mutations = dom.rebuild_async().await;
- 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 },
- ]
- );
- }
- #[async_std::test]
- async fn create_list() {
- static App: FC<()> = |cx| {
- cx.render(rsx! {
- {(0..3).map(|f| rsx!{ div {
- "hello"
- }})}
- })
- };
- test_logging::set_up_logging(LOGGING_ENABLED);
- let mut dom = VirtualDom::new(App);
- let mutations = dom.rebuild_async().await;
- // 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 },
- ]
- );
- }
- #[async_std::test]
- async fn create_simple() {
- static App: FC<()> = |cx| {
- cx.render(rsx! {
- div {}
- div {}
- div {}
- div {}
- })
- };
- test_logging::set_up_logging(LOGGING_ENABLED);
- let mut dom = VirtualDom::new(App);
- let mutations = dom.rebuild_async().await;
- // 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 },
- ]
- );
- }
- #[async_std::test]
- async fn create_components() {
- static App: FC<()> = |cx| {
- cx.render(rsx! {
- Child { "abc1" }
- Child { "abc2" }
- Child { "abc3" }
- })
- };
- static Child: FC<()> = |cx| {
- cx.render(rsx! {
- h1 {}
- div { {cx.children()} }
- p {}
- })
- };
- test_logging::set_up_logging(LOGGING_ENABLED);
- let mut dom = VirtualDom::new(App);
- let mutations = dom.rebuild_async().await;
- 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 },
- ]
- );
- }
- #[async_std::test]
- async fn anchors() {
- static App: FC<()> = |cx| {
- cx.render(rsx! {
- {true.then(|| rsx!{ div { "hello" } })}
- {false.then(|| rsx!{ div { "goodbye" } })}
- })
- };
- test_logging::set_up_logging(LOGGING_ENABLED);
- let mut dom = VirtualDom::new(App);
- let mutations = dom.rebuild_async().await;
- assert_eq!(
- mutations.edits,
- [
- CreateElement { id: 0, tag: "div" },
- CreateTextNode {
- id: 1,
- text: "hello"
- },
- AppendChildren { many: 1 },
- CreatePlaceholder { id: 2 },
- AppendChildren { many: 2 },
- ]
- );
- }
- #[async_std::test]
- async fn suspended() {
- static App: FC<()> = |cx| {
- let val = use_suspense(cx, || async {}, |cx, _| cx.render(rsx! { "hi "}));
- cx.render(rsx! { {val} })
- };
- test_logging::set_up_logging(LOGGING_ENABLED);
- let mut dom = VirtualDom::new(App);
- let mutations = dom.rebuild_async().await;
- assert_eq!(
- mutations.edits,
- [CreatePlaceholder { id: 0 }, AppendChildren { many: 1 },]
- );
- }
|