Skip to content

Commit

Permalink
Fix arrow visual test
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Apr 5, 2024
1 parent 1290b03 commit db81054
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
11 changes: 11 additions & 0 deletions packages/leptos/tests/playwright.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn playwright() {
let repository_dom_path = repository_path.join("packages/dom");
let repository_package_json_path = repository_dom_path.join("package.json");
let repository_playwright_config_path = repository_dom_path.join("playwright.config.ts");
let repository_arrow_test_path = repository_dom_path.join("test/functional/arrow.test.ts");

let visual_test_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/visual");

Expand Down Expand Up @@ -126,6 +127,16 @@ pub fn playwright() {
fs::write(repository_playwright_config_path, config_content)
.expect("Writing Playwright config file failed.");

let arrow_test_content = fs::read_to_string(repository_arrow_test_path.clone())
.expect("Reading arrow test file failed.")
.replace(
// Match React test behaviour
"await click(page, `[data-testid=\"arrow-padding-${arrowPadding}\"]`);",
"if (arrowPadding !== 0) { await click(page, `[data-testid=\"arrow-padding-${arrowPadding}\"]`); }",
);
fs::write(repository_arrow_test_path, arrow_test_content)
.expect("Writing arrow test file failed.");

let status = Command::new("pnpm")
.arg("run")
.arg("playwright")
Expand Down
8 changes: 4 additions & 4 deletions packages/leptos/tests/visual/src/spec/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ pub fn Arrow() -> impl IntoView {
rtl: None,
});

// Match React test behaviour
let placement_update_scroll = update_scroll.clone();
let padding_update_scroll = update_scroll.clone();
let add_offset_update_scroll = update_scroll.clone();
// _ = watch(placement, move |_, _, _| placement_update_scroll(), false);
// _ = watch(padding, move |_, _, _| padding_update_scroll(), false);
// _ = watch(add_offset, move |_, _, _| add_offset_update_scroll(), false);
let center_offset_update_scroll = update_scroll.clone();

view! {
<h1>Arrow</h1>
Expand Down Expand Up @@ -312,6 +309,7 @@ pub fn Arrow() -> impl IntoView {
each=|| [true, false]
key=|value| format!("{}", value)
children=move |value| {
let center_offset_update_scroll = center_offset_update_scroll.clone();
view! {
<button
data-testid=format!("centerOffset-{}", value)
Expand All @@ -332,6 +330,8 @@ pub fn Arrow() -> impl IntoView {
set_placement(Placement::Bottom);
set_padding(0);
}
// Match React test behaviour
center_offset_update_scroll();
}
>
{format!("{}", value)}
Expand Down
20 changes: 12 additions & 8 deletions packages/leptos/tests/visual/src/utils/use_scroll.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::rc::Rc;
use std::{ops::Deref, rc::Rc};

use floating_ui_leptos::{
dom::{get_overflow_ancestors, OverflowAncestor},
Expand Down Expand Up @@ -61,7 +61,6 @@ pub fn use_scroll(

let (ancestors, set_ancestors) = create_signal::<Vec<OverflowAncestor>>(vec![]);
let (scroll, set_scroll) = create_signal::<Option<(i32, i32)>>(None);
let is_update_required = create_rw_signal(false);

let local_update_update = update.clone();
let local_update_indicator_update = indicator_update_rc.clone();
Expand All @@ -75,11 +74,7 @@ pub fn use_scroll(
}));

let effect_local_update = local_update.clone();
create_effect(move |_| {
if is_update_required.get() {
is_update_required.set_untracked(false);
}

let effect = move || {
if let Some(reference) = reference_ref() {
let mut ancestors = get_overflow_ancestors(&reference, vec![], true);

Expand All @@ -102,8 +97,11 @@ pub fn use_scroll(
set_ancestors(ancestors);

if let Some(scroll) = scroll_ref() {
let h: &web_sys::HtmlDivElement = scroll.deref();
log::info!("{:?}", h);
let x = scroll.scroll_width() / 2 - scroll.offset_width() / 2;
let y = scroll.scroll_height() / 2 - scroll.offset_height() / 2;
log::info!("x {x} | y {y}");
scroll.set_scroll_top(y);
scroll.set_scroll_left(match rtl {
Some(true) => -x,
Expand All @@ -113,6 +111,12 @@ pub fn use_scroll(

update();
}
};
let effect_rc = Rc::new(effect);

let effect_effect = effect_rc.clone();
create_effect(move |_| {
effect_effect();
});

on_cleanup(move || {
Expand Down Expand Up @@ -144,7 +148,7 @@ pub fn use_scroll(
};

let update_scroll = move || {
is_update_required.set(true);
effect_rc();
};

UseScrollReturn {
Expand Down

0 comments on commit db81054

Please sign in to comment.