import mutations from '~/packages/list/stores/mutations';
import * as types from '~/packages/list/stores/mutation_types';
import createState from '~/packages/list/stores/state';
import * as commonUtils from '~/lib/utils/common_utils';
import { npmPackage, mavenPackage } from '../../mock_data';

describe('Mutations Registry Store', () => {
  let mockState;
  beforeEach(() => {
    mockState = createState();
  });

  describe('SET_INITIAL_STATE', () => {
    it('should set the initial state', () => {
      const config = {
        resourceId: '1',
        pageType: 'groups',
        userCanDelete: '',
        emptyListIllustration: 'foo',
        emptyListHelpUrl: 'baz',
      };

      const expectedState = {
        ...mockState,
        config: {
          ...config,
          isGroupPage: true,
          canDestroyPackage: true,
        },
      };
      mutations[types.SET_INITIAL_STATE](mockState, config);

      expect(mockState.projectId).toEqual(expectedState.projectId);
    });
  });

  describe('SET_PACKAGE_LIST_SUCCESS', () => {
    it('should set a packages list', () => {
      const payload = [npmPackage, mavenPackage];
      const expectedState = { ...mockState, packages: payload };
      mutations[types.SET_PACKAGE_LIST_SUCCESS](mockState, payload);

      expect(mockState.packages).toEqual(expectedState.packages);
    });
  });

  describe('SET_MAIN_LOADING', () => {
    it('should set main loading', () => {
      mutations[types.SET_MAIN_LOADING](mockState, true);

      expect(mockState.isLoading).toEqual(true);
    });
  });

  describe('SET_PAGINATION', () => {
    const mockPagination = { perPage: 10, page: 1 };
    beforeEach(() => {
      commonUtils.normalizeHeaders = jest.fn().mockReturnValue('baz');
      commonUtils.parseIntPagination = jest.fn().mockReturnValue(mockPagination);
    });
    it('should set a parsed pagination', () => {
      mutations[types.SET_PAGINATION](mockState, 'foo');
      expect(commonUtils.normalizeHeaders).toHaveBeenCalledWith('foo');
      expect(commonUtils.parseIntPagination).toHaveBeenCalledWith('baz');
      expect(mockState.pagination).toEqual(mockPagination);
    });
  });

  describe('SET_SORTING', () => {
    it('should merge the sorting object with sort value', () => {
      mutations[types.SET_SORTING](mockState, { sort: 'desc' });
      expect(mockState.sorting).toEqual({ ...mockState.sorting, sort: 'desc' });
    });

    it('should merge the sorting object with order_by value', () => {
      mutations[types.SET_SORTING](mockState, { orderBy: 'foo' });
      expect(mockState.sorting).toEqual({ ...mockState.sorting, orderBy: 'foo' });
    });
  });

  describe('SET_SELECTED_TYPE', () => {
    it('should set the selected type', () => {
      mutations[types.SET_SELECTED_TYPE](mockState, { type: 'maven' });
      expect(mockState.selectedType).toEqual({ type: 'maven' });
    });
  });

  describe('SET_FILTER', () => {
    it('should set the filter query', () => {
      mutations[types.SET_FILTER](mockState, 'foo');
      expect(mockState.filterQuery).toEqual('foo');
    });
  });
});
