get cargo check --all --examples --tests working!
This commit is contained in:
18 files changed
+109
-508
No files matched your search
@@ -1,84 +0,0 @@
|
||||
#![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| {
|
||||
rsx! {
|
||||
for _ in 0..10 {
|
||||
Child {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fn Child() -> Element {
|
||||
let signal = create_without_cx();
|
||||
|
||||
rsx! {
|
||||
"{signal}"
|
||||
}
|
||||
}
|
||||
|
||||
let _edits = dom.rebuild().santize();
|
||||
|
||||
fn create_without_cx() -> Signal<String> {
|
||||
Signal::new("hello world".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deref_signal() {
|
||||
let mut dom = VirtualDom::new(|cx| {
|
||||
rsx! {
|
||||
for _ in 0..10 {
|
||||
Child {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fn Child() -> Element {
|
||||
let signal = Signal::new("hello world".to_string());
|
||||
|
||||
// You can call signals like functions to get a Ref of their value.
|
||||
assert_eq!(&*signal(), "hello world");
|
||||
|
||||
rsx! {
|
||||
"hello world"
|
||||
}
|
||||
}
|
||||
|
||||
let _edits = dom.rebuild().santize();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_signals() {
|
||||
let mut dom = VirtualDom::new(|cx| {
|
||||
let generation = generation();
|
||||
|
||||
let count = if generation % 2 == 0 { 10 } else { 0 };
|
||||
rsx! {
|
||||
for _ in 0..count {
|
||||
Child {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fn Child() -> Element {
|
||||
let signal = create_without_cx();
|
||||
|
||||
rsx! {
|
||||
"{signal}"
|
||||
}
|
||||
}
|
||||
|
||||
let _ = dom.rebuild().santize();
|
||||
dom.mark_dirty(ScopeId::ROOT);
|
||||
dom.render_immediate();
|
||||
|
||||
fn create_without_cx() -> Signal<String> {
|
||||
Signal::new("hello world".to_string())
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#![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::*;
|
||||
|
||||
#[tokio::test]
|
||||
async 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(|| 0);
|
||||
cx.use_hook(move || {
|
||||
to_owned![counter];
|
||||
Effect::new(move || {
|
||||
counter.borrow_mut().effect += 1;
|
||||
println!("Signal: {:?}", signal);
|
||||
})
|
||||
});
|
||||
signal += 1;
|
||||
|
||||
rsx! { div {} }
|
||||
},
|
||||
counter.clone(),
|
||||
);
|
||||
|
||||
let _ = dom.rebuild().santize();
|
||||
dom.render_with_deadline(tokio::time::sleep(std::time::Duration::from_millis(100)))
|
||||
.await;
|
||||
|
||||
let current_counter = counter.borrow();
|
||||
assert_eq!(current_counter.component, 1);
|
||||
assert_eq!(current_counter.effect, 2);
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
#![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();
|
||||
let mapped = MappedSignal::new(signal, |v| v.as_bytes());
|
||||
|
||||
render! { "{signal:?}", "{mapped:?}" }
|
||||
}
|
||||
|
||||
let _edits = dom.rebuild().santize();
|
||||
|
||||
fn create_without_cx() -> Signal<String> {
|
||||
Signal::new("hello world".to_string())
|
||||
}
|
||||
}
|
||||
@@ -1,146 +0,0 @@
|
||||
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use dioxus::html::p;
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core::ElementId;
|
||||
use dioxus_signals::*;
|
||||
|
||||
#[test]
|
||||
fn memos_rerun() {
|
||||
let _ = simple_logger::SimpleLogger::new().init();
|
||||
|
||||
#[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(|| 0);
|
||||
let memo = cx.use_hook(move || {
|
||||
to_owned![counter];
|
||||
selector(move || {
|
||||
counter.borrow_mut().effect += 1;
|
||||
println!("Signal: {:?}", signal);
|
||||
signal.cloned()
|
||||
})
|
||||
});
|
||||
let generation = use_signal(cx, || cx.generation());
|
||||
generation.set(cx.generation());
|
||||
dioxus_signals::use_effect(cx, move || {
|
||||
if generation == 1 {
|
||||
assert_eq!(memo.value(), 0);
|
||||
}
|
||||
if generation == 3 {
|
||||
assert_eq!(memo.value(), 1);
|
||||
}
|
||||
});
|
||||
signal += 1;
|
||||
|
||||
rsx! { div {} }
|
||||
},
|
||||
counter.clone(),
|
||||
);
|
||||
|
||||
let _ = dom.rebuild().santize();
|
||||
let _ = dom.render_immediate();
|
||||
|
||||
let current_counter = counter.borrow();
|
||||
assert_eq!(current_counter.component, 1);
|
||||
assert_eq!(current_counter.effect, 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn memos_prevents_component_rerun() {
|
||||
let _ = simple_logger::SimpleLogger::new().init();
|
||||
|
||||
#[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 mut signal = use_signal(|| 0);
|
||||
|
||||
if generation() == 1 {
|
||||
*signal.write() = 0;
|
||||
}
|
||||
if generation() == 2 {
|
||||
println!("Writing to signal");
|
||||
*signal.write() = 1;
|
||||
}
|
||||
|
||||
rsx! { Child { signal: signal, counter: cx.props.clone() } }
|
||||
},
|
||||
counter.clone(),
|
||||
);
|
||||
|
||||
#[derive(Default, Props)]
|
||||
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 {
|
||||
let counter = &cx.props.counter;
|
||||
let signal = cx.props.signal;
|
||||
counter.borrow_mut().component += 1;
|
||||
|
||||
let memo = cx.use_hook(move || {
|
||||
to_owned![counter];
|
||||
selector(move || {
|
||||
counter.borrow_mut().effect += 1;
|
||||
println!("Signal: {:?}", signal);
|
||||
signal.value()
|
||||
})
|
||||
});
|
||||
match generation() {
|
||||
0 => {
|
||||
assert_eq!(memo.value(), 0);
|
||||
}
|
||||
1 => {
|
||||
assert_eq!(memo.value(), 1);
|
||||
}
|
||||
_ => panic!("Unexpected generation"),
|
||||
}
|
||||
|
||||
rsx! { div {} }
|
||||
}
|
||||
|
||||
let _ = dom.rebuild().santize();
|
||||
dom.mark_dirty(ScopeId::ROOT);
|
||||
dom.render_immediate();
|
||||
dom.render_immediate();
|
||||
|
||||
{
|
||||
let current_counter = counter.borrow();
|
||||
assert_eq!(current_counter.component, 1);
|
||||
assert_eq!(current_counter.effect, 2);
|
||||
}
|
||||
|
||||
dom.mark_dirty(ScopeId::ROOT);
|
||||
dom.render_immediate();
|
||||
dom.render_immediate();
|
||||
|
||||
{
|
||||
let current_counter = counter.borrow();
|
||||
assert_eq!(current_counter.component, 2);
|
||||
assert_eq!(current_counter.effect, 3);
|
||||
}
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
#![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(|| 0);
|
||||
|
||||
println!("Parent: {:?}", cx.scope_id());
|
||||
if generation() == 1 {
|
||||
signal += 1;
|
||||
}
|
||||
|
||||
cx.props.borrow_mut().parent += 1;
|
||||
|
||||
rsx! {
|
||||
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;
|
||||
|
||||
rsx! {
|
||||
"{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::ROOT);
|
||||
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