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

@SpringBootTest
class StoryArcViewTest {

    @Autowired
    private StoryArcView storyArcView;

    @Test
    void formShownWhenStoryArcSelected() {
        Grid<StoryArc> grid = storyArcView.getGrid();
        StoryArc firstStoryArc = getFirstItem(grid);

        StoryArcForm form = storyArcView.getForm();

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

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