|
| 1 | +/* |
| 2 | + * Copyright 2000-2026 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.component.grid.contextmenu; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 24 | + |
| 25 | +import com.vaadin.flow.component.grid.Grid; |
| 26 | +import com.vaadin.flow.component.internal.PendingJavaScriptInvocation; |
| 27 | +import com.vaadin.flow.component.shared.Tooltip.TooltipPosition; |
| 28 | +import com.vaadin.tests.MockUIExtension; |
| 29 | + |
| 30 | +class GridMenuItemTest { |
| 31 | + |
| 32 | + @RegisterExtension |
| 33 | + MockUIExtension ui = new MockUIExtension(); |
| 34 | + |
| 35 | + private GridContextMenu<String> contextMenu; |
| 36 | + private GridMenuItem<String> item; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setup() { |
| 40 | + Grid<String> grid = new Grid<>(); |
| 41 | + contextMenu = grid.addContextMenu(); |
| 42 | + item = contextMenu.addItem("item"); |
| 43 | + ui.add(grid); |
| 44 | + ui.add(contextMenu); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void setTooltipText_menuContentGenerated() { |
| 49 | + flushPendingInvocations(); |
| 50 | + |
| 51 | + item.setTooltipText("tooltip"); |
| 52 | + |
| 53 | + Assertions.assertEquals(1, getGenerateItemsInvocations().size()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void setTooltipPosition_menuContentGenerated() { |
| 58 | + flushPendingInvocations(); |
| 59 | + |
| 60 | + item.setTooltipPosition(TooltipPosition.END); |
| 61 | + |
| 62 | + Assertions.assertEquals(1, getGenerateItemsInvocations().size()); |
| 63 | + } |
| 64 | + |
| 65 | + private List<PendingJavaScriptInvocation> getGenerateItemsInvocations() { |
| 66 | + return getPendingInvocations() |
| 67 | + .stream().filter(invocation -> invocation.getInvocation() |
| 68 | + .getExpression().contains("$connector.generateItems")) |
| 69 | + .toList(); |
| 70 | + } |
| 71 | + |
| 72 | + private void flushPendingInvocations() { |
| 73 | + getPendingInvocations(); |
| 74 | + } |
| 75 | + |
| 76 | + private List<PendingJavaScriptInvocation> getPendingInvocations() { |
| 77 | + ui.fakeClientCommunication(); |
| 78 | + return ui.dumpPendingJavaScriptInvocations(); |
| 79 | + } |
| 80 | +} |
0 commit comments