debian-mirror-gitlab/spec/frontend/vue_shared/components/actions_button_spec.js

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

209 lines
5.5 KiB
JavaScript
Raw Normal View History

2023-03-04 22:38:38 +05:30
import { GlDropdown, GlDropdownDivider, GlButton, GlTooltip } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { shallowMount } from '@vue/test-utils';
2020-11-24 15:15:51 +05:30
import ActionsButton from '~/vue_shared/components/actions_button.vue';
const TEST_ACTION = {
key: 'action1',
text: 'Sample',
secondaryText: 'Lorem ipsum.',
tooltip: '',
href: '/sample',
2021-01-03 14:25:43 +05:30
attrs: {
'data-test': '123',
category: 'secondary',
href: '/sample',
variant: 'default',
},
2020-11-24 15:15:51 +05:30
};
const TEST_ACTION_2 = {
key: 'action2',
text: 'Sample 2',
secondaryText: 'Dolar sit amit.',
tooltip: 'Dolar sit amit.',
href: '#',
attrs: { 'data-test': '456' },
};
const TEST_TOOLTIP = 'Lorem ipsum dolar sit';
describe('Actions button component', () => {
let wrapper;
function createComponent(props) {
wrapper = shallowMount(ActionsButton, {
propsData: { ...props },
});
}
afterEach(() => {
wrapper.destroy();
});
2022-08-27 11:52:29 +05:30
const findButton = () => wrapper.findComponent(GlButton);
2023-03-04 22:38:38 +05:30
const findTooltip = () => wrapper.findComponent(GlTooltip);
2022-08-27 11:52:29 +05:30
const findDropdown = () => wrapper.findComponent(GlDropdown);
2020-11-24 15:15:51 +05:30
const parseDropdownItems = () =>
findDropdown()
.findAll('gl-dropdown-item-stub,gl-dropdown-divider-stub')
2021-03-08 18:12:59 +05:30
.wrappers.map((x) => {
if (x.is(GlDropdownDivider)) {
2020-11-24 15:15:51 +05:30
return { type: 'divider' };
}
const { isCheckItem, isChecked, secondaryText } = x.props();
return {
type: 'item',
isCheckItem,
isChecked,
secondaryText,
text: x.text(),
};
});
const clickOn = (child, evt = new Event('click')) => child.vm.$emit('click', evt);
2021-01-03 14:25:43 +05:30
const clickLink = (...args) => clickOn(findButton(), ...args);
2020-11-24 15:15:51 +05:30
const clickDropdown = (...args) => clickOn(findDropdown(), ...args);
describe('with 1 action', () => {
beforeEach(() => {
createComponent({ actions: [TEST_ACTION] });
});
it('should not render dropdown', () => {
expect(findDropdown().exists()).toBe(false);
});
it('should render single button', () => {
2021-01-03 14:25:43 +05:30
expect(findButton().attributes()).toMatchObject({
2020-11-24 15:15:51 +05:30
href: TEST_ACTION.href,
...TEST_ACTION.attrs,
});
2021-01-03 14:25:43 +05:30
expect(findButton().text()).toBe(TEST_ACTION.text);
2020-11-24 15:15:51 +05:30
});
2023-03-04 22:38:38 +05:30
it('should not have tooltip', () => {
expect(findTooltip().exists()).toBe(false);
2020-11-24 15:15:51 +05:30
});
it('should have attrs', () => {
2021-01-03 14:25:43 +05:30
expect(findButton().attributes()).toMatchObject(TEST_ACTION.attrs);
2020-11-24 15:15:51 +05:30
});
it('can click', () => {
expect(clickLink).not.toThrow();
});
});
describe('with 1 action with tooltip', () => {
it('should have tooltip', () => {
createComponent({ actions: [{ ...TEST_ACTION, tooltip: TEST_TOOLTIP }] });
2023-03-04 22:38:38 +05:30
expect(findTooltip().text()).toBe(TEST_TOOLTIP);
});
});
describe('when showActionTooltip is false', () => {
it('should not have tooltip', () => {
createComponent({
actions: [{ ...TEST_ACTION, tooltip: TEST_TOOLTIP }],
showActionTooltip: false,
});
expect(findTooltip().exists()).toBe(false);
2020-11-24 15:15:51 +05:30
});
});
describe('with 1 action with handle', () => {
it('can click and trigger handle', () => {
const handleClick = jest.fn();
createComponent({ actions: [{ ...TEST_ACTION, handle: handleClick }] });
const event = new Event('click');
clickLink(event);
expect(handleClick).toHaveBeenCalledWith(event);
});
});
describe('with multiple actions', () => {
let handleAction;
beforeEach(() => {
handleAction = jest.fn();
createComponent({ actions: [{ ...TEST_ACTION, handle: handleAction }, TEST_ACTION_2] });
});
it('should default to selecting first action', () => {
expect(findDropdown().attributes()).toMatchObject({
text: TEST_ACTION.text,
'split-href': TEST_ACTION.href,
});
});
it('should handle first action click', () => {
const event = new Event('click');
clickDropdown(event);
expect(handleAction).toHaveBeenCalledWith(event);
});
it('should render dropdown items', () => {
expect(parseDropdownItems()).toEqual([
{
type: 'item',
isCheckItem: true,
isChecked: true,
secondaryText: TEST_ACTION.secondaryText,
text: TEST_ACTION.text,
},
{ type: 'divider' },
{
type: 'item',
isCheckItem: true,
isChecked: false,
secondaryText: TEST_ACTION_2.secondaryText,
text: TEST_ACTION_2.text,
},
]);
});
it('should select action 2 when clicked', () => {
expect(wrapper.emitted('select')).toBeUndefined();
const action2 = wrapper.find(`[data-testid="action_${TEST_ACTION_2.key}"]`);
action2.vm.$emit('click');
expect(wrapper.emitted('select')).toEqual([[TEST_ACTION_2.key]]);
});
2023-03-04 22:38:38 +05:30
it('should not have tooltip value', () => {
expect(findTooltip().exists()).toBe(false);
2020-11-24 15:15:51 +05:30
});
});
describe('with multiple actions and selectedKey', () => {
beforeEach(() => {
createComponent({ actions: [TEST_ACTION, TEST_ACTION_2], selectedKey: TEST_ACTION_2.key });
});
it('should show action 2 as selected', () => {
expect(parseDropdownItems()).toEqual([
expect.objectContaining({
type: 'item',
isChecked: false,
}),
{ type: 'divider' },
expect.objectContaining({
type: 'item',
isChecked: true,
}),
]);
});
it('should have tooltip value', () => {
2023-03-04 22:38:38 +05:30
expect(findTooltip().text()).toBe(TEST_ACTION_2.tooltip);
2020-11-24 15:15:51 +05:30
});
});
});