-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logic for Popover auto-adding on setting target (#6565)
- Loading branch information
1 parent
f0062f8
commit 7000581
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...adin-popover-flow/src/test/java/com/vaadin/flow/component/popover/PopoverAutoAddTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* 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.component.popover; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.server.VaadinSession; | ||
|
||
/** | ||
* @author Vaadin Ltd. | ||
*/ | ||
public class PopoverAutoAddTest { | ||
private UI ui = new UI(); | ||
|
||
@Before | ||
public void setup() { | ||
UI.setCurrent(ui); | ||
|
||
VaadinSession session = Mockito.mock(VaadinSession.class); | ||
Mockito.when(session.hasLock()).thenReturn(true); | ||
ui.getInternals().setSession(session); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
UI.setCurrent(null); | ||
} | ||
|
||
@Test | ||
public void setTarget_autoAdded() { | ||
Popover popover = new Popover(); | ||
Div target = new Div(); | ||
popover.setTarget(target); | ||
ui.add(target); | ||
|
||
fakeClientResponse(); | ||
Assert.assertEquals(ui.getElement(), popover.getElement().getParent()); | ||
} | ||
|
||
@Test | ||
public void setTarget_clearTarget_autoRemoved() { | ||
Popover popover = new Popover(); | ||
Div target = new Div(); | ||
popover.setTarget(target); | ||
ui.add(target); | ||
fakeClientResponse(); | ||
|
||
popover.setTarget(null); | ||
|
||
fakeClientResponse(); | ||
Assert.assertNull(popover.getElement().getParent()); | ||
} | ||
|
||
@Test | ||
public void setTarget_detachTarget_autoRemoved() { | ||
Popover popover = new Popover(); | ||
Div target = new Div(); | ||
popover.setTarget(target); | ||
ui.add(target); | ||
fakeClientResponse(); | ||
|
||
ui.remove(target); | ||
|
||
fakeClientResponse(); | ||
Assert.assertNull(popover.getElement().getParent()); | ||
} | ||
|
||
@Test | ||
public void setTarget_changeTarget_notAutoRemoved() { | ||
Popover popover = new Popover(); | ||
Div target = new Div(); | ||
popover.setTarget(target); | ||
ui.add(target); | ||
fakeClientResponse(); | ||
|
||
Div other = new Div(); | ||
ui.add(other); | ||
popover.setTarget(other); | ||
fakeClientResponse(); | ||
|
||
Assert.assertEquals(ui.getElement(), popover.getElement().getParent()); | ||
} | ||
|
||
@Test | ||
public void setTarget_changeTarget_detachOldTarget_notAutoRemoved() { | ||
Popover popover = new Popover(); | ||
Div target = new Div(); | ||
popover.setTarget(target); | ||
ui.add(target); | ||
fakeClientResponse(); | ||
|
||
Div other = new Div(); | ||
ui.add(other); | ||
popover.setTarget(other); | ||
fakeClientResponse(); | ||
|
||
ui.remove(target); | ||
fakeClientResponse(); | ||
Assert.assertEquals(ui.getElement(), popover.getElement().getParent()); | ||
} | ||
|
||
@Test | ||
public void setTarget_changeToDetachedTarget_autoRemoved() { | ||
Popover popover = new Popover(); | ||
Div target = new Div(); | ||
popover.setTarget(target); | ||
ui.add(target); | ||
fakeClientResponse(); | ||
|
||
Div other = new Div(); | ||
popover.setTarget(other); | ||
fakeClientResponse(); | ||
|
||
Assert.assertNull(popover.getElement().getParent()); | ||
} | ||
|
||
private void fakeClientResponse() { | ||
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); | ||
ui.getInternals().getStateTree().collectChanges(ignore -> { | ||
}); | ||
} | ||
} |