2021-10-27 15:23:28 +05:30
|
|
|
import { GlModal, GlSprintf } from '@gitlab/ui';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2022-01-26 12:08:38 +05:30
|
|
|
import Vue, { nextTick } from 'vue';
|
|
|
|
import VueApollo from 'vue-apollo';
|
2022-08-13 15:12:31 +05:30
|
|
|
import { trimText } from 'helpers/text_helper';
|
2019-07-07 11:18:12 +05:30
|
|
|
import ConfirmRollbackModal from '~/environments/components/confirm_rollback_modal.vue';
|
2022-01-26 12:08:38 +05:30
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
2019-07-07 11:18:12 +05:30
|
|
|
import eventHub from '~/environments/event_hub';
|
|
|
|
|
|
|
|
describe('Confirm Rollback Modal Component', () => {
|
|
|
|
let environment;
|
2021-10-27 15:23:28 +05:30
|
|
|
let component;
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
const envWithLastDeployment = {
|
|
|
|
name: 'test',
|
|
|
|
last_deployment: {
|
|
|
|
commit: {
|
|
|
|
short_id: 'abc0123',
|
2019-07-07 11:18:12 +05:30
|
|
|
},
|
2021-10-27 15:23:28 +05:30
|
|
|
},
|
|
|
|
modalId: 'test',
|
|
|
|
};
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const envWithLastDeploymentGraphql = {
|
|
|
|
name: 'test',
|
|
|
|
lastDeployment: {
|
|
|
|
commit: {
|
|
|
|
shortId: 'abc0123',
|
|
|
|
},
|
2022-03-02 08:16:31 +05:30
|
|
|
isLast: true,
|
2022-01-26 12:08:38 +05:30
|
|
|
},
|
|
|
|
modalId: 'test',
|
|
|
|
};
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
const envWithoutLastDeployment = {
|
|
|
|
name: 'test',
|
|
|
|
modalId: 'test',
|
|
|
|
commitShortSha: 'abc0123',
|
|
|
|
commitUrl: 'test/-/commit/abc0123',
|
|
|
|
};
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
const retryPath = 'test/-/jobs/123/retry';
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const createComponent = (props = {}, options = {}) => {
|
2021-10-27 15:23:28 +05:30
|
|
|
component = shallowMount(ConfirmRollbackModal, {
|
2019-07-07 11:18:12 +05:30
|
|
|
propsData: {
|
2021-10-27 15:23:28 +05:30
|
|
|
...props,
|
|
|
|
},
|
|
|
|
stubs: {
|
|
|
|
GlSprintf,
|
2019-07-07 11:18:12 +05:30
|
|
|
},
|
2022-01-26 12:08:38 +05:30
|
|
|
...options,
|
2019-07-07 11:18:12 +05:30
|
|
|
});
|
2021-10-27 15:23:28 +05:30
|
|
|
};
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
describe.each`
|
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it('should show "Rollback" when isLastDeployment is false', () => {
|
|
|
|
createComponent({
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
isLastDeployment: false,
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
retryUrl,
|
|
|
|
});
|
2022-08-27 11:52:29 +05:30
|
|
|
const modal = component.findComponent(GlModal);
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
expect(modal.attributes('title')).toContain('Rollback');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(modal.props('actionPrimary').text).toBe('Rollback environment');
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(modal.props('actionPrimary').attributes).toEqual(primaryPropsAttrs);
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(trimText(modal.text())).toContain('commit abc0123');
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(modal.text()).toContain('Are you sure you want to continue?');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show "Re-deploy" when isLastDeployment is true', () => {
|
|
|
|
createComponent({
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
isLastDeployment: true,
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
});
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
const modal = component.findComponent(GlModal);
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
expect(modal.attributes('title')).toContain('Re-deploy');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(modal.props('actionPrimary').text).toBe('Re-deploy environment');
|
|
|
|
expect(trimText(modal.text())).toContain('commit abc0123');
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(modal.text()).toContain('Are you sure you want to continue?');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit the "rollback" event when "ok" is clicked', () => {
|
|
|
|
const env = { ...environmentData, isLastDeployment: true };
|
|
|
|
|
|
|
|
createComponent({
|
|
|
|
environment: env,
|
|
|
|
hasMultipleCommits,
|
|
|
|
});
|
|
|
|
|
|
|
|
const eventHubSpy = jest.spyOn(eventHub, '$emit');
|
2022-08-27 11:52:29 +05:30
|
|
|
const modal = component.findComponent(GlModal);
|
2021-10-27 15:23:28 +05:30
|
|
|
modal.vm.$emit('ok');
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(eventHubSpy).toHaveBeenCalledWith('rollbackEnvironment', env);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
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-03-02 08:16:31 +05:30
|
|
|
isLast: false,
|
2022-01-26 12:08:38 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
retryUrl,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
2022-08-27 11:52:29 +05:30
|
|
|
const modal = component.findComponent(GlModal);
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(trimText(modal.text())).toContain('commit abc0123');
|
2022-01-26 12:08:38 +05:30
|
|
|
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-03-02 08:16:31 +05:30
|
|
|
isLast: false,
|
2022-01-26 12:08:38 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
retryUrl,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
2022-08-27 11:52:29 +05:30
|
|
|
const modal = component.findComponent(GlModal);
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
expect(modal.attributes('title')).toContain('Rollback');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(modal.props('actionPrimary').text).toBe('Rollback environment');
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(modal.props('actionPrimary').attributes).toEqual(primaryPropsAttrs);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show "Re-deploy" when isLastDeployment is true', () => {
|
|
|
|
createComponent(
|
|
|
|
{
|
|
|
|
environment: {
|
|
|
|
...environment,
|
|
|
|
lastDeployment: {
|
|
|
|
...environment.lastDeployment,
|
2022-03-02 08:16:31 +05:30
|
|
|
isLast: true,
|
2022-01-26 12:08:38 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
hasMultipleCommits,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
const modal = component.findComponent(GlModal);
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
expect(modal.attributes('title')).toContain('Re-deploy');
|
|
|
|
expect(modal.attributes('title')).toContain('test');
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(modal.props('actionPrimary').text).toBe('Re-deploy environment');
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit the "rollback" mutation when "ok" is clicked', async () => {
|
|
|
|
const env = { ...environmentData, isLastDeployment: true };
|
|
|
|
|
|
|
|
createComponent(
|
|
|
|
{
|
|
|
|
environment: env,
|
|
|
|
hasMultipleCommits,
|
|
|
|
graphql: true,
|
|
|
|
},
|
|
|
|
{ apolloProvider },
|
|
|
|
);
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
const modal = component.findComponent(GlModal);
|
2022-01-26 12:08:38 +05:30
|
|
|
modal.vm.$emit('ok');
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
expect(rollbackResolver).toHaveBeenCalledWith(
|
|
|
|
expect.anything(),
|
|
|
|
{ environment: env },
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2019-07-07 11:18:12 +05:30
|
|
|
});
|