debian-mirror-gitlab/spec/frontend/environments/confirm_rollback_modal_spec.js

237 lines
7.3 KiB
JavaScript
Raw Normal View History

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';
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',
},
'last?': true,
},
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,
});
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);
expect(modal.text()).toContain('commit abc0123');
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,
});
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?');
});
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');
const modal = component.find(GlModal);
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,
'last?': false,
},
},
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,
'last?': false,
},
},
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,
'last?': true,
},
},
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-07-07 11:18:12 +05:30
});