leptos/leptos_reactive/tests/effect.rs

136 lines
3.4 KiB
Rust
Raw Normal View History

#[cfg(not(feature = "stable"))]
use leptos_reactive::{
2023-03-20 12:29:18 +00:00
create_isomorphic_effect, create_memo, create_runtime, create_rw_signal,
create_scope, create_signal, SignalSet,
};
2022-08-08 18:09:16 +00:00
#[cfg(not(feature = "stable"))]
2022-08-08 18:09:16 +00:00
#[test]
fn effect_runs() {
2023-02-11 19:30:06 +00:00
use std::{cell::RefCell, rc::Rc};
2022-08-08 18:09:16 +00:00
create_scope(create_runtime(), |cx| {
let (a, set_a) = create_signal(cx, -1);
2022-08-08 18:09:16 +00:00
// simulate an arbitrary side effect
let b = Rc::new(RefCell::new(String::new()));
create_isomorphic_effect(cx, {
2022-08-08 18:09:16 +00:00
let b = b.clone();
move |_| {
2022-08-08 18:09:16 +00:00
let formatted = format!("Value is {}", a());
*b.borrow_mut() = formatted;
}
});
assert_eq!(b.borrow().as_str(), "Value is -1");
set_a(1);
2022-08-08 18:09:16 +00:00
assert_eq!(b.borrow().as_str(), "Value is 1");
})
.dispose()
2022-08-08 18:09:16 +00:00
}
#[cfg(not(feature = "stable"))]
2022-08-08 18:09:16 +00:00
#[test]
fn effect_tracks_memo() {
2023-02-11 19:30:06 +00:00
use std::{cell::RefCell, rc::Rc};
2022-08-08 18:09:16 +00:00
create_scope(create_runtime(), |cx| {
let (a, set_a) = create_signal(cx, -1);
let b = create_memo(cx, move |_| format!("Value is {}", a()));
2022-08-08 18:09:16 +00:00
// simulate an arbitrary side effect
let c = Rc::new(RefCell::new(String::new()));
create_isomorphic_effect(cx, {
2022-08-08 18:09:16 +00:00
let c = c.clone();
move |_| {
2022-08-08 18:09:16 +00:00
*c.borrow_mut() = b();
}
});
assert_eq!(b().as_str(), "Value is -1");
assert_eq!(c.borrow().as_str(), "Value is -1");
set_a(1);
2022-08-08 18:09:16 +00:00
assert_eq!(b().as_str(), "Value is 1");
assert_eq!(c.borrow().as_str(), "Value is 1");
})
.dispose()
2022-08-08 18:09:16 +00:00
}
#[cfg(not(feature = "stable"))]
2022-08-08 18:09:16 +00:00
#[test]
fn untrack_mutes_effect() {
2023-02-11 19:30:06 +00:00
use std::{cell::RefCell, rc::Rc};
2022-08-08 18:09:16 +00:00
create_scope(create_runtime(), |cx| {
let (a, set_a) = create_signal(cx, -1);
2022-08-08 18:09:16 +00:00
// simulate an arbitrary side effect
let b = Rc::new(RefCell::new(String::new()));
create_isomorphic_effect(cx, {
2022-08-08 18:09:16 +00:00
let b = b.clone();
move |_| {
2022-08-08 18:09:16 +00:00
let formatted = format!("Value is {}", cx.untrack(a));
*b.borrow_mut() = formatted;
}
});
assert_eq!(a(), -1);
assert_eq!(b.borrow().as_str(), "Value is -1");
set_a(1);
2022-08-08 18:09:16 +00:00
assert_eq!(a(), 1);
assert_eq!(b.borrow().as_str(), "Value is -1");
})
.dispose()
2022-08-08 18:09:16 +00:00
}
2023-03-20 12:29:18 +00:00
#[cfg(not(feature = "stable"))]
#[test]
fn batching_actually_batches() {
use std::{cell::Cell, rc::Rc};
create_scope(create_runtime(), |cx| {
let first_name = create_rw_signal(cx, "Greg".to_string());
let last_name = create_rw_signal(cx, "Johnston".to_string());
// simulate an arbitrary side effect
let count = Rc::new(Cell::new(0));
create_isomorphic_effect(cx, {
let count = count.clone();
move |_| {
_ = first_name();
_ = last_name();
count.set(count.get() + 1);
}
});
// runs once initially
assert_eq!(count.get(), 1);
// individual updates run effect once each
first_name.set("Alice".to_string());
assert_eq!(count.get(), 2);
last_name.set("Smith".to_string());
assert_eq!(count.get(), 3);
// batched effect only runs twice
cx.batch(move || {
first_name.set("Bob".to_string());
last_name.set("Williams".to_string());
});
assert_eq!(count.get(), 4);
})
.dispose()
}