debian-mirror-gitlab/spec/frontend/deploy_keys/components/key_spec.js

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

178 lines
5.9 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { mount } from '@vue/test-utils';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2021-11-18 22:05:49 +05:30
import data from 'test_fixtures/deploy_keys/keys.json';
2023-06-20 00:43:36 +05:30
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
2020-05-24 23:13:21 +05:30
import key from '~/deploy_keys/components/key.vue';
2021-03-11 19:13:27 +05:30
import DeployKeysStore from '~/deploy_keys/store';
2023-06-20 00:43:36 +05:30
import { getTimeago, formatDate } from '~/lib/utils/datetime_utility';
2020-05-24 23:13:21 +05:30
describe('Deploy keys key', () => {
let wrapper;
let store;
2021-03-08 18:12:59 +05:30
const findTextAndTrim = (selector) => wrapper.find(selector).text().trim();
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
const createComponent = (propsData) => {
2020-05-24 23:13:21 +05:30
wrapper = mount(key, {
propsData: {
store,
endpoint: 'https://test.host/dummy/endpoint',
...propsData,
},
2023-06-20 00:43:36 +05:30
directives: {
GlTooltip: createMockDirective('gl-tooltip'),
},
2020-05-24 23:13:21 +05:30
});
};
beforeEach(() => {
store = new DeployKeysStore();
store.keys = data;
});
describe('enabled key', () => {
const deployKey = data.enabled_keys[0];
it('renders the keys title', () => {
createComponent({ deployKey });
expect(findTextAndTrim('.title')).toContain('My title');
});
it('renders human friendly formatted created date', () => {
createComponent({ deployKey });
expect(findTextAndTrim('.key-created-at')).toBe(
`${getTimeago().format(deployKey.created_at)}`,
);
});
2023-06-20 00:43:36 +05:30
it('renders human friendly expiration date', () => {
const expiresAt = new Date();
createComponent({
deployKey: { ...deployKey, expires_at: expiresAt },
});
expect(findTextAndTrim('.key-expires-at')).toBe(`${getTimeago().format(expiresAt)}`);
});
it('shows tooltip for expiration date', () => {
const expiresAt = new Date();
createComponent({
deployKey: { ...deployKey, expires_at: expiresAt },
});
const expiryComponent = wrapper.find('[data-testid="expires-at-tooltip"]');
const tooltip = getBinding(expiryComponent.element, 'gl-tooltip');
expect(tooltip).toBeDefined();
expect(expiryComponent.attributes('title')).toBe(`${formatDate(expiresAt)}`);
});
it('renders never when no expiration date', () => {
createComponent({
deployKey: { ...deployKey, expires_at: null },
});
expect(wrapper.find('[data-testid="expires-never"]').exists()).toBe(true);
});
2020-05-24 23:13:21 +05:30
it('shows pencil button for editing', () => {
createComponent({ deployKey });
2021-12-11 22:18:48 +05:30
expect(wrapper.find('.btn [data-testid="pencil-icon"]').exists()).toBe(true);
2020-05-24 23:13:21 +05:30
});
it('shows disable button when the project is not deletable', () => {
createComponent({ deployKey });
2021-12-11 22:18:48 +05:30
expect(wrapper.find('.btn [data-testid="cancel-icon"]').exists()).toBe(true);
2020-05-24 23:13:21 +05:30
});
it('shows remove button when the project is deletable', () => {
createComponent({
deployKey: { ...deployKey, destroyed_when_orphaned: true, almost_orphaned: true },
});
2021-12-11 22:18:48 +05:30
expect(wrapper.find('.btn [data-testid="remove-icon"]').exists()).toBe(true);
2020-05-24 23:13:21 +05:30
});
});
describe('deploy key labels', () => {
const deployKey = data.enabled_keys[0];
const deployKeysProjects = [...deployKey.deploy_keys_projects];
it('shows write access title when key has write access', () => {
deployKeysProjects[0] = { ...deployKeysProjects[0], can_push: true };
createComponent({ deployKey: { ...deployKey, deploy_keys_projects: deployKeysProjects } });
2021-01-29 00:20:46 +05:30
expect(wrapper.find('.deploy-project-label').attributes('title')).toBe(
2021-03-11 19:13:27 +05:30
'Grant write permissions to this key',
2020-05-24 23:13:21 +05:30
);
});
it('does not show write access title when key has write access', () => {
deployKeysProjects[0] = { ...deployKeysProjects[0], can_push: false };
createComponent({ deployKey: { ...deployKey, deploy_keys_projects: deployKeysProjects } });
2021-01-29 00:20:46 +05:30
expect(wrapper.find('.deploy-project-label').attributes('title')).toBe('Read access only');
2020-05-24 23:13:21 +05:30
});
it('shows expandable button if more than two projects', () => {
createComponent({ deployKey });
const labels = wrapper.findAll('.deploy-project-label');
expect(labels.length).toBe(2);
expect(labels.at(1).text()).toContain('others');
2021-01-29 00:20:46 +05:30
expect(labels.at(1).attributes('title')).toContain('Expand');
2020-05-24 23:13:21 +05:30
});
2022-04-04 11:22:00 +05:30
it('expands all project labels after click', async () => {
2020-05-24 23:13:21 +05:30
createComponent({ deployKey });
const { length } = deployKey.deploy_keys_projects;
2021-03-08 18:12:59 +05:30
wrapper.findAll('.deploy-project-label').at(1).trigger('click');
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
const labels = wrapper.findAll('.deploy-project-label');
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
expect(labels.length).toBe(length);
expect(labels.at(1).text()).not.toContain(`+${length} others`);
expect(labels.at(1).attributes('title')).not.toContain('Expand');
2020-05-24 23:13:21 +05:30
});
it('shows two projects', () => {
createComponent({
deployKey: { ...deployKey, deploy_keys_projects: [...deployKeysProjects].slice(0, 2) },
});
const labels = wrapper.findAll('.deploy-project-label');
expect(labels.length).toBe(2);
expect(labels.at(1).text()).toContain(deployKey.deploy_keys_projects[1].project.full_name);
});
});
describe('public keys', () => {
const deployKey = data.public_keys[0];
it('renders deploy keys without any enabled projects', () => {
createComponent({ deployKey: { ...deployKey, deploy_keys_projects: [] } });
expect(findTextAndTrim('.deploy-project-list')).toBe('None');
});
it('shows enable button', () => {
createComponent({ deployKey });
expect(findTextAndTrim('.btn')).toBe('Enable');
});
it('shows pencil button for editing', () => {
createComponent({ deployKey });
2021-12-11 22:18:48 +05:30
expect(wrapper.find('.btn [data-testid="pencil-icon"]').exists()).toBe(true);
2020-05-24 23:13:21 +05:30
});
it('shows disable button when key is enabled', () => {
store.keys.enabled_keys.push(deployKey);
createComponent({ deployKey });
2021-12-11 22:18:48 +05:30
expect(wrapper.find('.btn [data-testid="cancel-icon"]').exists()).toBe(true);
2020-05-24 23:13:21 +05:30
});
});
});