|
37 | 37 | import org.junit.Rule; |
38 | 38 | import org.junit.Test; |
39 | 39 | import org.junit.rules.ExpectedException; |
| 40 | +import org.mockito.MockedStatic; |
40 | 41 | import org.mockito.Mockito; |
41 | 42 |
|
42 | 43 | import com.vaadin.flow.component.Component; |
|
65 | 66 | import com.vaadin.flow.router.PreserveOnRefresh; |
66 | 67 | import com.vaadin.flow.router.QueryParameters; |
67 | 68 | import com.vaadin.flow.router.Route; |
| 69 | +import com.vaadin.flow.router.RouteAlias; |
68 | 70 | import com.vaadin.flow.router.RouteConfiguration; |
69 | 71 | import com.vaadin.flow.router.Router; |
70 | 72 | import com.vaadin.flow.router.RouterLayout; |
|
77 | 79 | import com.vaadin.flow.server.RouteRegistry; |
78 | 80 | import com.vaadin.flow.server.ServiceException; |
79 | 81 | import com.vaadin.flow.server.WrappedSession; |
| 82 | +import com.vaadin.flow.server.menu.MenuRegistry; |
80 | 83 | import com.vaadin.flow.server.startup.ApplicationRouteRegistry; |
81 | 84 | import com.vaadin.tests.util.AlwaysLockedVaadinSession; |
82 | 85 | import com.vaadin.tests.util.MockDeploymentConfiguration; |
@@ -323,6 +326,21 @@ private static class SingleView extends Component { |
323 | 326 | } |
324 | 327 | } |
325 | 328 |
|
| 329 | + @Route(value = "/:samplePersonID?/:action?(edit)") |
| 330 | + @RouteAlias(value = "") |
| 331 | + @Tag("div") |
| 332 | + private static class RootRouteWithParam extends Component |
| 333 | + implements BeforeEnterObserver { |
| 334 | + RootRouteWithParam() { |
| 335 | + addAttachListener(e -> viewAttachCount.getAndIncrement()); |
| 336 | + } |
| 337 | + |
| 338 | + @Override |
| 339 | + public void beforeEnter(BeforeEnterEvent event) { |
| 340 | + beforeEnterCount.getAndIncrement(); |
| 341 | + } |
| 342 | + } |
| 343 | + |
326 | 344 | @Test |
327 | 345 | public void handle_preserveOnRefreshAndWindowNameNotKnown_clientSideCallTriggered() { |
328 | 346 | // given a service with instantiator |
@@ -636,6 +654,7 @@ public void handle_preserveOnRefresh_sameUI_uiIsNotClosed_childrenAreNotRemoved( |
636 | 654 |
|
637 | 655 | private static AtomicInteger layoutAttachCount; |
638 | 656 | private static AtomicInteger viewAttachCount; |
| 657 | + private static AtomicInteger beforeEnterCount; |
639 | 658 | private static String layoutUUID; |
640 | 659 | private static String viewUUID; |
641 | 660 |
|
@@ -747,6 +766,52 @@ public void handle_normalView_refreshCurrentRouteRecreatesComponents() { |
747 | 766 |
|
748 | 767 | } |
749 | 768 |
|
| 769 | + @Test |
| 770 | + public void handle_clientNavigation_withMatchingFlowRoute() { |
| 771 | + viewAttachCount = new AtomicInteger(); |
| 772 | + beforeEnterCount = new AtomicInteger(); |
| 773 | + |
| 774 | + // given a service with instantiator |
| 775 | + MockVaadinServletService service = createMockServiceWithInstantiator(); |
| 776 | + |
| 777 | + // given a locked session |
| 778 | + MockVaadinSession session = new AlwaysLockedVaadinSession(service); |
| 779 | + session.setConfiguration(new MockDeploymentConfiguration()); |
| 780 | + |
| 781 | + // given a NavigationStateRenderer mapping to PreservedNestedView |
| 782 | + Router router = session.getService().getRouter(); |
| 783 | + NavigationStateRenderer renderer = new NavigationStateRenderer( |
| 784 | + new NavigationStateBuilder(router) |
| 785 | + .withTarget(RootRouteWithParam.class).withPath("") |
| 786 | + .build()); |
| 787 | + router.getRegistry().setRoute("", RootRouteWithParam.class, null); |
| 788 | + |
| 789 | + MockUI ui = new MockUI(session); |
| 790 | + |
| 791 | + renderer.handle(new NavigationEvent(router, new Location(""), ui, |
| 792 | + NavigationTrigger.PAGE_LOAD)); |
| 793 | + |
| 794 | + Assert.assertEquals(1, beforeEnterCount.get()); |
| 795 | + Assert.assertEquals(1, viewAttachCount.get()); |
| 796 | + |
| 797 | + ui.getInternals().clearLastHandledNavigation(); |
| 798 | + |
| 799 | + try (MockedStatic<MenuRegistry> menuRegistry = Mockito |
| 800 | + .mockStatic(MenuRegistry.class)) { |
| 801 | + menuRegistry.when( |
| 802 | + () -> MenuRegistry.hasClientRoute("client-route", true)) |
| 803 | + .thenReturn(true); |
| 804 | + |
| 805 | + // This should not call attach or beforeEnter on root route |
| 806 | + renderer.handle( |
| 807 | + new NavigationEvent(router, new Location("client-route"), |
| 808 | + ui, NavigationTrigger.CLIENT_SIDE)); |
| 809 | + |
| 810 | + Assert.assertEquals(1, beforeEnterCount.get()); |
| 811 | + Assert.assertEquals(1, viewAttachCount.get()); |
| 812 | + } |
| 813 | + } |
| 814 | + |
750 | 815 | private MockVaadinServletService createMockServiceWithInstantiator() { |
751 | 816 | MockVaadinServletService service = new MockVaadinServletService(); |
752 | 817 | service.init(new MockInstantiator() { |
|
0 commit comments