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

@SpringBootTest
class RoosterViewTest {

    @Autowired
    private RoosterView roosterView;

    @Test
    void formShownWhenRoosterSelected() {
        Grid<Rooster> grid = roosterView.getGrid();
        Rooster firstRooster = getFirstItem(grid);

        RoosterForm form = roosterView.getForm();

        assertFalse(form.isVisible());
        grid.asSingleSelect().setValue(firstRooster);
        assertTrue(form.isVisible());
        assertEquals(firstRooster.getYear(), form.year.getValue());
    }

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