debian-mirror-gitlab/spec/frontend/access_tokens/index_spec.js

79 lines
2.6 KiB
JavaScript
Raw Normal View History

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