2021-02-24 22:10:50 -05:00
|
|
|
import { createWrapper } from '@vue/test-utils';
|
2021-03-03 16:11:13 -05:00
|
|
|
import Vue from 'vue';
|
2021-02-24 22:10:50 -05:00
|
|
|
|
|
|
|
import { initExpiresAtField, initProjectsField } from '~/access_tokens';
|
2021-03-03 16:11:13 -05:00
|
|
|
import * as ExpiresAtField from '~/access_tokens/components/expires_at_field.vue';
|
|
|
|
import * as ProjectsField from '~/access_tokens/components/projects_field.vue';
|
2021-02-24 22:10:50 -05:00
|
|
|
|
|
|
|
describe('access tokens', () => {
|
2021-03-03 16:11:13 -05:00
|
|
|
const FakeComponent = Vue.component('FakeComponent', {
|
|
|
|
props: {
|
|
|
|
inputAttrs: {
|
|
|
|
type: Object,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
render: () => null,
|
|
|
|
});
|
|
|
|
|
2021-02-24 22:10:50 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
window.gon = { features: { personalAccessTokensScopedToProjects: true } };
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
2021-03-26 05:09:24 -04:00
|
|
|
initFunction | mountSelector | fieldName | expectedComponent
|
|
|
|
${initExpiresAtField} | ${'js-access-tokens-expires-at'} | ${'expiresAt'} | ${ExpiresAtField}
|
|
|
|
${initProjectsField} | ${'js-access-tokens-projects'} | ${'projects'} | ${ProjectsField}
|
|
|
|
`('$initFunction', ({ initFunction, mountSelector, fieldName, expectedComponent }) => {
|
2021-02-24 22:10:50 -05:00
|
|
|
describe('when mount element exists', () => {
|
2021-03-26 05:09:24 -04:00
|
|
|
const nameAttribute = `access_tokens[${fieldName}]`;
|
|
|
|
const idAttribute = `access_tokens_${fieldName}`;
|
|
|
|
|
2021-02-24 22:10:50 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
const mountEl = document.createElement('div');
|
|
|
|
mountEl.classList.add(mountSelector);
|
|
|
|
|
|
|
|
const input = document.createElement('input');
|
2021-03-26 05:09:24 -04:00
|
|
|
input.setAttribute('name', nameAttribute);
|
|
|
|
input.setAttribute('data-js-name', fieldName);
|
|
|
|
input.setAttribute('id', idAttribute);
|
2021-02-24 22:10:50 -05:00
|
|
|
input.setAttribute('placeholder', 'Foo bar');
|
2021-03-05 16:09:03 -05:00
|
|
|
input.setAttribute('value', '1,2');
|
2021-02-24 22:10:50 -05:00
|
|
|
|
|
|
|
mountEl.appendChild(input);
|
|
|
|
|
|
|
|
document.body.appendChild(mountEl);
|
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
// Mock component so we don't have to deal with mocking Apollo
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
expectedComponent.default = FakeComponent;
|
|
|
|
});
|
2021-02-24 22:10:50 -05:00
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
it('mounts component and sets `inputAttrs` prop', async () => {
|
|
|
|
const vueInstance = await initFunction();
|
2021-02-24 22:10:50 -05:00
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
const wrapper = createWrapper(vueInstance);
|
|
|
|
const component = wrapper.findComponent(FakeComponent);
|
2021-02-24 22:10:50 -05:00
|
|
|
|
|
|
|
expect(component.exists()).toBe(true);
|
|
|
|
expect(component.props('inputAttrs')).toEqual({
|
2021-03-26 05:09:24 -04:00
|
|
|
name: nameAttribute,
|
|
|
|
id: idAttribute,
|
2021-03-05 16:09:03 -05:00
|
|
|
value: '1,2',
|
2021-02-24 22:10:50 -05:00
|
|
|
placeholder: 'Foo bar',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when mount element does not exist', () => {
|
|
|
|
it('returns `null`', () => {
|
|
|
|
expect(initFunction()).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|