import { shallowMount } from '@vue/test-utils';
import VulnerabilityCountListVuex from 'ee/security_dashboard/components/vulnerability_count_list_vuex.vue';
import createStore from 'ee/security_dashboard/store';
import VulnerabilityCountListLayout from 'ee/security_dashboard/components/vulnerability_count_list_layout.vue';
import { resetStore } from '../helpers';
import mockData from '../store/modules/vulnerabilities/data/mock_data_vulnerabilities_count.json';

describe('Vulnerability Count List', () => {
  const projectFullPath = 'root/security-imports';
  const store = createStore();
  let wrapper;

  const findVulnerabilityCountListLayout = () => wrapper.find(VulnerabilityCountListLayout);

  beforeEach(() => {
    wrapper = shallowMount(VulnerabilityCountListVuex, {
      store,
      propsData: {
        projectFullPath,
      },
    });
  });

  afterEach(() => {
    wrapper.destroy();
    resetStore(store);
  });

  it('should pass down the data to the layout', () => {
    store.dispatch('vulnerabilities/receiveVulnerabilitiesCountSuccess', { data: mockData });

    return wrapper.vm.$nextTick(() => {
      const layout = findVulnerabilityCountListLayout();
      expect(layout.props('isLoading')).toBe(false);
      expect(layout.props('showError')).toBe(false);
      expect(layout.props('vulnerabilitiesCount')).toEqual(mockData);
    });
  });

  it('should pass down the loading flag when vulnerabilities are loading', () => {
    store.dispatch('vulnerabilities/requestVulnerabilitiesCount');

    return wrapper.vm.$nextTick(() => {
      const layout = findVulnerabilityCountListLayout();
      expect(layout.props('isLoading')).toBe(true);
      expect(layout.props('showError')).toBe(false);
      expect(layout.props('vulnerabilitiesCount')).toEqual({});
    });
  });

  it('should pass down the error flag when vulnerabilities are loading', () => {
    store.dispatch('vulnerabilities/receiveVulnerabilitiesCountError');

    return wrapper.vm.$nextTick(() => {
      const layout = findVulnerabilityCountListLayout();
      expect(layout.props('isLoading')).toBe(false);
      expect(layout.props('showError')).toBe(true);
      expect(layout.props('vulnerabilitiesCount')).toEqual({});
    });
  });
});
