summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/runner_instructions/instructions/runner_aws_instructions_spec.js
blob: 4d566dbec0c24d24b63ef972fbef40ab6c2a4a6e (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
import {
  GlAccordion,
  GlAccordionItem,
  GlButton,
  GlFormRadio,
  GlFormRadioGroup,
  GlLink,
  GlSprintf,
} from '@gitlab/ui';
import { getBaseURL, visitUrl } from '~/lib/utils/url_utility';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { mockTracking } from 'helpers/tracking_helper';
import {
  AWS_README_URL,
  AWS_CF_BASE_URL,
  AWS_TEMPLATES_BASE_URL,
  AWS_EASY_BUTTONS,
} from '~/vue_shared/components/runner_instructions/constants';
import RunnerAwsInstructions from '~/vue_shared/components/runner_instructions/instructions/runner_aws_instructions.vue';
import { __ } from '~/locale';

jest.mock('~/lib/utils/url_utility', () => ({
  ...jest.requireActual('~/lib/utils/url_utility'),
  visitUrl: jest.fn(),
}));

describe('RunnerAwsInstructions', () => {
  let wrapper;

  const findEasyButtonsRadioGroup = () => wrapper.findComponent(GlFormRadioGroup);
  const findEasyButtons = () => wrapper.findAllComponents(GlFormRadio);
  const findEasyButtonAt = (i) => findEasyButtons().at(i);
  const findLink = () => wrapper.findComponent(GlLink);
  const findOkButton = () =>
    wrapper
      .findAllComponents(GlButton)
      .filter((w) => w.props('variant') === 'confirm')
      .at(0);
  const findCloseButton = () => wrapper.findByText(__('Close'));

  const createComponent = () => {
    wrapper = shallowMountExtended(RunnerAwsInstructions, {
      stubs: {
        GlSprintf,
      },
    });
  };

  beforeEach(() => {
    createComponent();
  });

  it('should contain every button', () => {
    expect(findEasyButtons()).toHaveLength(AWS_EASY_BUTTONS.length);
  });

  const AWS_EASY_BUTTONS_PARAMS = AWS_EASY_BUTTONS.map((val, idx) => ({ ...val, idx }));

  describe.each(AWS_EASY_BUTTONS_PARAMS)(
    'easy button %#',
    ({ idx, description, moreDetails1, moreDetails2, templateName, stackName }) => {
      it('should contain button description', () => {
        const text = findEasyButtonAt(idx).text();

        expect(text).toContain(description);
        expect(text).toContain(moreDetails1);
        expect(text).toContain(moreDetails2);
      });

      it('should show more details', () => {
        const accordion = findEasyButtonAt(idx).findComponent(GlAccordion);
        const accordionItem = accordion.findComponent(GlAccordionItem);

        expect(accordion.props('headerLevel')).toBe(3);
        expect(accordionItem.props('title')).toBe(__('More Details'));
        expect(accordionItem.props('titleVisible')).toBe(__('Less Details'));
      });

      describe('when clicked', () => {
        let trackingSpy;

        beforeEach(() => {
          trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);

          findEasyButtonsRadioGroup().vm.$emit('input', idx);
          findOkButton().vm.$emit('click');
        });

        it('should contain the correct link', () => {
          const templateUrl = encodeURIComponent(AWS_TEMPLATES_BASE_URL + templateName);
          const instanceUrl = encodeURIComponent(getBaseURL());
          const url = `${AWS_CF_BASE_URL}templateURL=${templateUrl}&stackName=${stackName}&param_3GITLABRunnerInstanceURL=${instanceUrl}`;

          expect(visitUrl).toHaveBeenCalledTimes(1);
          expect(visitUrl).toHaveBeenCalledWith(url, true);
        });

        it('should track an event when clicked', () => {
          expect(trackingSpy).toHaveBeenCalledTimes(1);
          expect(trackingSpy).toHaveBeenCalledWith(undefined, 'template_clicked', {
            label: stackName,
          });
        });
      });
    },
  );

  it('displays link with more information', () => {
    expect(findLink().attributes('href')).toBe(AWS_README_URL);
  });

  it('triggers the modal to close', () => {
    findCloseButton().vm.$emit('click');

    expect(wrapper.emitted('close')).toHaveLength(1);
  });
});