2020-11-18 01:09:29 -05:00
|
|
|
import { shallowMount, mount } from '@vue/test-utils';
|
2020-02-05 10:08:48 -05:00
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2020-11-18 01:09:29 -05:00
|
|
|
import { GlDropdown, GlDropdownItem, GlLoadingIcon, GlIcon } from '@gitlab/ui';
|
2020-10-12 05:08:38 -04:00
|
|
|
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
|
2020-02-05 10:08:48 -05:00
|
|
|
import eventHub from '~/environments/event_hub';
|
|
|
|
import EnvironmentActions from '~/environments/components/environment_actions.vue';
|
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
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-02-05 10:08:48 -05:00
|
|
|
describe('EnvironmentActions Component', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
let wrapper;
|
2020-02-05 10:08:48 -05:00
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
const findEnvironmentActionsButton = () =>
|
|
|
|
wrapper.find('[data-testid="environment-actions-button"]');
|
2020-10-12 05:08:38 -04:00
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
function createComponent(props, { mountFn = shallowMount } = {}) {
|
|
|
|
wrapper = mountFn(EnvironmentActions, {
|
|
|
|
propsData: { actions: [], ...props },
|
2020-10-12 05:08:38 -04:00
|
|
|
directives: {
|
|
|
|
GlTooltip: createMockDirective(),
|
|
|
|
},
|
|
|
|
});
|
2020-11-18 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function createComponentWithScheduledJobs(opts = {}) {
|
|
|
|
return createComponent({ actions: [scheduledJobAction, expiredJobAction] }, opts);
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const findDropdownItem = (action) => {
|
2020-11-18 01:09:29 -05:00
|
|
|
const buttons = wrapper.findAll(GlDropdownItem);
|
2020-12-23 16:10:24 -05:00
|
|
|
return buttons.filter((button) => button.text().startsWith(action.name)).at(0);
|
2020-11-18 01:09:29 -05:00
|
|
|
};
|
2020-02-05 10:08:48 -05:00
|
|
|
|
|
|
|
afterEach(() => {
|
2020-11-18 01:09:29 -05:00
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render a dropdown button with 2 icons', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
createComponent({}, { mountFn: mount });
|
|
|
|
expect(wrapper.find(GlDropdown).findAll(GlIcon).length).toBe(2);
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render a dropdown button with aria-label description', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
createComponent();
|
|
|
|
expect(wrapper.find(GlDropdown).attributes('aria-label')).toBe('Deploy to...');
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
|
2020-10-12 05:08:38 -04:00
|
|
|
it('should render a tooltip', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
createComponent();
|
2020-10-12 05:08:38 -04:00
|
|
|
const tooltip = getBinding(findEnvironmentActionsButton().element, 'gl-tooltip');
|
|
|
|
expect(tooltip).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2020-02-05 10:08:48 -05:00
|
|
|
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(() => {
|
2020-11-18 01:09:29 -05:00
|
|
|
createComponent({ actions });
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render a dropdown with the provided list of actions', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
expect(wrapper.findAll(GlDropdownItem)).toHaveLength(actions.length);
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should render a disabled action when it's not playable", () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
const dropdownItems = wrapper.findAll(GlDropdownItem);
|
|
|
|
const lastDropdownItem = dropdownItems.at(dropdownItems.length - 1);
|
|
|
|
expect(lastDropdownItem.attributes('disabled')).toBe('true');
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('scheduled jobs', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
let emitSpy;
|
|
|
|
|
|
|
|
const clickAndConfirm = async ({ confirm = true } = {}) => {
|
|
|
|
jest.spyOn(window, 'confirm').mockImplementation(() => confirm);
|
|
|
|
|
|
|
|
findDropdownItem(scheduledJobAction).vm.$emit('click');
|
|
|
|
await wrapper.vm.$nextTick();
|
2020-02-05 10:08:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-11-18 01:09:29 -05:00
|
|
|
emitSpy = jest.fn();
|
|
|
|
eventHub.$on('postAction', emitSpy);
|
2020-02-05 10:08:48 -05:00
|
|
|
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
|
|
|
|
});
|
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
describe('when postAction event is confirmed', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
createComponentWithScheduledJobs({ mountFn: mount });
|
|
|
|
clickAndConfirm();
|
|
|
|
});
|
2020-02-05 10:08:48 -05:00
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
it('emits postAction event', () => {
|
|
|
|
expect(window.confirm).toHaveBeenCalled();
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith({ endpoint: scheduledJobAction.playPath });
|
|
|
|
});
|
2020-02-05 10:08:48 -05:00
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
it('should render a dropdown button with a loading icon', () => {
|
|
|
|
expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(true);
|
|
|
|
});
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
describe('when postAction event is denied', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponentWithScheduledJobs({ mountFn: mount });
|
|
|
|
clickAndConfirm({ confirm: false });
|
|
|
|
});
|
2020-02-05 10:08:48 -05:00
|
|
|
|
2020-11-18 01:09:29 -05:00
|
|
|
it('does not emit postAction event if confirmation is cancelled', () => {
|
|
|
|
expect(window.confirm).toHaveBeenCalled();
|
|
|
|
expect(emitSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2020-02-05 10:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the remaining time in the dropdown', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
createComponentWithScheduledJobs();
|
2020-02-05 10:08:48 -05:00
|
|
|
expect(findDropdownItem(scheduledJobAction).text()).toContain('24:00:00');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays 00:00:00 for expired jobs in the dropdown', () => {
|
2020-11-18 01:09:29 -05:00
|
|
|
createComponentWithScheduledJobs();
|
2020-02-05 10:08:48 -05:00
|
|
|
expect(findDropdownItem(expiredJobAction).text()).toContain('00:00:00');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|