test(counters_stable/wasm): enter count (#1307)

This commit is contained in:
Joseph Cruz 2023-07-08 12:07:11 -04:00 committed by GitHub
parent aef7c4ce8e
commit f6a272498d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 60 additions and 14 deletions

View file

@ -16,5 +16,12 @@ wasm-bindgen-test = "0.3.37"
pretty_assertions = "1.3.0"
[dev-dependencies.web-sys]
features = ["Element", "HtmlElement", "XPathResult"]
features = [
"Event",
"EventInit",
"EventTarget",
"HtmlElement",
"HtmlInputElement",
"XPathResult",
]
version = "0.3.64"

View file

@ -14,7 +14,6 @@ test.describe("Add 1000 Counters", () => {
await ui.addOneThousandCounters();
await ui.addOneThousandCounters();
await expect(ui.total).toHaveText("0");
await expect(ui.counters).toHaveText("3000");
});
});

View file

@ -10,7 +10,6 @@ test.describe("Add Counter", () => {
await ui.addCounter();
await ui.addCounter();
await expect(ui.total).toHaveText("0");
await expect(ui.counters).toHaveText("3");
});
});

View file

@ -12,6 +12,5 @@ test.describe("Decrement Count", () => {
await ui.decrementCount();
await expect(ui.total).toHaveText("-3");
await expect(ui.counters).toHaveText("1");
});
});

View file

@ -26,6 +26,5 @@ test.describe("Enter Count", () => {
await ui.enterCount("50", 1);
await expect(ui.total).toHaveText("250");
await expect(ui.counters).toHaveText("3");
});
});

View file

@ -12,6 +12,5 @@ test.describe("Increment Count", () => {
await ui.incrementCount();
await expect(ui.total).toHaveText("3");
await expect(ui.counters).toHaveText("1");
});
});

View file

@ -12,7 +12,6 @@ test.describe("Remove Counter", () => {
await ui.removeCounter(1);
await expect(ui.total).toHaveText("0");
await expect(ui.counters).toHaveText("2");
});
});

View file

@ -97,7 +97,7 @@ fn Counter(
view! { cx,
<li>
<button data-testid="decrement_count" on:click=move |_| set_value.update(move |value| *value -= 1)>"-1"</button>
<input type="text"
<input data-testid="counter_input" type="text"
prop:value={move || value.get().to_string()}
on:input=input
/>

View file

@ -13,6 +13,5 @@ fn should_increase_the_number_of_counters() {
ui::add_1k_counters();
// Then
assert_eq!(ui::total(), 0);
assert_eq!(ui::counters(), 3000);
}

View file

@ -13,6 +13,5 @@ fn should_increase_the_number_of_counters() {
ui::add_counter();
// Then
assert_eq!(ui::total(), 0);
assert_eq!(ui::counters(), 3);
}

View file

@ -15,5 +15,4 @@ fn should_decrease_the_total_count() {
// Then
assert_eq!(ui::total(), -3);
assert_eq!(ui::counters(), 1);
}

View file

@ -0,0 +1,34 @@
use super::*;
use crate::counters_page as ui;
use pretty_assertions::assert_eq;
#[wasm_bindgen_test]
fn should_increase_the_total_count() {
// Given
ui::view_counters();
ui::add_counter();
// When
ui::enter_count(1, 5);
// Then
assert_eq!(ui::total(), 5);
}
#[wasm_bindgen_test]
fn should_decrease_the_total_count() {
// Given
ui::view_counters();
ui::add_counter();
ui::add_counter();
ui::add_counter();
// When
ui::enter_count(1, 100);
ui::enter_count(2, 100);
ui::enter_count(3, 100);
ui::enter_count(1, 50);
// Then
assert_eq!(ui::total(), 250);
}

View file

@ -1,7 +1,7 @@
use counters_stable::Counters;
use leptos::*;
use wasm_bindgen::JsCast;
use web_sys::{Element, HtmlElement};
use web_sys::{Element, Event, EventInit, HtmlElement, HtmlInputElement};
// Actions
@ -21,6 +21,15 @@ pub fn decrement_counter(index: u32) {
counter_html_element(index, "decrement_count").click();
}
pub fn enter_count(index: u32, count: i32) {
let input = counter_input_element(index, "counter_input");
input.set_value(count.to_string().as_str());
let mut event_init = EventInit::new();
event_init.bubbles(true);
let event = Event::new_with_event_init_dict("input", &event_init).unwrap();
input.dispatch_event(&event).unwrap();
}
pub fn increment_counter(index: u32) {
counter_html_element(index, "increment_count").click();
}
@ -65,6 +74,12 @@ fn counter_html_element(index: u32, text: &str) -> HtmlElement {
.unwrap()
}
fn counter_input_element(index: u32, text: &str) -> HtmlInputElement {
counter_element(index, text)
.dyn_into::<HtmlInputElement>()
.unwrap()
}
fn data_test_id(id: &str) -> String {
let selector = format!("[data-testid=\"{}\"]", id);
leptos::document()

View file

@ -15,5 +15,4 @@ fn should_increase_the_total_count() {
// Then
assert_eq!(ui::total(), 3);
assert_eq!(ui::counters(), 1);
}

View file

@ -5,6 +5,7 @@ pub mod add_1k_counters;
pub mod add_counter;
pub mod clear_counters;
pub mod decrement_counter;
pub mod enter_count;
pub mod increment_counter;
pub mod remove_counter;
pub mod view_counters;

View file

@ -14,6 +14,5 @@ fn should_decrement_the_number_of_counters() {
ui::remove_counter(2);
// Then
assert_eq!(ui::total(), 0);
assert_eq!(ui::counters(), 2);
}