import React from 'react';
import { Simulate } from 'react-dom/test-utils';
import { renderHook, act } from '@testing-library/react-hooks';
import { Observable } from 'rxjs';
import { Resolve } from '../../services/resolve.service';
import { useSelectGroup } from './useSelectGroup';

test('useSelectGroup adicionar valor', async () => {
   const { result, waitForNextUpdate } = renderHook(() => useSelectGroup());

   let [onSelect, onUnselectAll, selected] = result.current;

   // Inicial
   expect(Array.from(selected.keys())).toEqual([]);

   act(() => {
      onSelect(1, true);
   });

   // Adicionar 1 a array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([1]);
});

test('useSelectGroup adicionar o mesmo valor', async () => {
   const { result, waitForNextUpdate } = renderHook(() => useSelectGroup());

   let [onSelect, onUnselectAll, selected] = result.current;

   // Inicial
   expect(Array.from(selected.keys())).toEqual([]);

   act(() => {
      onSelect(1, true);
      onSelect(1, true);
      onSelect(1, true);
   });

   // Adicionar 1 a array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([1]);
});

test('useSelectGroup remover valor', async () => {
   const { result, waitForNextUpdate } = renderHook(() => useSelectGroup());

   let [onSelect, onUnselectAll, selected] = result.current;

   // Inicial
   expect(Array.from(selected.keys())).toEqual([]);

   act(() => {
      onSelect(1, true);
   });

   // Adicionar 1 a array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([1]);

   act(() => {
      onSelect(1, false);
   });

   // Remover 1 a array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([]);
});

test('useSelectGroup remover o mesmo valor', async () => {
   const { result, waitForNextUpdate } = renderHook(() => useSelectGroup());

   let [onSelect, onUnselectAll, selected] = result.current;

   // Inicial
   expect(Array.from(selected.keys())).toEqual([]);

   act(() => {
      onSelect(1, true);
   });

   // Adicionar 1 a array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([1]);

   act(() => {
      onSelect(1, false);
      onSelect(1, false);
      onSelect(1, false);
   });

   // Remover 1 a array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([]);
});

test('useSelectGroup remover todos os valores com onUnselectAll', async () => {
   const { result, waitForNextUpdate } = renderHook(() => useSelectGroup());

   let [onSelect, onUnselectAll, selected] = result.current;

   // Inicial
   expect(Array.from(selected.keys())).toEqual([]);

   act(() => {
      onSelect(1, true);
      onSelect(2, true);
      onSelect(3, true);
   });

   // Adicionar 3 valores a array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([1, 2, 3]);

   act(() => {
      onUnselectAll();
   });

   // Remover todos os valores da array
   [onSelect, onUnselectAll, selected] = result.current;
   expect(Array.from(selected.keys())).toEqual([]);
});

test('useSelectGroup verificar checkSelected', async () => {
   const { result, waitForNextUpdate } = renderHook(() => useSelectGroup());

   let [onSelect, onUnselectAll, selected, checkSelected] = result.current;

   // Inicial
   expect(Array.from(selected.keys())).toEqual([]);

   act(() => {
      onSelect(1, true);
      onSelect(2, true);
      onSelect(3, true);
      onSelect(3, false);
   });
   
   [onSelect, onUnselectAll, selected, checkSelected] = result.current;

   expect(checkSelected(1)).toEqual(true);
   expect(checkSelected(2)).toEqual(true);
   expect(checkSelected(3)).toEqual(false);
   expect(checkSelected(4)).toEqual(false);
});
