polish: tests and documentation

This commit is contained in:
Jonathan Kelley committed 2021-09-09 20:58:48 -04:00
1 parent 0896f34493
commit da8159190b
27 files changed
+467 -569

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
# Testing of Dioxus core
NodeFactory
- [] rsx, html, NodeFactory generate the same structures
Diffing
- [x] create elements
- [x] create text
- [x] create fragments
- [x] create empty fragments (placeholders)
- [x] diff elements
- [x] diff from element/text to fragment
- [x] diff from element/text to empty fragment
- [x] diff to element with children works too
- [x] replace with works forward
- [x] replace with works backward
- [x] un-keyed diffing
- [x] keyed diffing
- [x] keyed diffing out of order
- [x] keyed diffing with prefix/suffix
- [x] suspended nodes work
Lifecycle
- [] Components mount properly
- [] Components create new child components
- [] Replaced components unmount old components and mount new
- [] Post-render effects are called
- []
Shared Context
- [] Shared context propagates downwards
- [] unwrapping shared context if it doesn't exist works too
Suspense
- [] use_suspense generates suspended nodes
Hooks
- [] Drop order is maintained
- [] Shared hook state is okay
- [] use_hook works
- [] use_ref works
- [] use_noderef works
- [] use_provide_state
- [] use_consume_state
VirtualDOM API
- [] work
- [] rebuild
- [] change props
-19
View File
@@ -1,19 +0,0 @@
use futures_channel::mpsc::unbounded;
#[async_std::test]
async fn channels() {
let (sender, mut receiver) = unbounded::<u32>();
// drop(sender);
match receiver.try_next() {
Ok(a) => {
dbg!(a);
}
Err(no) => {
dbg!(no);
}
}
sender.unbounded_send(1).unwrap();
}
@@ -1,17 +1,23 @@
//! tests to prove that the iterative implementation works
//! Prove that the dom works normally through virtualdom methods.
//! This methods all use "rebuild" which completely bypasses the scheduler.
//! Hard rebuilds don't consume any events from the event queue.
use anyhow::{Context, Result};
use dioxus::{prelude::*, DomEdit, Mutations};
mod test_logging;
use dioxus::{prelude::*, DomEdit};
use dioxus_core as dioxus;
use dioxus_html as dioxus_elements;
mod test_logging;
use DomEdit::*;
const LOGGING_ENABLED: bool = false;
fn new_dom<P: Properties + 'static>(app: FC<P>, props: P) -> VirtualDom {
const IS_LOGGING_ENABLED: bool = false;
test_logging::set_up_logging(IS_LOGGING_ENABLED);
VirtualDom::new_with_props(app, props)
}
#[test]
fn test_original_diff() {
static App: FC<()> = |cx| {
static APP: FC<()> = |cx| {
cx.render(rsx! {
div {
div {
@@ -21,7 +27,7 @@ fn test_original_diff() {
})
};
let mut dom = VirtualDom::new(App);
let mut dom = new_dom(APP, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
@@ -41,7 +47,7 @@ fn test_original_diff() {
#[async_std::test]
async fn create() {
static App: FC<()> = |cx| {
static APP: FC<()> = |cx| {
cx.render(rsx! {
div {
div {
@@ -59,9 +65,9 @@ async fn create() {
})
};
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await;
let mut dom = new_dom(APP, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[
@@ -92,7 +98,7 @@ async fn create() {
#[async_std::test]
async fn create_list() {
static App: FC<()> = |cx| {
static APP: FC<()> = |cx| {
cx.render(rsx! {
{(0..3).map(|f| rsx!{ div {
"hello"
@@ -100,10 +106,8 @@ async fn create_list() {
})
};
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await;
let mut dom = new_dom(APP, ());
let mutations = dom.rebuild();
// copilot wrote this test :P
assert_eq!(
@@ -134,7 +138,7 @@ async fn create_list() {
#[async_std::test]
async fn create_simple() {
static App: FC<()> = |cx| {
static APP: FC<()> = |cx| {
cx.render(rsx! {
div {}
div {}
@@ -143,10 +147,8 @@ async fn create_simple() {
})
};
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await;
let mut dom = new_dom(APP, ());
let mutations = dom.rebuild();
// copilot wrote this test :P
assert_eq!(
@@ -179,10 +181,8 @@ async fn create_components() {
})
};
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await;
let mut dom = new_dom(App, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
@@ -225,10 +225,8 @@ async fn anchors() {
})
};
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await;
let mut dom = new_dom(App, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[
@@ -247,14 +245,18 @@ async fn anchors() {
#[async_std::test]
async fn suspended() {
static App: FC<()> = |cx| {
let val = use_suspense(cx, || async {}, |cx, _| cx.render(rsx! { "hi "}));
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;
let mut dom = new_dom(App, ());
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
-2
View File
@@ -1,2 +0,0 @@
/// A virtualdom wrapper used for testing purposes.
pub struct DebugDiff {}
-48
View File
@@ -1,48 +0,0 @@
//! tests to prove that the iterative implementation works
use dioxus::prelude::*;
mod test_logging;
use dioxus_core as dioxus;
use dioxus_html as dioxus_elements;
const LOGGING_ENABLED: bool = false;
#[async_std::test]
async fn test_iterative_create_components() {
static App: FC<()> = |cx| {
// test root fragments
cx.render(rsx! {
Child { "abc1" }
Child { "abc2" }
Child { "abc3" }
})
};
fn Child(cx: Context<()>) -> DomTree {
// test root fragments, anchors, and ChildNode type
cx.render(rsx! {
h1 {}
div { {cx.children()} }
Fragment {
Fragment {
Fragment {
"wozza"
}
}
}
{(0..0).map(|_f| rsx!{ div { "walalla"}})}
p {}
})
}
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await;
dbg!(mutations);
let mutations = dom.diff();
dbg!(mutations);
}
+32 -19
View File
@@ -4,24 +4,19 @@
//!
//! It does not validated that component lifecycles work properly. This is done in another test file.
use dioxus::{prelude::*, DomEdit, TestDom};
use dioxus::{nodes::VSuspended, prelude::*, DomEdit, TestDom};
use dioxus_core as dioxus;
use dioxus_html as dioxus_elements;
mod test_logging;
use DomEdit::*;
// logging is wired up to the test harness
// feel free to enable while debugging
const IS_LOGGING_ENABLED: bool = false;
fn new_dom() -> TestDom {
const IS_LOGGING_ENABLED: bool = false;
test_logging::set_up_logging(IS_LOGGING_ENABLED);
TestDom::new()
}
#[test]
fn diffing_works() {}
use DomEdit::*;
/// Should push the text node onto the stack and modify it
#[test]
@@ -98,8 +93,8 @@ fn fragments_create_properly() {
fn empty_fragments_create_anchors() {
let dom = new_dom();
let left = rsx!({ (0..0).map(|f| rsx! { div {}}) });
let right = rsx!({ (0..1).map(|f| rsx! { div {}}) });
let left = rsx!({ (0..0).map(|_f| rsx! { div {}}) });
let right = rsx!({ (0..1).map(|_f| rsx! { div {}}) });
let (create, change) = dom.lazy_diff(left, right);
@@ -121,8 +116,8 @@ fn empty_fragments_create_anchors() {
fn empty_fragments_create_many_anchors() {
let dom = new_dom();
let left = rsx!({ (0..0).map(|f| rsx! { div {}}) });
let right = rsx!({ (0..5).map(|f| rsx! { div {}}) });
let left = rsx!({ (0..0).map(|_f| rsx! { div {}}) });
let right = rsx!({ (0..5).map(|_f| rsx! { div {}}) });
let (create, change) = dom.lazy_diff(left, right);
assert_eq!(
@@ -148,7 +143,7 @@ fn empty_fragments_create_many_anchors() {
fn empty_fragments_create_anchors_with_many_children() {
let dom = new_dom();
let left = rsx!({ (0..0).map(|f| rsx! { div {} }) });
let left = rsx!({ (0..0).map(|_| rsx! { div {} }) });
let right = rsx!({
(0..3).map(|f| {
rsx! { div { "hello: {f}" }}
@@ -192,11 +187,11 @@ fn many_items_become_fragment() {
let dom = new_dom();
let left = rsx!({
(0..2).map(|f| {
(0..2).map(|_| {
rsx! { div { "hello" }}
})
});
let right = rsx!({ (0..0).map(|f| rsx! { div {} }) });
let right = rsx!({ (0..0).map(|_| rsx! { div {} }) });
let (create, change) = dom.lazy_diff(left, right);
assert_eq!(
@@ -235,17 +230,17 @@ fn two_equal_fragments_are_equal() {
let dom = new_dom();
let left = rsx!({
(0..2).map(|f| {
(0..2).map(|_| {
rsx! { div { "hello" }}
})
});
let right = rsx!({
(0..2).map(|f| {
(0..2).map(|_| {
rsx! { div { "hello" }}
})
});
let (create, change) = dom.lazy_diff(left, right);
let (_create, change) = dom.lazy_diff(left, right);
assert!(change.edits.is_empty());
}
@@ -263,7 +258,7 @@ fn two_fragments_with_differrent_elements_are_differet() {
p {}
);
let (create, changes) = dom.lazy_diff(left, right);
let (_create, changes) = dom.lazy_diff(left, right);
log::debug!("{:#?}", &changes);
assert_eq!(
changes.edits,
@@ -700,3 +695,21 @@ fn controlled_keyed_diffing_out_of_order_max_test() {
]
);
}
#[test]
fn suspense() {
let dom = new_dom();
let edits = dom.create(LazyNodes::new(|f| {
use std::cell::{Cell, RefCell};
VNode::Suspended(f.bump().alloc(VSuspended {
task_id: 0,
callback: RefCell::new(None),
dom_id: Cell::new(None),
}))
}));
assert_eq!(
edits.edits,
[CreatePlaceholder { id: 0 }, AppendChildren { many: 1 }]
);
}
-6
View File
@@ -1,6 +0,0 @@
// type VirtualNode = VNode;
/// Test a basic usage of a virtual dom + text renderer combo
#[test]
fn simple_integration() {}
+1
View File
@@ -0,0 +1 @@
//! Tests for the lifecycle of components.
+1
View File
@@ -0,0 +1 @@
//! Tests for the scheduler.
-30
View File
@@ -1,30 +0,0 @@
//! Diffing is interruptible, but uses yield_now which is loop-pollable
//!
//! This means you can actually call it synchronously if you want.
use anyhow::{Context, Result};
use dioxus::{prelude::*, scope::Scope};
use dioxus_core as dioxus;
use dioxus_html as dioxus_elements;
use futures_util::FutureExt;
#[test]
fn worksync() {
static App: FC<()> = |cx| {
cx.render(rsx! {
div {"hello"}
})
};
let mut dom = VirtualDom::new(App);
let mut fut = dom.rebuild_async().boxed_local();
let mutations = loop {
let g = (&mut fut).now_or_never();
if g.is_some() {
break g.unwrap();
}
};
dbg!(mutations);
}