2021-03-03 16:11:13 -05:00
|
|
|
import { within, fireEvent } from '@testing-library/dom';
|
2021-02-24 22:10:50 -05:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import ProjectsField from '~/access_tokens/components/projects_field.vue';
|
2021-03-03 16:11:13 -05:00
|
|
|
import ProjectsTokenSelector from '~/access_tokens/components/projects_token_selector.vue';
|
2021-02-24 22:10:50 -05:00
|
|
|
|
|
|
|
describe('ProjectsField', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
2021-03-05 16:09:03 -05:00
|
|
|
const createComponent = ({ inputAttrsValue = '' } = {}) => {
|
2021-02-24 22:10:50 -05:00
|
|
|
wrapper = mount(ProjectsField, {
|
|
|
|
propsData: {
|
|
|
|
inputAttrs: {
|
|
|
|
id: 'projects',
|
|
|
|
name: 'projects',
|
2021-03-05 16:09:03 -05:00
|
|
|
value: inputAttrsValue,
|
2021-02-24 22:10:50 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const queryByLabelText = (text) => within(wrapper.element).queryByLabelText(text);
|
|
|
|
const queryByText = (text) => within(wrapper.element).queryByText(text);
|
2021-03-03 16:11:13 -05:00
|
|
|
const findAllProjectsRadio = () => queryByLabelText('All projects');
|
|
|
|
const findSelectedProjectsRadio = () => queryByLabelText('Selected projects');
|
|
|
|
const findProjectsTokenSelector = () => wrapper.findComponent(ProjectsTokenSelector);
|
|
|
|
const findHiddenInput = () => wrapper.find('input[type="hidden"]');
|
2021-02-24 22:10:50 -05:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders label and sub-label', () => {
|
2021-03-05 16:09:03 -05:00
|
|
|
createComponent();
|
|
|
|
|
2021-02-24 22:10:50 -05:00
|
|
|
expect(queryByText('Projects')).not.toBe(null);
|
|
|
|
expect(queryByText('Set access permissions for this token.')).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
2021-03-05 16:09:03 -05:00
|
|
|
describe('when `inputAttrs.value` is empty', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent();
|
|
|
|
});
|
2021-02-24 22:10:50 -05:00
|
|
|
|
2021-03-05 16:09:03 -05:00
|
|
|
it('renders "All projects" radio as checked', () => {
|
|
|
|
expect(findAllProjectsRadio().checked).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders "Selected projects" radio as unchecked', () => {
|
|
|
|
expect(findSelectedProjectsRadio().checked).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets `projects-token-selector` `initialProjectIds` prop to an empty array', () => {
|
|
|
|
expect(findProjectsTokenSelector().props('initialProjectIds')).toEqual([]);
|
|
|
|
});
|
2021-02-24 22:10:50 -05:00
|
|
|
});
|
|
|
|
|
2021-03-05 16:09:03 -05:00
|
|
|
describe('when `inputAttrs.value` is a comma separated list of project IDs', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent({ inputAttrsValue: '1,2' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders "All projects" radio as unchecked', () => {
|
|
|
|
expect(findAllProjectsRadio().checked).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders "Selected projects" radio as checked', () => {
|
|
|
|
expect(findSelectedProjectsRadio().checked).toBe(true);
|
|
|
|
});
|
2021-02-24 22:10:50 -05:00
|
|
|
|
2021-03-05 16:09:03 -05:00
|
|
|
it('sets `projects-token-selector` `initialProjectIds` prop to an array of project IDs', () => {
|
|
|
|
expect(findProjectsTokenSelector().props('initialProjectIds')).toEqual(['1', '2']);
|
|
|
|
});
|
2021-02-24 22:10:50 -05:00
|
|
|
});
|
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
it('renders `projects-token-selector` component', () => {
|
2021-03-05 16:09:03 -05:00
|
|
|
createComponent();
|
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
expect(findProjectsTokenSelector().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-02-24 22:10:50 -05:00
|
|
|
it('renders hidden input with correct `name` and `id` attributes', () => {
|
2021-03-05 16:09:03 -05:00
|
|
|
createComponent();
|
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
expect(findHiddenInput().attributes()).toEqual(
|
2021-02-24 22:10:50 -05:00
|
|
|
expect.objectContaining({
|
|
|
|
id: 'projects',
|
|
|
|
name: 'projects',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2021-03-03 16:11:13 -05:00
|
|
|
|
|
|
|
describe('when `projects-token-selector` is focused', () => {
|
|
|
|
beforeEach(() => {
|
2021-03-05 16:09:03 -05:00
|
|
|
createComponent();
|
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
findProjectsTokenSelector().vm.$emit('focus');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('auto selects the "Selected projects" radio', () => {
|
|
|
|
expect(findSelectedProjectsRadio().checked).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when `projects-token-selector` is changed', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
findProjectsTokenSelector().vm.$emit('input', [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates the hidden input value to a comma separated list of project IDs', () => {
|
|
|
|
expect(findHiddenInput().attributes('value')).toBe('1,2');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when radio is changed back to "All projects"', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
fireEvent.click(findAllProjectsRadio());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes the hidden input value', () => {
|
|
|
|
expect(findHiddenInput().attributes('value')).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-02-24 22:10:50 -05:00
|
|
|
});
|