2021-10-22 05:16:39 +00:00
|
|
|
#![allow(unused, non_upper_case_globals)]
|
2022-02-11 02:00:15 +00:00
|
|
|
#![allow(non_snake_case)]
|
2021-09-13 04:59:08 +00:00
|
|
|
|
2021-10-22 05:16:39 +00:00
|
|
|
//! Tests for the lifecycle of components.
|
2021-09-13 04:59:08 +00:00
|
|
|
use dioxus::prelude::*;
|
2021-11-11 16:49:07 +00:00
|
|
|
use dioxus_core::DomEdit::*;
|
2021-10-01 06:07:12 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2021-09-25 01:46:23 +00:00
|
|
|
|
2021-09-13 04:59:08 +00:00
|
|
|
mod test_logging;
|
|
|
|
|
|
|
|
const IS_LOGGING_ENABLED: bool = true;
|
2021-10-01 06:07:12 +00:00
|
|
|
type Shared<T> = Arc<Mutex<T>>;
|
2021-09-13 04:59:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn manual_diffing() {
|
|
|
|
struct AppProps {
|
|
|
|
value: Shared<&'static str>,
|
|
|
|
}
|
|
|
|
|
2021-12-15 02:46:19 +00:00
|
|
|
static App: Component<AppProps> = |cx| {
|
|
|
|
let val = cx.props.value.lock().unwrap();
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx! { div { "{val}" } })
|
2021-09-13 04:59:08 +00:00
|
|
|
};
|
|
|
|
|
2021-10-01 06:07:12 +00:00
|
|
|
let value = Arc::new(Mutex::new("Hello"));
|
2022-01-30 23:34:24 +00:00
|
|
|
let mut dom = VirtualDom::new_with_props(App, AppProps { value: value.clone() });
|
2021-09-13 04:59:08 +00:00
|
|
|
|
|
|
|
let _ = dom.rebuild();
|
|
|
|
|
2021-10-01 06:07:12 +00:00
|
|
|
*value.lock().unwrap() = "goodbye";
|
2021-09-13 04:59:08 +00:00
|
|
|
|
2021-11-08 01:59:09 +00:00
|
|
|
let edits = dom.rebuild();
|
2021-09-13 04:59:08 +00:00
|
|
|
|
2022-03-27 15:21:57 +00:00
|
|
|
log::trace!("edits: {:?}", edits);
|
2021-09-13 04:59:08 +00:00
|
|
|
}
|
2021-11-11 16:49:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn events_generate() {
|
2022-01-30 23:34:24 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-01-02 07:15:04 +00:00
|
|
|
let count = cx.use_hook(|_| 0);
|
2021-11-11 16:49:07 +00:00
|
|
|
|
|
|
|
let inner = match *count {
|
|
|
|
0 => {
|
|
|
|
rsx! {
|
|
|
|
div {
|
2021-12-21 03:33:13 +00:00
|
|
|
onclick: move |_| *count += 1,
|
2021-11-11 16:49:07 +00:00
|
|
|
div {
|
|
|
|
"nested"
|
|
|
|
}
|
|
|
|
"Click me!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
cx.render(inner)
|
|
|
|
};
|
|
|
|
|
2022-01-30 23:34:24 +00:00
|
|
|
let mut dom = VirtualDom::new(app);
|
2021-11-11 16:49:07 +00:00
|
|
|
let mut channel = dom.get_scheduler_channel();
|
2021-12-13 00:47:13 +00:00
|
|
|
assert!(dom.has_work());
|
2021-11-11 16:49:07 +00:00
|
|
|
|
2021-11-12 02:34:20 +00:00
|
|
|
let edits = dom.rebuild();
|
2021-11-11 16:49:07 +00:00
|
|
|
assert_eq!(
|
2021-11-12 02:34:20 +00:00
|
|
|
edits.edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateElement { tag: "div", root: 1 },
|
|
|
|
NewEventListener { event_name: "click", scope: ScopeId(0), root: 1 },
|
|
|
|
CreateElement { tag: "div", root: 2 },
|
|
|
|
CreateTextNode { text: "nested", root: 3 },
|
2021-11-11 16:49:07 +00:00
|
|
|
AppendChildren { many: 1 },
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateTextNode { text: "Click me!", root: 4 },
|
2021-11-11 16:49:07 +00:00
|
|
|
AppendChildren { many: 2 },
|
2021-11-12 02:34:20 +00:00
|
|
|
AppendChildren { many: 1 },
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn components_generate() {
|
2022-01-30 23:34:24 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-01-02 07:15:04 +00:00
|
|
|
let render_phase = cx.use_hook(|_| 0);
|
2021-12-21 03:33:13 +00:00
|
|
|
*render_phase += 1;
|
2021-11-11 16:49:07 +00:00
|
|
|
|
|
|
|
cx.render(match *render_phase {
|
2021-12-21 05:46:10 +00:00
|
|
|
1 => rsx!("Text0"),
|
|
|
|
2 => rsx!(div {}),
|
|
|
|
3 => rsx!("Text2"),
|
|
|
|
4 => rsx!(Child {}),
|
|
|
|
5 => rsx!({ None as Option<()> }),
|
|
|
|
6 => rsx!("text 3"),
|
|
|
|
7 => rsx!({ (0..2).map(|f| rsx!("text {f}")) }),
|
|
|
|
8 => rsx!(Child {}),
|
2021-11-11 16:49:07 +00:00
|
|
|
_ => todo!(),
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2022-01-30 23:34:24 +00:00
|
|
|
fn Child(cx: Scope) -> Element {
|
2022-03-27 15:21:57 +00:00
|
|
|
log::trace!("Running child");
|
2021-11-11 16:49:07 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
h1 {}
|
|
|
|
})
|
2022-01-30 23:34:24 +00:00
|
|
|
}
|
2021-11-11 16:49:07 +00:00
|
|
|
|
2022-01-30 23:34:24 +00:00
|
|
|
let mut dom = VirtualDom::new(app);
|
2021-11-11 16:49:07 +00:00
|
|
|
let edits = dom.rebuild();
|
|
|
|
assert_eq!(
|
|
|
|
edits.edits,
|
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateTextNode { text: "Text0", root: 1 },
|
2021-11-11 16:49:07 +00:00
|
|
|
AppendChildren { many: 1 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-01-30 23:34:24 +00:00
|
|
|
dom.hard_diff(ScopeId(0)).edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateElement { tag: "div", root: 2 },
|
2021-11-12 02:50:08 +00:00
|
|
|
ReplaceWith { root: 1, m: 1 },
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-01-30 23:34:24 +00:00
|
|
|
dom.hard_diff(ScopeId(0)).edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateTextNode { text: "Text2", root: 1 },
|
2021-11-12 02:50:08 +00:00
|
|
|
ReplaceWith { root: 2, m: 1 },
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2022-01-30 23:34:24 +00:00
|
|
|
// child {}
|
2021-11-11 16:49:07 +00:00
|
|
|
assert_eq!(
|
2022-01-30 23:34:24 +00:00
|
|
|
dom.hard_diff(ScopeId(0)).edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateElement { tag: "h1", root: 2 },
|
|
|
|
ReplaceWith { root: 1, m: 1 },
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2022-01-30 23:34:24 +00:00
|
|
|
// placeholder
|
2021-11-11 16:49:07 +00:00
|
|
|
assert_eq!(
|
2022-01-30 23:34:24 +00:00
|
|
|
dom.hard_diff(ScopeId(0)).edits,
|
|
|
|
[CreatePlaceholder { root: 1 }, ReplaceWith { root: 2, m: 1 },]
|
2021-11-11 16:49:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-01-30 23:34:24 +00:00
|
|
|
dom.hard_diff(ScopeId(0)).edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateTextNode { text: "text 3", root: 2 },
|
|
|
|
ReplaceWith { root: 1, m: 1 },
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-01-30 23:34:24 +00:00
|
|
|
dom.hard_diff(ScopeId(0)).edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateTextNode { text: "text 0", root: 1 },
|
|
|
|
CreateTextNode { text: "text 1", root: 3 },
|
|
|
|
ReplaceWith { root: 2, m: 2 },
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-01-30 23:34:24 +00:00
|
|
|
dom.hard_diff(ScopeId(0)).edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-01-30 23:34:24 +00:00
|
|
|
CreateElement { tag: "h1", root: 2 },
|
|
|
|
ReplaceWith { root: 1, m: 1 },
|
|
|
|
Remove { root: 3 },
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2021-11-11 21:36:51 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn component_swap() {
|
2022-01-30 23:34:24 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-01-02 07:15:04 +00:00
|
|
|
let render_phase = cx.use_hook(|_| 0);
|
2021-12-21 03:33:13 +00:00
|
|
|
*render_phase += 1;
|
2021-11-11 21:36:51 +00:00
|
|
|
|
|
|
|
cx.render(match *render_phase {
|
|
|
|
0 => rsx!(
|
|
|
|
div {
|
|
|
|
NavBar {}
|
|
|
|
Dashboard {}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
1 => rsx!(
|
|
|
|
div {
|
|
|
|
NavBar {}
|
|
|
|
Results {}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
2 => rsx!(
|
|
|
|
div {
|
|
|
|
NavBar {}
|
|
|
|
Dashboard {}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
3 => rsx!(
|
|
|
|
div {
|
|
|
|
NavBar {}
|
|
|
|
Results {}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
4 => rsx!(
|
|
|
|
div {
|
|
|
|
NavBar {}
|
|
|
|
Dashboard {}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
_ => rsx!("blah"),
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-12-29 04:48:25 +00:00
|
|
|
static NavBar: Component = |cx| {
|
2021-11-11 21:36:51 +00:00
|
|
|
println!("running navbar");
|
|
|
|
cx.render(rsx! {
|
|
|
|
h1 {
|
|
|
|
"NavBar"
|
|
|
|
{(0..3).map(|f| rsx!(NavLink {}))}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-12-29 04:48:25 +00:00
|
|
|
static NavLink: Component = |cx| {
|
2021-11-11 21:36:51 +00:00
|
|
|
println!("running navlink");
|
|
|
|
cx.render(rsx! {
|
|
|
|
h1 {
|
|
|
|
"NavLink"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-12-29 04:48:25 +00:00
|
|
|
static Dashboard: Component = |cx| {
|
2021-11-11 21:36:51 +00:00
|
|
|
println!("running dashboard");
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
"dashboard"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-12-29 04:48:25 +00:00
|
|
|
static Results: Component = |cx| {
|
2021-11-11 21:36:51 +00:00
|
|
|
println!("running results");
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
"results"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2022-01-30 23:34:24 +00:00
|
|
|
let mut dom = VirtualDom::new(app);
|
2021-11-11 21:36:51 +00:00
|
|
|
let edits = dom.rebuild();
|
|
|
|
dbg!(&edits);
|
|
|
|
|
|
|
|
let edits = dom.work_with_deadline(|| false);
|
|
|
|
dbg!(&edits);
|
|
|
|
let edits = dom.work_with_deadline(|| false);
|
|
|
|
dbg!(&edits);
|
|
|
|
let edits = dom.work_with_deadline(|| false);
|
|
|
|
dbg!(&edits);
|
|
|
|
let edits = dom.work_with_deadline(|| false);
|
|
|
|
dbg!(&edits);
|
|
|
|
}
|