package de.thpeetz.kontor.comics.views;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

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.ComicWork;

@SpringBootTest
class ComicWorkViewTest {

    @Autowired
    private ComicWorkView comicWorkView;

    @Test
    void formShownWhenComicSelected() {
        Grid<ComicWork> grid = comicWorkView.getGrid();
        ComicWork firstComicWork = getFirstItem(grid);

        ComicWorkForm form = comicWorkView.getForm();

        assertFalse(form.isVisible());
        grid.asSingleSelect().setValue(firstComicWork);
        assertTrue(form.isVisible());
        assertEquals(firstComicWork.getComic(), form.comic.getValue());
    }

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