Skip to content

Commit

Permalink
fix: update browser URL on preserve on refresh navigation (#19718) (C…
Browse files Browse the repository at this point in the history
…P: 24.3) (#19720)

* fix: update browser URL on preserve on refresh navigation (#19718)

Programmatic self navigation with query parameters on view with preserve on
refresh do not update the URL in the browser.
This change fixes the regression introduced by #15331, updating push
history also for navigation events with trigger REFRESH.

Fixes #19701

* restore removed imports

---------

Co-authored-by: Marco Collovati <marco@vaadin.com>
  • Loading branch information
vaadin-bot and mcollovati authored Jul 24, 2024
1 parent d00357a commit 94b67dd
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ protected void pushHistoryState(NavigationEvent event) {
}

protected boolean shouldPushHistoryState(NavigationEvent event) {
return NavigationTrigger.UI_NAVIGATE.equals(event.getTrigger());
return NavigationTrigger.UI_NAVIGATE.equals(event.getTrigger())
|| NavigationTrigger.REFRESH.equals(event.getTrigger());
}

private boolean isRouterLinkNotFoundNavigationError(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.flow.uitest.ui;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.ElementFactory;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.Route;

@Route(value = PreserveOnRefreshNavigationView.VIEW_PATH)
@PreserveOnRefresh
public class PreserveOnRefreshNavigationView extends Div {

static final String VIEW_PATH = "com.vaadin.flow.uitest.ui.PreserveOnRefreshNavigationView";

public PreserveOnRefreshNavigationView() {
add(createNavigationButton("one"));
add(createNavigationButton("two"));
add(createNavigationButton("three"));

getElement().appendChild(createRouterLink("one"),
createRouterLink("two"), createRouterLink("three"));
}

private NativeButton createNavigationButton(String param) {
NativeButton button = new NativeButton("navigate to " + param,
ev -> selfNavigate(param));
button.setId("button-" + param);
return button;
}

private void selfNavigate(String param) {
UI.getCurrent().navigate(PreserveOnRefreshNavigationView.class,
QueryParameters.of("param", param));
}

private Element createRouterLink(String param) {
Element routerLink = ElementFactory.createRouterLink(
VIEW_PATH + "?param=" + param, "link to " + param);
routerLink.setAttribute("id", "link-" + param);
return routerLink;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.flow.uitest.ui;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;

public class PreserveOnRefreshNavigationIT extends ChromeBrowserTest {

@Test
public void routerLink_selfNavigationWithQueryParams_urlChanges() {
open();
$(TestBenchElement.class).id("link-one").click();
Assert.assertTrue(driver.getCurrentUrl().contains("?param=one"));
$(TestBenchElement.class).id("link-two").click();
Assert.assertTrue(driver.getCurrentUrl().contains("?param=two"));
$(TestBenchElement.class).id("link-three").click();
Assert.assertTrue(driver.getCurrentUrl().contains("?param=three"));
}

@Test
public void programmaticNavigation_selfNavigationWithQueryParams_urlChanges() {
open();
$(TestBenchElement.class).id("button-one").click();
Assert.assertTrue(driver.getCurrentUrl().contains("?param=one"));
$(TestBenchElement.class).id("button-two").click();
Assert.assertTrue(driver.getCurrentUrl().contains("?param=two"));
$(TestBenchElement.class).id("button-three").click();
Assert.assertTrue(driver.getCurrentUrl().contains("?param=three"));
}

}

0 comments on commit 94b67dd

Please sign in to comment.