2021-10-22 18:13:20 +00:00
|
|
|
import { GlToast } from '@gitlab/ui';
|
2022-01-20 18:14:18 +00:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2022-04-14 03:08:57 +00:00
|
|
|
import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper';
|
2021-10-22 18:13:20 +00:00
|
|
|
import RegistrationToken from '~/runner/components/registration/registration_token.vue';
|
2022-04-14 03:08:57 +00:00
|
|
|
import InputCopyToggleVisibility from '~/vue_shared/components/form/input_copy_toggle_visibility.vue';
|
2021-10-22 18:13:20 +00:00
|
|
|
|
|
|
|
const mockToken = '01234567890';
|
|
|
|
const mockMasked = '***********';
|
|
|
|
|
|
|
|
describe('RegistrationToken', () => {
|
|
|
|
let wrapper;
|
|
|
|
let showToast;
|
|
|
|
|
2022-04-14 03:08:57 +00:00
|
|
|
const findInputCopyToggleVisibility = () => wrapper.findComponent(InputCopyToggleVisibility);
|
2021-10-22 18:13:20 +00:00
|
|
|
|
|
|
|
const vueWithGlToast = () => {
|
|
|
|
const localVue = createLocalVue();
|
|
|
|
localVue.use(GlToast);
|
|
|
|
return localVue;
|
|
|
|
};
|
|
|
|
|
2022-04-14 03:08:57 +00:00
|
|
|
const createComponent = ({
|
|
|
|
props = {},
|
|
|
|
withGlToast = true,
|
|
|
|
mountFn = shallowMountExtended,
|
|
|
|
} = {}) => {
|
2021-10-22 18:13:20 +00:00
|
|
|
const localVue = withGlToast ? vueWithGlToast() : undefined;
|
|
|
|
|
2022-04-14 03:08:57 +00:00
|
|
|
wrapper = mountFn(RegistrationToken, {
|
2022-01-20 18:14:18 +00:00
|
|
|
propsData: {
|
|
|
|
value: mockToken,
|
2022-06-09 18:08:13 +00:00
|
|
|
inputId: 'token-value',
|
2022-01-20 18:14:18 +00:00
|
|
|
...props,
|
|
|
|
},
|
|
|
|
localVue,
|
|
|
|
});
|
2021-10-22 18:13:20 +00:00
|
|
|
|
|
|
|
showToast = wrapper.vm.$toast ? jest.spyOn(wrapper.vm.$toast, 'show') : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
2022-04-14 03:08:57 +00:00
|
|
|
it('Displays value and copy button', () => {
|
|
|
|
createComponent();
|
2021-10-22 18:13:20 +00:00
|
|
|
|
2022-04-14 03:08:57 +00:00
|
|
|
expect(findInputCopyToggleVisibility().props('value')).toBe(mockToken);
|
|
|
|
expect(findInputCopyToggleVisibility().props('copyButtonTitle')).toBe(
|
|
|
|
'Copy registration token',
|
|
|
|
);
|
2021-10-22 18:13:20 +00:00
|
|
|
});
|
|
|
|
|
2022-04-14 03:08:57 +00:00
|
|
|
// 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-10-22 18:13:20 +00:00
|
|
|
});
|
|
|
|
|
2022-04-14 03:08:57 +00:00
|
|
|
describe('When the copy to clipboard button is clicked', () => {
|
2021-10-22 18:13:20 +00:00
|
|
|
beforeEach(() => {
|
2022-04-14 03:08:57 +00:00
|
|
|
createComponent();
|
2021-10-22 18:13:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows a copied message', () => {
|
2022-04-14 03:08:57 +00:00
|
|
|
findInputCopyToggleVisibility().vm.$emit('copy');
|
2021-10-22 18:13:20 +00:00
|
|
|
|
|
|
|
expect(showToast).toHaveBeenCalledTimes(1);
|
|
|
|
expect(showToast).toHaveBeenCalledWith('Registration token copied!');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not fail when toast is not defined', () => {
|
|
|
|
createComponent({ withGlToast: false });
|
2022-04-14 03:08:57 +00:00
|
|
|
findInputCopyToggleVisibility().vm.$emit('copy');
|
2021-10-22 18:13:20 +00:00
|
|
|
|
|
|
|
// This block also tests for unhandled errors
|
|
|
|
expect(showToast).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|