chore: get event test working
This commit is contained in:
1 parent
565df11f7b
commit
0027cdd938
5 files changed
+263
-214
No files matched your search
+207
-202
@@ -4,6 +4,7 @@
|
||||
//! Tests for the lifecycle of components.
|
||||
use dioxus::core::{ElementId, Mutation::*};
|
||||
use dioxus::prelude::*;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
type Shared<T> = Arc<Mutex<T>>;
|
||||
@@ -14,21 +15,26 @@ fn manual_diffing() {
|
||||
value: Shared<&'static str>,
|
||||
}
|
||||
|
||||
static App: Component<AppProps> = |cx| {
|
||||
fn app(cx: Scope<AppProps>) -> Element {
|
||||
let val = cx.props.value.lock().unwrap();
|
||||
cx.render(rsx_without_templates! { div { "{val}" } })
|
||||
cx.render(rsx! { div { "{val}" } })
|
||||
};
|
||||
|
||||
let value = Arc::new(Mutex::new("Hello"));
|
||||
let mut dom = VirtualDom::new_with_props(App, AppProps { value: value.clone() });
|
||||
let mut dom = VirtualDom::new_with_props(app, AppProps { value: value.clone() });
|
||||
|
||||
let _ = dom.rebuild();
|
||||
|
||||
*value.lock().unwrap() = "goodbye";
|
||||
|
||||
let edits = dom.rebuild();
|
||||
|
||||
println!("edits: {:?}", edits);
|
||||
assert_eq!(
|
||||
dom.rebuild().santize().edits,
|
||||
[
|
||||
LoadTemplate { name: "template", index: 0, id: ElementId(3) },
|
||||
HydrateText { path: &[0], value: "goodbye", id: ElementId(4) },
|
||||
AppendChildren { m: 1 }
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -36,229 +42,228 @@ fn events_generate() {
|
||||
fn app(cx: Scope) -> Element {
|
||||
let count = cx.use_hook(|| 0);
|
||||
|
||||
let inner = match *count {
|
||||
0 => {
|
||||
rsx_without_templates! {
|
||||
div {
|
||||
onclick: move |_| *count += 1,
|
||||
div {
|
||||
"nested"
|
||||
}
|
||||
"Click me!"
|
||||
}
|
||||
match *count {
|
||||
0 => cx.render(rsx! {
|
||||
div {
|
||||
onclick: move |_| {
|
||||
*count += 1
|
||||
},
|
||||
div { "nested" }
|
||||
"Click me!"
|
||||
}
|
||||
}
|
||||
_ => todo!(),
|
||||
};
|
||||
|
||||
cx.render(inner)
|
||||
}),
|
||||
_ => cx.render(rsx!(())),
|
||||
}
|
||||
};
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let mut channel = dom.get_scheduler_channel();
|
||||
assert!(dom.has_work());
|
||||
_ = dom.rebuild();
|
||||
|
||||
dom.handle_event(
|
||||
"click",
|
||||
Rc::new(MouseData::default()),
|
||||
ElementId(1),
|
||||
true,
|
||||
EventPriority::Immediate,
|
||||
);
|
||||
|
||||
dom.mark_dirty_scope(ScopeId(0));
|
||||
let edits = dom.render_immediate();
|
||||
|
||||
let edits = dom.rebuild();
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
[
|
||||
CreateElement { root: Some(1), tag: "div", children: 0 },
|
||||
NewEventListener { event_name: "click", scope: ScopeId(0), root: Some(1) },
|
||||
CreateElement { root: Some(2), tag: "div", children: 0 },
|
||||
CreateTextNode { root: Some(3), text: "nested" },
|
||||
AppendChildren { root: Some(2), children: vec![3] },
|
||||
CreateTextNode { root: Some(4), text: "Click me!" },
|
||||
AppendChildren { root: Some(1), children: vec![2, 4] },
|
||||
AppendChildren { root: Some(0), children: vec![1] }
|
||||
CreatePlaceholder { id: ElementId(2) },
|
||||
ReplaceWith { id: ElementId(1), m: 1 }
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn components_generate() {
|
||||
fn app(cx: Scope) -> Element {
|
||||
let render_phase = cx.use_hook(|| 0);
|
||||
*render_phase += 1;
|
||||
// #[test]
|
||||
// fn components_generate() {
|
||||
// fn app(cx: Scope) -> Element {
|
||||
// let render_phase = cx.use_hook(|| 0);
|
||||
// *render_phase += 1;
|
||||
|
||||
cx.render(match *render_phase {
|
||||
1 => rsx_without_templates!("Text0"),
|
||||
2 => rsx_without_templates!(div {}),
|
||||
3 => rsx_without_templates!("Text2"),
|
||||
4 => rsx_without_templates!(Child {}),
|
||||
5 => rsx_without_templates!({ None as Option<()> }),
|
||||
6 => rsx_without_templates!("text 3"),
|
||||
7 => rsx_without_templates!({ (0..2).map(|f| rsx_without_templates!("text {f}")) }),
|
||||
8 => rsx_without_templates!(Child {}),
|
||||
_ => todo!(),
|
||||
})
|
||||
};
|
||||
// cx.render(match *render_phase {
|
||||
// 1 => rsx_without_templates!("Text0"),
|
||||
// 2 => rsx_without_templates!(div {}),
|
||||
// 3 => rsx_without_templates!("Text2"),
|
||||
// 4 => rsx_without_templates!(Child {}),
|
||||
// 5 => rsx_without_templates!({ None as Option<()> }),
|
||||
// 6 => rsx_without_templates!("text 3"),
|
||||
// 7 => rsx_without_templates!({ (0..2).map(|f| rsx_without_templates!("text {f}")) }),
|
||||
// 8 => rsx_without_templates!(Child {}),
|
||||
// _ => todo!(),
|
||||
// })
|
||||
// };
|
||||
|
||||
fn Child(cx: Scope) -> Element {
|
||||
println!("Running child");
|
||||
cx.render(rsx_without_templates! {
|
||||
h1 {}
|
||||
})
|
||||
}
|
||||
// fn Child(cx: Scope) -> Element {
|
||||
// println!("Running child");
|
||||
// cx.render(rsx_without_templates! {
|
||||
// h1 {}
|
||||
// })
|
||||
// }
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let edits = dom.rebuild();
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
[
|
||||
CreateTextNode { root: Some(1), text: "Text0" },
|
||||
AppendChildren { root: Some(0), children: vec![1] }
|
||||
]
|
||||
);
|
||||
// let mut dom = VirtualDom::new(app);
|
||||
// let edits = dom.rebuild();
|
||||
// assert_eq!(
|
||||
// edits.edits,
|
||||
// [
|
||||
// CreateTextNode { root: Some(1), text: "Text0" },
|
||||
// AppendChildren { root: Some(0), children: vec![1] }
|
||||
// ]
|
||||
// );
|
||||
|
||||
assert_eq!(
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateElement { root: Some(2), tag: "div", children: 0 },
|
||||
ReplaceWith { root: Some(1), nodes: vec![2] }
|
||||
]
|
||||
);
|
||||
// assert_eq!(
|
||||
// dom.hard_diff(ScopeId(0)).edits,
|
||||
// [
|
||||
// CreateElement { root: Some(2), tag: "div", children: 0 },
|
||||
// ReplaceWith { root: Some(1), nodes: vec![2] }
|
||||
// ]
|
||||
// );
|
||||
|
||||
assert_eq!(
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateTextNode { root: Some(1), text: "Text2" },
|
||||
ReplaceWith { root: Some(2), nodes: vec![1] }
|
||||
]
|
||||
);
|
||||
// assert_eq!(
|
||||
// dom.hard_diff(ScopeId(0)).edits,
|
||||
// [
|
||||
// CreateTextNode { root: Some(1), text: "Text2" },
|
||||
// ReplaceWith { root: Some(2), nodes: vec![1] }
|
||||
// ]
|
||||
// );
|
||||
|
||||
// child {}
|
||||
assert_eq!(
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateElement { root: Some(2), tag: "h1", children: 0 },
|
||||
ReplaceWith { root: Some(1), nodes: vec![2] }
|
||||
]
|
||||
);
|
||||
// // child {}
|
||||
// assert_eq!(
|
||||
// dom.hard_diff(ScopeId(0)).edits,
|
||||
// [
|
||||
// CreateElement { root: Some(2), tag: "h1", children: 0 },
|
||||
// ReplaceWith { root: Some(1), nodes: vec![2] }
|
||||
// ]
|
||||
// );
|
||||
|
||||
// placeholder
|
||||
assert_eq!(
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreatePlaceholder { root: Some(1) },
|
||||
ReplaceWith { root: Some(2), nodes: vec![1] }
|
||||
]
|
||||
);
|
||||
// // placeholder
|
||||
// assert_eq!(
|
||||
// dom.hard_diff(ScopeId(0)).edits,
|
||||
// [
|
||||
// CreatePlaceholder { root: Some(1) },
|
||||
// ReplaceWith { root: Some(2), nodes: vec![1] }
|
||||
// ]
|
||||
// );
|
||||
|
||||
assert_eq!(
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateTextNode { root: Some(2), text: "text 3" },
|
||||
ReplaceWith { root: Some(1), nodes: vec![2] }
|
||||
]
|
||||
);
|
||||
// assert_eq!(
|
||||
// dom.hard_diff(ScopeId(0)).edits,
|
||||
// [
|
||||
// CreateTextNode { root: Some(2), text: "text 3" },
|
||||
// ReplaceWith { root: Some(1), nodes: vec![2] }
|
||||
// ]
|
||||
// );
|
||||
|
||||
assert_eq!(
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateTextNode { text: "text 0", root: Some(1) },
|
||||
CreateTextNode { text: "text 1", root: Some(3) },
|
||||
ReplaceWith { root: Some(2), nodes: vec![1, 3] },
|
||||
]
|
||||
);
|
||||
// assert_eq!(
|
||||
// dom.hard_diff(ScopeId(0)).edits,
|
||||
// [
|
||||
// CreateTextNode { text: "text 0", root: Some(1) },
|
||||
// CreateTextNode { text: "text 1", root: Some(3) },
|
||||
// ReplaceWith { root: Some(2), nodes: vec![1, 3] },
|
||||
// ]
|
||||
// );
|
||||
|
||||
assert_eq!(
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateElement { tag: "h1", root: Some(2), children: 0 },
|
||||
ReplaceWith { root: Some(1), nodes: vec![2] },
|
||||
Remove { root: Some(3) },
|
||||
]
|
||||
);
|
||||
}
|
||||
// assert_eq!(
|
||||
// dom.hard_diff(ScopeId(0)).edits,
|
||||
// [
|
||||
// CreateElement { tag: "h1", root: Some(2), children: 0 },
|
||||
// ReplaceWith { root: Some(1), nodes: vec![2] },
|
||||
// Remove { root: Some(3) },
|
||||
// ]
|
||||
// );
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn component_swap() {
|
||||
fn app(cx: Scope) -> Element {
|
||||
let render_phase = cx.use_hook(|| 0);
|
||||
*render_phase += 1;
|
||||
// #[test]
|
||||
// fn component_swap() {
|
||||
// fn app(cx: Scope) -> Element {
|
||||
// let render_phase = cx.use_hook(|| 0);
|
||||
// *render_phase += 1;
|
||||
|
||||
cx.render(match *render_phase {
|
||||
0 => rsx_without_templates!(
|
||||
div {
|
||||
NavBar {}
|
||||
Dashboard {}
|
||||
}
|
||||
),
|
||||
1 => rsx_without_templates!(
|
||||
div {
|
||||
NavBar {}
|
||||
Results {}
|
||||
}
|
||||
),
|
||||
2 => rsx_without_templates!(
|
||||
div {
|
||||
NavBar {}
|
||||
Dashboard {}
|
||||
}
|
||||
),
|
||||
3 => rsx_without_templates!(
|
||||
div {
|
||||
NavBar {}
|
||||
Results {}
|
||||
}
|
||||
),
|
||||
4 => rsx_without_templates!(
|
||||
div {
|
||||
NavBar {}
|
||||
Dashboard {}
|
||||
}
|
||||
),
|
||||
_ => rsx_without_templates!("blah"),
|
||||
})
|
||||
};
|
||||
// cx.render(match *render_phase {
|
||||
// 0 => rsx_without_templates!(
|
||||
// div {
|
||||
// NavBar {}
|
||||
// Dashboard {}
|
||||
// }
|
||||
// ),
|
||||
// 1 => rsx_without_templates!(
|
||||
// div {
|
||||
// NavBar {}
|
||||
// Results {}
|
||||
// }
|
||||
// ),
|
||||
// 2 => rsx_without_templates!(
|
||||
// div {
|
||||
// NavBar {}
|
||||
// Dashboard {}
|
||||
// }
|
||||
// ),
|
||||
// 3 => rsx_without_templates!(
|
||||
// div {
|
||||
// NavBar {}
|
||||
// Results {}
|
||||
// }
|
||||
// ),
|
||||
// 4 => rsx_without_templates!(
|
||||
// div {
|
||||
// NavBar {}
|
||||
// Dashboard {}
|
||||
// }
|
||||
// ),
|
||||
// _ => rsx_without_templates!("blah"),
|
||||
// })
|
||||
// };
|
||||
|
||||
static NavBar: Component = |cx| {
|
||||
println!("running navbar");
|
||||
cx.render(rsx_without_templates! {
|
||||
h1 {
|
||||
"NavBar"
|
||||
{(0..3).map(|f| rsx_without_templates!(NavLink {}))}
|
||||
}
|
||||
})
|
||||
};
|
||||
// static NavBar: Component = |cx| {
|
||||
// println!("running navbar");
|
||||
// cx.render(rsx_without_templates! {
|
||||
// h1 {
|
||||
// "NavBar"
|
||||
// {(0..3).map(|f| rsx_without_templates!(NavLink {}))}
|
||||
// }
|
||||
// })
|
||||
// };
|
||||
|
||||
static NavLink: Component = |cx| {
|
||||
println!("running navlink");
|
||||
cx.render(rsx_without_templates! {
|
||||
h1 {
|
||||
"NavLink"
|
||||
}
|
||||
})
|
||||
};
|
||||
// static NavLink: Component = |cx| {
|
||||
// println!("running navlink");
|
||||
// cx.render(rsx_without_templates! {
|
||||
// h1 {
|
||||
// "NavLink"
|
||||
// }
|
||||
// })
|
||||
// };
|
||||
|
||||
static Dashboard: Component = |cx| {
|
||||
println!("running dashboard");
|
||||
cx.render(rsx_without_templates! {
|
||||
div {
|
||||
"dashboard"
|
||||
}
|
||||
})
|
||||
};
|
||||
// static Dashboard: Component = |cx| {
|
||||
// println!("running dashboard");
|
||||
// cx.render(rsx_without_templates! {
|
||||
// div {
|
||||
// "dashboard"
|
||||
// }
|
||||
// })
|
||||
// };
|
||||
|
||||
static Results: Component = |cx| {
|
||||
println!("running results");
|
||||
cx.render(rsx_without_templates! {
|
||||
div {
|
||||
"results"
|
||||
}
|
||||
})
|
||||
};
|
||||
// static Results: Component = |cx| {
|
||||
// println!("running results");
|
||||
// cx.render(rsx_without_templates! {
|
||||
// div {
|
||||
// "results"
|
||||
// }
|
||||
// })
|
||||
// };
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let edits = dom.rebuild();
|
||||
dbg!(&edits);
|
||||
// let mut dom = VirtualDom::new(app);
|
||||
// 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);
|
||||
}
|
||||
// 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);
|
||||
// }
|
||||
Reference in new issue
Block a user