package de.thpeetz.kontor.comics.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.comics.data.Issue;

@SpringBootTest
public class IssueViewTest {

    @Autowired
    private IssueView issueView;

    @Test
    void formShownWhenIssueSelected() {
        Grid<Issue> grid = issueView.getGrid();
        Issue firstIssue = getFirstItem(grid);

        IssueForm form = issueView.getForm();

        assertFalse(form.isVisible());
        grid.asSingleSelect().setValue(firstIssue);
        assertTrue(form.isVisible());
        assertEquals(firstIssue.getIssueNumber(), form.issueNumber.getValue());
    }

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