Skip to content

Commit

Permalink
fix: make auto-added Popover work with PreserveOnRefresh (#6796) (#6799)
Browse files Browse the repository at this point in the history
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
vaadin-bot and web-padawan authored Nov 8, 2024
1 parent c7e2501 commit 1d81219
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,8 @@ private void onTargetAttach(UI ui) {
if (target != null) {
ui.beforeClientResponse(ui, context -> {
if (getElement().getNode().getParent() == null) {
// Remove the popover from its current state tree
getElement().removeFromTree(false);
ui.addToModalComponent(this);
autoAddedToTheUi = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
*/
public class PopoverAutoAddTest {
private UI ui = new UI();
private VaadinSession session;

@Before
public void setup() {
UI.setCurrent(ui);

VaadinSession session = Mockito.mock(VaadinSession.class);
session = Mockito.mock(VaadinSession.class);
Mockito.when(session.hasLock()).thenReturn(true);
ui.getInternals().setSession(session);
}
Expand Down Expand Up @@ -133,6 +134,25 @@ public void setTarget_changeToDetachedTarget_autoRemoved() {
Assert.assertNull(popover.getElement().getParent());
}

@Test
public void setTarget_changeUI_autoAdded() {
Popover popover = new Popover();
Div target = new Div();
popover.setTarget(target);
ui.add(target);
fakeClientResponse();

// Create a new UI and move the component to it (@PreserveOnRefresh)
ui = new UI();
UI.setCurrent(ui);
ui.getInternals().setSession(session);
target.getElement().removeFromTree(false);
ui.add(target);

fakeClientResponse();
Assert.assertEquals(ui.getElement(), popover.getElement().getParent());
}

private void fakeClientResponse() {
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
ui.getInternals().getStateTree().collectChanges(ignore -> {
Expand Down

0 comments on commit 1d81219

Please sign in to comment.