Welcome to mirror list, hosted at ThFree Co, Russian Federation.

registration_token_spec.js « registration « components « runner « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb42c7c8493fbf3dd021515243ce70a190b49b7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { GlToast } from '@gitlab/ui';
import { createLocalVue } from '@vue/test-utils';
import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper';
import RegistrationToken from '~/runner/components/registration/registration_token.vue';
import InputCopyToggleVisibility from '~/vue_shared/components/form/input_copy_toggle_visibility.vue';

const mockToken = '01234567890';
const mockMasked = '***********';

describe('RegistrationToken', () => {
  let wrapper;
  let showToast;

  const findInputCopyToggleVisibility = () => wrapper.findComponent(InputCopyToggleVisibility);

  const vueWithGlToast = () => {
    const localVue = createLocalVue();
    localVue.use(GlToast);
    return localVue;
  };

  const createComponent = ({
    props = {},
    withGlToast = true,
    mountFn = shallowMountExtended,
  } = {}) => {
    const localVue = withGlToast ? vueWithGlToast() : undefined;

    wrapper = mountFn(RegistrationToken, {
      propsData: {
        value: mockToken,
        ...props,
      },
      localVue,
    });

    showToast = wrapper.vm.$toast ? jest.spyOn(wrapper.vm.$toast, 'show') : null;
  };

  afterEach(() => {
    wrapper.destroy();
  });

  it('Displays value and copy button', () => {
    createComponent();

    expect(findInputCopyToggleVisibility().props('value')).toBe(mockToken);
    expect(findInputCopyToggleVisibility().props('copyButtonTitle')).toBe(
      'Copy registration token',
    );
  });

  // Component integration test to ensure secure masking
  it('Displays masked value by default', () => {
    createComponent({ mountFn: mountExtended });

    expect(wrapper.find('input').element.value).toBe(mockMasked);
  });

  describe('When the copy to clipboard button is clicked', () => {
    beforeEach(() => {
      createComponent();
    });

    it('shows a copied message', () => {
      findInputCopyToggleVisibility().vm.$emit('copy');

      expect(showToast).toHaveBeenCalledTimes(1);
      expect(showToast).toHaveBeenCalledWith('Registration token copied!');
    });

    it('does not fail when toast is not defined', () => {
      createComponent({ withGlToast: false });
      findInputCopyToggleVisibility().vm.$emit('copy');

      // This block also tests for unhandled errors
      expect(showToast).toBeNull();
    });
  });
});