debian-mirror-gitlab/spec/frontend/members/utils_spec.js

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

324 lines
11 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import setWindowLocation from 'helpers/set_window_location_helper';
2022-10-11 01:57:18 +05:30
import {
DEFAULT_SORT,
MEMBER_TYPES,
I18N_USER_YOU,
I18N_USER_BLOCKED,
I18N_USER_BOT,
I188N_USER_2FA,
} from '~/members/constants';
2021-01-29 00:20:46 +05:30
import {
generateBadges,
isGroup,
isDirectMember,
isCurrentUser,
canRemove,
2023-03-17 16:20:25 +05:30
canRemoveBlockedByLastOwner,
2021-01-29 00:20:46 +05:30
canResend,
canUpdate,
2023-03-17 16:20:25 +05:30
canDisableTwoFactor,
2021-01-29 00:20:46 +05:30
canOverride,
2021-02-22 17:27:13 +05:30
parseSortParam,
buildSortHref,
2021-03-11 19:13:27 +05:30
parseDataAttributes,
groupLinkRequestFormatter,
2021-02-22 17:27:13 +05:30
} from '~/members/utils';
2021-03-11 19:13:27 +05:30
import {
member as memberMock,
directMember,
inheritedMember,
2021-04-17 20:07:23 +05:30
member2faEnabled,
2021-03-11 19:13:27 +05:30
group,
invite,
members,
2021-06-08 01:23:25 +05:30
pagination,
dataAttribute,
2021-03-11 19:13:27 +05:30
} from './mock_data';
2021-01-29 00:20:46 +05:30
const IS_CURRENT_USER_ID = 123;
const IS_NOT_CURRENT_USER_ID = 124;
2021-02-22 17:27:13 +05:30
const URL_HOST = 'https://localhost/';
2021-01-03 14:25:43 +05:30
describe('Members Utils', () => {
describe('generateBadges', () => {
it('has correct properties for each badge', () => {
2021-04-17 20:07:23 +05:30
const badges = generateBadges({
member: memberMock,
isCurrentUser: true,
canManageMembers: true,
});
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
badges.forEach((badge) => {
2021-01-03 14:25:43 +05:30
expect(badge).toEqual(
expect.objectContaining({
show: expect.any(Boolean),
text: expect.any(String),
variant: expect.stringMatching(/muted|neutral|info|success|danger|warning/),
}),
);
});
});
it.each`
2021-04-17 20:07:23 +05:30
member | expected
2022-10-11 01:57:18 +05:30
${memberMock} | ${{ show: true, text: I18N_USER_YOU, variant: 'success' }}
${{ ...memberMock, user: { ...memberMock.user, blocked: true } }} | ${{ show: true, text: I18N_USER_BLOCKED, variant: 'danger' }}
${{ ...memberMock, user: { ...memberMock.user, isBot: true } }} | ${{ show: true, text: I18N_USER_BOT, variant: 'muted' }}
${member2faEnabled} | ${{ show: true, text: I188N_USER_2FA, variant: 'info' }}
2021-01-03 14:25:43 +05:30
`('returns expected output for "$expected.text" badge', ({ member, expected }) => {
2021-04-17 20:07:23 +05:30
expect(
generateBadges({ member, isCurrentUser: true, canManageMembers: true }),
).toContainEqual(expect.objectContaining(expected));
});
describe('when `canManageMembers` argument is `false`', () => {
describe.each`
description | memberIsCurrentUser | expectedBadgeToBeShown
${'is not the current user'} | ${false} | ${false}
${'is the current user'} | ${true} | ${true}
`('when member is $description', ({ memberIsCurrentUser, expectedBadgeToBeShown }) => {
it(`sets 'show' to '${expectedBadgeToBeShown}' for 2FA badge`, () => {
const badges = generateBadges({
member: member2faEnabled,
isCurrentUser: memberIsCurrentUser,
canManageMembers: false,
});
expect(badges.find((badge) => badge.text === '2FA').show).toBe(expectedBadgeToBeShown);
});
});
2021-01-03 14:25:43 +05:30
});
});
2021-01-29 00:20:46 +05:30
describe('isGroup', () => {
2022-11-25 23:54:43 +05:30
it.each`
2021-01-29 00:20:46 +05:30
member | expected
${group} | ${true}
${memberMock} | ${false}
`('returns $expected', ({ member, expected }) => {
expect(isGroup(member)).toBe(expected);
});
});
describe('isDirectMember', () => {
2022-11-25 23:54:43 +05:30
it.each`
2021-03-11 19:13:27 +05:30
member | expected
${directMember} | ${true}
${inheritedMember} | ${false}
`('returns $expected', ({ member, expected }) => {
expect(isDirectMember(member)).toBe(expected);
2021-01-29 00:20:46 +05:30
});
});
describe('isCurrentUser', () => {
2022-11-25 23:54:43 +05:30
it.each`
2021-01-29 00:20:46 +05:30
currentUserId | expected
${IS_CURRENT_USER_ID} | ${true}
${IS_NOT_CURRENT_USER_ID} | ${false}
`('returns $expected', ({ currentUserId, expected }) => {
expect(isCurrentUser(memberMock, currentUserId)).toBe(expected);
});
});
describe('canRemove', () => {
2022-11-25 23:54:43 +05:30
it.each`
2021-03-11 19:13:27 +05:30
member | expected
${{ ...directMember, canRemove: true }} | ${true}
${{ ...inheritedMember, canRemove: true }} | ${false}
${{ ...memberMock, canRemove: false }} | ${false}
`('returns $expected', ({ member, expected }) => {
expect(canRemove(member)).toBe(expected);
2021-01-29 00:20:46 +05:30
});
});
2023-03-17 16:20:25 +05:30
describe('canRemoveBlockedByLastOwner', () => {
it.each`
member | canManageMembers | expected
${{ ...directMember, isLastOwner: true }} | ${true} | ${true}
${{ ...inheritedMember, isLastOwner: false }} | ${true} | ${false}
${{ ...directMember, isLastOwner: true }} | ${false} | ${false}
`('returns $expected', ({ member, canManageMembers, expected }) => {
expect(canRemoveBlockedByLastOwner(member, canManageMembers)).toBe(expected);
});
});
2021-01-29 00:20:46 +05:30
describe('canResend', () => {
2022-11-25 23:54:43 +05:30
it.each`
2021-01-29 00:20:46 +05:30
member | expected
${invite} | ${true}
${{ ...invite, invite: { ...invite.invite, canResend: false } }} | ${false}
2021-03-11 19:13:27 +05:30
`('returns $expected', ({ member, expected }) => {
expect(canResend(member)).toBe(expected);
2021-01-29 00:20:46 +05:30
});
});
describe('canUpdate', () => {
2022-11-25 23:54:43 +05:30
it.each`
2021-03-11 19:13:27 +05:30
member | currentUserId | expected
${{ ...directMember, canUpdate: true }} | ${IS_NOT_CURRENT_USER_ID} | ${true}
${{ ...directMember, canUpdate: true }} | ${IS_CURRENT_USER_ID} | ${false}
${{ ...inheritedMember, canUpdate: true }} | ${IS_CURRENT_USER_ID} | ${false}
${{ ...directMember, canUpdate: false }} | ${IS_NOT_CURRENT_USER_ID} | ${false}
`('returns $expected', ({ member, currentUserId, expected }) => {
expect(canUpdate(member, currentUserId)).toBe(expected);
2021-01-29 00:20:46 +05:30
});
});
2023-03-17 16:20:25 +05:30
describe('canDisableTwoFactor', () => {
it.each`
2023-04-23 21:23:45 +05:30
member | expected
${{ ...memberMock, canDisableTwoFactor: true }} | ${false}
${{ ...memberMock, canDisableTwoFactor: false }} | ${false}
2023-03-17 16:20:25 +05:30
`(
'returns $expected for members whose two factor authentication can be disabled',
({ member, expected }) => {
expect(canDisableTwoFactor(member)).toBe(expected);
},
);
});
2021-01-29 00:20:46 +05:30
describe('canOverride', () => {
it('returns `false`', () => {
expect(canOverride(memberMock)).toBe(false);
});
});
2021-02-22 17:27:13 +05:30
describe('parseSortParam', () => {
beforeEach(() => {
2021-10-27 15:23:28 +05:30
setWindowLocation(URL_HOST);
2021-02-22 17:27:13 +05:30
});
describe('when `sort` param is not present', () => {
it('returns default sort options', () => {
expect(parseSortParam(['account'])).toEqual(DEFAULT_SORT);
});
});
describe('when field passed in `sortableFields` argument does not have `sort` key defined', () => {
it('returns default sort options', () => {
2021-10-27 15:23:28 +05:30
setWindowLocation('?sort=source_asc');
2021-02-22 17:27:13 +05:30
expect(parseSortParam(['source'])).toEqual(DEFAULT_SORT);
});
});
describe.each`
sortParam | expected
${'name_asc'} | ${{ sortByKey: 'account', sortDesc: false }}
${'name_desc'} | ${{ sortByKey: 'account', sortDesc: true }}
${'last_joined'} | ${{ sortByKey: 'granted', sortDesc: false }}
${'oldest_joined'} | ${{ sortByKey: 'granted', sortDesc: true }}
${'access_level_asc'} | ${{ sortByKey: 'maxRole', sortDesc: false }}
${'access_level_desc'} | ${{ sortByKey: 'maxRole', sortDesc: true }}
${'recent_sign_in'} | ${{ sortByKey: 'lastSignIn', sortDesc: false }}
${'oldest_sign_in'} | ${{ sortByKey: 'lastSignIn', sortDesc: true }}
`('when `sort` query string param is `$sortParam`', ({ sortParam, expected }) => {
2023-06-20 00:43:36 +05:30
it(`returns ${JSON.stringify(expected)}`, () => {
2021-10-27 15:23:28 +05:30
setWindowLocation(`?sort=${sortParam}`);
2021-02-22 17:27:13 +05:30
expect(parseSortParam(['account', 'granted', 'expires', 'maxRole', 'lastSignIn'])).toEqual(
expected,
);
});
});
});
describe('buildSortHref', () => {
beforeEach(() => {
2021-10-27 15:23:28 +05:30
setWindowLocation(URL_HOST);
2021-02-22 17:27:13 +05:30
});
describe('when field passed in `sortBy` argument does not have `sort` key defined', () => {
it('returns an empty string', () => {
expect(
buildSortHref({
sortBy: 'source',
sortDesc: false,
filteredSearchBarTokens: [],
filteredSearchBarSearchParam: 'search',
}),
).toBe('');
});
});
describe('when there are no filter params set', () => {
it('sets `sort` param', () => {
expect(
buildSortHref({
sortBy: 'account',
sortDesc: false,
filteredSearchBarTokens: [],
filteredSearchBarSearchParam: 'search',
}),
).toBe(`${URL_HOST}?sort=name_asc`);
});
});
describe('when filter params are set', () => {
it('merges the `sort` param with the filter params', () => {
2021-10-27 15:23:28 +05:30
setWindowLocation('?two_factor=enabled&with_inherited_permissions=exclude');
2021-02-22 17:27:13 +05:30
expect(
buildSortHref({
sortBy: 'account',
sortDesc: false,
filteredSearchBarTokens: ['two_factor', 'with_inherited_permissions'],
filteredSearchBarSearchParam: 'search',
}),
).toBe(`${URL_HOST}?two_factor=enabled&with_inherited_permissions=exclude&sort=name_asc`);
});
});
describe('when search param is set', () => {
it('merges the `sort` param with the search param', () => {
2021-10-27 15:23:28 +05:30
setWindowLocation('?search=foobar');
2021-02-22 17:27:13 +05:30
expect(
buildSortHref({
sortBy: 'account',
sortDesc: false,
filteredSearchBarTokens: ['two_factor', 'with_inherited_permissions'],
filteredSearchBarSearchParam: 'search',
}),
).toBe(`${URL_HOST}?search=foobar&sort=name_asc`);
});
});
});
2021-03-11 19:13:27 +05:30
describe('parseDataAttributes', () => {
let el;
beforeEach(() => {
el = document.createElement('div');
2022-07-23 23:45:48 +05:30
el.dataset.membersData = dataAttribute;
2021-03-11 19:13:27 +05:30
});
afterEach(() => {
el = null;
});
2021-06-08 01:23:25 +05:30
it('correctly parses the data attribute', () => {
expect(parseDataAttributes(el)).toMatchObject({
2021-09-04 01:27:46 +05:30
[MEMBER_TYPES.user]: {
members,
pagination,
memberPath: '/groups/foo-bar/-/group_members/:id',
},
2021-03-11 19:13:27 +05:30
sourceId: 234,
canManageMembers: true,
});
});
});
describe('groupLinkRequestFormatter', () => {
it('returns expected format', () => {
expect(
groupLinkRequestFormatter({
accessLevel: 50,
expires_at: '2020-10-16',
}),
).toEqual({ group_link: { group_access: 50, expires_at: '2020-10-16' } });
});
});
2021-01-03 14:25:43 +05:30
});