Commit b26fb357 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'afontaine/environment-auto-stop' into 'master'

Make environment pin work with GraphQL

See merge request gitlab-org/gitlab!79439
parents bf4b3ad9 f0aa39f2
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
import { GlDropdownItem } from '@gitlab/ui'; import { GlDropdownItem } from '@gitlab/ui';
import { __ } from '~/locale'; import { __ } from '~/locale';
import eventHub from '../event_hub'; import eventHub from '../event_hub';
import cancelAutoStopMutation from '../graphql/mutations/cancel_auto_stop.mutation.graphql';
export default { export default {
components: { components: {
...@@ -16,10 +17,22 @@ export default { ...@@ -16,10 +17,22 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
graphql: {
type: Boolean,
required: false,
default: false,
},
}, },
methods: { methods: {
onPinClick() { onPinClick() {
eventHub.$emit('cancelAutoStop', this.autoStopUrl); if (this.graphql) {
this.$apollo.mutate({
mutation: cancelAutoStopMutation,
variables: { autoStopUrl: this.autoStopUrl },
});
} else {
eventHub.$emit('cancelAutoStop', this.autoStopUrl);
}
}, },
}, },
title: __('Prevent auto-stopping'), title: __('Prevent auto-stopping'),
......
...@@ -243,6 +243,7 @@ export default { ...@@ -243,6 +243,7 @@ export default {
<pin <pin
v-if="canShowAutoStopDate" v-if="canShowAutoStopDate"
:auto-stop-url="autoStopPath" :auto-stop-url="autoStopPath"
graphql
data-track-action="click_button" data-track-action="click_button"
data-track-label="environment_pin" data-track-label="environment_pin"
/> />
......
mutation cancelAutoStop($environment: LocalEnvironment) { mutation cancelAutoStop($autoStopUrl: String!) {
cancelAutoStop(environment: $environment) @client { cancelAutoStop(autoStopUrl: $autoStopUrl) @client {
errors errors
} }
} }
...@@ -134,9 +134,9 @@ export const resolvers = (endpoint) => ({ ...@@ -134,9 +134,9 @@ export const resolvers = (endpoint) => ({
data: { environmentToRollback: environment }, data: { environmentToRollback: environment },
}); });
}, },
cancelAutoStop(_, { environment: { autoStopPath } }) { cancelAutoStop(_, { autoStopUrl }) {
return axios return axios
.post(autoStopPath) .post(autoStopUrl)
.then(() => buildErrors()) .then(() => buildErrors())
.catch((err) => .catch((err) =>
buildErrors([ buildErrors([
......
...@@ -77,7 +77,7 @@ extend type Mutation { ...@@ -77,7 +77,7 @@ extend type Mutation {
stopEnvironment(environment: LocalEnvironmentInput): LocalErrors stopEnvironment(environment: LocalEnvironmentInput): LocalErrors
deleteEnvironment(environment: LocalEnvironmentInput): LocalErrors deleteEnvironment(environment: LocalEnvironmentInput): LocalErrors
rollbackEnvironment(environment: LocalEnvironmentInput): LocalErrors rollbackEnvironment(environment: LocalEnvironmentInput): LocalErrors
cancelAutoStop(environment: LocalEnvironmentInput): LocalErrors cancelAutoStop(autoStopUrl: String!): LocalErrors
setEnvironmentToDelete(environment: LocalEnvironmentInput): LocalErrors setEnvironmentToDelete(environment: LocalEnvironmentInput): LocalErrors
setEnvironmentToRollback(environment: LocalEnvironmentInput): LocalErrors setEnvironmentToRollback(environment: LocalEnvironmentInput): LocalErrors
setEnvironmentToStop(environment: LocalEnvironmentInput): LocalErrors setEnvironmentToStop(environment: LocalEnvironmentInput): LocalErrors
......
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlDropdownItem } from '@gitlab/ui'; import { GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import cancelAutoStopMutation from '~/environments/graphql/mutations/cancel_auto_stop.mutation.graphql';
import createMockApollo from 'helpers/mock_apollo_helper';
import PinComponent from '~/environments/components/environment_pin.vue'; import PinComponent from '~/environments/components/environment_pin.vue';
import eventHub from '~/environments/event_hub'; import eventHub from '~/environments/event_hub';
...@@ -18,28 +22,66 @@ describe('Pin Component', () => { ...@@ -18,28 +22,66 @@ describe('Pin Component', () => {
const autoStopUrl = '/root/auto-stop-env-test/-/environments/38/cancel_auto_stop'; const autoStopUrl = '/root/auto-stop-env-test/-/environments/38/cancel_auto_stop';
beforeEach(() => { describe('without graphql', () => {
factory({ beforeEach(() => {
propsData: { factory({
autoStopUrl, propsData: {
}, autoStopUrl,
},
});
}); });
});
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
}); });
it('should render the component with descriptive text', () => { it('should render the component with descriptive text', () => {
expect(wrapper.text()).toBe('Prevent auto-stopping'); expect(wrapper.text()).toBe('Prevent auto-stopping');
});
it('should emit onPinClick when clicked', () => {
const eventHubSpy = jest.spyOn(eventHub, '$emit');
const item = wrapper.find(GlDropdownItem);
item.vm.$emit('click');
expect(eventHubSpy).toHaveBeenCalledWith('cancelAutoStop', autoStopUrl);
});
}); });
it('should emit onPinClick when clicked', () => { describe('with graphql', () => {
const eventHubSpy = jest.spyOn(eventHub, '$emit'); Vue.use(VueApollo);
const item = wrapper.find(GlDropdownItem); let mockApollo;
item.vm.$emit('click'); beforeEach(() => {
mockApollo = createMockApollo();
factory({
propsData: {
autoStopUrl,
graphql: true,
},
apolloProvider: mockApollo,
});
});
expect(eventHubSpy).toHaveBeenCalledWith('cancelAutoStop', autoStopUrl); afterEach(() => {
wrapper.destroy();
});
it('should render the component with descriptive text', () => {
expect(wrapper.text()).toBe('Prevent auto-stopping');
});
it('should emit onPinClick when clicked', () => {
jest.spyOn(mockApollo.defaultClient, 'mutate');
const item = wrapper.find(GlDropdownItem);
item.vm.$emit('click');
expect(mockApollo.defaultClient.mutate).toHaveBeenCalledWith({
mutation: cancelAutoStopMutation,
variables: { autoStopUrl },
});
});
}); });
}); });
...@@ -173,9 +173,7 @@ describe('~/frontend/environments/graphql/resolvers', () => { ...@@ -173,9 +173,7 @@ describe('~/frontend/environments/graphql/resolvers', () => {
it('should post to the auto stop path', async () => { it('should post to the auto stop path', async () => {
mock.onPost(ENDPOINT).reply(200); mock.onPost(ENDPOINT).reply(200);
await mockResolvers.Mutation.cancelAutoStop(null, { await mockResolvers.Mutation.cancelAutoStop(null, { autoStopUrl: ENDPOINT });
environment: { autoStopPath: ENDPOINT },
});
expect(mock.history.post).toContainEqual( expect(mock.history.post).toContainEqual(
expect.objectContaining({ url: ENDPOINT, method: 'post' }), expect.objectContaining({ url: ENDPOINT, method: 'post' }),
......
...@@ -191,9 +191,9 @@ describe('~/environments/components/new_environment_item.vue', () => { ...@@ -191,9 +191,9 @@ describe('~/environments/components/new_environment_item.vue', () => {
}); });
it('shows the option to pin the environment if there is an autostop date', () => { it('shows the option to pin the environment if there is an autostop date', () => {
const rollback = wrapper.findByRole('menuitem', { name: __('Prevent auto-stopping') }); const pin = wrapper.findByRole('menuitem', { name: __('Prevent auto-stopping') });
expect(rollback.exists()).toBe(true); expect(pin.exists()).toBe(true);
}); });
it('shows when the environment auto stops', () => { it('shows when the environment auto stops', () => {
...@@ -211,9 +211,9 @@ describe('~/environments/components/new_environment_item.vue', () => { ...@@ -211,9 +211,9 @@ describe('~/environments/components/new_environment_item.vue', () => {
it('does not show the option to pin the environment if there is no autostop date', () => { it('does not show the option to pin the environment if there is no autostop date', () => {
wrapper = createWrapper({ apolloProvider: createApolloProvider() }); wrapper = createWrapper({ apolloProvider: createApolloProvider() });
const rollback = wrapper.findByRole('menuitem', { name: __('Prevent auto-stopping') }); const pin = wrapper.findByRole('menuitem', { name: __('Prevent auto-stopping') });
expect(rollback.exists()).toBe(false); expect(pin.exists()).toBe(false);
}); });
it('does not show when the environment auto stops', () => { it('does not show when the environment auto stops', () => {
...@@ -246,9 +246,9 @@ describe('~/environments/components/new_environment_item.vue', () => { ...@@ -246,9 +246,9 @@ describe('~/environments/components/new_environment_item.vue', () => {
it('does not show the option to pin the environment if there is no autostop date', () => { it('does not show the option to pin the environment if there is no autostop date', () => {
wrapper = createWrapper({ apolloProvider: createApolloProvider() }); wrapper = createWrapper({ apolloProvider: createApolloProvider() });
const rollback = wrapper.findByRole('menuitem', { name: __('Prevent auto-stopping') }); const pin = wrapper.findByRole('menuitem', { name: __('Prevent auto-stopping') });
expect(rollback.exists()).toBe(false); expect(pin.exists()).toBe(false);
}); });
it('does not show when the environment auto stops', () => { it('does not show when the environment auto stops', () => {
......
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