import { cleanup } from '@testing-library/react';
import uniqueId from 'lodash/uniqueId';

import {
  areEqualDropdownMenuItems,
  getDropdownMenuItemsByCountryCodes,
  getMockUsersDropdownMenuItems,
  toggleClickedDropdownMenuItem
} from '../helpers';
import { mockListItems } from '../__mocks__/menuItems';

afterEach(cleanup);

describe('Check DropdownMenu helpers', () => {
  describe('areEqualDropdownMenuItems', () => {
    it(`check with menu item clones without values`, () => {
      const item1 = { ...mockListItems[0] };
      const item2 = { ...mockListItems[0] };
      expect(areEqualDropdownMenuItems(item1, item2)).toBeTruthy();
    });

    it(`check with different names and equal values`, () => {
      const value = uniqueId();
      const item1 = { ...mockListItems[1], value };
      const item2 = { ...mockListItems[2], value };
      expect(areEqualDropdownMenuItems(item1, item2)).toBeTruthy();
    });

    it(`check with equal names but different values`, () => {
      const item1 = { ...mockListItems[1], value: uniqueId() };
      const item2 = { ...mockListItems[1], value: uniqueId() };
      expect(areEqualDropdownMenuItems(item1, item2)).toBeFalsy();
    });
  });

  describe('getDropdownMenuItemsByCountryCodes', () => {
    it(`check with empty countryCodes`, () => {
      expect(getDropdownMenuItemsByCountryCodes([])).toStrictEqual([]);
    });

    it(`check with invalid countryCode`, () => {
      const countryCode = 'XYZ' as 'LW';
      const result = getDropdownMenuItemsByCountryCodes([countryCode]);
      expect(result[0].name).toBeUndefined();
    });

    it(`check with the countryCode for United States`, () => {
      const countryCode = 'US';
      const result = getDropdownMenuItemsByCountryCodes([countryCode]);
      expect(result[0].name).toBe('United States of America');
    });
  });

  describe('getMockUsersDropdownMenuItems', () => {
    it(`check with default parameters`, () => {
      const result = getMockUsersDropdownMenuItems(10);
      expect(result.length).toBe(10);
    });
  });

  describe('toggleClickedDropdownMenuItem', () => {
    it(`check with different isClickedItem values`, () => {
      const item = { ...mockListItems[0], selected: true };
      expect(toggleClickedDropdownMenuItem(false)(item).selected).toBeTruthy();
      expect(toggleClickedDropdownMenuItem(true)(item).selected).toBeFalsy();
    });
  });
});
