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

187 lines
5.6 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlDropdown, GlDropdownItem, GlLoadingIcon, GlIcon } from '@gitlab/ui';
2021-02-22 17:27:13 +05:30
import { shallowMount, mount } from '@vue/test-utils';
2022-03-02 08:16:31 +05:30
import Vue from 'vue';
import VueApollo from 'vue-apollo';
2020-03-13 15:44:24 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2021-01-03 14:25:43 +05:30
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
2020-03-13 15:44:24 +05:30
import EnvironmentActions from '~/environments/components/environment_actions.vue';
2021-03-11 19:13:27 +05:30
import eventHub from '~/environments/event_hub';
2022-03-02 08:16:31 +05:30
import actionMutation from '~/environments/graphql/mutations/action.mutation.graphql';
import createMockApollo from 'helpers/mock_apollo_helper';
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
const scheduledJobAction = {
name: 'scheduled action',
playPath: `${TEST_HOST}/scheduled/job/action`,
playable: true,
scheduledAt: '2063-04-05T00:42:00Z',
};
const expiredJobAction = {
name: 'expired action',
playPath: `${TEST_HOST}/expired/job/action`,
playable: true,
scheduledAt: '2018-10-05T08:23:00Z',
};
2020-03-13 15:44:24 +05:30
describe('EnvironmentActions Component', () => {
2021-02-22 17:27:13 +05:30
let wrapper;
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
const findEnvironmentActionsButton = () =>
wrapper.find('[data-testid="environment-actions-button"]');
2021-01-03 14:25:43 +05:30
2022-03-02 08:16:31 +05:30
function createComponent(props, { mountFn = shallowMount, options = {} } = {}) {
2021-02-22 17:27:13 +05:30
wrapper = mountFn(EnvironmentActions, {
propsData: { actions: [], ...props },
2021-01-03 14:25:43 +05:30
directives: {
GlTooltip: createMockDirective(),
},
2022-03-02 08:16:31 +05:30
...options,
2021-01-03 14:25:43 +05:30
});
2021-02-22 17:27:13 +05:30
}
function createComponentWithScheduledJobs(opts = {}) {
return createComponent({ actions: [scheduledJobAction, expiredJobAction] }, opts);
}
2021-03-08 18:12:59 +05:30
const findDropdownItem = (action) => {
2021-02-22 17:27:13 +05:30
const buttons = wrapper.findAll(GlDropdownItem);
2021-03-08 18:12:59 +05:30
return buttons.filter((button) => button.text().startsWith(action.name)).at(0);
2021-02-22 17:27:13 +05:30
};
2020-03-13 15:44:24 +05:30
afterEach(() => {
2021-02-22 17:27:13 +05:30
wrapper.destroy();
wrapper = null;
2020-03-13 15:44:24 +05:30
});
it('should render a dropdown button with 2 icons', () => {
2021-02-22 17:27:13 +05:30
createComponent({}, { mountFn: mount });
expect(wrapper.find(GlDropdown).findAll(GlIcon).length).toBe(2);
2020-03-13 15:44:24 +05:30
});
it('should render a dropdown button with aria-label description', () => {
2021-02-22 17:27:13 +05:30
createComponent();
expect(wrapper.find(GlDropdown).attributes('aria-label')).toBe('Deploy to...');
2020-03-13 15:44:24 +05:30
});
2021-01-03 14:25:43 +05:30
it('should render a tooltip', () => {
2021-02-22 17:27:13 +05:30
createComponent();
2021-01-03 14:25:43 +05:30
const tooltip = getBinding(findEnvironmentActionsButton().element, 'gl-tooltip');
expect(tooltip).toBeDefined();
});
2020-03-13 15:44:24 +05:30
describe('manual actions', () => {
const actions = [
{
name: 'bar',
play_path: 'https://gitlab.com/play',
},
{
name: 'foo',
play_path: '#',
},
{
name: 'foo bar',
play_path: 'url',
playable: false,
},
];
beforeEach(() => {
2021-02-22 17:27:13 +05:30
createComponent({ actions });
2020-03-13 15:44:24 +05:30
});
it('should render a dropdown with the provided list of actions', () => {
2021-02-22 17:27:13 +05:30
expect(wrapper.findAll(GlDropdownItem)).toHaveLength(actions.length);
2020-03-13 15:44:24 +05:30
});
it("should render a disabled action when it's not playable", () => {
2021-02-22 17:27:13 +05:30
const dropdownItems = wrapper.findAll(GlDropdownItem);
const lastDropdownItem = dropdownItems.at(dropdownItems.length - 1);
expect(lastDropdownItem.attributes('disabled')).toBe('true');
2020-03-13 15:44:24 +05:30
});
});
describe('scheduled jobs', () => {
2021-02-22 17:27:13 +05:30
let emitSpy;
const clickAndConfirm = async ({ confirm = true } = {}) => {
jest.spyOn(window, 'confirm').mockImplementation(() => confirm);
findDropdownItem(scheduledJobAction).vm.$emit('click');
await wrapper.vm.$nextTick();
2020-03-13 15:44:24 +05:30
};
beforeEach(() => {
2021-02-22 17:27:13 +05:30
emitSpy = jest.fn();
eventHub.$on('postAction', emitSpy);
2020-03-13 15:44:24 +05:30
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
});
2021-02-22 17:27:13 +05:30
describe('when postAction event is confirmed', () => {
beforeEach(async () => {
createComponentWithScheduledJobs({ mountFn: mount });
clickAndConfirm();
});
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
it('emits postAction event', () => {
expect(window.confirm).toHaveBeenCalled();
expect(emitSpy).toHaveBeenCalledWith({ endpoint: scheduledJobAction.playPath });
});
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
it('should render a dropdown button with a loading icon', () => {
expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(true);
});
2020-03-13 15:44:24 +05:30
});
2021-02-22 17:27:13 +05:30
describe('when postAction event is denied', () => {
beforeEach(() => {
createComponentWithScheduledJobs({ mountFn: mount });
clickAndConfirm({ confirm: false });
});
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
it('does not emit postAction event if confirmation is cancelled', () => {
expect(window.confirm).toHaveBeenCalled();
expect(emitSpy).not.toHaveBeenCalled();
});
2020-03-13 15:44:24 +05:30
});
it('displays the remaining time in the dropdown', () => {
2021-02-22 17:27:13 +05:30
createComponentWithScheduledJobs();
2020-03-13 15:44:24 +05:30
expect(findDropdownItem(scheduledJobAction).text()).toContain('24:00:00');
});
it('displays 00:00:00 for expired jobs in the dropdown', () => {
2021-02-22 17:27:13 +05:30
createComponentWithScheduledJobs();
2020-03-13 15:44:24 +05:30
expect(findDropdownItem(expiredJobAction).text()).toContain('00:00:00');
});
});
2022-03-02 08:16:31 +05:30
describe('graphql', () => {
Vue.use(VueApollo);
const action = {
name: 'bar',
play_path: 'https://gitlab.com/play',
};
let mockApollo;
beforeEach(() => {
mockApollo = createMockApollo();
createComponent(
{ actions: [action], graphql: true },
{ options: { apolloProvider: mockApollo } },
);
});
it('should trigger a graphql mutation on click', () => {
jest.spyOn(mockApollo.defaultClient, 'mutate');
findDropdownItem(action).vm.$emit('click');
expect(mockApollo.defaultClient.mutate).toHaveBeenCalledWith({
mutation: actionMutation,
variables: { action },
});
});
});
2020-03-13 15:44:24 +05:30
});