Commit 8d591588 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '326196-devops-adoption-refactor-jest-tests' into 'master'

DevOps Adoption - Refactor jest tests

See merge request gitlab-org/gitlab!65229
parents f30de9a7 8b1df1c6
......@@ -21,12 +21,7 @@ import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import DevopsScore from '~/analytics/devops_report/components/devops_score.vue';
import API from '~/api';
import {
groupNodes,
groupPageInfo,
devopsAdoptionNamespaceData,
devopsAdoptionNamespaceDataEmpty,
} from '../mock_data';
import { groupNodes, devopsAdoptionNamespaceData } from '../mock_data';
jest.mock('ee/analytics/devops_report/devops_adoption/utils/cache_updates', () => ({
addSegmentsToCache: jest.fn(),
......@@ -35,39 +30,70 @@ jest.mock('ee/analytics/devops_report/devops_adoption/utils/cache_updates', () =
const localVue = createLocalVue();
Vue.use(VueApollo);
const initialResponse = {
__typename: 'Groups',
nodes: groupNodes,
pageInfo: groupPageInfo,
const NETWORK_ERROR = new Error('foo!');
const RESOURCE_TYPE_GROUP = 'groups';
const RESOURCE_TYPE_ENABLED_NAMESPACE = 'devopsAdoptionEnabledNamespaces';
const RESOURCE_TYPE_BULK_ENABLE_NAMESPACES = 'bulkEnableDevopsAdoptionNamespaces';
const STATE_EMPTY = 'empty';
const STATE_WITH_DATA = 'withData';
const STATE_NETWORK_ERROR = 'networkError';
const dataFactory = (resource) => {
switch (resource) {
case RESOURCE_TYPE_GROUP:
return { nodes: groupNodes };
case RESOURCE_TYPE_ENABLED_NAMESPACE:
return devopsAdoptionNamespaceData;
case RESOURCE_TYPE_BULK_ENABLE_NAMESPACES:
return {
enabledNamespaces: [devopsAdoptionNamespaceData.nodes[0]],
errors: [],
};
default:
return jest.fn();
}
};
const promiseFactory = (state, resource) => {
switch (state) {
case STATE_EMPTY:
return jest.fn().mockResolvedValue({
data: { [resource]: { nodes: [] } },
});
case STATE_WITH_DATA:
return jest.fn().mockResolvedValue({
data: { [resource]: dataFactory(resource) },
});
case STATE_NETWORK_ERROR:
return jest.fn().mockRejectedValue(NETWORK_ERROR);
default:
return jest.fn();
}
};
describe('DevopsAdoptionApp', () => {
let wrapper;
const groupsEmpty = jest.fn().mockResolvedValue({ __typename: 'Groups', nodes: [] });
const segmentsEmpty = jest.fn().mockResolvedValue({
data: { devopsAdoptionEnabledNamespaces: devopsAdoptionNamespaceDataEmpty },
});
const addSegmentMutationSpy = jest.fn().mockResolvedValue({
data: {
bulkEnableDevopsAdoptionNamespaces: {
enabledNamespaces: [devopsAdoptionNamespaceData.nodes[0]],
errors: [],
},
},
});
const groupsEmpty = promiseFactory(STATE_EMPTY, RESOURCE_TYPE_GROUP);
const enabledNamespacesEmpty = promiseFactory(STATE_EMPTY, RESOURCE_TYPE_ENABLED_NAMESPACE);
const enableNamespacesMutationSpy = promiseFactory(
STATE_WITH_DATA,
RESOURCE_TYPE_BULK_ENABLE_NAMESPACES,
);
function createMockApolloProvider(options = {}) {
const {
groupsSpy = groupsEmpty,
segmentsSpy = segmentsEmpty,
addSegmentsSpy = addSegmentMutationSpy,
enabledNamespacesSpy = enabledNamespacesEmpty,
enableNamespacesMutation = enableNamespacesMutationSpy,
} = options;
const mockApollo = createMockApollo(
[
[bulkEnableDevopsAdoptionNamespacesMutation, addSegmentsSpy],
[devopsAdoptionEnabledNamespaces, segmentsSpy],
[bulkEnableDevopsAdoptionNamespacesMutation, enableNamespacesMutation],
[devopsAdoptionEnabledNamespaces, enabledNamespacesSpy],
],
{
Query: {
......@@ -117,7 +143,7 @@ describe('DevopsAdoptionApp', () => {
describe('when group data is present', () => {
beforeEach(async () => {
groupsSpy = jest.fn().mockResolvedValueOnce({ ...initialResponse, pageInfo: null });
groupsSpy = promiseFactory(STATE_WITH_DATA, RESOURCE_TYPE_GROUP);
const mockApollo = createMockApolloProvider({ groupsSpy });
wrapper = createComponent({ mockApollo });
await waitForPromises();
......@@ -129,11 +155,9 @@ describe('DevopsAdoptionApp', () => {
});
describe('when error is thrown fetching group data', () => {
const error = new Error('foo!');
beforeEach(async () => {
jest.spyOn(Sentry, 'captureException');
groupsSpy = jest.fn().mockRejectedValueOnce(error);
groupsSpy = promiseFactory(STATE_NETWORK_ERROR, RESOURCE_TYPE_GROUP);
const mockApollo = createMockApolloProvider({ groupsSpy });
wrapper = createComponent({ mockApollo });
await waitForPromises();
......@@ -147,12 +171,12 @@ describe('DevopsAdoptionApp', () => {
const alert = wrapper.findComponent(GlAlert);
expect(alert.exists()).toBe(true);
expect(alert.text()).toBe(DEVOPS_ADOPTION_STRINGS.app.groupsError);
expect(Sentry.captureException.mock.calls[0][0].networkError).toBe(error);
expect(Sentry.captureException.mock.calls[0][0].networkError).toBe(NETWORK_ERROR);
});
});
});
describe('segments data', () => {
describe('enabled namespaces data', () => {
describe('when there is no active group', () => {
beforeEach(async () => {
const mockApollo = createMockApolloProvider();
......@@ -161,7 +185,7 @@ describe('DevopsAdoptionApp', () => {
});
it('does not attempt to enable a group', () => {
expect(addSegmentMutationSpy).toHaveBeenCalledTimes(0);
expect(enableNamespacesMutationSpy).toHaveBeenCalledTimes(0);
});
});
......@@ -170,11 +194,8 @@ describe('DevopsAdoptionApp', () => {
describe('which is enabled', () => {
beforeEach(async () => {
const segmentsWithData = jest.fn().mockResolvedValue({
data: { devopsAdoptionEnabledNamespaces: devopsAdoptionNamespaceData },
});
const mockApollo = createMockApolloProvider({
segmentsSpy: segmentsWithData,
enabledNamespacesSpy: promiseFactory(STATE_WITH_DATA, RESOURCE_TYPE_ENABLED_NAMESPACE),
});
const provide = {
isGroup: true,
......@@ -186,7 +207,7 @@ describe('DevopsAdoptionApp', () => {
});
it('does not attempt to enable a group', () => {
expect(addSegmentMutationSpy).toHaveBeenCalledTimes(0);
expect(enableNamespacesMutationSpy).toHaveBeenCalledTimes(0);
});
});
......@@ -204,8 +225,8 @@ describe('DevopsAdoptionApp', () => {
describe('enables the group', () => {
it('makes a request with the correct variables', () => {
expect(addSegmentMutationSpy).toHaveBeenCalledTimes(1);
expect(addSegmentMutationSpy).toHaveBeenCalledWith(
expect(enableNamespacesMutationSpy).toHaveBeenCalledTimes(1);
expect(enableNamespacesMutationSpy).toHaveBeenCalledWith(
expect.objectContaining({
namespaceIds: [groupGid],
displayNamespaceId: groupGid,
......@@ -225,16 +246,18 @@ describe('DevopsAdoptionApp', () => {
});
describe('error handling', () => {
const addSegmentsSpyErrorMessage = 'Error: bar!';
beforeEach(async () => {
jest.spyOn(Sentry, 'captureException');
const addSegmentsSpyError = jest.fn().mockRejectedValue(addSegmentsSpyErrorMessage);
const provide = {
isGroup: true,
groupGid,
};
const mockApollo = createMockApolloProvider({ addSegmentsSpy: addSegmentsSpyError });
const mockApollo = createMockApolloProvider({
enableNamespacesMutation: promiseFactory(
STATE_NETWORK_ERROR,
RESOURCE_TYPE_BULK_ENABLE_NAMESPACES,
),
});
wrapper = createComponent({ mockApollo, provide });
await waitForPromises();
await wrapper.vm.$nextTick();
......@@ -251,9 +274,7 @@ describe('DevopsAdoptionApp', () => {
});
it('calls Sentry', () => {
expect(Sentry.captureException.mock.calls[0][0].networkError).toBe(
addSegmentsSpyErrorMessage,
);
expect(Sentry.captureException.mock.calls[0][0].networkError).toBe(NETWORK_ERROR);
});
});
});
......@@ -261,12 +282,14 @@ describe('DevopsAdoptionApp', () => {
});
describe('when there is an error', () => {
const segmentsErrorMessage = 'Error: bar!';
beforeEach(async () => {
jest.spyOn(Sentry, 'captureException');
const segmentsError = jest.fn().mockRejectedValue(segmentsErrorMessage);
const mockApollo = createMockApolloProvider({ segmentsSpy: segmentsError });
const mockApollo = createMockApolloProvider({
enabledNamespacesSpy: promiseFactory(
STATE_NETWORK_ERROR,
RESOURCE_TYPE_ENABLED_NAMESPACE,
),
});
wrapper = createComponent({ mockApollo });
await waitForPromises();
});
......@@ -282,7 +305,7 @@ describe('DevopsAdoptionApp', () => {
});
it('calls Sentry', () => {
expect(Sentry.captureException.mock.calls[0][0].networkError).toBe(segmentsErrorMessage);
expect(Sentry.captureException.mock.calls[0][0].networkError).toBe(NETWORK_ERROR);
});
});
......@@ -295,7 +318,7 @@ describe('DevopsAdoptionApp', () => {
wrapper = createComponent({
mockApollo: createMockApolloProvider({
groupsSpy: jest.fn().mockResolvedValueOnce({ ...initialResponse, pageInfo: null }),
groupsSpy: promiseFactory(STATE_WITH_DATA, RESOURCE_TYPE_ENABLED_NAMESPACE),
}),
});
......
......@@ -25,10 +25,6 @@ export const groupIds = [1, 2];
export const groupGids = ['gid://gitlab/Group/1', 'gid://gitlab/Group/2'];
export const groupPageInfo = {
nextPage: 2,
};
export const devopsAdoptionNamespaceData = {
nodes: [
{
......@@ -66,11 +62,6 @@ export const devopsAdoptionNamespaceData = {
__typename: 'devopsAdoptionEnabledNamespaces',
};
export const devopsAdoptionNamespaceDataEmpty = {
nodes: [],
__typename: 'devopsAdoptionSegments',
};
export const devopsAdoptionTableHeaders = [
{
index: 0,
......
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