Commit c15b1fa2 authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera

Merge branch 'pb-move-spec-to-mock-apollo-client' into 'master'

Refactor keep latest artifact checkbox spec to use mock-apollo-client

See merge request gitlab-org/gitlab!51611
parents 1370189e e53d8c73
import { GlFormCheckbox, GlLink } from '@gitlab/ui'; import { GlFormCheckbox, GlLink } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount, createLocalVue } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import createMockApollo from 'jest/helpers/mock_apollo_helper';
import KeepLatestArtifactCheckbox from '~/artifacts_settings/keep_latest_artifact_checkbox.vue'; import KeepLatestArtifactCheckbox from '~/artifacts_settings/keep_latest_artifact_checkbox.vue';
import GetKeepLatestArtifactProjectSetting from '~/artifacts_settings/graphql/queries/get_keep_latest_artifact_project_setting.query.graphql';
import UpdateKeepLatestArtifactProjectSetting from '~/artifacts_settings/graphql/mutations/update_keep_latest_artifact_project_setting.mutation.graphql'; import UpdateKeepLatestArtifactProjectSetting from '~/artifacts_settings/graphql/mutations/update_keep_latest_artifact_project_setting.mutation.graphql';
const localVue = createLocalVue();
localVue.use(VueApollo);
const keepLatestArtifactMock = {
data: {
project: {
ciCdSettings: { keepLatestArtifact: true },
},
},
};
const keepLatestArtifactMockResponse = {
data: { ciCdSettingsUpdate: { errors: [], __typename: 'CiCdSettingsUpdatePayload' } },
};
describe('Keep latest artifact checkbox', () => { describe('Keep latest artifact checkbox', () => {
let wrapper; let wrapper;
let apolloProvider;
let requestHandlers;
const mutate = jest.fn().mockResolvedValue();
const fullPath = 'gitlab-org/gitlab'; const fullPath = 'gitlab-org/gitlab';
const helpPagePath = '/help/ci/pipelines/job_artifacts'; const helpPagePath = '/help/ci/pipelines/job_artifacts';
const findCheckbox = () => wrapper.find(GlFormCheckbox); const findCheckbox = () => wrapper.find(GlFormCheckbox);
const findHelpLink = () => wrapper.find(GlLink); const findHelpLink = () => wrapper.find(GlLink);
const createComponent = () => { const createComponent = (handlers) => {
requestHandlers = {
keepLatestArtifactQueryHandler: jest.fn().mockResolvedValue(keepLatestArtifactMock),
keepLatestArtifactMutationHandler: jest
.fn()
.mockResolvedValue(keepLatestArtifactMockResponse),
...handlers,
};
apolloProvider = createMockApollo([
[GetKeepLatestArtifactProjectSetting, requestHandlers.keepLatestArtifactQueryHandler],
[UpdateKeepLatestArtifactProjectSetting, requestHandlers.keepLatestArtifactMutationHandler],
]);
wrapper = shallowMount(KeepLatestArtifactCheckbox, { wrapper = shallowMount(KeepLatestArtifactCheckbox, {
provide: { provide: {
fullPath, fullPath,
helpPagePath, helpPagePath,
}, },
mocks: { localVue,
$apollo: { apolloProvider,
mutate,
},
},
}); });
}; };
...@@ -34,6 +63,7 @@ describe('Keep latest artifact checkbox', () => { ...@@ -34,6 +63,7 @@ describe('Keep latest artifact checkbox', () => {
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
wrapper = null; wrapper = null;
apolloProvider = null;
}); });
it('displays the checkbox and the help link', () => { it('displays the checkbox and the help link', () => {
...@@ -42,21 +72,17 @@ describe('Keep latest artifact checkbox', () => { ...@@ -42,21 +72,17 @@ describe('Keep latest artifact checkbox', () => {
}); });
it('sets correct setting value in checkbox with query result', async () => { it('sets correct setting value in checkbox with query result', async () => {
await wrapper.setData({ keepLatestArtifact: true }); await wrapper.vm.$nextTick();
expect(wrapper.element).toMatchSnapshot(); expect(wrapper.element).toMatchSnapshot();
}); });
it('calls mutation on artifact setting change with correct payload', () => { it('calls mutation on artifact setting change with correct payload', () => {
findCheckbox().vm.$emit('change', false); findCheckbox().vm.$emit('change', false);
const expected = { expect(requestHandlers.keepLatestArtifactMutationHandler).toHaveBeenCalledWith({
mutation: UpdateKeepLatestArtifactProjectSetting,
variables: {
fullPath, fullPath,
keepLatestArtifact: false, keepLatestArtifact: false,
}, });
};
expect(mutate).toHaveBeenCalledWith(expected);
}); });
}); });
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