import { shallowMount } from '@vue/test-utils';
import VulnerabilityCountList from 'ee/security_dashboard/components/vulnerability_count_list.vue';
import VulnerabilityCountListLayout from 'ee/security_dashboard/components/vulnerability_count_list_layout.vue';

describe('Vulnerabilities count list component', () => {
  let wrapper;

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

  const createWrapper = ({ query } = {}) => {
    return shallowMount(VulnerabilityCountList, {
      propsData: {
        projectFullPath: '/root/security-project',
      },
      mocks: {
        $apollo: { queries: { vulnerabilitiesCount: query } },
      },
    });
  };

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

  describe('when loading', () => {
    it('passes down to the loading indicator', () => {
      wrapper = createWrapper({ query: { loading: true } });
      expect(findVulnerabilityLayout().props('isLoading')).toBe(true);
    });
  });

  describe('when counts are loaded', () => {
    beforeEach(() => {
      wrapper = createWrapper({ query: { loading: false } });
      wrapper.setData({
        vulnerabilitiesCount: {
          critical: 5,
          high: 3,
          low: 19,
          info: 4,
          medium: 2,
          unknown: 4,
        },
      });
    });

    it('sets the loading indicator false and passes it down', () => {
      expect(findVulnerabilityLayout().props('isLoading')).toBe(false);
    });

    it('should load the vulnerabilities and pass them down to the layout', () => {
      expect(findVulnerabilityLayout().props('vulnerabilitiesCount')).toEqual({
        critical: 5,
        high: 3,
        low: 19,
        info: 4,
        medium: 2,
        unknown: 4,
      });
    });
  });

  describe('when there is an error', () => {
    beforeEach(() => {
      wrapper = createWrapper({ query: {} });
      wrapper.setData({ queryError: true });
    });

    it('should tell the layout to display an error', () => {
      expect(findVulnerabilityLayout().props('showError')).toBe(true);
    });
  });
});
