|
| 1 | +"""Unit tests for the non-modal view-pop target computation. |
| 2 | +
|
| 3 | +The `Router`'s default `on_view_pop` handler navigates to the previous |
| 4 | +*view entry*'s resolved URL when the top view is popped. It must derive |
| 5 | +that target from the chain's view entries (`_split_chain_into_view_levels`) |
| 6 | +rather than `chain[-2]`: an intermediate `outlet=True` layout sits in the |
| 7 | +chain between two views but is NOT a view of its own, and its resolved URL |
| 8 | +can equal the current view's URL. Using `chain[-2]` there would navigate to |
| 9 | +the URL we're already at — a no-op that leaves the page route stranded so |
| 10 | +the next navigation to the same URL does nothing. |
| 11 | +
|
| 12 | +This replicates the handler's target computation (the handler itself is a |
| 13 | +closure inside `Router`) the same way `test_router_modal` replicates the |
| 14 | +modal-index lookup. |
| 15 | +""" |
| 16 | + |
| 17 | +from flet.components.router import ( |
| 18 | + Route, |
| 19 | + _match_routes, |
| 20 | + _split_chain_into_view_levels, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _dummy(): # placeholder component — Route requires a callable |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +def _pop_target(chain): |
| 29 | + """Replicates the Router's non-modal pop-target computation.""" |
| 30 | + if not chain: |
| 31 | + return None |
| 32 | + _, view_entries = _split_chain_into_view_levels(chain) |
| 33 | + if len(view_entries) > 1: |
| 34 | + parent_match = view_entries[-2][0] |
| 35 | + return parent_match.resolved_path or parent_match.full_path or "/" |
| 36 | + return None |
| 37 | + |
| 38 | + |
| 39 | +# Mirrors examples/apps/router/nested_outlet_views: a Home route whose |
| 40 | +# child is an `outlet=True` products layout wrapping its own children. |
| 41 | +_routes = [ |
| 42 | + Route( |
| 43 | + component=_dummy, |
| 44 | + children=[ |
| 45 | + Route( |
| 46 | + path="products", |
| 47 | + component=_dummy, |
| 48 | + outlet=True, |
| 49 | + children=[ |
| 50 | + Route( |
| 51 | + component=_dummy, |
| 52 | + children=[ |
| 53 | + Route(path=":pid", component=_dummy), |
| 54 | + ], |
| 55 | + ), |
| 56 | + ], |
| 57 | + ), |
| 58 | + ], |
| 59 | + ), |
| 60 | +] |
| 61 | + |
| 62 | + |
| 63 | +def test_pop_from_outlet_layout_view_skips_layout(): |
| 64 | + """`/products` builds views [Home(/), Products(/products)] but the |
| 65 | + chain is [Home, products-layout, ProductsList]. Popping the Products |
| 66 | + view must land on Home (`/`) — not the layout's own `/products`, |
| 67 | + which `chain[-2]` would wrongly yield.""" |
| 68 | + chain = _match_routes(_routes, "/products") |
| 69 | + |
| 70 | + assert chain is not None |
| 71 | + # The layout really is `chain[-2]` and shares the leaf's resolved URL, |
| 72 | + # which is exactly the trap the view-entry computation avoids. |
| 73 | + assert chain[-2].route.outlet is True |
| 74 | + assert chain[-2].resolved_path == "/products" |
| 75 | + |
| 76 | + assert _pop_target(chain) == "/" |
| 77 | + |
| 78 | + |
| 79 | +def test_pop_from_nested_product_lands_on_products_list(): |
| 80 | + """`/products/1` builds views [Home(/), ProductsList(/products), |
| 81 | + ProductDetails(/products/1)]. Popping the details view lands on the |
| 82 | + products list (`/products`).""" |
| 83 | + chain = _match_routes(_routes, "/products/1") |
| 84 | + |
| 85 | + assert chain is not None |
| 86 | + assert _pop_target(chain) == "/products" |
| 87 | + |
| 88 | + |
| 89 | +def test_single_view_has_no_pop_target(): |
| 90 | + """At `/` the stack is one view — nothing to pop to.""" |
| 91 | + chain = _match_routes(_routes, "/") |
| 92 | + |
| 93 | + assert chain is not None |
| 94 | + assert _pop_target(chain) is None |
0 commit comments