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

@SpringBootTest
public class ComicViewTest {

    @Autowired
    private ComicView comicView;

    @Test
    void formShownWhenComicSelected() {
        Grid<Comic> grid = comicView.getGrid();
        Comic firstComic = getFirstItem(grid);

        ComicForm form = comicView.getForm();

        assertFalse(form.isVisible());
        grid.asSingleSelect().setValue(firstComic);
        assertTrue(form.isVisible());
        assertEquals(firstComic.getTitle(), form.title.getValue());
    }

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