create tests for signals
This commit is contained in:
1 parent
b0d499036a
commit
717c09c4a3
8 files changed
+234
-5
No files matched your search
@@ -0,0 +1,60 @@
|
||||
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core::ElementId;
|
||||
use dioxus_signals::*;
|
||||
|
||||
#[test]
|
||||
fn create_signals_global() {
|
||||
let mut dom = VirtualDom::new(|cx| {
|
||||
render! {
|
||||
for _ in 0..10 {
|
||||
Child {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fn Child(cx: Scope) -> Element {
|
||||
let signal = create_without_cx();
|
||||
|
||||
render! {
|
||||
"{signal}"
|
||||
}
|
||||
}
|
||||
|
||||
let _edits = dom.rebuild().santize();
|
||||
|
||||
fn create_without_cx() -> Signal<String> {
|
||||
Signal::new("hello world".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_signals() {
|
||||
let mut dom = VirtualDom::new(|cx| {
|
||||
let generation = cx.generation();
|
||||
|
||||
let count = if generation % 2 == 0 { 10 } else { 0 };
|
||||
render! {
|
||||
for _ in 0..count {
|
||||
Child {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fn Child(cx: Scope) -> Element {
|
||||
let signal = create_without_cx();
|
||||
|
||||
render! {
|
||||
"{signal}"
|
||||
}
|
||||
}
|
||||
|
||||
let _ = dom.rebuild().santize();
|
||||
dom.mark_dirty(ScopeId(0));
|
||||
dom.render_immediate();
|
||||
|
||||
fn create_without_cx() -> Signal<String> {
|
||||
Signal::new("hello world".to_string())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core::ElementId;
|
||||
use dioxus_signals::*;
|
||||
|
||||
#[test]
|
||||
fn effects_rerun() {
|
||||
simple_logger::SimpleLogger::new().init().unwrap();
|
||||
|
||||
#[derive(Default)]
|
||||
struct RunCounter {
|
||||
component: usize,
|
||||
effect: usize,
|
||||
}
|
||||
|
||||
let counter = Rc::new(RefCell::new(RunCounter::default()));
|
||||
let mut dom = VirtualDom::new_with_props(
|
||||
|cx| {
|
||||
let counter = cx.props;
|
||||
counter.borrow_mut().component += 1;
|
||||
|
||||
let mut signal = use_signal(cx, || 0);
|
||||
cx.use_hook(move || {
|
||||
to_owned![counter];
|
||||
Effect::new(move || {
|
||||
counter.borrow_mut().effect += 1;
|
||||
println!("Signal: {:?}", signal);
|
||||
})
|
||||
});
|
||||
signal += 1;
|
||||
|
||||
render! {
|
||||
div {}
|
||||
}
|
||||
},
|
||||
counter.clone(),
|
||||
);
|
||||
|
||||
let _ = dom.rebuild().santize();
|
||||
|
||||
let current_counter = counter.borrow();
|
||||
assert_eq!(current_counter.component, 1);
|
||||
assert_eq!(current_counter.effect, 2);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core::ElementId;
|
||||
use dioxus_signals::*;
|
||||
|
||||
#[test]
|
||||
fn reading_subscribes() {
|
||||
simple_logger::SimpleLogger::new().init().unwrap();
|
||||
|
||||
#[derive(Default)]
|
||||
struct RunCounter {
|
||||
parent: usize,
|
||||
children: HashMap<ScopeId, usize>,
|
||||
}
|
||||
|
||||
let counter = Rc::new(RefCell::new(RunCounter::default()));
|
||||
let mut dom = VirtualDom::new_with_props(
|
||||
|cx| {
|
||||
let mut signal = use_signal(cx, || 0);
|
||||
|
||||
println!("Parent: {:?}", cx.scope_id());
|
||||
if cx.generation() == 1 {
|
||||
signal += 1;
|
||||
}
|
||||
|
||||
cx.props.borrow_mut().parent += 1;
|
||||
|
||||
render! {
|
||||
for id in 0..10 {
|
||||
Child {
|
||||
signal: signal,
|
||||
counter: cx.props.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
counter.clone(),
|
||||
);
|
||||
|
||||
#[derive(Props, Clone)]
|
||||
struct ChildProps {
|
||||
signal: Signal<usize>,
|
||||
counter: Rc<RefCell<RunCounter>>,
|
||||
}
|
||||
|
||||
impl PartialEq for ChildProps {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.signal == other.signal
|
||||
}
|
||||
}
|
||||
|
||||
fn Child(cx: Scope<ChildProps>) -> Element {
|
||||
println!("Child: {:?}", cx.scope_id());
|
||||
*cx.props
|
||||
.counter
|
||||
.borrow_mut()
|
||||
.children
|
||||
.entry(cx.scope_id())
|
||||
.or_default() += 1;
|
||||
|
||||
render! {
|
||||
"{cx.props.signal}"
|
||||
}
|
||||
}
|
||||
|
||||
let _ = dom.rebuild().santize();
|
||||
|
||||
{
|
||||
let current_counter = counter.borrow();
|
||||
assert_eq!(current_counter.parent, 1);
|
||||
|
||||
for (scope_id, rerun_count) in current_counter.children.iter() {
|
||||
assert_eq!(rerun_count, &1);
|
||||
}
|
||||
}
|
||||
|
||||
dom.mark_dirty(ScopeId(0));
|
||||
dom.render_immediate();
|
||||
dom.render_immediate();
|
||||
|
||||
{
|
||||
let current_counter = counter.borrow();
|
||||
assert_eq!(current_counter.parent, 2);
|
||||
|
||||
for (scope_id, rerun_count) in current_counter.children.iter() {
|
||||
assert_eq!(rerun_count, &2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user