Commit d1ed968d authored by Lukas 'Eipi' Eipert's avatar Lukas 'Eipi' Eipert Committed by Mark Florian

Move Secure Tests from Karma to Jest

This moves the last three Secure tests from Karma to Jest
parent 941fabd1
...@@ -511,11 +511,11 @@ export const createMergeRequest = ({ state, dispatch }) => { ...@@ -511,11 +511,11 @@ export const createMergeRequest = ({ state, dispatch }) => {
}; };
export const downloadPatch = ({ state }) => { export const downloadPatch = ({ state }) => {
/* /*
This action doesn't actually mutate the Vuex state and is a dirty This action doesn't actually mutate the Vuex state and is a dirty
workaround to modifying the dom. We do this because gl-split-button workaround to modifying the dom. We do this because gl-split-button
relies on a old version of vue-bootstrap and it doesn't allow us to relies on a old version of vue-bootstrap and it doesn't allow us to
set a href for a file download. set a href for a file download.
https://gitlab.com/gitlab-org/gitlab-ui/issues/188#note_165808493 https://gitlab.com/gitlab-org/gitlab-ui/issues/188#note_165808493
*/ */
......
...@@ -6,9 +6,10 @@ import * as types from 'ee/vue_shared/security_reports/store/mutation_types'; ...@@ -6,9 +6,10 @@ import * as types from 'ee/vue_shared/security_reports/store/mutation_types';
import sastState from 'ee/vue_shared/security_reports/store/modules/sast/state'; import sastState from 'ee/vue_shared/security_reports/store/modules/sast/state';
import * as sastTypes from 'ee/vue_shared/security_reports/store/modules/sast/mutation_types'; import * as sastTypes from 'ee/vue_shared/security_reports/store/modules/sast/mutation_types';
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import { waitForMutation } from 'spec/helpers/vue_test_utils_helper'; import { waitForMutation } from 'helpers/vue_test_utils_helper';
import { trimText } from 'spec/helpers/text_helper'; import { trimText } from 'helpers/text_helper';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { import {
sastDiffSuccessMock, sastDiffSuccessMock,
dastDiffSuccessMock, dastDiffSuccessMock,
...@@ -82,7 +83,7 @@ describe('Grouped security reports app', () => { ...@@ -82,7 +83,7 @@ describe('Grouped security reports app', () => {
}); });
describe('with error', () => { describe('with error', () => {
beforeEach(done => { beforeEach(() => {
mock.onGet(CONTAINER_SCANNING_DIFF_ENDPOINT).reply(500); mock.onGet(CONTAINER_SCANNING_DIFF_ENDPOINT).reply(500);
mock.onGet(DEPENDENCY_SCANNING_DIFF_ENDPOINT).reply(500); mock.onGet(DEPENDENCY_SCANNING_DIFF_ENDPOINT).reply(500);
mock.onGet(DAST_DIFF_ENDPOINT).reply(500); mock.onGet(DAST_DIFF_ENDPOINT).reply(500);
...@@ -90,14 +91,12 @@ describe('Grouped security reports app', () => { ...@@ -90,14 +91,12 @@ describe('Grouped security reports app', () => {
createWrapper(allReportProps); createWrapper(allReportProps);
Promise.all([ return Promise.all([
waitForMutation(wrapper.vm.$store, `sast/${sastTypes.RECEIVE_DIFF_ERROR}`), waitForMutation(wrapper.vm.$store, `sast/${sastTypes.RECEIVE_DIFF_ERROR}`),
waitForMutation(wrapper.vm.$store, types.RECEIVE_SAST_CONTAINER_DIFF_ERROR), waitForMutation(wrapper.vm.$store, types.RECEIVE_SAST_CONTAINER_DIFF_ERROR),
waitForMutation(wrapper.vm.$store, types.RECEIVE_DAST_DIFF_ERROR), waitForMutation(wrapper.vm.$store, types.RECEIVE_DAST_DIFF_ERROR),
waitForMutation(wrapper.vm.$store, types.RECEIVE_DEPENDENCY_SCANNING_DIFF_ERROR), waitForMutation(wrapper.vm.$store, types.RECEIVE_DEPENDENCY_SCANNING_DIFF_ERROR),
]) ]);
.then(done)
.catch(done.fail);
}); });
it('renders error state', () => { it('renders error state', () => {
...@@ -154,7 +153,7 @@ describe('Grouped security reports app', () => { ...@@ -154,7 +153,7 @@ describe('Grouped security reports app', () => {
}); });
describe('with successful responses', () => { describe('with successful responses', () => {
beforeEach(done => { beforeEach(() => {
mock.onGet(CONTAINER_SCANNING_DIFF_ENDPOINT).reply(200, containerScanningDiffSuccessMock); mock.onGet(CONTAINER_SCANNING_DIFF_ENDPOINT).reply(200, containerScanningDiffSuccessMock);
mock.onGet(DEPENDENCY_SCANNING_DIFF_ENDPOINT).reply(200, dependencyScanningDiffSuccessMock); mock.onGet(DEPENDENCY_SCANNING_DIFF_ENDPOINT).reply(200, dependencyScanningDiffSuccessMock);
mock.onGet(DAST_DIFF_ENDPOINT).reply(200, dastDiffSuccessMock); mock.onGet(DAST_DIFF_ENDPOINT).reply(200, dastDiffSuccessMock);
...@@ -162,14 +161,12 @@ describe('Grouped security reports app', () => { ...@@ -162,14 +161,12 @@ describe('Grouped security reports app', () => {
createWrapper(allReportProps); createWrapper(allReportProps);
Promise.all([ return Promise.all([
waitForMutation(wrapper.vm.$store, `sast/${sastTypes.RECEIVE_DIFF_SUCCESS}`), waitForMutation(wrapper.vm.$store, `sast/${sastTypes.RECEIVE_DIFF_SUCCESS}`),
waitForMutation(wrapper.vm.$store, types.RECEIVE_DAST_DIFF_SUCCESS), waitForMutation(wrapper.vm.$store, types.RECEIVE_DAST_DIFF_SUCCESS),
waitForMutation(wrapper.vm.$store, types.RECEIVE_SAST_CONTAINER_DIFF_SUCCESS), waitForMutation(wrapper.vm.$store, types.RECEIVE_SAST_CONTAINER_DIFF_SUCCESS),
waitForMutation(wrapper.vm.$store, types.RECEIVE_DEPENDENCY_SCANNING_DIFF_SUCCESS), waitForMutation(wrapper.vm.$store, types.RECEIVE_DEPENDENCY_SCANNING_DIFF_SUCCESS),
]) ]);
.then(done)
.catch(done.fail);
}); });
it('renders reports', () => { it('renders reports', () => {
...@@ -207,33 +204,26 @@ describe('Grouped security reports app', () => { ...@@ -207,33 +204,26 @@ describe('Grouped security reports app', () => {
); );
}); });
it('opens modal with more information', done => { it('opens modal with more information', () => {
setTimeout(() => { wrapper.vm.$el.querySelector('.break-link').click();
wrapper.vm.$el.querySelector('.break-link').click();
Vue.nextTick(() => { return Vue.nextTick().then(() => {
expect(wrapper.vm.$el.querySelector('.modal-title').textContent.trim()).toEqual( expect(wrapper.vm.$el.querySelector('.modal-title').textContent.trim()).toEqual(
mockFindings[0].name, mockFindings[0].name,
); );
expect(wrapper.vm.$el.querySelector('.modal-body').textContent).toContain(
mockFindings[0].solution,
);
done(); expect(wrapper.vm.$el.querySelector('.modal-body').textContent).toContain(
}); mockFindings[0].solution,
}, 0); );
});
}); });
it('has the success icon for fixed vulnerabilities', done => { it('has the success icon for fixed vulnerabilities', () => {
setTimeout(() => { const icon = wrapper.vm.$el.querySelector(
const icon = wrapper.vm.$el.querySelector( '.js-sast-container~.js-plain-element .ic-status_success_borderless',
'.js-sast-container~.js-plain-element .ic-status_success_borderless', );
);
expect(icon).not.toBeNull(); expect(icon).not.toBeNull();
done();
}, 0);
}); });
}); });
}); });
...@@ -257,7 +247,7 @@ describe('Grouped security reports app', () => { ...@@ -257,7 +247,7 @@ describe('Grouped security reports app', () => {
}); });
describe('container scanning reports', () => { describe('container scanning reports', () => {
beforeEach(done => { beforeEach(() => {
gl.mrWidgetData = gl.mrWidgetData || {}; gl.mrWidgetData = gl.mrWidgetData || {};
gl.mrWidgetData.container_scanning_comparison_path = CONTAINER_SCANNING_DIFF_ENDPOINT; gl.mrWidgetData.container_scanning_comparison_path = CONTAINER_SCANNING_DIFF_ENDPOINT;
...@@ -270,9 +260,7 @@ describe('Grouped security reports app', () => { ...@@ -270,9 +260,7 @@ describe('Grouped security reports app', () => {
}, },
}); });
waitForMutation(wrapper.vm.$store, types.RECEIVE_SAST_CONTAINER_DIFF_SUCCESS) return waitForMutation(wrapper.vm.$store, types.RECEIVE_SAST_CONTAINER_DIFF_SUCCESS);
.then(done)
.catch(done.fail);
}); });
it('should set setSastContainerDiffEndpoint', () => { it('should set setSastContainerDiffEndpoint', () => {
...@@ -287,7 +275,7 @@ describe('Grouped security reports app', () => { ...@@ -287,7 +275,7 @@ describe('Grouped security reports app', () => {
}); });
describe('dependency scanning reports', () => { describe('dependency scanning reports', () => {
beforeEach(done => { beforeEach(() => {
gl.mrWidgetData = gl.mrWidgetData || {}; gl.mrWidgetData = gl.mrWidgetData || {};
gl.mrWidgetData.dependency_scanning_comparison_path = DEPENDENCY_SCANNING_DIFF_ENDPOINT; gl.mrWidgetData.dependency_scanning_comparison_path = DEPENDENCY_SCANNING_DIFF_ENDPOINT;
...@@ -300,9 +288,7 @@ describe('Grouped security reports app', () => { ...@@ -300,9 +288,7 @@ describe('Grouped security reports app', () => {
}, },
}); });
waitForMutation(wrapper.vm.$store, types.RECEIVE_DEPENDENCY_SCANNING_DIFF_SUCCESS) return waitForMutation(wrapper.vm.$store, types.RECEIVE_DEPENDENCY_SCANNING_DIFF_SUCCESS);
.then(done)
.catch(done.fail);
}); });
it('should set setDependencyScanningDiffEndpoint', () => { it('should set setDependencyScanningDiffEndpoint', () => {
...@@ -319,7 +305,7 @@ describe('Grouped security reports app', () => { ...@@ -319,7 +305,7 @@ describe('Grouped security reports app', () => {
}); });
describe('dast reports', () => { describe('dast reports', () => {
beforeEach(done => { beforeEach(() => {
gl.mrWidgetData = gl.mrWidgetData || {}; gl.mrWidgetData = gl.mrWidgetData || {};
gl.mrWidgetData.dast_comparison_path = DAST_DIFF_ENDPOINT; gl.mrWidgetData.dast_comparison_path = DAST_DIFF_ENDPOINT;
...@@ -334,9 +320,7 @@ describe('Grouped security reports app', () => { ...@@ -334,9 +320,7 @@ describe('Grouped security reports app', () => {
}, },
}); });
waitForMutation(wrapper.vm.$store, types.RECEIVE_DAST_DIFF_SUCCESS) return waitForMutation(wrapper.vm.$store, types.RECEIVE_DAST_DIFF_SUCCESS);
.then(done)
.catch(done.fail);
}); });
it('should set setDastDiffEndpoint', () => { it('should set setDastDiffEndpoint', () => {
...@@ -357,7 +341,7 @@ describe('Grouped security reports app', () => { ...@@ -357,7 +341,7 @@ describe('Grouped security reports app', () => {
}); });
describe('sast reports', () => { describe('sast reports', () => {
beforeEach(done => { beforeEach(() => {
gl.mrWidgetData = gl.mrWidgetData || {}; gl.mrWidgetData = gl.mrWidgetData || {};
gl.mrWidgetData.sast_comparison_path = SAST_DIFF_ENDPOINT; gl.mrWidgetData.sast_comparison_path = SAST_DIFF_ENDPOINT;
gl.mrWidgetData.diverged_commits_count = 100; gl.mrWidgetData.diverged_commits_count = 100;
...@@ -374,9 +358,7 @@ describe('Grouped security reports app', () => { ...@@ -374,9 +358,7 @@ describe('Grouped security reports app', () => {
}, },
}); });
waitForMutation(wrapper.vm.$store, `sast/${sastTypes.RECEIVE_DIFF_SUCCESS}`) return waitForMutation(wrapper.vm.$store, `sast/${sastTypes.RECEIVE_DIFF_SUCCESS}`);
.then(done)
.catch(done.fail);
}); });
it('should set setSastDiffEndpoint', () => { it('should set setSastDiffEndpoint', () => {
......
import Vue from 'vue';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import actions, { import {
setHeadBlobPath, setHeadBlobPath,
setBaseBlobPath, setBaseBlobPath,
setVulnerabilityFeedbackPath, setVulnerabilityFeedbackPath,
...@@ -70,7 +69,7 @@ import actions, { ...@@ -70,7 +69,7 @@ import actions, {
} from 'ee/vue_shared/security_reports/store/actions'; } from 'ee/vue_shared/security_reports/store/actions';
import * as types from 'ee/vue_shared/security_reports/store/mutation_types'; import * as types from 'ee/vue_shared/security_reports/store/mutation_types';
import state from 'ee/vue_shared/security_reports/store/state'; import state from 'ee/vue_shared/security_reports/store/state';
import testAction from 'spec/helpers/vuex_action_helper'; import testAction from 'helpers/vuex_action_helper';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { import {
sastIssues, sastIssues,
...@@ -84,6 +83,17 @@ import { ...@@ -84,6 +83,17 @@ import {
containerScanningFeedbacks, containerScanningFeedbacks,
dependencyScanningFeedbacks, dependencyScanningFeedbacks,
} from '../mock_data'; } from '../mock_data';
import toasted from '~/vue_shared/plugins/global_toast';
// Mock bootstrap modal implementation
jest.mock('jquery', () => () => ({
modal: jest.fn(),
}));
jest.mock('~/lib/utils/url_utility', () => ({
visitUrl: jest.fn(),
}));
jest.mock('~/vue_shared/plugins/global_toast', () => jest.fn());
const createVulnerability = options => ({ const createVulnerability = options => ({
...options, ...options,
...@@ -113,6 +123,7 @@ describe('security reports actions', () => { ...@@ -113,6 +123,7 @@ describe('security reports actions', () => {
afterEach(() => { afterEach(() => {
mock.restore(); mock.restore();
toasted.mockClear();
}); });
describe('setHeadBlobPath', () => { describe('setHeadBlobPath', () => {
...@@ -967,7 +978,7 @@ describe('security reports actions', () => { ...@@ -967,7 +978,7 @@ describe('security reports actions', () => {
mockedState.createVulnerabilityFeedbackDismissalPath = 'dismiss_vulnerability_path'; mockedState.createVulnerabilityFeedbackDismissalPath = 'dismiss_vulnerability_path';
}); });
it(`should dispatch ${types.receiveDismissVulnerability}`, done => { it(`should dispatch receiveDismissVulnerability`, done => {
testAction( testAction(
dismissVulnerability, dismissVulnerability,
payload, payload,
...@@ -990,10 +1001,8 @@ describe('security reports actions', () => { ...@@ -990,10 +1001,8 @@ describe('security reports actions', () => {
}); });
it('show dismiss vulnerability toast message', done => { it('show dismiss vulnerability toast message', done => {
spyOn(Vue.toasted, 'show');
const checkToastMessage = () => { const checkToastMessage = () => {
expect(Vue.toasted.show).toHaveBeenCalledTimes(1); expect(toasted).toHaveBeenCalledTimes(1);
done(); done();
}; };
...@@ -1076,10 +1085,8 @@ describe('security reports actions', () => { ...@@ -1076,10 +1085,8 @@ describe('security reports actions', () => {
}); });
it('should show added dismissal comment toast message', done => { it('should show added dismissal comment toast message', done => {
spyOn(Vue.toasted, 'show').and.callThrough();
const checkToastMessage = () => { const checkToastMessage = () => {
expect(Vue.toasted.show).toHaveBeenCalledTimes(1); expect(toasted).toHaveBeenCalledTimes(1);
done(); done();
}; };
...@@ -1203,10 +1210,8 @@ describe('security reports actions', () => { ...@@ -1203,10 +1210,8 @@ describe('security reports actions', () => {
}); });
it('should show deleted dismissal comment toast message', done => { it('should show deleted dismissal comment toast message', done => {
spyOn(Vue.toasted, 'show').and.callThrough();
const checkToastMessage = () => { const checkToastMessage = () => {
expect(Vue.toasted.show).toHaveBeenCalledTimes(1); expect(toasted).toHaveBeenCalledTimes(1);
done(); done();
}; };
...@@ -1440,10 +1445,6 @@ describe('security reports actions', () => { ...@@ -1440,10 +1445,6 @@ describe('security reports actions', () => {
}); });
describe('createNewIssue', () => { describe('createNewIssue', () => {
beforeEach(() => {
spyOnDependency(actions, 'visitUrl');
});
it('with success should dispatch `requestCreateIssue` and `receiveCreateIssue`', done => { it('with success should dispatch `requestCreateIssue` and `receiveCreateIssue`', done => {
mock.onPost('create_issue_path').reply(200, { issue_path: 'new_issue' }); mock.onPost('create_issue_path').reply(200, { issue_path: 'new_issue' });
mockedState.createVulnerabilityFeedbackIssuePath = 'create_issue_path'; mockedState.createVulnerabilityFeedbackIssuePath = 'create_issue_path';
...@@ -1490,9 +1491,9 @@ describe('security reports actions', () => { ...@@ -1490,9 +1491,9 @@ describe('security reports actions', () => {
describe('downloadPatch', () => { describe('downloadPatch', () => {
it('creates a download link and clicks on it to download the file', () => { it('creates a download link and clicks on it to download the file', () => {
spyOn(document, 'createElement').and.callThrough(); jest.spyOn(document, 'createElement');
spyOn(document.body, 'appendChild').and.callThrough(); jest.spyOn(document.body, 'appendChild');
spyOn(document.body, 'removeChild').and.callThrough(); jest.spyOn(document.body, 'removeChild');
downloadPatch({ downloadPatch({
state: { state: {
...@@ -1569,10 +1570,6 @@ describe('security reports actions', () => { ...@@ -1569,10 +1570,6 @@ describe('security reports actions', () => {
}); });
describe('createMergeRequest', () => { describe('createMergeRequest', () => {
beforeEach(() => {
spyOnDependency(actions, 'visitUrl');
});
it('with success should dispatch `receiveCreateMergeRequestSuccess`', done => { it('with success should dispatch `receiveCreateMergeRequestSuccess`', done => {
const data = { merge_request_path: 'fakepath.html' }; const data = { merge_request_path: 'fakepath.html' };
mockedState.createVulnerabilityFeedbackMergeRequestPath = 'create_merge_request_path'; mockedState.createVulnerabilityFeedbackMergeRequestPath = 'create_merge_request_path';
......
import downloadPatchHelper from 'ee/vue_shared/security_reports/store/utils/download_patch_helper'; import downloadPatchHelper from 'ee/vue_shared/security_reports/store/utils/download_patch_helper';
describe('downloadPatchHelper', () => { describe('downloadPatchHelper', () => {
beforeAll(() => { beforeEach(() => {
spyOn(document, 'createElement').and.callThrough(); jest.spyOn(document, 'createElement');
spyOn(document.body, 'appendChild').and.callThrough(); jest.spyOn(document.body, 'appendChild');
spyOn(document.body, 'removeChild').and.callThrough(); jest.spyOn(document.body, 'removeChild');
});
afterEach(() => {
document.createElement.calls.reset();
document.body.appendChild.calls.reset();
document.body.removeChild.calls.reset();
}); });
describe('with a base64 encoded string', () => { describe('with a base64 encoded string', () => {
...@@ -25,7 +19,7 @@ describe('downloadPatchHelper', () => { ...@@ -25,7 +19,7 @@ describe('downloadPatchHelper', () => {
downloadPatchHelper(base64String); downloadPatchHelper(base64String);
expect(document.createElement).toHaveBeenCalledTimes(1); expect(document.createElement).toHaveBeenCalledWith('a');
expect(document.body.appendChild).toHaveBeenCalledTimes(1); expect(document.body.appendChild).toHaveBeenCalledTimes(1);
expect(document.body.removeChild).toHaveBeenCalledTimes(1); expect(document.body.removeChild).toHaveBeenCalledTimes(1);
}); });
...@@ -43,7 +37,7 @@ describe('downloadPatchHelper', () => { ...@@ -43,7 +37,7 @@ describe('downloadPatchHelper', () => {
downloadPatchHelper(unencodedString, { isEncoded: false }); downloadPatchHelper(unencodedString, { isEncoded: false });
expect(document.createElement).toHaveBeenCalledTimes(1); expect(document.createElement).toHaveBeenCalledWith('a');
expect(document.body.appendChild).toHaveBeenCalledTimes(1); expect(document.body.appendChild).toHaveBeenCalledTimes(1);
expect(document.body.removeChild).toHaveBeenCalledTimes(1); expect(document.body.removeChild).toHaveBeenCalledTimes(1);
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment