import {
  groupBy,
  removeDuplicates,
  clonePageForCards,
  removeCurrentPage,
  updateMenuPrefixPath,
} from './common';
import getPageDataList from '~tests/factories/pages/list.factory';

describe('Common Helper', () => {
  test('group by', () => {
    const pagesNum = 10;
    const pagesMappedById = groupBy(getPageDataList(pagesNum), 'id');
    expect(Object.keys(pagesMappedById).length).toBe(pagesNum);

    const pagesMappedByMetaTitle = groupBy(getPageDataList(pagesNum), 'meta_title');
    expect(Object.keys(pagesMappedByMetaTitle).length).toBe(2);
  });

  test('Remove duplicates function', () => {
    const array = getPageDataList(2);
    array[2] = { id: 1, name: 'Test' };

    const filtered = removeDuplicates(array, 'id');
    expect(filtered).toHaveLength(2);
  });

  test('ClonePageForCards', () => {
    const data = getPageDataList(2);
    const result = clonePageForCards(data, 'comparison_table');
    expect(result?.relation?.bonus?.deposit_methods).toBeFalsy();
  });

  test('Remove vcurrent page', () => {
    const data = getPageDataList(2);
    const result = removeCurrentPage(data, 2);
    expect(result.id === 2).toBeFalsy();
  });

  test('menu update function', () => {
    const data = [
      {
        value: 'test',
        page_id: 123,
        children: [
          {
            value: 'null',
            page_id: 123,
          },
          {
            value: 'test3',
            page_id: 123,
            children: [
              {
                value: 'test4',
                page_id: 123,
                children: [
                  {
                    value: 'test5',
                  },
                ],
              },
            ],
          },
          {
            value: 'test6',
            page_id: 123,
          },
        ],
      },
      {
        value: 'test3',
        page_id: 123,
        children: [
          {
            value: 'test4',
            page_id: 123,
            children: [
              {
                value: 'test5',
              },
            ],
          },
        ],
      },
    ];

    updateMenuPrefixPath(data, 'prefix/');
    data.map((menuItem) => {
      if (menuItem.value) {
        expect(menuItem.value).toContain('prefix/');
      }
      return true;
    });
  });
});
