Commit 2ad579e5 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Merge branch '324681-group-project-members-migrate-to-one-vue-app-and-gltabs-3' into 'master'

Add `MembersTabs` Vue component

See merge request gitlab-org/gitlab!61659
parents 684d0513 8fedc6c9
<script>
import { GlTabs, GlTab, GlBadge } from '@gitlab/ui';
import { mapState } from 'vuex';
import { urlParamsToObject } from '~/lib/utils/common_utils';
import { __ } from '~/locale';
import { MEMBER_TYPES } from '../constants';
import MembersApp from './app.vue';
const countComputed = (state, namespace) => state[namespace]?.pagination?.totalItems || 0;
export default {
name: 'MembersTabs',
tabs: [
{
namespace: MEMBER_TYPES.user,
title: __('Members'),
},
{
namespace: MEMBER_TYPES.group,
title: __('Groups'),
attrs: { 'data-qa-selector': 'groups_list_tab' },
},
{
namespace: MEMBER_TYPES.invite,
title: __('Invited'),
canManageMembersPermissionsRequired: true,
},
{
namespace: MEMBER_TYPES.accessRequest,
title: __('Access requests'),
canManageMembersPermissionsRequired: true,
},
],
urlParams: [],
components: { MembersApp, GlTabs, GlTab, GlBadge },
inject: ['canManageMembers'],
data() {
return {
selectedTabIndex: 0,
};
},
computed: {
...mapState({
userCount(state) {
return countComputed(state, MEMBER_TYPES.user);
},
groupCount(state) {
return countComputed(state, MEMBER_TYPES.group);
},
inviteCount(state) {
return countComputed(state, MEMBER_TYPES.invite);
},
accessRequestCount(state) {
return countComputed(state, MEMBER_TYPES.accessRequest);
},
}),
urlParams() {
return Object.keys(urlParamsToObject(window.location.search));
},
activeTabIndexCalculatedFromUrlParams() {
return this.$options.tabs.findIndex(({ namespace }) => {
return this.getTabUrlParams(namespace).some((urlParam) =>
this.urlParams.includes(urlParam),
);
});
},
},
created() {
if (this.activeTabIndexCalculatedFromUrlParams === -1) {
return;
}
this.selectedTabIndex = this.activeTabIndexCalculatedFromUrlParams;
},
methods: {
getTabUrlParams(namespace) {
const state = this.$store.state[namespace];
const urlParams = [];
if (state?.pagination?.paramName) {
urlParams.push(state.pagination.paramName);
}
if (state?.filteredSearchBar?.searchParam) {
urlParams.push(state.filteredSearchBar.searchParam);
}
return urlParams;
},
getTabCount({ namespace }) {
return this[`${namespace}Count`];
},
showTab(tab, index) {
if (tab.namespace === MEMBER_TYPES.user) {
return true;
}
const { canManageMembersPermissionsRequired = false } = tab;
const tabCanBeShown =
this.getTabCount(tab) > 0 || this.activeTabIndexCalculatedFromUrlParams === index;
if (canManageMembersPermissionsRequired) {
return this.canManageMembers && tabCanBeShown;
}
return tabCanBeShown;
},
},
};
</script>
<template>
<gl-tabs v-model="selectedTabIndex">
<template v-for="(tab, index) in $options.tabs">
<gl-tab v-if="showTab(tab, index)" :key="tab.namespace" :title-link-attributes="tab.attrs">
<template slot="title">
<span>{{ tab.title }}</span>
<gl-badge size="sm" class="gl-tab-counter-badge">{{ getTabCount(tab) }}</gl-badge>
</template>
<members-app :namespace="tab.namespace" />
</gl-tab>
</template>
</gl-tabs>
</template>
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import MembersApp from '~/members/components/app.vue';
import MembersTabs from '~/members/components/members_tabs.vue';
import { MEMBER_TYPES } from '~/members/constants';
import { pagination } from '../mock_data';
describe('MembersApp', () => {
Vue.use(Vuex);
let wrapper;
const createComponent = ({ totalItems = 10, options = {} } = {}) => {
const store = new Vuex.Store({
modules: {
[MEMBER_TYPES.user]: {
namespaced: true,
state: {
pagination: {
...pagination,
totalItems,
},
filteredSearchBar: {
searchParam: 'search',
},
},
},
[MEMBER_TYPES.group]: {
namespaced: true,
state: {
pagination: {
...pagination,
totalItems,
paramName: 'groups_page',
},
filteredSearchBar: {
searchParam: 'search_groups',
},
},
},
[MEMBER_TYPES.invite]: {
namespaced: true,
state: {
pagination: {
...pagination,
totalItems,
paramName: 'invited_page',
},
filteredSearchBar: {
searchParam: 'search_invited',
},
},
},
[MEMBER_TYPES.accessRequest]: {
namespaced: true,
state: {
pagination: {
...pagination,
totalItems,
paramName: 'access_requests_page',
},
filteredSearchBar: {
searchParam: 'search_access_requests',
},
},
},
},
});
wrapper = mountExtended(MembersTabs, {
store,
stubs: ['members-app'],
provide: {
canManageMembers: true,
},
...options,
});
return nextTick();
};
const findTabs = () => wrapper.findAllByRole('tab').wrappers;
const findTabByText = (text) => findTabs().find((tab) => tab.text().includes(text));
const findActiveTab = () => wrapper.findByRole('tab', { selected: true });
beforeEach(() => {
delete window.location;
window.location = new URL('https://localhost');
});
afterEach(() => {
wrapper.destroy();
});
describe('when tabs have a count', () => {
it('renders tabs with count', async () => {
await createComponent();
const tabs = findTabs();
expect(tabs[0].text()).toBe('Members 10');
expect(tabs[1].text()).toBe('Groups 10');
expect(tabs[2].text()).toBe('Invited 10');
expect(tabs[3].text()).toBe('Access requests 10');
expect(findActiveTab().text()).toContain('Members');
});
it('renders `MembersApp` and passes `namespace` prop', async () => {
await createComponent();
const membersApps = wrapper.findAllComponents(MembersApp).wrappers;
expect(membersApps[0].attributes('namespace')).toBe(MEMBER_TYPES.user);
expect(membersApps[1].attributes('namespace')).toBe(MEMBER_TYPES.group);
expect(membersApps[2].attributes('namespace')).toBe(MEMBER_TYPES.invite);
expect(membersApps[3].attributes('namespace')).toBe(MEMBER_TYPES.accessRequest);
});
});
describe('when tabs do not have a count', () => {
it('only renders `Members` tab', async () => {
await createComponent({ totalItems: 0 });
expect(findTabByText('Members')).not.toBeUndefined();
expect(findTabByText('Groups')).toBeUndefined();
expect(findTabByText('Invited')).toBeUndefined();
expect(findTabByText('Access requests')).toBeUndefined();
});
});
describe('when url param matches `filteredSearchBar.searchParam`', () => {
beforeEach(() => {
window.location.search = '?search_groups=foo+bar';
});
const expectGroupsTabActive = () => {
expect(findActiveTab().text()).toContain('Groups');
};
describe('when tab has a count', () => {
it('sets tab that corresponds to search param as active tab', async () => {
await createComponent();
expectGroupsTabActive();
});
});
describe('when tab does not have a count', () => {
it('sets tab that corresponds to search param as active tab', async () => {
await createComponent({ totalItems: 0 });
expectGroupsTabActive();
});
});
});
describe('when url param matches `pagination.paramName`', () => {
beforeEach(() => {
window.location.search = '?invited_page=2';
});
const expectInvitedTabActive = () => {
expect(findActiveTab().text()).toContain('Invited');
};
describe('when tab has a count', () => {
it('sets tab that corresponds to pagination param as active tab', async () => {
await createComponent();
expectInvitedTabActive();
});
});
describe('when tab does not have a count', () => {
it('sets tab that corresponds to pagination param as active tab', async () => {
await createComponent({ totalItems: 0 });
expectInvitedTabActive();
});
});
});
describe('when `canManageMembers` is `false`', () => {
it('shows all tabs except `Invited` and `Access requests`', async () => {
await createComponent({ options: { provide: { canManageMembers: false } } });
expect(findTabByText('Members')).not.toBeUndefined();
expect(findTabByText('Groups')).not.toBeUndefined();
expect(findTabByText('Invited')).toBeUndefined();
expect(findTabByText('Access requests')).toBeUndefined();
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment