debian-mirror-gitlab/spec/frontend/runner/components/registration/registration_token_spec.js

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

81 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-12-11 22:18:48 +05:30
import { GlToast } from '@gitlab/ui';
2022-04-04 11:22:00 +05:30
import { createLocalVue } from '@vue/test-utils';
2022-06-21 17:19:12 +05:30
import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper';
2021-12-11 22:18:48 +05:30
import RegistrationToken from '~/runner/components/registration/registration_token.vue';
2022-06-21 17:19:12 +05:30
import InputCopyToggleVisibility from '~/vue_shared/components/form/input_copy_toggle_visibility.vue';
2021-12-11 22:18:48 +05:30
const mockToken = '01234567890';
const mockMasked = '***********';
describe('RegistrationToken', () => {
let wrapper;
let showToast;
2022-06-21 17:19:12 +05:30
const findInputCopyToggleVisibility = () => wrapper.findComponent(InputCopyToggleVisibility);
2021-12-11 22:18:48 +05:30
const vueWithGlToast = () => {
const localVue = createLocalVue();
localVue.use(GlToast);
return localVue;
};
2022-06-21 17:19:12 +05:30
const createComponent = ({
props = {},
withGlToast = true,
mountFn = shallowMountExtended,
} = {}) => {
2021-12-11 22:18:48 +05:30
const localVue = withGlToast ? vueWithGlToast() : undefined;
2022-06-21 17:19:12 +05:30
wrapper = mountFn(RegistrationToken, {
2022-04-04 11:22:00 +05:30
propsData: {
value: mockToken,
...props,
},
localVue,
});
2021-12-11 22:18:48 +05:30
showToast = wrapper.vm.$toast ? jest.spyOn(wrapper.vm.$toast, 'show') : null;
};
afterEach(() => {
wrapper.destroy();
});
2022-06-21 17:19:12 +05:30
it('Displays value and copy button', () => {
createComponent();
2021-12-11 22:18:48 +05:30
2022-06-21 17:19:12 +05:30
expect(findInputCopyToggleVisibility().props('value')).toBe(mockToken);
expect(findInputCopyToggleVisibility().props('copyButtonTitle')).toBe(
'Copy registration token',
);
2021-12-11 22:18:48 +05:30
});
2022-06-21 17:19:12 +05:30
// Component integration test to ensure secure masking
it('Displays masked value by default', () => {
createComponent({ mountFn: mountExtended });
expect(wrapper.find('input').element.value).toBe(mockMasked);
2021-12-11 22:18:48 +05:30
});
2022-06-21 17:19:12 +05:30
describe('When the copy to clipboard button is clicked', () => {
2021-12-11 22:18:48 +05:30
beforeEach(() => {
2022-06-21 17:19:12 +05:30
createComponent();
2021-12-11 22:18:48 +05:30
});
it('shows a copied message', () => {
2022-06-21 17:19:12 +05:30
findInputCopyToggleVisibility().vm.$emit('copy');
2021-12-11 22:18:48 +05:30
expect(showToast).toHaveBeenCalledTimes(1);
expect(showToast).toHaveBeenCalledWith('Registration token copied!');
});
it('does not fail when toast is not defined', () => {
createComponent({ withGlToast: false });
2022-06-21 17:19:12 +05:30
findInputCopyToggleVisibility().vm.$emit('copy');
2021-12-11 22:18:48 +05:30
// This block also tests for unhandled errors
expect(showToast).toBeNull();
});
});
});