vulnerability_spec.js 3.07 KB
Newer Older
Kev's avatar
Kev committed
1
import { shallowMount } from '@vue/test-utils';
2
import AxiosMockAdapter from 'axios-mock-adapter';
Kev's avatar
Kev committed
3 4
import Details from 'ee/vulnerabilities/components/details.vue';
import Footer from 'ee/vulnerabilities/components/footer.vue';
5 6
import Header from 'ee/vulnerabilities/components/header.vue';
import Main from 'ee/vulnerabilities/components/vulnerability.vue';
Kev's avatar
Kev committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

const mockAxios = new AxiosMockAdapter();

describe('Vulnerability', () => {
  let wrapper;

  const vulnerability = {
    id: 1,
    created_at: new Date().toISOString(),
    report_type: 'sast',
    state: 'detected',
    create_mr_url: '/create_mr_url',
    create_issue_url: '/create_issue_url',
    project_fingerprint: 'abc123',
    pipeline: {
      id: 2,
      created_at: new Date().toISOString(),
      url: 'pipeline_url',
      sourceBranch: 'master',
    },
    description: 'description',
    identifiers: 'identifiers',
    links: 'links',
    location: 'location',
    name: 'name',
    project: {
      full_path: '/project_full_path',
      full_name: 'Test Project',
    },
    discussions_url: '/discussion_url',
    notes_url: '/notes_url',
    can_modify_related_issues: false,
    related_issues_help_path: '/help_path',
    merge_request_feedback: null,
    issue_feedback: null,
    remediation: null,
  };

  const createWrapper = () => {
    wrapper = shallowMount(Main, {
      propsData: {
        vulnerability,
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
    mockAxios.reset();
  });

  beforeEach(createWrapper);

  const findHeader = () => wrapper.find(Header);
  const findDetails = () => wrapper.find(Details);
  const findFooter = () => wrapper.find(Footer);

  describe('default behavior', () => {
66
    it('consists of header, details, and footer', () => {
Kev's avatar
Kev committed
67 68 69 70 71 72
      expect(findHeader().exists()).toBe(true);
      expect(findDetails().exists()).toBe(true);
      expect(findFooter().exists()).toBe(true);
    });

    it('passes the correct properties to the children', () => {
Kev's avatar
Kev committed
73 74 75
      expect(findHeader().props('initialVulnerability')).toBe(vulnerability);
      expect(findDetails().props('vulnerability')).toBe(vulnerability);
      expect(findFooter().props('vulnerability')).toBe(vulnerability);
Kev's avatar
Kev committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    });
  });

  describe('vulnerability state change event', () => {
    let fetchDiscussions;
    let refreshVulnerability;

    beforeEach(() => {
      fetchDiscussions = jest.fn();
      refreshVulnerability = jest.fn();

      findHeader().vm.refreshVulnerability = refreshVulnerability;
      findFooter().vm.fetchDiscussions = fetchDiscussions;
    });

91 92
    it('updates the footer notes when the vulnerbility state was changed', () => {
      findHeader().vm.$emit('vulnerability-state-change');
Kev's avatar
Kev committed
93 94 95 96 97 98

      expect(fetchDiscussions).toHaveBeenCalledTimes(1);
      expect(refreshVulnerability).not.toHaveBeenCalled();
    });

    it('updates the header when the footer received a state-change note', () => {
99
      findFooter().vm.$emit('vulnerability-state-change');
Kev's avatar
Kev committed
100 101 102 103 104 105

      expect(fetchDiscussions).not.toHaveBeenCalled();
      expect(refreshVulnerability).toHaveBeenCalledTimes(1);
    });
  });
});