2021-08-13 08:10:58 -04:00
|
|
|
import { GlModal, GlSprintf } from '@gitlab/ui';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2021-12-13 13:15:18 -05:00
|
|
|
import Vue, { nextTick } from 'vue';
|
|
|
|
import VueApollo from 'vue-apollo';
|
2019-03-05 13:33:10 -05:00
|
|
|
import ConfirmRollbackModal from '~/environments/components/confirm_rollback_modal.vue';
|
2021-12-13 13:15:18 -05:00
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
2019-03-05 13:33:10 -05:00
|
|
|
import eventHub from '~/environments/event_hub';
|
|
|
|
|
|
|
|
describe('Confirm Rollback Modal Component', () => {
|
|
|
|
let environment;
|
2021-08-13 08:10:58 -04:00
|
|
|
let component;
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2021-08-01 23:09:42 -04:00
|
|
|
const envWithLastDeployment = {
|
|
|
|
name: 'test',
|
|
|
|
last_deployment: {
|
|
|
|
commit: {
|
|
|
|
short_id: 'abc0123',
|
2019-03-05 13:33:10 -05:00
|
|
|
},
|
2021-08-01 23:09:42 -04:00
|
|
|
},
|
|
|
|
modalId: 'test',
|
|
|
|
};
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2021-12-13 13:15:18 -05:00
|
|
|
const envWithLastDeploymentGraphql = {
|
|
|
|
name: 'test',
|
|
|
|
lastDeployment: {
|
|
|
|
commit: {
|
|
|
|
shortId: 'abc0123',
|
|
|
|
},
|
2022-01-10 19:13:53 -05:00
|
|
|
isLast: true,
|
2021-12-13 13:15:18 -05:00
|
|
|
},
|
|
|
|
modalId: 'test',
|
|
|
|
};
|
|
|
|
|
2021-08-01 23:09:42 -04:00
|
|
|
const envWithoutLastDeployment = {
|
|
|
|
name: 'test',
|
|
|
|
modalId: 'test',
|
|
|
|
commitShortSha: 'abc0123',
|
|
|
|
commitUrl: 'test/-/commit/abc0123',
|
|
|
|
};
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
const retryPath = 'test/-/jobs/123/retry';
|
|
|
|
|
2021-12-13 13:15:18 -05:00
|
|
|
const createComponent = (props = {}, options = {}) => {
|
2021-08-13 08:10:58 -04:00
|
|
|
component = shallowMount(ConfirmRollbackModal, {
|
|
|
|
propsData: {
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
stubs: {
|
|
|
|
GlSprintf,
|
|
|
|
},
|
2021-12-13 13:15:18 -05:00
|
|
|
...options,
|
2021-08-13 08:10:58 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-08-01 23:09:42 -04:00
|
|
|
describe.each`
|
2021-08-11 11:10:57 -04:00
|
|
|
hasMultipleCommits | environmentData | retryUrl | primaryPropsAttrs
|
|
|
|
${true} | ${envWithLastDeployment} | ${null} | ${[{ variant: 'danger' }]}
|
|
|
|
${false} | ${envWithoutLastDeployment} | ${retryPath} | ${[{ variant: 'danger' }, { 'data-method': 'post' }, { href: retryPath }]}
|
|
|
|
`(
|
|
|
|
'when hasMultipleCommits=$hasMultipleCommits',
|
|
|
|
({ hasMultipleCommits, environmentData, retryUrl, primaryPropsAttrs }) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
environment = environmentData;
|
|
|
|
});
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
it('should show "Rollback" when isLastDeployment is false', () => {
|
2021-08-13 08:10:58 -04:00
|
|
|
createComponent({
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
isLastDeployment: false,
|
2021-08-01 23:09:42 -04:00
|
|
|
},
|
2021-08-13 08:10:58 -04:00
|
|
|
hasMultipleCommits,
|
|
|
|
retryUrl,
|
2021-08-11 11:10:57 -04:00
|
|
|
});
|
|
|
|
const modal = component.find(GlModal);
|
2021-08-01 23:09:42 -04:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
expect(modal.attributes('title')).toContain('Rollback');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
|
|
|
expect(modal.props('actionPrimary').text).toBe('Rollback');
|
|
|
|
expect(modal.props('actionPrimary').attributes).toEqual(primaryPropsAttrs);
|
|
|
|
expect(modal.text()).toContain('commit abc0123');
|
|
|
|
expect(modal.text()).toContain('Are you sure you want to continue?');
|
|
|
|
});
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
it('should show "Re-deploy" when isLastDeployment is true', () => {
|
2021-08-13 08:10:58 -04:00
|
|
|
createComponent({
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
isLastDeployment: true,
|
2021-08-01 23:09:42 -04:00
|
|
|
},
|
2021-08-13 08:10:58 -04:00
|
|
|
hasMultipleCommits,
|
2021-08-11 11:10:57 -04:00
|
|
|
});
|
2021-08-13 08:10:58 -04:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
const modal = component.find(GlModal);
|
|
|
|
|
|
|
|
expect(modal.attributes('title')).toContain('Re-deploy');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
|
|
|
expect(modal.props('actionPrimary').text).toBe('Re-deploy');
|
|
|
|
expect(modal.text()).toContain('commit abc0123');
|
|
|
|
expect(modal.text()).toContain('Are you sure you want to continue?');
|
2021-08-01 23:09:42 -04:00
|
|
|
});
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
it('should emit the "rollback" event when "ok" is clicked', () => {
|
|
|
|
const env = { ...environmentData, isLastDeployment: true };
|
2021-08-13 08:10:58 -04:00
|
|
|
|
|
|
|
createComponent({
|
|
|
|
environment: env,
|
|
|
|
hasMultipleCommits,
|
2021-08-11 11:10:57 -04:00
|
|
|
});
|
2021-08-13 08:10:58 -04:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
const eventHubSpy = jest.spyOn(eventHub, '$emit');
|
|
|
|
const modal = component.find(GlModal);
|
|
|
|
modal.vm.$emit('ok');
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2021-08-11 11:10:57 -04:00
|
|
|
expect(eventHubSpy).toHaveBeenCalledWith('rollbackEnvironment', env);
|
2021-08-01 23:09:42 -04:00
|
|
|
});
|
2021-08-11 11:10:57 -04:00
|
|
|
},
|
|
|
|
);
|
2021-12-13 13:15:18 -05:00
|
|
|
|
|
|
|
describe('graphql', () => {
|
|
|
|
describe.each`
|
|
|
|
hasMultipleCommits | environmentData | retryUrl | primaryPropsAttrs
|
|
|
|
${true} | ${envWithLastDeploymentGraphql} | ${null} | ${[{ variant: 'danger' }]}
|
|
|
|
${false} | ${envWithoutLastDeployment} | ${retryPath} | ${[{ variant: 'danger' }, { 'data-method': 'post' }, { href: retryPath }]}
|
|
|
|
`(
|
|
|
|
'when hasMultipleCommits=$hasMultipleCommits',
|
|
|
|
({ hasMultipleCommits, environmentData, retryUrl, primaryPropsAttrs }) => {
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
|
|
|
let apolloProvider;
|
|
|
|
let rollbackResolver;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
rollbackResolver = jest.fn();
|
|
|
|
apolloProvider = createMockApollo([], {
|
|
|
|
Mutation: { rollbackEnvironment: rollbackResolver },
|
|
|
|
});
|
|
|
|
environment = environmentData;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set contain the commit hash and ask for confirmation', () => {
|
|
|
|
createComponent(
|
|
|
|
{
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
lastDeployment: {
|
|
|
|
...environment.lastDeployment,
|
2022-01-10 19:13:53 -05:00
|
|
|
isLast: false,
|
2021-12-13 13:15:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
retryUrl,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
|
|
|
const modal = component.find(GlModal);
|
|
|
|
|
|
|
|
expect(modal.text()).toContain('commit abc0123');
|
|
|
|
expect(modal.text()).toContain('Are you sure you want to continue?');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show "Rollback" when isLastDeployment is false', () => {
|
|
|
|
createComponent(
|
|
|
|
{
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
lastDeployment: {
|
|
|
|
...environment.lastDeployment,
|
2022-01-10 19:13:53 -05:00
|
|
|
isLast: false,
|
2021-12-13 13:15:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
retryUrl,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
|
|
|
const modal = component.find(GlModal);
|
|
|
|
|
|
|
|
expect(modal.attributes('title')).toContain('Rollback');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
|
|
|
expect(modal.props('actionPrimary').text).toBe('Rollback');
|
|
|
|
expect(modal.props('actionPrimary').attributes).toEqual(primaryPropsAttrs);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show "Re-deploy" when isLastDeployment is true', () => {
|
|
|
|
createComponent(
|
|
|
|
{
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
lastDeployment: {
|
|
|
|
...environment.lastDeployment,
|
2022-01-10 19:13:53 -05:00
|
|
|
isLast: true,
|
2021-12-13 13:15:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
|
|
|
|
|
|
|
const modal = component.find(GlModal);
|
|
|
|
|
|
|
|
expect(modal.attributes('title')).toContain('Re-deploy');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
|
|
|
expect(modal.props('actionPrimary').text).toBe('Re-deploy');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit the "rollback" mutation when "ok" is clicked', async () => {
|
|
|
|
const env = { ...environmentData, isLastDeployment: true };
|
|
|
|
|
|
|
|
createComponent(
|
|
|
|
{
|
|
|
|
environment: env,
|
|
|
|
hasMultipleCommits,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
|
|
|
|
|
|
|
const modal = component.find(GlModal);
|
|
|
|
modal.vm.$emit('ok');
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
expect(rollbackResolver).toHaveBeenCalledWith(
|
|
|
|
expect.anything(),
|
|
|
|
{ environment: env },
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2019-03-05 13:33:10 -05:00
|
|
|
});
|