How to test if an UnorderedList is not empty?

I’m back with another question about testing. Please let me know if this is in the docs somewhere. I searched, but could not find it.

I simply want to test the behavior of a list in the view, that it is not empty. My hunch is that that I’m not instantiating the HomeView correctly because none of the "Adding event: logs from the HomeView are printing to the console, but I my intention is for it to use the InMemoryDatabase, which is set up for testing.

If it’s easier, here’s a link to the repo.

HomeViewTest

@Tag("ui")
class HomeViewTest extends KaribuTest {

    private HomeView view;
    private ClockRepository repository;
    private ClockEvent clockInEvent;
    private static final Logger log = LoggerFactory.getLogger(HomeViewTest.class);

    @BeforeEach
    public void login() {
        repository = InMemoryClockRepository.createEmpty();
        clockInEvent = new ClockEvent(ClockService.aug7at8am(), ClockEventType.IN);
        ClockEventService clockEventService = new ClockEventService(repository, ClockService.fixedAtAug7at8am());
        view = new HomeView(clockEventService);
    }

    @Test
    public void getRequestToIndex_returnsView() {
        assertThat(view).isNotNull();
    }

    @Test
    void viewIndex_returnsListOfClockEventViews() {
        repository.save(clockInEvent);
        assertThat(repository.findAll()).hasSize(1);
        UnorderedList eventList = _get(UnorderedList.class, spec -> spec.withId("events-list"));

        assertThat(eventList).isNotNull();

        List<ListItem> items = eventList.getChildren()
                .filter(component -> component instanceof ListItem)
                .map(component -> (ListItem) component)
                .collect(Collectors.toList());

        items.forEach(item -> log.info(item.getText()));

        assertThat(items).isNotEmpty();
    }
}

Test Results

java.lang.AssertionError: Expecting actual not to be empty

HomeView

@PageTitle("clock")
@Route(value = "")
@RouteAlias(value = "")
@Uses(Icon.class)
public class HomeView extends Composite<VerticalLayout> {
    private final ClockEventService service;
    private final Button buttonPrimary;
    private final Button buttonSecondary;
    private final UnorderedList eventsList;
    private static final Logger log = LoggerFactory.getLogger(HomeView.class);

    public HomeView(ClockEventService service) {
        this.service = service;
        HorizontalLayout mainRow = new HorizontalLayout();
        VerticalLayout column1 = new VerticalLayout();
        VerticalLayout column2 = new VerticalLayout();

        getContent().setWidth("100%");
        getContent().getStyle().set("flex-grow", "1");
        mainRow.addClassName(Gap.MEDIUM);
        mainRow.setWidth("100%");
        mainRow.getStyle().set("flex-grow", "1");

        column1.getStyle().set("flex-grow", "1");
        column1.setJustifyContentMode(JustifyContentMode.CENTER);
        column1.setAlignItems(Alignment.CENTER);

        buttonPrimary = new Button("Clock In", event -> clockIn());
        buttonSecondary = new Button("Clock Out", event -> clockOut());
        eventsList = new UnorderedList();

        buttonPrimary.setId("button-primary");
        buttonSecondary.setId("button-secondary");
        eventsList.setId("events-list");

        buttonPrimary.setWidth("min-content");
        buttonSecondary.setWidth("min-content");
        buttonPrimary.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

        column2.getStyle().set("flex-grow", "1");
        eventsList.setWidth("100%");
        eventsList.setHeight("100%");
        addClockEvents();
        updateButtonVisibility();

        getContent().add(mainRow);
        mainRow.add(column1);
        column1.add(buttonPrimary);
        column1.add(buttonSecondary);
        mainRow.add(column2);
        column2.add(eventsList);
    }

    private void clockIn() {
        ClockEventView clockEventView = service.clockIn();
        eventsList.addComponentAsFirst(new ListItem(clockEventView.toString()));
        updateButtonVisibility();
    }

    private void clockOut() {
        ClockEventView clockEventView = service.clockOut();
        eventsList.addComponentAsFirst(new ListItem(clockEventView.toString()));
        updateButtonVisibility();
    }

    void addClockEvents() {
        List<ClockEventView> allClockEvents = service.all();
        for (ClockEventView clockEvent : allClockEvents) {
            log.info("Adding event: {}", clockEvent);
            eventsList.add(new ListItem(clockEvent.toString()));
        }
    }

    private void updateButtonVisibility() {
        ClockEventType lastEventType = service.getLastClockEventType();
        if (lastEventType == ClockEventType.IN) {
            buttonPrimary.setVisible(false);
            buttonSecondary.setVisible(true);
        } else {
            buttonPrimary.setVisible(true);
            buttonSecondary.setVisible(false);
        }
    }

}

Let me ask you this:

Why would you think the view (that was opened before adding ClockEvent to the DB) would have it magically appear once you persist it? :wink: If you open your view after persisting, the data should be available / visible

1 Like

Thanks @knoobie ! I guess I’m so used to how it was set up in Spring MVC that I didn’t even consider that! :slight_smile: