Skip to content

Commit

Permalink
add testbench api
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki committed May 29, 2024
1 parent 7f02e7c commit 998c70e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.vaadin.flow.component.grid.it;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.data.bean.Person;
import com.vaadin.flow.router.Route;

@Route("vaadin-grid/empty-state")
public class GridEmptyStatePage extends Div {
public GridEmptyStatePage() {
var grid = new Grid<>(Person.class);
add(grid);

// Button to set the empty state content
var setEmptyStateContentButton = new Button("Set empty state content",
event -> grid.setEmptyStateText("Custom empty state content"));
setEmptyStateContentButton.setId("set-empty-state-content");

// Button to set the grid items
var setItemsButton = new Button("Set items", event -> grid
.setItems(new Person("John", 20), new Person("Jane", 30)));
setItemsButton.setId("set-items");

// Button to clear the grid items
var clearItemsButton = new Button("Clear items",
event -> grid.setItems());
clearItemsButton.setId("clear-items");

add(setEmptyStateContentButton, setItemsButton, clearItemsButton);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.vaadin.flow.component.grid.it;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.NoSuchElementException;

import com.vaadin.flow.component.button.testbench.ButtonElement;
import com.vaadin.flow.component.grid.testbench.GridElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.AbstractComponentIT;

@TestPath("vaadin-grid/empty-state")
public class GridEmptyStateIT extends AbstractComponentIT {
private GridElement grid;
private ButtonElement setEmptyStateContentButton;
private ButtonElement clearItemsButton;
private ButtonElement setItemsButton;

@Before
public void init() {
open();
grid = $(GridElement.class).first();
setEmptyStateContentButton = $(ButtonElement.class)
.id("set-empty-state-content");
clearItemsButton = $(ButtonElement.class).id("clear-items");
setItemsButton = $(ButtonElement.class).id("set-items");
}

@Test
public void getEmptyStateContent_throws() {
Assert.assertThrows("No empty state content was found",
NoSuchElementException.class,
() -> grid.getEmptyStateContent());
}

@Test
public void setEmptyStateContent_emptyStateContentDisplayed() {
setEmptyStateContentButton.click();
var content = grid.getEmptyStateContent();
Assert.assertEquals("Custom empty state content", content.getText());
Assert.assertTrue(content.isDisplayed());
}

@Test
public void setEmptyStateContent_setItems_emptyStateContentNotDisplayed() {
setEmptyStateContentButton.click();
setItemsButton.click();
var content = grid.getEmptyStateContent();
Assert.assertFalse(content.isDisplayed());
}

@Test
public void setEmptyStateContent_setItems_clearItems_emptyStateContentDisplayed() {
setEmptyStateContentButton.click();
setItemsButton.click();
clearItemsButton.click();
var content = grid.getEmptyStateContent();
Assert.assertTrue(content.isDisplayed());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,19 @@ public List<GridTHTDElement> getCells(int rowIndex) {
getAllColumns().toArray(new GridColumnElement[0]));
}

/**
* Gets the empty state content.
*
* @return the empty state content
* @throws NoSuchElementException
* if no empty state content was found
*/
public TestBenchElement getEmptyStateContent() {
try {
return findElement(By.cssSelector("[slot='empty-state']"));
} catch (NoSuchElementException e) {
throw new NoSuchElementException(
"No empty state content was found");
}
}
}

0 comments on commit 998c70e

Please sign in to comment.