mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
examples: test business logic for counter_without_macros (#927)
This commit is contained in:
parent
0a7dbb0ca4
commit
27b5991ee3
4 changed files with 90 additions and 6 deletions
|
@ -17,6 +17,7 @@ console_error_panic_hook = "0.1.7"
|
|||
wasm-bindgen = "0.2.84"
|
||||
wasm-bindgen-test = "0.3.34"
|
||||
pretty_assertions = "1.3.0"
|
||||
rstest = "0.17.0"
|
||||
|
||||
[dev-dependencies.web-sys]
|
||||
features = ["HtmlElement", "XPathResult"]
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
[env]
|
||||
CARGO_MAKE_WASM_TEST_ARGS = "--headless --chrome"
|
||||
|
||||
[tasks.test-all]
|
||||
dependencies = ["test", "web-test"]
|
||||
|
||||
[tasks.web-test]
|
||||
command = "cargo"
|
||||
args = ["make", "wasm-pack-test"]
|
||||
|
|
|
@ -2,8 +2,8 @@ use leptos::{ev, html::*, *};
|
|||
|
||||
/// A simple counter view.
|
||||
// A component is really just a function call: it runs once to create the DOM and reactive system
|
||||
pub fn counter(cx: Scope, initial_value: i32, step: i32) -> impl IntoView {
|
||||
let (value, set_value) = create_signal(cx, initial_value);
|
||||
pub fn counter(cx: Scope, initial_value: i32, step: u32) -> impl IntoView {
|
||||
let (count, set_count) = create_signal(cx, Count::new(initial_value, step));
|
||||
|
||||
// elements are created by calling a function with a Scope argument
|
||||
// the function name is the same as the HTML tag name
|
||||
|
@ -16,13 +16,13 @@ pub fn counter(cx: Scope, initial_value: i32, step: i32) -> impl IntoView {
|
|||
// typed events found in leptos::ev
|
||||
// 1) prevent typos in event names
|
||||
// 2) allow for correct type inference in callbacks
|
||||
.on(ev::click, move |_| set_value.update(|value| *value = 0))
|
||||
.on(ev::click, move |_| set_count.update(|count| count.clear()))
|
||||
.child("Clear"),
|
||||
)
|
||||
.child(
|
||||
button(cx)
|
||||
.on(ev::click, move |_| {
|
||||
set_value.update(|value| *value -= step)
|
||||
set_count.update(|count| count.decrease())
|
||||
})
|
||||
.child("-1"),
|
||||
)
|
||||
|
@ -31,14 +31,45 @@ pub fn counter(cx: Scope, initial_value: i32, step: i32) -> impl IntoView {
|
|||
.child("Value: ")
|
||||
// reactive values are passed to .child() as a tuple
|
||||
// (Scope, [child function]) so an effect can be created
|
||||
.child((cx, move || value.get()))
|
||||
.child(move || count.get().value())
|
||||
.child("!"),
|
||||
)
|
||||
.child(
|
||||
button(cx)
|
||||
.on(ev::click, move |_| {
|
||||
set_value.update(|value| *value += step)
|
||||
set_count.update(|count| count.increase())
|
||||
})
|
||||
.child("+1"),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Count {
|
||||
value: i32,
|
||||
step: i32,
|
||||
}
|
||||
|
||||
impl Count {
|
||||
pub fn new(value: i32, step: u32) -> Self {
|
||||
Count {
|
||||
value,
|
||||
step: step as i32,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value(&self) -> i32 {
|
||||
self.value
|
||||
}
|
||||
|
||||
pub fn increase(&mut self) {
|
||||
self.value += self.step;
|
||||
}
|
||||
|
||||
pub fn decrease(&mut self) {
|
||||
self.value += -self.step;
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.value = 0;
|
||||
}
|
||||
}
|
||||
|
|
49
examples/counter_without_macros/tests/business.rs
Normal file
49
examples/counter_without_macros/tests/business.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
mod count {
|
||||
use counter_without_macros::Count;
|
||||
use pretty_assertions::assert_eq;
|
||||
use rstest::rstest;
|
||||
|
||||
#[rstest]
|
||||
#[case(-2, 1)]
|
||||
#[case(-1, 1)]
|
||||
#[case(0, 1)]
|
||||
#[case(1, 1)]
|
||||
#[case(2, 1)]
|
||||
#[case(3, 2)]
|
||||
#[case(4, 3)]
|
||||
fn should_increase_count(#[case] initial_value: i32, #[case] step: u32) {
|
||||
let mut count = Count::new(initial_value, step);
|
||||
count.increase();
|
||||
assert_eq!(count.value(), initial_value + step as i32);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(-2, 1)]
|
||||
#[case(-1, 1)]
|
||||
#[case(0, 1)]
|
||||
#[case(1, 1)]
|
||||
#[case(2, 1)]
|
||||
#[case(3, 2)]
|
||||
#[case(4, 3)]
|
||||
#[trace]
|
||||
fn should_decrease_count(#[case] initial_value: i32, #[case] step: u32) {
|
||||
let mut count = Count::new(initial_value, step);
|
||||
count.decrease();
|
||||
assert_eq!(count.value(), initial_value - step as i32);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(-2, 1)]
|
||||
#[case(-1, 1)]
|
||||
#[case(0, 1)]
|
||||
#[case(1, 1)]
|
||||
#[case(2, 1)]
|
||||
#[case(3, 2)]
|
||||
#[case(4, 3)]
|
||||
#[trace]
|
||||
fn should_clear_count(#[case] initial_value: i32, #[case] step: u32) {
|
||||
let mut count = Count::new(initial_value, step);
|
||||
count.clear();
|
||||
assert_eq!(count.value(), 0);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue