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

@SpringBootTest
class TeamViewTest {

    @Autowired
    private TeamView teamView;

    @Test
    void formShownWhenTeamSelected() {
        Grid<Team> grid = teamView.getGrid();
        Team firstTeam = getFirstItem(grid);

        TeamForm form = teamView.getForm();

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

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