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

@SpringBootTest
class VendorViewTest {

    @Autowired
    private VendorView vendorView;

    @Test
    void formShownWhenVendorSelected() {
        Grid<Vendor> grid = vendorView.getGrid();
        Vendor firstVendor = getFirstItem(grid);

        VendorForm form = vendorView.getForm();

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

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