2021-10-22 05:16:39 +00:00
|
|
|
#![allow(unused, non_upper_case_globals)]
|
|
|
|
|
2022-01-30 19:08:03 +00:00
|
|
|
use dioxus::{prelude::*, DomEdit, Mutations, SchedulerMsg, ScopeId};
|
2021-09-16 17:20:04 +00:00
|
|
|
use dioxus_core as dioxus;
|
2021-09-25 01:46:23 +00:00
|
|
|
use dioxus_core_macro::*;
|
2021-09-16 17:20:04 +00:00
|
|
|
use dioxus_html as dioxus_elements;
|
2021-09-25 01:46:23 +00:00
|
|
|
|
2021-09-16 17:20:04 +00:00
|
|
|
use DomEdit::*;
|
|
|
|
|
|
|
|
mod test_logging;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shared_state_test() {
|
|
|
|
struct MySharedState(&'static str);
|
|
|
|
|
2021-12-29 04:48:25 +00:00
|
|
|
static App: Component = |cx| {
|
2021-12-29 04:20:01 +00:00
|
|
|
cx.provide_context(MySharedState("world!"));
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx!(Child {}))
|
2021-09-16 17:20:04 +00:00
|
|
|
};
|
|
|
|
|
2021-12-29 04:48:25 +00:00
|
|
|
static Child: Component = |cx| {
|
2021-12-29 04:20:01 +00:00
|
|
|
let shared = cx.consume_context::<MySharedState>()?;
|
2021-11-01 06:22:08 +00:00
|
|
|
cx.render(rsx!("Hello, {shared.0}"))
|
2021-09-16 17:20:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut dom = VirtualDom::new(App);
|
|
|
|
let Mutations { edits, .. } = dom.rebuild();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
edits,
|
|
|
|
[
|
2022-01-31 07:40:12 +00:00
|
|
|
CreateTextNode { root: 1, text: "Hello, world!" },
|
2021-09-16 17:20:04 +00:00
|
|
|
AppendChildren { many: 1 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2022-01-30 19:08:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn swap_test() {
|
|
|
|
struct MySharedState(&'static str);
|
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
|
|
|
let val = cx.use_hook(|_| 0);
|
|
|
|
*val += 1;
|
|
|
|
|
|
|
|
cx.provide_context(MySharedState("world!"));
|
|
|
|
|
|
|
|
let child = match *val % 2 {
|
|
|
|
0 => rsx!(
|
|
|
|
Child1 {
|
|
|
|
Child1 { }
|
|
|
|
Child2 { }
|
|
|
|
}
|
|
|
|
),
|
|
|
|
_ => rsx!(
|
|
|
|
Child2 {
|
|
|
|
Child2 { }
|
|
|
|
Child2 { }
|
|
|
|
}
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
cx.render(rsx!(
|
|
|
|
Router {
|
|
|
|
div { child }
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline_props]
|
|
|
|
fn Router<'a>(cx: Scope, children: Element<'a>) -> Element<'a> {
|
|
|
|
cx.render(rsx!(div { children }))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline_props]
|
|
|
|
fn Child1<'a>(cx: Scope, children: Element<'a>) -> Element {
|
|
|
|
let shared = cx.consume_context::<MySharedState>().unwrap();
|
|
|
|
println!("Child1: {}", shared.0);
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
"{shared.0}",
|
|
|
|
children
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline_props]
|
|
|
|
fn Child2<'a>(cx: Scope, children: Element<'a>) -> Element {
|
|
|
|
let shared = cx.consume_context::<MySharedState>().unwrap();
|
|
|
|
println!("Child2: {}", shared.0);
|
|
|
|
cx.render(rsx! {
|
|
|
|
h1 {
|
|
|
|
"{shared.0}",
|
|
|
|
children
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut dom = VirtualDom::new(app);
|
|
|
|
let Mutations { edits, .. } = dom.rebuild();
|
|
|
|
|
|
|
|
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
dom.work_with_deadline(|| false);
|
|
|
|
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
dom.work_with_deadline(|| false);
|
|
|
|
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
dom.work_with_deadline(|| false);
|
|
|
|
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
dom.work_with_deadline(|| false);
|
|
|
|
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
dom.work_with_deadline(|| false);
|
|
|
|
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
dom.work_with_deadline(|| false);
|
|
|
|
|
|
|
|
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
|
|
|
|
// dom.work_with_deadline(|| false);
|
|
|
|
|
|
|
|
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
|
|
|
|
// dom.work_with_deadline(|| false);
|
|
|
|
|
|
|
|
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
|
|
|
|
// dom.work_with_deadline(|| false);
|
|
|
|
|
|
|
|
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
// dom.work_with_deadline(|| false);
|
|
|
|
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
// dom.work_with_deadline(|| false);
|
|
|
|
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
|
|
|
// dom.work_with_deadline(|| false);
|
|
|
|
}
|