wip: migrate all tests to core

This commit is contained in:
Jonathan Kelley committed 2022-11-27 00:56:49 -05:00
1 parent d0554b9ed6
commit f614cbb401
12 files changed
+104 -273

No files matched your search

+1 -1
View File
@@ -235,7 +235,7 @@ impl<'b: 'static> VirtualDom {
TemplateNode::DynamicText { .. } => self
.mutations
.template_mutations
.push(CreateStaticText { value: "d" }),
.push(CreateTextPlaceholder),
TemplateNode::Element {
attrs,
+1
View File
@@ -81,6 +81,7 @@ pub enum Mutation<'a> {
id: ElementId,
},
CreateStaticPlaceholder,
CreateTextPlaceholder,
CreateStaticText {
value: &'a str,
},
File renamed without changes.
File renamed without changes.
+78
View File
@@ -0,0 +1,78 @@
use dioxus::core::{ElementId, Mutation};
use dioxus::prelude::*;
fn basic_syntax_is_a_template(cx: Scope) -> Element {
let asd = 123;
let var = 123;
cx.render(rsx! {
div { key: "12345",
class: "asd",
class: "{asd}",
onclick: move |_| {},
div { "{var}" }
div {
h1 { "var" }
p { "you're great!" }
div { background_color: "red",
h1 { "var" }
div { b { "asd" } "not great" }
}
p { "you're great!" }
}
}
})
}
#[test]
fn dual_stream() {
let mut dom = VirtualDom::new(basic_syntax_is_a_template);
let edits = dom.rebuild().santize();
use Mutation::*;
assert_eq!(
edits.template_mutations,
[
CreateElement { name: "div" },
SetStaticAttribute { name: "class", value: "asd", ns: None },
CreateElement { name: "div" },
CreateTextPlaceholder,
AppendChildren { m: 1 },
CreateElement { name: "div" },
CreateElement { name: "h1" },
CreateStaticText { value: "var" },
AppendChildren { m: 1 },
CreateElement { name: "p" },
CreateStaticText { value: "you're great!" },
AppendChildren { m: 1 },
CreateElement { name: "div" },
SetStaticAttribute { name: "background-color", value: "red", ns: Some("style") },
CreateElement { name: "h1" },
CreateStaticText { value: "var" },
AppendChildren { m: 1 },
CreateElement { name: "div" },
CreateElement { name: "b" },
CreateStaticText { value: "asd" },
AppendChildren { m: 1 },
CreateStaticText { value: "not great" },
AppendChildren { m: 2 },
AppendChildren { m: 2 },
CreateElement { name: "p" },
CreateStaticText { value: "you're great!" },
AppendChildren { m: 1 },
AppendChildren { m: 4 },
AppendChildren { m: 2 },
SaveTemplate { name: "template", m: 1 }
],
);
assert_eq!(
edits.edits,
[
LoadTemplate { name: "template", index: 0, id: ElementId(1) },
SetAttribute { name: "class", value: "123", id: ElementId(1), ns: None },
NewEventListener { event_name: "click", scope: ScopeId(0), id: ElementId(1) },
HydrateText { path: &[0, 0], value: "123", id: ElementId(2) },
AppendChildren { m: 1 }
],
);
}
File renamed without changes.
File renamed without changes.
@@ -2,6 +2,7 @@ use dioxus::core::ElementId;
use dioxus::core::{Mutation::*, SuspenseBoundary};
use dioxus::prelude::*;
use dioxus_core::SuspenseContext;
use std::future::IntoFuture;
use std::{rc::Rc, time::Duration};
#[tokio::test]
@@ -63,5 +64,27 @@ async fn async_child(cx: Scope<'_>) -> Element {
}
async fn async_text(cx: Scope<'_>) -> Element {
cx.render(rsx!("async_text"))
let username = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
"async child 1"
});
let age = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
println!("long future completed");
1234
});
let (_user, _age) = use_future!(cx, || async {
tokio::join!(
tokio::time::sleep(std::time::Duration::from_secs(1)),
tokio::time::sleep(std::time::Duration::from_secs(2))
);
("async child 1", 1234)
})
.await;
let (username, age) = tokio::join!(username.into_future(), age.into_future());
cx.render(rsx!( div { "Hello! {username}, you are {age}, {_user} {_age}" } ))
}
-1
View File
@@ -1 +0,0 @@
struct_lit_width = 80
-83
View File
@@ -1,83 +0,0 @@
fn basic_syntax_is_a_template(cx: Scope) -> Element {
let asd = 123;
let var = 123;
cx.render(rsx! {
div { key: "12345",
class: "asd",
class: "{asd}",
onclick: move |_| {},
div { "{var}" }
div {
h1 { "var" }
p { "you're great!" }
div { background_color: "red",
h1 { "var" }
div { b { "asd" } "not great" }
}
p { "you're great!" }
}
}
})
}
// imports come after the test since the rsx! macro is sensitive to its location in the file
// (the byte index is used to differentiate sub templates)
use dioxus::core::{ElementId, Mutation};
use dioxus::prelude::*;
#[test]
fn dual_stream() {
let mut dom = VirtualDom::new(basic_syntax_is_a_template);
let edits = dom.rebuild();
use Mutation::*;
assert_eq!(
edits.template_mutations,
vec![
CreateElement { name: "div", namespace: None, id: ElementId(1) },
SetAttribute { name: "class", value: "asd", id: ElementId(1) },
CreateElement { name: "div", namespace: None, id: ElementId(2) },
CreatePlaceholder { id: ElementId(3) },
AppendChildren { m: 1 },
CreateElement { name: "div", namespace: None, id: ElementId(4) },
CreateElement { name: "h1", namespace: None, id: ElementId(5) },
CreateTextNode { value: "var" },
AppendChildren { m: 1 },
CreateElement { name: "p", namespace: None, id: ElementId(6) },
CreateTextNode { value: "you're great!" },
AppendChildren { m: 1 },
CreateElement { name: "div", namespace: None, id: ElementId(7) },
SetAttribute { name: "background-color", value: "red", id: ElementId(7) },
CreateElement { name: "h1", namespace: None, id: ElementId(8) },
CreateTextNode { value: "var" },
AppendChildren { m: 1 },
CreateElement { name: "div", namespace: None, id: ElementId(9) },
CreateElement { name: "b", namespace: None, id: ElementId(10) },
CreateTextNode { value: "asd" },
AppendChildren { m: 1 },
CreateTextNode { value: "not great" },
AppendChildren { m: 2 },
AppendChildren { m: 2 },
CreateElement { name: "p", namespace: None, id: ElementId(11) },
CreateTextNode { value: "you're great!" },
AppendChildren { m: 1 },
AppendChildren { m: 4 },
AppendChildren { m: 2 },
SaveTemplate { name: "packages/dioxus/tests/rsx_syntax.rs:5:15:122", m: 1 }
]
);
assert_eq!(
edits.edits,
vec![
LoadTemplate { name: "packages/dioxus/tests/rsx_syntax.rs:5:15:122", index: 0 },
AssignId { path: &[], id: ElementId(12) },
SetAttribute { name: "class", value: "123", id: ElementId(12) },
SetAttribute { name: "onclick", value: "asd", id: ElementId(12) }, // ---- todo: listeners
HydrateText { path: &[0, 0], value: "123", id: ElementId(13) },
ReplacePlaceholder { m: 1, path: &[0, 0] },
AppendChildren { m: 1 }
]
);
}
-126
View File
@@ -1,126 +0,0 @@
#![allow(unused, non_upper_case_globals, non_snake_case)]
use dioxus::{core_macro::rsx_without_templates, prelude::*};
use dioxus_core::{DomEdit, Mutations, SchedulerMsg, ScopeId};
use std::rc::Rc;
use DomEdit::*;
#[test]
fn shared_state_test() {
struct MySharedState(&'static str);
static App: Component = |cx| {
cx.provide_context(Rc::new(MySharedState("world!")));
cx.render(rsx_without_templates!(Child {}))
};
static Child: Component = |cx| {
let shared = cx.consume_context::<Rc<MySharedState>>()?;
cx.render(rsx_without_templates!("Hello, {shared.0}"))
};
let mut dom = VirtualDom::new(App);
let Mutations { edits, .. } = dom.rebuild();
assert_eq!(
edits,
[
CreateTextNode { root: Some(1), text: "Hello, world!" },
AppendChildren { root: Some(0), children: vec![1] },
]
);
}
#[test]
fn swap_test() {
struct MySharedState(&'static str);
fn app(cx: Scope) -> Element {
let val = cx.use_hook(|| 0);
*val += 1;
cx.provide_context(Rc::new(MySharedState("world!")));
let child = match *val % 2 {
0 => rsx_without_templates!(
Child1 {
Child1 { }
Child2 { }
}
),
_ => rsx_without_templates!(
Child2 {
Child2 { }
Child2 { }
}
),
};
cx.render(rsx_without_templates!(
Router {
div { child }
}
))
}
#[inline_props]
fn Router<'a>(cx: Scope, children: Element<'a>) -> Element<'a> {
cx.render(rsx_without_templates!(div { children }))
}
#[inline_props]
fn Child1<'a>(cx: Scope, children: Element<'a>) -> Element {
let shared = cx.consume_context::<Rc<MySharedState>>().unwrap();
println!("Child1: {}", shared.0);
cx.render(rsx_without_templates! {
div {
"{shared.0}",
children
}
})
}
#[inline_props]
fn Child2<'a>(cx: Scope, children: Element<'a>) -> Element {
let shared = cx.consume_context::<Rc<MySharedState>>().unwrap();
println!("Child2: {}", shared.0);
cx.render(rsx_without_templates! {
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);
}
-61
View File
@@ -1,61 +0,0 @@
use std::{future::IntoFuture, rc::Rc};
use dioxus::prelude::*;
use dioxus_core::SuspenseBoundary;
#[tokio::test]
async fn basic_prints() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
div {
h1 { "var" }
suspense_boundary {
basic_child { }
async_child { }
}
}
})
});
dbg!(dom.rebuild());
dom.wait_for_work().await;
dbg!(dom.rebuild());
}
#[inline_props]
fn suspense_boundary<'a>(cx: Scope<'a>, children: Element<'a>) -> Element {
cx.use_hook(|| cx.provide_context(Rc::new(SuspenseBoundary::new(cx.scope_id()))));
cx.render(rsx! { children })
}
fn basic_child(cx: Scope) -> Element {
cx.render(rsx!( div { "basic child 1" } ))
}
async fn async_child(cx: Scope<'_>) -> Element {
let username = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
"async child 1"
});
let age = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
println!("long future completed");
1234
});
let (_user, _age) = use_future!(cx, || async {
tokio::join!(
tokio::time::sleep(std::time::Duration::from_secs(1)),
tokio::time::sleep(std::time::Duration::from_secs(2))
);
("async child 1", 1234)
})
.await;
let (username, age) = tokio::join!(username.into_future(), age.into_future());
cx.render(rsx!( div { "Hello! {username}, you are {age}, {_user} {_age}" } ))
}