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

@SpringBootTest
class ArtistViewTest {

    @Autowired
    private ArtistView artistView;

    @Test
    void formShownWhenArtistSelected() {
        Grid<Artist> grid = artistView.getGrid();
        Artist firstArtist = getFirstItem(grid);

        ArtistForm form = artistView.getForm();

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

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