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

@SpringBootTest
class FieldPositionViewTest {

    @Autowired
    private PositionView positionView;

    @Test
    void formShownWhenPositionSelected() {
        Grid<FieldPosition> grid = positionView.getGrid();
        FieldPosition firstFieldPosition = getFirstItem(grid);

        PositionForm form = positionView.getForm();

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

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