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

@SpringBootTest
class SportViewTest {

    @Autowired
    private SportView sportView;

    @Test
    void formShownWhenSportSelected() {
        Grid<Sport> grid = sportView.getGrid();
        Sport firstSport = getFirstItem(grid);

        SportForm form = sportView.getForm();

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

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