summaryrefslogtreecommitdiff
path: root/spec/frontend/runner/components/registration/registration_token_spec.js
blob: f53ae16534454ac4688c65166e53ce137aef2053 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { nextTick } from 'vue';
import { GlToast } from '@gitlab/ui';
import { createLocalVue, shallowMount } from '@vue/test-utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import RegistrationToken from '~/runner/components/registration/registration_token.vue';
import ModalCopyButton from '~/vue_shared/components/modal_copy_button.vue';

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

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

  const findToggleMaskButton = () => wrapper.findByTestId('toggle-masked');
  const findCopyButton = () => wrapper.findComponent(ModalCopyButton);

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

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

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

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

  beforeEach(() => {
    stopPropagation = jest.fn();

    createComponent();
  });

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

  it('Displays masked value by default', () => {
    expect(wrapper.text()).toBe(mockMasked);
  });

  it('Displays button to reveal token', () => {
    expect(findToggleMaskButton().attributes('aria-label')).toBe('Click to reveal');
  });

  it('Can copy the original token value', () => {
    expect(findCopyButton().props('text')).toBe(mockToken);
  });

  describe('When the reveal icon is clicked', () => {
    beforeEach(() => {
      findToggleMaskButton().vm.$emit('click', { stopPropagation });
    });

    it('Click event is not propagated', async () => {
      expect(stopPropagation).toHaveBeenCalledTimes(1);
    });

    it('Displays the actual value', () => {
      expect(wrapper.text()).toBe(mockToken);
    });

    it('Can copy the original token value', () => {
      expect(findCopyButton().props('text')).toBe(mockToken);
    });

    it('Displays button to mask token', () => {
      expect(findToggleMaskButton().attributes('aria-label')).toBe('Click to hide');
    });

    it('When user clicks again, displays masked value', async () => {
      findToggleMaskButton().vm.$emit('click', { stopPropagation });
      await nextTick();

      expect(wrapper.text()).toBe(mockMasked);
      expect(findToggleMaskButton().attributes('aria-label')).toBe('Click to reveal');
    });
  });

  describe('When the copy to clipboard button is clicked', () => {
    it('shows a copied message', () => {
      findCopyButton().vm.$emit('success');

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

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

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