package de.thpeetz.kontor.comics.views;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.vaadin.flow.component.grid.Grid;

import de.thpeetz.kontor.comics.data.Publisher;

@SpringBootTest
class PublisherViewTest {

    @Autowired
    private PublisherView publisherView;

    @Test
    void formShownWhenPublisherSelected() {
        Grid<Publisher> grid = publisherView.getGrid();
        Publisher firstPublisher = getFirstItem(grid);

        PublisherForm form = publisherView.getForm();

        assertFalse(form.isVisible());
        grid.asSingleSelect().setValue(firstPublisher);
        assertTrue(form.isVisible());
        assertEquals(firstPublisher.getName(), form.name.getValue());
    }

    private Publisher getFirstItem(Grid<Publisher> grid) {
        int count = grid.getListDataView().getItemCount();
        List<Publisher> publishers = grid.getListDataView().getItems().collect(Collectors.toList());
        assertEquals(18, count);
        return publishers.get(0);
    }
}
