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

153 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';
import { mount, createWrapper, createLocalVue } from '@vue/test-utils';
import { nextTick } from 'vue';
import Vuex from 'vuex';
2021-01-03 14:25:43 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2021-03-11 19:13:27 +05:30
import { BV_DROPDOWN_SHOW } from '~/lib/utils/constants';
2021-02-22 17:27:13 +05:30
import RoleDropdown from '~/members/components/table/role_dropdown.vue';
import { member } from '../../mock_data';
2021-01-03 14:25:43 +05:30
const localVue = createLocalVue();
localVue.use(Vuex);
describe('RoleDropdown', () => {
let wrapper;
let actions;
const $toast = {
show: jest.fn(),
};
const createStore = () => {
actions = {
updateMemberRole: jest.fn(() => Promise.resolve()),
};
return new Vuex.Store({ actions });
};
const createComponent = (propsData = {}) => {
wrapper = mount(RoleDropdown, {
propsData: {
member,
2021-01-29 00:20:46 +05:30
permissions: {},
2021-01-03 14:25:43 +05:30
...propsData,
},
localVue,
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
.findAll(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"]');
const findDropdown = () => wrapper.find(GlDropdown);
afterEach(() => {
wrapper.destroy();
});
describe('when dropdown is open', () => {
2021-03-08 18:12:59 +05:30
beforeEach((done) => {
2021-01-03 14:25:43 +05:30
createComponent();
findDropdownToggle().trigger('click');
2021-03-11 19:13:27 +05:30
wrapper.vm.$root.$on(BV_DROPDOWN_SHOW, () => {
2021-01-03 14:25:43 +05:30
done();
});
});
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', () => {
expect(getByTextInDropdownMenu('Change permissions').exists()).toBe(true);
});
it('sets dropdown toggle and checks selected role', () => {
expect(findDropdownToggle().text()).toBe('Owner');
expect(getCheckedDropdownItem().text()).toBe('Owner');
});
describe('when dropdown item is selected', () => {
it('does nothing if the item selected was already selected', () => {
getDropdownItemByText('Owner').trigger('click');
expect(actions.updateMemberRole).not.toHaveBeenCalled();
});
it('calls `updateMemberRole` Vuex action', () => {
getDropdownItemByText('Developer').trigger('click');
expect(actions.updateMemberRole).toHaveBeenCalledWith(expect.any(Object), {
memberId: member.id,
accessLevel: { integerValue: 30, stringValue: 'Developer' },
});
});
it('displays toast when successful', async () => {
getDropdownItemByText('Developer').trigger('click');
await waitForPromises();
expect($toast.show).toHaveBeenCalledWith('Role updated successfully.');
});
it('disables dropdown while waiting for `updateMemberRole` to resolve', async () => {
getDropdownItemByText('Developer').trigger('click');
await nextTick();
2021-01-29 00:20:46 +05:30
expect(findDropdown().props('disabled')).toBe(true);
2021-01-03 14:25:43 +05:30
await waitForPromises();
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
});
});