summaryrefslogtreecommitdiff
path: root/spec/frontend/pages/projects/learn_gitlab/components/learn_gitlab_section_link_spec.js
blob: 293353083701fe50d4428bf04fa203e16e62ed96 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { GlPopover, GlLink } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import { stubExperiments } from 'helpers/experimentation_helper';
import { mockTracking, triggerEvent, unmockTracking } from 'helpers/tracking_helper';
import eventHub from '~/invite_members/event_hub';
import LearnGitlabSectionLink from '~/pages/projects/learn_gitlab/components/learn_gitlab_section_link.vue';
import { ACTION_LABELS } from '~/pages/projects/learn_gitlab/constants';

const defaultAction = 'gitWrite';
const defaultProps = {
  title: 'Create Repository',
  description: 'Some description',
  url: 'https://example.com',
  completed: false,
  enabled: true,
};

const openInNewTabProps = {
  url: 'https://docs.gitlab.com/ee/user/application_security/security_dashboard/',
  openInNewTab: true,
};

describe('Learn GitLab Section Link', () => {
  let wrapper;

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

  const createWrapper = (action = defaultAction, props = {}) => {
    wrapper = extendedWrapper(
      mount(LearnGitlabSectionLink, {
        propsData: { action, value: { ...defaultProps, ...props } },
      }),
    );
  };

  const openInviteMembesrModalLink = () =>
    wrapper.find('[data-testid="invite-for-help-continuous-onboarding-experiment-link"]');

  const findUncompletedLink = () => wrapper.find('[data-testid="uncompleted-learn-gitlab-link"]');
  const findDisabledLink = () => wrapper.findByTestId('disabled-learn-gitlab-link');
  const findPopoverTrigger = () => wrapper.findByTestId('contact-admin-popover-trigger');
  const findPopover = () => wrapper.findComponent(GlPopover);
  const findPopoverLink = () => findPopover().findComponent(GlLink);
  const videoTutorialLink = () => wrapper.find('[data-testid="video-tutorial-link"]');

  it('renders no icon when not completed', () => {
    createWrapper(undefined, { completed: false });

    expect(wrapper.find('[data-testid="completed-icon"]').exists()).toBe(false);
  });

  it('renders the completion icon when completed', () => {
    createWrapper(undefined, { completed: true });

    expect(wrapper.find('[data-testid="completed-icon"]').exists()).toBe(true);
  });

  it('renders no trial only when it is not required', () => {
    createWrapper();

    expect(wrapper.find('[data-testid="trial-only"]').exists()).toBe(false);
  });

  it('renders trial only when trial is required', () => {
    createWrapper('codeOwnersEnabled');

    expect(wrapper.find('[data-testid="trial-only"]').exists()).toBe(true);
  });

  describe('disabled links', () => {
    beforeEach(() => {
      createWrapper('trialStarted', { enabled: false });
    });

    it('renders text without a link', () => {
      expect(findDisabledLink().exists()).toBe(true);
      expect(findDisabledLink().text()).toBe(ACTION_LABELS.trialStarted.title);
      expect(findDisabledLink().attributes('href')).toBeUndefined();
    });

    it('renders a popover trigger with question icon', () => {
      expect(findPopoverTrigger().exists()).toBe(true);
      expect(findPopoverTrigger().props('icon')).toBe('question-o');
      expect(findPopoverTrigger().attributes('aria-label')).toBe(
        LearnGitlabSectionLink.i18n.contactAdmin,
      );
    });

    it('renders a popover', () => {
      expect(findPopoverTrigger().attributes('id')).toBe(findPopover().props('target'));
      expect(findPopover().props()).toMatchObject({
        placement: 'top',
        triggers: 'hover focus',
      });
    });

    it('renders default disabled message', () => {
      expect(findPopover().text()).toContain(LearnGitlabSectionLink.i18n.contactAdmin);
    });

    it('renders custom disabled message if provided', () => {
      createWrapper('trialStarted', { enabled: false, message: 'Custom message' });
      expect(findPopover().text()).toContain('Custom message');
    });

    it('renders a link inside the popover', () => {
      expect(findPopoverLink().exists()).toBe(true);
      expect(findPopoverLink().attributes('href')).toBe(defaultProps.url);
    });
  });

  describe('links marked with openInNewTab', () => {
    beforeEach(() => {
      createWrapper('securityScanEnabled', openInNewTabProps);
    });

    it('renders links with blank target', () => {
      const linkElement = findUncompletedLink();

      expect(linkElement.exists()).toBe(true);
      expect(linkElement.attributes('target')).toEqual('_blank');
    });

    it('tracks the click', () => {
      const trackingSpy = mockTracking('_category_', wrapper.element, jest.spyOn);

      findUncompletedLink().trigger('click');

      expect(trackingSpy).toHaveBeenCalledWith('_category_', 'click_link', {
        label: 'run_a_security_scan_using_ci_cd',
      });

      unmockTracking();
    });
  });

  describe('rendering a link to open the invite_members modal instead of a regular link', () => {
    it.each`
      action           | experimentVariant | showModal
      ${'userAdded'}   | ${'candidate'}    | ${true}
      ${'userAdded'}   | ${'control'}      | ${false}
      ${defaultAction} | ${'candidate'}    | ${false}
      ${defaultAction} | ${'control'}      | ${false}
    `(
      'when the invite_for_help_continuous_onboarding experiment has variant: $experimentVariant and action is $action, the modal link is shown: $showModal',
      ({ action, experimentVariant, showModal }) => {
        stubExperiments({ invite_for_help_continuous_onboarding: experimentVariant });
        createWrapper(action);

        expect(openInviteMembesrModalLink().exists()).toBe(showModal);
      },
    );
  });

  describe('clicking the link to open the invite_members modal', () => {
    beforeEach(() => {
      jest.spyOn(eventHub, '$emit').mockImplementation();

      stubExperiments({ invite_for_help_continuous_onboarding: 'candidate' });
      createWrapper('userAdded');
    });

    it('calls the eventHub', () => {
      openInviteMembesrModalLink().vm.$emit('click');

      expect(eventHub.$emit).toHaveBeenCalledWith('openModal', { source: 'learn_gitlab' });
    });

    it('tracks the click', async () => {
      const trackingSpy = mockTracking('_category_', wrapper.element, jest.spyOn);

      triggerEvent(openInviteMembesrModalLink().element);

      expect(trackingSpy).toHaveBeenCalledWith('_category_', 'click_link', {
        label: 'invite_your_colleagues',
        property: 'Growth::Activation::Experiment::InviteForHelpContinuousOnboarding',
      });

      unmockTracking();
    });
  });

  describe('video_tutorials_continuous_onboarding experiment', () => {
    describe('when control', () => {
      beforeEach(() => {
        stubExperiments({ video_tutorials_continuous_onboarding: 'control' });
        createWrapper('codeOwnersEnabled');
      });

      it('renders no video link', () => {
        expect(videoTutorialLink().exists()).toBe(false);
      });
    });

    describe('when candidate', () => {
      beforeEach(() => {
        stubExperiments({ video_tutorials_continuous_onboarding: 'candidate' });
        createWrapper('codeOwnersEnabled');
      });

      it('renders video link with blank target', () => {
        const videoLinkElement = videoTutorialLink();

        expect(videoLinkElement.exists()).toBe(true);
        expect(videoLinkElement.attributes('target')).toEqual('_blank');
      });

      it('tracks the click', () => {
        const trackingSpy = mockTracking('_category_', wrapper.element, jest.spyOn);

        videoTutorialLink().trigger('click');

        expect(trackingSpy).toHaveBeenCalledWith('_category_', 'click_video_link', {
          label: 'add_code_owners',
          property: 'Growth::Conversion::Experiment::LearnGitLab',
          context: {
            data: {
              experiment: 'video_tutorials_continuous_onboarding',
              variant: 'candidate',
            },
            schema: 'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0',
          },
        });

        unmockTracking();
      });
    });
  });
});