debian-mirror-gitlab/spec/frontend/members/components/table/role_dropdown_spec.js

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

152 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
2021-03-11 19:13:27 +05:30
import { within } from '@testing-library/dom';
2022-04-04 11:22:00 +05:30
import { mount, createWrapper } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
2021-03-11 19:13:27 +05:30
import Vuex from 'vuex';
2021-02-22 17:27:13 +05:30
import RoleDropdown from '~/members/components/table/role_dropdown.vue';
2021-04-29 21:17:54 +05:30
import { MEMBER_TYPES } from '~/members/constants';
2021-02-22 17:27:13 +05:30
import { member } from '../../mock_data';
2021-01-03 14:25:43 +05:30
2022-04-04 11:22:00 +05:30
Vue.use(Vuex);
2021-01-03 14:25:43 +05:30
describe('RoleDropdown', () => {
let wrapper;
let actions;
const $toast = {
show: jest.fn(),
};
const createStore = () => {
actions = {
updateMemberRole: jest.fn(() => Promise.resolve()),
};
2021-04-29 21:17:54 +05:30
return new Vuex.Store({
modules: {
[MEMBER_TYPES.user]: { namespaced: true, actions },
},
});
2021-01-03 14:25:43 +05:30
};
const createComponent = (propsData = {}) => {
wrapper = mount(RoleDropdown, {
2021-04-29 21:17:54 +05:30
provide: {
namespace: MEMBER_TYPES.user,
},
2021-01-03 14:25:43 +05:30
propsData: {
member,
2021-01-29 00:20:46 +05:30
permissions: {},
2021-01-03 14:25:43 +05:30
...propsData,
},
store: createStore(),
mocks: {
$toast,
},
});
};
const getDropdownMenu = () => within(wrapper.element).getByRole('menu');
const getByTextInDropdownMenu = (text, options = {}) =>
createWrapper(within(getDropdownMenu()).getByText(text, options));
2021-03-08 18:12:59 +05:30
const getDropdownItemByText = (text) =>
2021-01-03 14:25:43 +05:30
createWrapper(
within(getDropdownMenu())
.getByText(text, { selector: '[role="menuitem"] p' })
.closest('[role="menuitem"]'),
);
const getCheckedDropdownItem = () =>
wrapper
2022-08-27 11:52:29 +05:30
.findAllComponents(GlDropdownItem)
2021-03-08 18:12:59 +05:30
.wrappers.find((dropdownItemWrapper) => dropdownItemWrapper.props('isChecked'));
2021-01-03 14:25:43 +05:30
const findDropdownToggle = () => wrapper.find('button[aria-haspopup="true"]');
2022-08-27 11:52:29 +05:30
const findDropdown = () => wrapper.findComponent(GlDropdown);
2021-01-03 14:25:43 +05:30
afterEach(() => {
wrapper.destroy();
});
describe('when dropdown is open', () => {
2022-07-16 23:28:13 +05:30
beforeEach(() => {
2021-01-03 14:25:43 +05:30
createComponent();
2022-07-16 23:28:13 +05:30
return findDropdownToggle().trigger('click');
2021-01-03 14:25:43 +05:30
});
it('renders all valid roles', () => {
2021-03-08 18:12:59 +05:30
Object.keys(member.validRoles).forEach((role) => {
2021-01-03 14:25:43 +05:30
expect(getDropdownItemByText(role).exists()).toBe(true);
});
});
it('renders dropdown header', () => {
2021-09-04 01:27:46 +05:30
expect(getByTextInDropdownMenu('Change role').exists()).toBe(true);
2021-01-03 14:25:43 +05:30
});
it('sets dropdown toggle and checks selected role', () => {
expect(findDropdownToggle().text()).toBe('Owner');
expect(getCheckedDropdownItem().text()).toBe('Owner');
});
describe('when dropdown item is selected', () => {
2022-07-16 23:28:13 +05:30
it('does nothing if the item selected was already selected', async () => {
await getDropdownItemByText('Owner').trigger('click');
2021-01-03 14:25:43 +05:30
expect(actions.updateMemberRole).not.toHaveBeenCalled();
});
2022-07-16 23:28:13 +05:30
it('calls `updateMemberRole` Vuex action', async () => {
await getDropdownItemByText('Developer').trigger('click');
2021-01-03 14:25:43 +05:30
expect(actions.updateMemberRole).toHaveBeenCalledWith(expect.any(Object), {
memberId: member.id,
accessLevel: { integerValue: 30, stringValue: 'Developer' },
});
});
it('displays toast when successful', async () => {
2022-07-16 23:28:13 +05:30
await getDropdownItemByText('Developer').trigger('click');
2021-01-03 14:25:43 +05:30
2022-07-16 23:28:13 +05:30
await nextTick();
2021-01-03 14:25:43 +05:30
expect($toast.show).toHaveBeenCalledWith('Role updated successfully.');
});
it('disables dropdown while waiting for `updateMemberRole` to resolve', async () => {
2022-07-16 23:28:13 +05:30
await getDropdownItemByText('Developer').trigger('click');
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
expect(findDropdown().props('disabled')).toBe(true);
2021-01-03 14:25:43 +05:30
2022-07-16 23:28:13 +05:30
await nextTick();
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
expect(findDropdown().props('disabled')).toBe(false);
2021-01-03 14:25:43 +05:30
});
});
});
it("sets initial dropdown toggle value to member's role", () => {
createComponent();
expect(findDropdownToggle().text()).toBe('Owner');
});
it('sets the dropdown alignment to right on mobile', async () => {
jest.spyOn(bp, 'isDesktop').mockReturnValue(false);
createComponent();
await nextTick();
2021-02-22 17:27:13 +05:30
expect(findDropdown().props('right')).toBe(true);
2021-01-03 14:25:43 +05:30
});
it('sets the dropdown alignment to left on desktop', async () => {
jest.spyOn(bp, 'isDesktop').mockReturnValue(true);
createComponent();
await nextTick();
2021-02-22 17:27:13 +05:30
expect(findDropdown().props('right')).toBe(false);
2021-01-03 14:25:43 +05:30
});
});