2020-04-22 19:07:51 +05:30
|
|
|
import { GlFormRadio, GlIcon, GlFormRadioGroup, GlLink } from '@gitlab/ui';
|
2020-10-24 23:57:45 +05:30
|
|
|
import { mount, shallowMount } from '@vue/test-utils';
|
|
|
|
import SnippetVisibilityEdit from '~/snippets/components/snippet_visibility_edit.vue';
|
2022-10-11 01:57:18 +05:30
|
|
|
import {
|
|
|
|
VISIBILITY_LEVEL_PRIVATE_STRING,
|
|
|
|
VISIBILITY_LEVEL_INTERNAL_STRING,
|
|
|
|
VISIBILITY_LEVEL_PUBLIC_STRING,
|
|
|
|
} from '~/visibility_level/constants';
|
2020-04-22 19:07:51 +05:30
|
|
|
import {
|
|
|
|
SNIPPET_VISIBILITY,
|
2020-11-24 15:15:51 +05:30
|
|
|
SNIPPET_LEVELS_RESTRICTED,
|
|
|
|
SNIPPET_LEVELS_DISABLED,
|
2020-04-22 19:07:51 +05:30
|
|
|
} from '~/snippets/constants';
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
describe('Snippet Visibility Edit component', () => {
|
|
|
|
let wrapper;
|
|
|
|
const defaultHelpLink = '/foo/bar';
|
2020-04-22 19:07:51 +05:30
|
|
|
const defaultVisibilityLevel = 'private';
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
function createComponent({
|
|
|
|
propsData = {},
|
2021-01-29 00:20:46 +05:30
|
|
|
visibilityLevels = [0, 10, 20],
|
2020-11-24 15:15:51 +05:30
|
|
|
multipleLevelsRestricted = false,
|
|
|
|
deep = false,
|
|
|
|
} = {}) {
|
2020-04-08 14:13:33 +05:30
|
|
|
const method = deep ? mount : shallowMount;
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
wrapper = method.call(this, SnippetVisibilityEdit, {
|
|
|
|
propsData: {
|
2020-04-22 19:07:51 +05:30
|
|
|
helpLink: defaultHelpLink,
|
|
|
|
isProjectSnippet: false,
|
|
|
|
value: defaultVisibilityLevel,
|
|
|
|
...propsData,
|
2020-04-08 14:13:33 +05:30
|
|
|
},
|
2021-01-29 00:20:46 +05:30
|
|
|
provide: {
|
|
|
|
visibilityLevels,
|
|
|
|
multipleLevelsRestricted,
|
2020-11-24 15:15:51 +05:30
|
|
|
},
|
2020-04-08 14:13:33 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
const findLink = () => wrapper.find('label').findComponent(GlLink);
|
|
|
|
const findRadios = () => wrapper.findComponent(GlFormRadioGroup).findAllComponents(GlFormRadio);
|
2020-04-22 19:07:51 +05:30
|
|
|
const findRadiosData = () =>
|
2021-03-08 18:12:59 +05:30
|
|
|
findRadios().wrappers.map((x) => {
|
2020-04-22 19:07:51 +05:30
|
|
|
return {
|
|
|
|
value: x.find('input').attributes('value'),
|
2022-11-25 23:54:43 +05:30
|
|
|
icon: x.findComponent(GlIcon).props('name'),
|
2020-04-22 19:07:51 +05:30
|
|
|
description: x.find('.help-text').text(),
|
|
|
|
text: x.find('.js-visibility-option').text(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rendering', () => {
|
|
|
|
it('matches the snapshot', () => {
|
|
|
|
createComponent();
|
|
|
|
expect(wrapper.element).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it('renders label help link', () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(findLink().attributes('href')).toBe(defaultHelpLink);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when helpLink is not defined, does not render label help link', () => {
|
|
|
|
createComponent({ propsData: { helpLink: null } });
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(findLink().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Visibility options', () => {
|
|
|
|
const findRestrictedInfo = () => wrapper.find('[data-testid="restricted-levels-info"]');
|
|
|
|
const RESULTING_OPTIONS = {
|
|
|
|
0: {
|
2022-10-11 01:57:18 +05:30
|
|
|
value: VISIBILITY_LEVEL_PRIVATE_STRING,
|
2020-04-22 19:07:51 +05:30
|
|
|
icon: SNIPPET_VISIBILITY.private.icon,
|
|
|
|
text: SNIPPET_VISIBILITY.private.label,
|
|
|
|
description: SNIPPET_VISIBILITY.private.description,
|
|
|
|
},
|
2020-11-24 15:15:51 +05:30
|
|
|
10: {
|
2022-10-11 01:57:18 +05:30
|
|
|
value: VISIBILITY_LEVEL_INTERNAL_STRING,
|
2020-04-22 19:07:51 +05:30
|
|
|
icon: SNIPPET_VISIBILITY.internal.icon,
|
|
|
|
text: SNIPPET_VISIBILITY.internal.label,
|
|
|
|
description: SNIPPET_VISIBILITY.internal.description,
|
|
|
|
},
|
2020-11-24 15:15:51 +05:30
|
|
|
20: {
|
2022-10-11 01:57:18 +05:30
|
|
|
value: VISIBILITY_LEVEL_PUBLIC_STRING,
|
2020-04-22 19:07:51 +05:30
|
|
|
icon: SNIPPET_VISIBILITY.public.icon,
|
|
|
|
text: SNIPPET_VISIBILITY.public.label,
|
|
|
|
description: SNIPPET_VISIBILITY.public.description,
|
|
|
|
},
|
2020-11-24 15:15:51 +05:30
|
|
|
};
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it.each`
|
|
|
|
levels | resultOptions
|
|
|
|
${''} | ${[]}
|
|
|
|
${[]} | ${[]}
|
|
|
|
${[0]} | ${[RESULTING_OPTIONS[0]]}
|
|
|
|
${[0, 10]} | ${[RESULTING_OPTIONS[0], RESULTING_OPTIONS[10]]}
|
|
|
|
${[0, 10, 20]} | ${[RESULTING_OPTIONS[0], RESULTING_OPTIONS[10], RESULTING_OPTIONS[20]]}
|
|
|
|
${[0, 20]} | ${[RESULTING_OPTIONS[0], RESULTING_OPTIONS[20]]}
|
|
|
|
${[10, 20]} | ${[RESULTING_OPTIONS[10], RESULTING_OPTIONS[20]]}
|
|
|
|
`('renders correct visibility options for $levels', ({ levels, resultOptions }) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
createComponent({ visibilityLevels: levels, deep: true });
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(findRadiosData()).toEqual(resultOptions);
|
2020-04-08 14:13:33 +05:30
|
|
|
});
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it.each`
|
|
|
|
levels | levelsRestricted | resultText
|
|
|
|
${[]} | ${false} | ${SNIPPET_LEVELS_DISABLED}
|
|
|
|
${[]} | ${true} | ${SNIPPET_LEVELS_DISABLED}
|
|
|
|
${[0]} | ${true} | ${SNIPPET_LEVELS_RESTRICTED}
|
|
|
|
${[0]} | ${false} | ${''}
|
|
|
|
${[0, 10, 20]} | ${false} | ${''}
|
|
|
|
`(
|
|
|
|
'renders correct information about restricted visibility levels for $levels',
|
|
|
|
({ levels, levelsRestricted, resultText }) => {
|
|
|
|
createComponent({
|
2021-01-29 00:20:46 +05:30
|
|
|
visibilityLevels: levels,
|
2020-11-24 15:15:51 +05:30
|
|
|
multipleLevelsRestricted: levelsRestricted,
|
|
|
|
});
|
|
|
|
expect(findRestrictedInfo().text()).toBe(resultText);
|
|
|
|
},
|
|
|
|
);
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it('when project snippet, renders special private description', () => {
|
|
|
|
createComponent({ propsData: { isProjectSnippet: true }, deep: true });
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(findRadiosData()[0]).toEqual({
|
2022-10-11 01:57:18 +05:30
|
|
|
value: VISIBILITY_LEVEL_PRIVATE_STRING,
|
2020-11-24 15:15:51 +05:30
|
|
|
icon: SNIPPET_VISIBILITY.private.icon,
|
|
|
|
text: SNIPPET_VISIBILITY.private.label,
|
|
|
|
description: SNIPPET_VISIBILITY.private.description_project,
|
|
|
|
});
|
|
|
|
});
|
2020-04-08 14:13:33 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('functionality', () => {
|
|
|
|
it('pre-selects correct option in the list', () => {
|
2022-10-11 01:57:18 +05:30
|
|
|
const value = VISIBILITY_LEVEL_INTERNAL_STRING;
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
createComponent({ propsData: { value } });
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.findComponent(GlFormRadioGroup).attributes('checked')).toBe(value);
|
2020-04-08 14:13:33 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|