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

@SpringBootTest
class CardViewTest {

    @Autowired
    private CardView cardView;

    @Test
    void formShownWhenCardSelected() {
        Grid<Card> grid = cardView.getGrid();
        Card firstCard = getFirstItem(grid);

        CardForm form = cardView.getForm();

        assertFalse(form.isVisible());
        grid.asSingleSelect().setValue(firstCard);
        assertTrue(form.isVisible());
        assertEquals(String.valueOf(firstCard.getCardNumber()), form.cardNumber.getValue());
    }

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