Skip to content

Commit

Permalink
fix: always fetch reactEnabled from service (#20125) (#20131)
Browse files Browse the repository at this point in the history
* fix: always fetch reactEnabled from service

Co-authored-by: Teppo Kurki <teppo.kurki@vaadin.com>
  • Loading branch information
vaadin-bot and tepi authored Oct 3, 2024
1 parent 46043b5 commit 736ecc8
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 13 deletions.
3 changes: 2 additions & 1 deletion flow-server/src/main/java/com/vaadin/flow/component/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2056,7 +2056,8 @@ private void handleNavigation(Location location,
} catch (Exception exception) {
handleExceptionNavigation(location, exception);
} finally {
if (getInternals().getSession().getConfiguration().isReactEnabled()
if (getInternals().getSession().getService()
.getDeploymentConfiguration().isReactEnabled()
&& getInternals().getContinueNavigationAction() != null) {
getInternals().clearLastHandledNavigation();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ protected Optional<Integer> handleTriggeredBeforeEvent(

@Override
protected boolean shouldPushHistoryState(NavigationEvent event) {
if (event.getUI().getInternals().getSession().getConfiguration()
.isReactEnabled()) {
if (event.getUI().getInternals().getSession().getService()
.getDeploymentConfiguration().isReactEnabled()) {
return super.shouldPushHistoryState(event);
}
if (NavigationTrigger.CLIENT_SIDE.equals(event.getTrigger())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ public void showRouteTarget(Location viewLocation, Component target,
}
previous = current;
}
if (getSession().getConfiguration().isReactEnabled()
if (getSession().getService().getDeploymentConfiguration()
.isReactEnabled()
&& getRouter().getRegistry()
.getNavigationTarget(viewLocation.getPath()).isEmpty()
&& target instanceof RouterLayout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ public void pushState(JsonValue state, Location location,
location);
// Second parameter is title which is currently ignored according to
// https://developer.mozilla.org/en-US/docs/Web/API/History_API
if (ui.getSession().getConfiguration().isReactEnabled()) {
if (ui.getSession().getService().getDeploymentConfiguration()
.isReactEnabled()) {
ui.getPage().executeJs(
"window.dispatchEvent(new CustomEvent('vaadin-navigate', { detail: { state: $0, url: $1, replace: false, callback: $2 } }));",
state, pathWithQueryParameters, callback);
Expand Down Expand Up @@ -314,7 +315,8 @@ public void replaceState(JsonValue state, Location location,
location);
// Second parameter is title which is currently ignored according to
// https://developer.mozilla.org/en-US/docs/Web/API/History_API
if (ui.getSession().getConfiguration().isReactEnabled()) {
if (ui.getSession().getService().getDeploymentConfiguration()
.isReactEnabled()) {
ui.getPage().executeJs(
"window.dispatchEvent(new CustomEvent('vaadin-navigate', { detail: { state: $0, url: $1, replace: true, callback: $2 } }));",
state, pathWithQueryParameters, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public void proceed() {
// If the server updates the url also we will get 2 history
// changes instead of 1.
if (NavigationTrigger.ROUTER_LINK.equals(event.getTrigger())
&& !event.getUI().getSession().getConfiguration()
&& !event.getUI().getSession().getService()
.getDeploymentConfiguration()
.isReactEnabled()) {
event = new NavigationEvent(event.getSource(),
event.getLocation(), event.getUI(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ private void pushHistoryStateIfNeeded(NavigationEvent event, UI ui) {
}

ui.getInternals().setLastHandledNavigation(event.getLocation());
} else if (ui.getInternals().getSession().getConfiguration()
.isReactEnabled()) {
} else if (ui.getInternals().getSession().getService()
.getDeploymentConfiguration().isReactEnabled()) {
if (shouldPushHistoryState(event)) {
pushHistoryState(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.VaadinSessionState;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.tests.util.MockDeploymentConfiguration;

public class JavaScriptBootstrapUITest {

Expand Down Expand Up @@ -419,7 +420,8 @@ public void should_update_pushState_when_navigationHasBeenAlreadyStarted() {
.mock(DeploymentConfiguration.class);
Mockito.when(internals.getSession()).thenReturn(session);
Mockito.when(session.getConfiguration()).thenReturn(configuration);
Mockito.when(configuration.isReactEnabled()).thenReturn(false);
((MockDeploymentConfiguration) session.getService()
.getDeploymentConfiguration()).setReactEnabled(false);

Mockito.when(internals.hasLastHandledLocation()).thenReturn(true);
Location lastLocation = new Location("clean");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.vaadin.flow.component.UI;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;

import elemental.json.Json;
Expand Down Expand Up @@ -67,6 +68,7 @@ public PendingJavaScriptResult executeJs(String expression,
private TestPage page = new TestPage(ui);
private History history;

private VaadinService service = Mockito.mock(VaadinService.class);
private VaadinSession session = Mockito.mock(VaadinSession.class);
private DeploymentConfiguration configuration;

Expand All @@ -80,6 +82,10 @@ public PendingJavaScriptResult executeJs(String expression,
public void setup() {
history = new History(ui);
configuration = Mockito.mock(DeploymentConfiguration.class);

Mockito.when(session.getService()).thenReturn(service);
Mockito.when(service.getDeploymentConfiguration())
.thenReturn(configuration);
Mockito.when(session.getConfiguration()).thenReturn(configuration);
Mockito.when(configuration.isReactEnabled()).thenReturn(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
import com.vaadin.flow.shared.Registration;
import com.vaadin.tests.util.MockDeploymentConfiguration;

import elemental.json.Json;
import elemental.json.JsonObject;
Expand Down Expand Up @@ -2934,6 +2935,9 @@ public void repeatedly_navigating_to_same_ur_through_ui_navigate_should_not_loop
@Test
public void ui_navigate_should_only_have_one_history_marking_on_loop()
throws InvalidRouteConfigurationException {
((MockDeploymentConfiguration) ui.getSession().getService()
.getDeploymentConfiguration()).setReactEnabled(false);

setNavigationTargets(LoopByUINavigate.class);

ui.navigate("loop");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,8 @@ private NavigationState navigationStateFromTarget(
public void handle_variousInputs_checkPushStateShouldBeCalledOrNot() {
// given a service with instantiator
MockVaadinServletService service = createMockServiceWithInstantiator();

((MockDeploymentConfiguration) service.getDeploymentConfiguration())
.setReactEnabled(false);
// given a locked session
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
MockDeploymentConfiguration configuration = new MockDeploymentConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void StateView() {
@Override
protected void onAttach(AttachEvent attachEvent) {
Span enabled = new Span("React enabled: " + getUI().get().getSession()
.getConfiguration().isReactEnabled());
.getService().getDeploymentConfiguration().isReactEnabled());
enabled.setId(ENABLED_SPAN);

File baseDir = attachEvent.getSession().getConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ protected Element createPushStateButtons(String target) {
Element button = ElementFactory.createButton(target).setAttribute("id",
target);
String historyPush = "window.history.pushState(null, null, event.target.textContent)";
if (VaadinSession.getCurrent().getConfiguration().isReactEnabled()) {
if (VaadinSession.getCurrent().getService().getDeploymentConfiguration()
.isReactEnabled()) {
historyPush = "window.dispatchEvent(new CustomEvent('vaadin-navigate', { detail: { url: event.target.textContent, replace: false } }))";
}
button.addEventListener("click", e -> {
Expand Down

0 comments on commit 736ecc8

Please sign in to comment.