Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dashboard sections support #6606

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;

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;
Expand All @@ -23,6 +24,8 @@
public class DashboardPage extends Div {

public DashboardPage() {
Dashboard dashboard = new Dashboard();

DashboardWidget widget1 = new DashboardWidget();
widget1.setTitle("Widget 1");
widget1.setId("widget-1");
Expand All @@ -35,9 +38,27 @@ public DashboardPage() {
widget3.setTitle("Widget 3");
widget3.setId("widget-3");

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");

dashboard.add(widget1, widget2, widget3);

DashboardSection section1 = new DashboardSection("Section 1");
section1.add(widget1InSection1, widget2InSection1);
dashboard.addSection(section1);

DashboardSection section2 = dashboard.addSection("Section 2");
section2.add(widget1InSection2);

NativeButton addWidgetAtIndex1 = new NativeButton(
"Add widget at index 1");
addWidgetAtIndex1.addClickListener(click -> {
Expand All @@ -57,10 +78,11 @@ public DashboardPage() {
}
int currentWidgetCount = currentWidgets.size();
if (currentWidgetCount == 1) {
dashboard.remove(dashboard.getWidgets().get(0));
dashboard.getWidgets().get(0).removeFromParent();
} else {
dashboard.remove(dashboard.getWidgets().get(0),
dashboard.getWidgets().get(currentWidgetCount - 1));
dashboard.getWidgets().get(currentWidgetCount - 1)
.removeFromParent();
dashboard.getWidgets().get(0).removeFromParent();
}
});
removeFirstAndLastWidgets.setId("remove-first-and-last-widgets");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand All @@ -36,26 +37,45 @@ public void init() {

@Test
public void addWidgets_widgetsAreCorrectlyAdded() {
assertWidgetsByTitle("Widget 1", "Widget 2", "Widget 3");
assertDashboardWidgetsByTitle("Widget 1", "Widget 2", "Widget 3",
"Widget 1 in Section 1", "Widget 2 in Section 1",
"Widget 1 in Section 2");
}

@Test
public void addWidgetsAtIndex1_widgetIsAddedIntoTheCorrectPlace() {
clickElementWithJs("add-widget-at-index-1");
assertWidgetsByTitle("Widget 1", "Widget at index 1", "Widget 2",
"Widget 3");
assertDashboardWidgetsByTitle("Widget 1", "Widget at index 1",
"Widget 2", "Widget 3", "Widget 1 in Section 1",
"Widget 2 in Section 1", "Widget 1 in Section 2");
}

@Test
public void removeFirstAndLastWidgets_widgetsAreCorrectlyRemoved() {
clickElementWithJs("remove-first-and-last-widgets");
assertWidgetsByTitle("Widget 2");
assertDashboardWidgetsByTitle("Widget 2", "Widget 3",
"Widget 1 in Section 1", "Widget 2 in Section 1");
}

@Test
public void removeAllWidgets_widgetsAreCorrectlyRemoved() {
clickElementWithJs("remove-all-widgets");
assertWidgetsByTitle();
assertDashboardWidgetsByTitle();
}

@Test
public void addSectionsWithWidgets_widgetsAreCorrectlyAdded() {
List<DashboardSectionElement> sections = dashboardElement.getSections();
Assert.assertEquals(2, sections.size());

DashboardSectionElement section1 = sections.get(0);
Assert.assertEquals("Section 1", section1.getTitle());
assertSectionWidgetsByTitle(section1, "Widget 1 in Section 1",
"Widget 2 in Section 1");

DashboardSectionElement section2 = sections.get(1);
Assert.assertEquals("Section 2", section2.getTitle());
assertSectionWidgetsByTitle(section2, "Widget 1 in Section 2");
}

@Test
Expand Down Expand Up @@ -103,9 +123,20 @@ public void updateColspans_colspansForAllWidgetsUpdated() {
widget.getColspan()));
}

private void assertWidgetsByTitle(String... expectedWidgetTitles) {
List<DashboardWidgetElement> widgets = dashboardElement.getWidgets();
List<String> widgetTitles = widgets.stream()
private void assertDashboardWidgetsByTitle(String... expectedWidgetTitles) {
assertWidgetsByTitle(dashboardElement.getWidgets(),
expectedWidgetTitles);
}

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);
}
Expand Down
Loading