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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

190 lines
5.8 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-04-04 11:22:00 +05:30
import Vue, { nextTick } from 'vue';
2022-03-02 08:16:31 +05:30
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';
2022-05-07 20:08:51 +05:30
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
2022-03-02 08:16:31 +05:30
import createMockApollo from 'helpers/mock_apollo_helper';
2020-03-13 15:44:24 +05:30
2023-01-13 00:05:48 +05:30
jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal');
2022-05-07 20:08:51 +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) => {
2022-08-27 11:52:29 +05:30
const buttons = wrapper.findAllComponents(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();
2022-05-07 20:08:51 +05:30
confirmAction.mockReset();
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 });
2022-08-27 11:52:29 +05:30
expect(wrapper.findComponent(GlDropdown).findAllComponents(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();
2022-08-27 11:52:29 +05:30
expect(wrapper.findComponent(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', () => {
2022-08-27 11:52:29 +05:30
expect(wrapper.findAllComponents(GlDropdownItem)).toHaveLength(actions.length);
2020-03-13 15:44:24 +05:30
});
it("should render a disabled action when it's not playable", () => {
2022-08-27 11:52:29 +05:30
const dropdownItems = wrapper.findAllComponents(GlDropdownItem);
2021-02-22 17:27:13 +05:30
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 } = {}) => {
2022-05-07 20:08:51 +05:30
confirmAction.mockResolvedValueOnce(confirm);
2021-02-22 17:27:13 +05:30
findDropdownItem(scheduledJobAction).vm.$emit('click');
2022-04-04 11:22:00 +05:30
await 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', () => {
2022-05-07 20:08:51 +05:30
expect(confirmAction).toHaveBeenCalled();
2021-02-22 17:27:13 +05:30
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', () => {
2022-08-27 11:52:29 +05:30
expect(wrapper.findComponent(GlLoadingIcon).isVisible()).toBe(true);
2021-02-22 17:27:13 +05:30
});
2020-03-13 15:44:24 +05:30
});
2021-02-22 17:27:13 +05:30
describe('when postAction event is denied', () => {
2022-05-07 20:08:51 +05:30
beforeEach(async () => {
2021-02-22 17:27:13 +05:30
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', () => {
2022-05-07 20:08:51 +05:30
expect(confirmAction).toHaveBeenCalled();
2021-02-22 17:27:13 +05:30
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
});