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

154 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-02-22 17:27:13 +05:30
import { shallowMount, mount } from '@vue/test-utils';
2020-03-13 15:44:24 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2021-02-22 17:27:13 +05:30
import { GlDropdown, GlDropdownItem, GlLoadingIcon, GlIcon } from '@gitlab/ui';
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 eventHub from '~/environments/event_hub';
import EnvironmentActions from '~/environments/components/environment_actions.vue';
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
2021-02-22 17:27:13 +05:30
function createComponent(props, { mountFn = shallowMount } = {}) {
wrapper = mountFn(EnvironmentActions, {
propsData: { actions: [], ...props },
2021-01-03 14:25:43 +05:30
directives: {
GlTooltip: createMockDirective(),
},
});
2021-02-22 17:27:13 +05:30
}
function createComponentWithScheduledJobs(opts = {}) {
return createComponent({ actions: [scheduledJobAction, expiredJobAction] }, opts);
}
const findDropdownItem = action => {
const buttons = wrapper.findAll(GlDropdownItem);
return buttons.filter(button => button.text().startsWith(action.name)).at(0);
};
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');
});
});
});