Commit c8df6a46 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch '352839-add-tests-for-vulnerability-report-pagination' into 'master'

Add tests for vulnerability report pagination feature

See merge request gitlab-org/gitlab!83098
parents 44dc8069 7618a30c
import Vue, { nextTick } from 'vue'; import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
import { GlIntersectionObserver } from '@gitlab/ui'; import { GlIntersectionObserver, GlKeysetPagination } from '@gitlab/ui';
import VueRouter from 'vue-router'; import VueRouter from 'vue-router';
import VulnerabilityListGraphql from 'ee/security_dashboard/components/shared/vulnerability_report/vulnerability_list_graphql.vue'; import VulnerabilityListGraphql from 'ee/security_dashboard/components/shared/vulnerability_report/vulnerability_list_graphql.vue';
import VulnerabilityList from 'ee/security_dashboard/components/shared/vulnerability_report/vulnerability_list.vue'; import VulnerabilityList from 'ee/security_dashboard/components/shared/vulnerability_report/vulnerability_list.vue';
...@@ -24,7 +24,7 @@ const portalName = 'portal-name'; ...@@ -24,7 +24,7 @@ const portalName = 'portal-name';
const SORT_OBJECT = { sortBy: 'state', sortDesc: true }; const SORT_OBJECT = { sortBy: 'state', sortDesc: true };
const DEFAULT_SORT = `${FIELDS.SEVERITY.key}_desc`; const DEFAULT_SORT = `${FIELDS.SEVERITY.key}_desc`;
const createVulnerabilitiesRequestHandler = ({ hasNextPage }) => const createVulnerabilitiesRequestHandler = (options) =>
jest.fn().mockResolvedValue({ jest.fn().mockResolvedValue({
data: { data: {
group: { group: {
...@@ -35,15 +35,16 @@ const createVulnerabilitiesRequestHandler = ({ hasNextPage }) => ...@@ -35,15 +35,16 @@ const createVulnerabilitiesRequestHandler = ({ hasNextPage }) =>
__typename: 'PageInfo', __typename: 'PageInfo',
startCursor: 'abc', startCursor: 'abc',
endCursor: 'def', endCursor: 'def',
hasNextPage, hasNextPage: true,
hasPreviousPage: false, hasPreviousPage: false,
...options,
}, },
}, },
}, },
}, },
}); });
const vulnerabilitiesRequestHandler = createVulnerabilitiesRequestHandler({ hasNextPage: true }); const vulnerabilitiesRequestHandler = createVulnerabilitiesRequestHandler();
describe('Vulnerability list GraphQL component', () => { describe('Vulnerability list GraphQL component', () => {
let wrapper; let wrapper;
...@@ -53,6 +54,7 @@ describe('Vulnerability list GraphQL component', () => { ...@@ -53,6 +54,7 @@ describe('Vulnerability list GraphQL component', () => {
canViewFalsePositive = false, canViewFalsePositive = false,
showProjectNamespace = false, showProjectNamespace = false,
hasJiraVulnerabilitiesIntegrationEnabled = false, hasJiraVulnerabilitiesIntegrationEnabled = false,
vulnerabilityReportPagination = false,
filters = {}, filters = {},
fields = [], fields = [],
} = {}) => { } = {}) => {
...@@ -64,6 +66,7 @@ describe('Vulnerability list GraphQL component', () => { ...@@ -64,6 +66,7 @@ describe('Vulnerability list GraphQL component', () => {
dashboardType: DASHBOARD_TYPES.GROUP, dashboardType: DASHBOARD_TYPES.GROUP,
canViewFalsePositive, canViewFalsePositive,
hasJiraVulnerabilitiesIntegrationEnabled, hasJiraVulnerabilitiesIntegrationEnabled,
glFeatures: { vulnerabilityReportPagination },
}, },
propsData: { propsData: {
query: vulnerabilitiesQuery, query: vulnerabilitiesQuery,
...@@ -77,6 +80,7 @@ describe('Vulnerability list GraphQL component', () => { ...@@ -77,6 +80,7 @@ describe('Vulnerability list GraphQL component', () => {
const findVulnerabilityList = () => wrapper.findComponent(VulnerabilityList); const findVulnerabilityList = () => wrapper.findComponent(VulnerabilityList);
const findIntersectionObserver = () => wrapper.findComponent(GlIntersectionObserver); const findIntersectionObserver = () => wrapper.findComponent(GlIntersectionObserver);
const findPagination = () => wrapper.findComponent(GlKeysetPagination);
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
...@@ -259,7 +263,62 @@ describe('Vulnerability list GraphQL component', () => { ...@@ -259,7 +263,62 @@ describe('Vulnerability list GraphQL component', () => {
}); });
}); });
describe('pagination', () => {
it('is not shown if the pagination feature flag is off', () => {
createWrapper();
expect(findPagination().exists()).toBe(false);
});
it.each([
{ startCursor: 'abc', endCursor: 'def', hasPreviousPage: true, hasNextPage: false },
{ startCursor: 'ghi', endCursor: 'jkl', hasPreviousPage: false, hasNextPage: true },
])('has the expected props for pageInfo %s', async (pageInfo) => {
const vulnerabilitiesHandler = createVulnerabilitiesRequestHandler(pageInfo);
createWrapper({ vulnerabilityReportPagination: true, vulnerabilitiesHandler });
await waitForPromises();
expect(findPagination().props()).toMatchObject(pageInfo);
});
it('syncs the disabled prop with the query loading state', async () => {
createWrapper({ vulnerabilityReportPagination: true });
// When the component is mounted, the Apollo query will immediately start to load.
expect(findPagination().props('disabled')).toBe(true);
await waitForPromises();
// After the query handler is resolved, the Apollo query will no longer be loading.
expect(findPagination().props('disabled')).toBe(false);
});
it.each`
navigationDirection | expectedQueryVariables
${'next'} | ${{ after: 'endCursor' }}
${'prev'} | ${{ before: 'startCursor' }}
`(
'it navigates to the $navigationDirection page',
async ({ navigationDirection, expectedQueryVariables }) => {
const pageInfo = { startCursor: 'startCursor', endCursor: 'endCursor' };
const vulnerabilitiesHandler = createVulnerabilitiesRequestHandler(pageInfo);
createWrapper({ vulnerabilityReportPagination: true, vulnerabilitiesHandler });
await waitForPromises();
findPagination().vm.$emit(navigationDirection);
await nextTick();
expect(vulnerabilitiesHandler).toHaveBeenLastCalledWith(
expect.objectContaining(expectedQueryVariables),
);
},
);
});
describe('intersection observer', () => { describe('intersection observer', () => {
it('is not shown if the pagination feature flag is on', () => {
createWrapper({ vulnerabilityReportPagination: true });
expect(findIntersectionObserver().exists()).toBe(false);
});
it('is not shown when the vulnerabilities query is loading for the first time', () => { it('is not shown when the vulnerabilities query is loading for the first time', () => {
createWrapper(); createWrapper();
...@@ -281,8 +340,7 @@ describe('Vulnerability list GraphQL component', () => { ...@@ -281,8 +340,7 @@ describe('Vulnerability list GraphQL component', () => {
createWrapper({ createWrapper({
vulnerabilitiesHandler: createVulnerabilitiesRequestHandler({ hasNextPage: false }), vulnerabilitiesHandler: createVulnerabilitiesRequestHandler({ hasNextPage: false }),
}); });
await waitForPromises();
await nextTick();
expect(findIntersectionObserver().exists()).toBe(false); expect(findIntersectionObserver().exists()).toBe(false);
}); });
......
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