Commit 2800a8fe authored by Scott Hampton's avatar Scott Hampton

Merge branch '328254-add-tests-for-count-list' into 'master'

Add unit test for vulnerability severities count query

See merge request gitlab-org/gitlab!59915
parents b24494e7 bbf0d8cb
...@@ -42,6 +42,7 @@ export default { ...@@ -42,6 +42,7 @@ export default {
query: vulnerabilitySeveritiesCountQuery, query: vulnerabilitySeveritiesCountQuery,
variables() { variables() {
const { dashboardType, fullPath } = this; const { dashboardType, fullPath } = this;
return { return {
fullPath, fullPath,
isInstance: dashboardType === DASHBOARD_TYPES.INSTANCE, isInstance: dashboardType === DASHBOARD_TYPES.INSTANCE,
......
...@@ -6,6 +6,7 @@ import countQuery from 'ee/security_dashboard/graphql/queries/vulnerability_seve ...@@ -6,6 +6,7 @@ import countQuery from 'ee/security_dashboard/graphql/queries/vulnerability_seve
import { DASHBOARD_TYPES } from 'ee/security_dashboard/store/constants'; import { DASHBOARD_TYPES } from 'ee/security_dashboard/store/constants';
import eventHub from 'ee/security_dashboard/utils/event_hub'; import eventHub from 'ee/security_dashboard/utils/event_hub';
import createMockApollo from 'helpers/mock_apollo_helper'; import createMockApollo from 'helpers/mock_apollo_helper';
import { mockVulnerabilitySeveritiesGraphQLResponse } from '../mock_data';
const localVue = createLocalVue(); const localVue = createLocalVue();
localVue.use(VueApollo); localVue.use(VueApollo);
...@@ -14,7 +15,7 @@ describe('Vulnerabilities count list component', () => { ...@@ -14,7 +15,7 @@ describe('Vulnerabilities count list component', () => {
let wrapper; let wrapper;
let refetchSpy; let refetchSpy;
const findVulnerabilityLayout = () => wrapper.find(VulnerabilityCountListLayout); const findVulnerabilityLayout = () => wrapper.findComponent(VulnerabilityCountListLayout);
const createWrapper = ({ query = { isLoading: false }, provide, data = {} } = {}) => { const createWrapper = ({ query = { isLoading: false }, provide, data = {} } = {}) => {
refetchSpy = jest.fn(); refetchSpy = jest.fn();
...@@ -28,6 +29,16 @@ describe('Vulnerabilities count list component', () => { ...@@ -28,6 +29,16 @@ describe('Vulnerabilities count list component', () => {
}); });
}; };
const createWrapperWithApollo = ({ query, provide, propsData, stubs }) => {
wrapper = shallowMount(VulnerabilityCountList, {
localVue,
apolloProvider: createMockApollo([[countQuery, query]]),
provide,
propsData,
stubs,
});
};
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
}); });
...@@ -76,15 +87,25 @@ describe('Vulnerabilities count list component', () => { ...@@ -76,15 +87,25 @@ describe('Vulnerabilities count list component', () => {
}); });
describe.each` describe.each`
dashboardType | expectedContainedQueryVariables dashboardType | fullPath | expectedContainedQueryVariables
${DASHBOARD_TYPES.INSTANCE} | ${{ isInstance: true, isGroup: false, isProject: false }} ${DASHBOARD_TYPES.INSTANCE} | ${undefined} | ${{ isInstance: true, isGroup: false, isProject: false }}
${DASHBOARD_TYPES.GROUP} | ${{ isInstance: false, isGroup: true, isProject: false }} ${DASHBOARD_TYPES.GROUP} | ${'group/path'} | ${{ isInstance: false, isGroup: true, isProject: false }}
${DASHBOARD_TYPES.PROJECT} | ${{ isInstance: false, isGroup: false, isProject: true }} ${DASHBOARD_TYPES.PROJECT} | ${'project/path'} | ${{ isInstance: false, isGroup: false, isProject: true }}
`( `(
'when the dashboard type is $dashboardType', 'when the dashboard type is $dashboardType',
({ dashboardType, expectedContainedQueryVariables }) => { ({ dashboardType, fullPath, expectedContainedQueryVariables }) => {
beforeEach(() => { beforeEach(() => {
wrapper = createWrapper({ provide: { dashboardType } }); const mockResponse = jest
.fn()
.mockResolvedValue(mockVulnerabilitySeveritiesGraphQLResponse({ dashboardType }));
createWrapperWithApollo({
provide: { dashboardType },
propsData: { fullPath, filters: { someFilter: 1 } },
query: mockResponse,
stubs: { VulnerabilityCountListLayout },
});
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick();
}); });
...@@ -93,6 +114,17 @@ describe('Vulnerabilities count list component', () => { ...@@ -93,6 +114,17 @@ describe('Vulnerabilities count list component', () => {
wrapper.vm.$options.apollo.vulnerabilitiesCount.variables.call(wrapper.vm), wrapper.vm.$options.apollo.vulnerabilitiesCount.variables.call(wrapper.vm),
).toMatchObject(expectedContainedQueryVariables); ).toMatchObject(expectedContainedQueryVariables);
}); });
it('should set the data properly', () => {
expect(findVulnerabilityLayout().props('vulnerabilitiesCount')).toEqual({
critical: 0,
high: 0,
info: 0,
low: 0,
medium: 4,
unknown: 2,
});
});
}, },
); );
...@@ -109,23 +141,22 @@ describe('Vulnerabilities count list component', () => { ...@@ -109,23 +141,22 @@ describe('Vulnerabilities count list component', () => {
describe('filters prop', () => { describe('filters prop', () => {
const mockQuery = jest.fn().mockResolvedValue(null); const mockQuery = jest.fn().mockResolvedValue(null);
const createWrapperWithApollo = ({ query, filters }) => { it('does not run the query when filters is null', () => {
wrapper = shallowMount(VulnerabilityCountList, { createWrapperWithApollo({
localVue, query: mockQuery,
apolloProvider: createMockApollo([[countQuery, query]]), propsData: { filters: null },
provide: { dashboardType: DASHBOARD_TYPES.PROJECT }, provide: { dashboardType: DASHBOARD_TYPES.PROJECT },
propsData: { filters },
}); });
};
it('does not run the query when filters is null', () => {
createWrapperWithApollo({ query: mockQuery, filters: null });
expect(mockQuery).not.toHaveBeenCalled(); expect(mockQuery).not.toHaveBeenCalled();
}); });
it('runs query when filters is an object', () => { it('runs query when filters is an object', () => {
createWrapperWithApollo({ query: mockQuery, filters: {} }); createWrapperWithApollo({
query: mockQuery,
propsData: { filters: {} },
provide: { dashboardType: DASHBOARD_TYPES.PROJECT },
});
expect(mockQuery).toHaveBeenCalled(); expect(mockQuery).toHaveBeenCalled();
}); });
......
...@@ -223,3 +223,22 @@ export const mockVulnerableProjectsGroup = () => ({ ...@@ -223,3 +223,22 @@ export const mockVulnerableProjectsGroup = () => ({
}, },
}, },
}); });
export const mockVulnerabilitySeveritiesGraphQLResponse = ({ dashboardType }) => ({
data: {
[dashboardType]: {
vulnerabilitySeveritiesCount: {
__typename: 'VulnerabilitySeveritiesCount',
critical: 0,
high: 0,
info: 0,
low: 0,
medium: 4,
unknown: 2,
},
},
__typename: { project: 'Project', instance: 'InstanceSecurityDashboard', group: 'Group' }[
dashboardType
],
},
});
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