package de.thpeetz.kontor.tysc.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.tysc.data.CardSet;

@SpringBootTest
class CardSetViewTest {

    @Autowired
    private CardSetView cardSetView;

    @Test
    void formShownWhenCardSetSelected() {
        Grid<CardSet> grid = cardSetView.getGrid();
        CardSet firstCardSet = getFirstItem(grid);

        CardSetForm form = cardSetView.getForm();

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

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