-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add content support to dashboard widget (#6618)
* feat: add content related api to dashboard widget * refactor: make set get content separate from slotted elements * test: update testbench content to check for all slots * feat: add content related api to dashboard widget * refactor: make set get content separate from slotted elements * test: update testbench content to check for all slots * chore: run formatter * test: move tests into appropriate classes
- Loading branch information
1 parent
621ec81
commit 284eec9
Showing
9 changed files
with
362 additions
and
98 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
89 changes: 89 additions & 0 deletions
89
...n-tests/src/main/java/com/vaadin/flow/component/dashboard/tests/DashboardSectionPage.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,89 @@ | ||
/** | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.dashboard.tests; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import com.vaadin.flow.component.dashboard.Dashboard; | ||
import com.vaadin.flow.component.dashboard.DashboardSection; | ||
import com.vaadin.flow.component.dashboard.DashboardWidget; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
/** | ||
* @author Vaadin Ltd | ||
*/ | ||
@Route("vaadin-dashboard-section") | ||
public class DashboardSectionPage extends Div { | ||
|
||
public DashboardSectionPage() { | ||
Dashboard dashboard = new Dashboard(); | ||
|
||
DashboardWidget widget1InSection1 = new DashboardWidget(); | ||
widget1InSection1.setTitle("Widget 1 in Section 1"); | ||
widget1InSection1.setId("widget-1-in-section-1"); | ||
|
||
DashboardWidget widget2InSection1 = new DashboardWidget(); | ||
widget2InSection1.setTitle("Widget 2 in Section 1"); | ||
widget2InSection1.setId("widget-2-in-section-1"); | ||
|
||
DashboardWidget widget1InSection2 = new DashboardWidget(); | ||
widget1InSection2.setTitle("Widget 1 in Section 2"); | ||
widget1InSection2.setId("widget-1-in-section-2"); | ||
|
||
DashboardSection section1 = new DashboardSection("Section 1"); | ||
section1.add(widget1InSection1, widget2InSection1); | ||
dashboard.addSection(section1); | ||
|
||
DashboardSection section2 = dashboard.addSection("Section 2"); | ||
section2.add(widget1InSection2); | ||
|
||
NativeButton addWidgetToFirstSection = new NativeButton( | ||
"Add widget to first section"); | ||
addWidgetToFirstSection.addClickListener( | ||
click -> getFirstSection(dashboard).ifPresent(section -> { | ||
DashboardWidget newWidget = new DashboardWidget(); | ||
newWidget.setTitle("New widget"); | ||
section.add(newWidget); | ||
})); | ||
addWidgetToFirstSection.setId("add-widget-to-first-section"); | ||
|
||
NativeButton removeFirstWidgetFromFirstSection = new NativeButton( | ||
"Remove first widget from first section"); | ||
removeFirstWidgetFromFirstSection.addClickListener( | ||
click -> getFirstSection(dashboard).ifPresent(section -> { | ||
List<DashboardWidget> currentWidgets = section.getWidgets(); | ||
if (currentWidgets.isEmpty()) { | ||
return; | ||
} | ||
section.remove(currentWidgets.get(0)); | ||
})); | ||
removeFirstWidgetFromFirstSection | ||
.setId("remove-first-widget-from-first-section"); | ||
|
||
NativeButton removeAllFromFirstSection = new NativeButton( | ||
"Remove all from first section"); | ||
removeAllFromFirstSection | ||
.addClickListener(click -> getFirstSection(dashboard) | ||
.ifPresent(DashboardSection::removeAll)); | ||
removeAllFromFirstSection.setId("remove-all-from-first-section"); | ||
|
||
add(addWidgetToFirstSection, removeFirstWidgetFromFirstSection, | ||
removeAllFromFirstSection, dashboard); | ||
} | ||
|
||
private static Optional<DashboardSection> getFirstSection( | ||
Dashboard dashboard) { | ||
return dashboard.getChildren() | ||
.filter(DashboardSection.class::isInstance) | ||
.map(DashboardSection.class::cast).findFirst(); | ||
} | ||
} |
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
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
78 changes: 78 additions & 0 deletions
78
...ion-tests/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardSectionIT.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,78 @@ | ||
/** | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.dashboard.tests; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.vaadin.flow.component.dashboard.testbench.DashboardElement; | ||
import com.vaadin.flow.component.dashboard.testbench.DashboardSectionElement; | ||
import com.vaadin.flow.component.dashboard.testbench.DashboardWidgetElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.tests.AbstractComponentIT; | ||
|
||
/** | ||
* @author Vaadin Ltd | ||
*/ | ||
@TestPath("vaadin-dashboard-section") | ||
public class DashboardSectionIT extends AbstractComponentIT { | ||
|
||
private DashboardElement dashboardElement; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
dashboardElement = $(DashboardElement.class).waitForFirst(); | ||
} | ||
|
||
@Test | ||
public void addWidgetToFirstSection_widgetsAreAdded() { | ||
clickElementWithJs("add-widget-to-first-section"); | ||
List<DashboardSectionElement> sections = dashboardElement.getSections(); | ||
DashboardSectionElement firstSection = sections.get(0); | ||
Assert.assertEquals("Section 1", firstSection.getTitle()); | ||
assertSectionWidgetsByTitle(firstSection, "Widget 1 in Section 1", | ||
"Widget 2 in Section 1", "New widget"); | ||
} | ||
|
||
@Test | ||
public void removeFirstWidgetFromFirstSection_widgetIsRemoved() { | ||
clickElementWithJs("remove-first-widget-from-first-section"); | ||
List<DashboardSectionElement> sections = dashboardElement.getSections(); | ||
DashboardSectionElement firstSection = sections.get(0); | ||
Assert.assertEquals("Section 1", firstSection.getTitle()); | ||
assertSectionWidgetsByTitle(firstSection, "Widget 2 in Section 1"); | ||
} | ||
|
||
@Test | ||
public void removeAllFromFirstSection_widgetsAreRemoved() { | ||
clickElementWithJs("remove-all-from-first-section"); | ||
List<DashboardSectionElement> sections = dashboardElement.getSections(); | ||
DashboardSectionElement firstSection = sections.get(0); | ||
Assert.assertEquals("Section 1", firstSection.getTitle()); | ||
assertSectionWidgetsByTitle(firstSection); | ||
} | ||
|
||
private static void assertSectionWidgetsByTitle( | ||
DashboardSectionElement section, String... expectedWidgetTitles) { | ||
assertWidgetsByTitle(section.getWidgets(), expectedWidgetTitles); | ||
} | ||
|
||
private static void assertWidgetsByTitle( | ||
List<DashboardWidgetElement> actualWidgets, | ||
String... expectedWidgetTitles) { | ||
List<String> widgetTitles = actualWidgets.stream() | ||
.map(DashboardWidgetElement::getTitle).toList(); | ||
Assert.assertEquals(Arrays.asList(expectedWidgetTitles), widgetTitles); | ||
} | ||
} |
Oops, something went wrong.