|
| 1 | +import pytest |
| 2 | + |
| 3 | +from dash import Dash, dcc, html |
| 4 | +from dash.testing.wait import until |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize( |
| 8 | + "link_props,scrolls_to_top", |
| 9 | + [({}, True), ({"scrollToTop": False}, False)], |
| 10 | + ids=["default", "disabled"], |
| 11 | +) |
| 12 | +def test_lisc001_scroll_to_top(dash_dcc, link_props, scrolls_to_top): |
| 13 | + behavior = "enabled (default)" if scrolls_to_top else "disabled" |
| 14 | + app = Dash(__name__) |
| 15 | + app.layout = html.Div( |
| 16 | + [ |
| 17 | + html.Div( |
| 18 | + [ |
| 19 | + html.H1("TOP OF PAGE", id="top-marker"), |
| 20 | + html.P("The default Link behavior returns here after a click."), |
| 21 | + ], |
| 22 | + id="top-section", |
| 23 | + style={ |
| 24 | + "height": "100vh", |
| 25 | + "padding": "1rem", |
| 26 | + "boxSizing": "border-box", |
| 27 | + }, |
| 28 | + ), |
| 29 | + html.Div( |
| 30 | + [ |
| 31 | + html.H1("BOTTOM OF PAGE", id="bottom-marker"), |
| 32 | + html.P(f"scrollToTop is {behavior}."), |
| 33 | + dcc.Link( |
| 34 | + "Click to navigate", |
| 35 | + href="/test-link", |
| 36 | + id="test-link", |
| 37 | + **link_props, |
| 38 | + ), |
| 39 | + ], |
| 40 | + id="bottom-section", |
| 41 | + style={ |
| 42 | + "height": "100vh", |
| 43 | + "padding": "1rem", |
| 44 | + "boxSizing": "border-box", |
| 45 | + "borderTop": "1px solid", |
| 46 | + }, |
| 47 | + ), |
| 48 | + ] |
| 49 | + ) |
| 50 | + |
| 51 | + dash_dcc.start_server(app) |
| 52 | + |
| 53 | + test_link = dash_dcc.wait_for_element("#test-link") |
| 54 | + dash_dcc.driver.execute_script( |
| 55 | + "document.getElementById('bottom-section').scrollIntoView()" |
| 56 | + ) |
| 57 | + until(lambda: dash_dcc.driver.execute_script("return window.scrollY") > 0, 3) |
| 58 | + initial_scroll_position = dash_dcc.driver.execute_script("return window.scrollY") |
| 59 | + |
| 60 | + test_link.click() |
| 61 | + |
| 62 | + until(lambda: dash_dcc.driver.current_url.endswith("/test-link"), 3) |
| 63 | + expected_scroll_position = 0 if scrolls_to_top else initial_scroll_position |
| 64 | + until( |
| 65 | + lambda: dash_dcc.driver.execute_script("return window.scrollY") |
| 66 | + == expected_scroll_position, |
| 67 | + 3, |
| 68 | + ) |
| 69 | + |
| 70 | + assert dash_dcc.get_logs() == [] |
0 commit comments