create tests for signals

This commit is contained in:
Evan Almloff committed 2023-08-07 11:52:54 -07:00
1 parent b0d499036a
commit 717c09c4a3
8 files changed
+234 -5

No files matched your search

+15
View File
@@ -1,5 +1,6 @@
use std::{
cell::{Cell, Ref, RefCell, RefMut},
fmt::Debug,
marker::PhantomData,
rc::Rc,
};
@@ -151,6 +152,20 @@ pub struct CopyHandle<T> {
_marker: PhantomData<T>,
}
impl<T: 'static> Debug for CopyHandle<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
#[cfg(any(debug_assertions, feature = "check_generation"))]
f.write_fmt(format_args!(
"{:?}@{:?}",
self.raw.data.as_ptr(),
self.generation
))?;
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
f.write_fmt(format_args!("{:?}", self.raw.data.as_ptr()))?;
Ok(())
}
}
impl<T: 'static> CopyHandle<T> {
#[inline(always)]
fn validate(&self) -> bool {
+5
View File
@@ -9,3 +9,8 @@ edition = "2018"
[dependencies]
dioxus-core = { workspace = true }
dioxus-copy = { workspace = true }
log.workspace = true
simple_logger = "4.2.0"
[dev-dependencies]
dioxus = { workspace = true }
+4 -4
View File
@@ -38,25 +38,25 @@ macro_rules! impls {
impl<T: Add<Output = T> + Copy + 'static> std::ops::AddAssign<T> for $ty<T> {
fn add_assign(&mut self, rhs: T) {
self.set(self.value() + rhs);
self.with_mut(|v| *v = *v + rhs)
}
}
impl<T: Sub<Output = T> + Copy + 'static> std::ops::SubAssign<T> for $ty<T> {
fn sub_assign(&mut self, rhs: T) {
self.set(self.value() - rhs);
self.with_mut(|v| *v = *v - rhs)
}
}
impl<T: Mul<Output = T> + Copy + 'static> std::ops::MulAssign<T> for $ty<T> {
fn mul_assign(&mut self, rhs: T) {
self.set(self.value() * rhs);
self.with_mut(|v| *v = *v * rhs)
}
}
impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for $ty<T> {
fn div_assign(&mut self, rhs: T) {
self.set(self.value() / rhs);
self.with_mut(|v| *v = *v / rhs)
}
}
+10
View File
@@ -77,6 +77,11 @@ impl<T: 'static> Signal<T> {
pub fn read(&self) -> Ref<T> {
let inner = self.inner.read();
if let Some(current_scope_id) = current_scope_id() {
log::trace!(
"{:?} subscribed to {:?}",
self.inner.value,
current_scope_id
);
let mut subscribers = inner.subscribers.borrow_mut();
if !subscribers.contains(&current_scope_id) {
subscribers.push(current_scope_id);
@@ -98,6 +103,11 @@ impl<T: 'static> Signal<T> {
{
let inner = self.inner.read();
for &scope_id in &*inner.subscribers.borrow() {
log::trace!(
"Write on {:?} triggered update on {:?}",
self.inner.value,
scope_id
);
(inner.update_any)(scope_id);
}
}
+1 -1
View File
@@ -41,7 +41,7 @@ fn owner_in_scope(scope: ScopeId) -> Rc<Owner> {
}
pub struct CopyValue<T: 'static> {
pub value: CopyHandle<T>,
pub(crate) value: CopyHandle<T>,
origin_scope: ScopeId,
}
+60
View File
@@ -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())
}
}
+47
View File
@@ -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);
}
+92
View File
@@ -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);
}
}
}