summaryrefslogtreecommitdiff
path: root/spec/frontend/gitlab_version_check/components/security_patch_upgrade_alert_modal_spec.js
blob: f1ed32a5f79ced5d76dcdcce776c88366b645bc3 (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
import { GlModal, GlLink, GlSprintf } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import { sprintf } from '~/locale';
import SecurityPatchUpgradeAlertModal from '~/gitlab_version_check/components/security_patch_upgrade_alert_modal.vue';
import * as utils from '~/gitlab_version_check/utils';
import {
  UPGRADE_DOCS_URL,
  ABOUT_RELEASES_PAGE,
  TRACKING_ACTIONS,
  TRACKING_LABELS,
} from '~/gitlab_version_check/constants';

describe('SecurityPatchUpgradeAlertModal', () => {
  let wrapper;
  let trackingSpy;

  const defaultProps = {
    currentVersion: '11.1.1',
  };

  const createComponent = (props = {}) => {
    trackingSpy = mockTracking(undefined, undefined, jest.spyOn);

    wrapper = shallowMountExtended(SecurityPatchUpgradeAlertModal, {
      propsData: {
        ...defaultProps,
        ...props,
      },
      stubs: {
        GlModal,
        GlSprintf,
      },
    });
  };

  afterEach(() => {
    unmockTracking();
  });

  const expectDispatchedTracking = (action, label) => {
    expect(trackingSpy).toHaveBeenCalledWith(undefined, action, {
      label,
      property: defaultProps.currentVersion,
    });
  };

  const findGlModal = () => wrapper.findComponent(GlModal);
  const findGlModalTitle = () => wrapper.findByTestId('alert-modal-title');
  const findGlModalBody = () => wrapper.findByTestId('alert-modal-body');
  const findGlModalDetails = () => wrapper.findByTestId('alert-modal-details');
  const findGlLink = () => wrapper.findComponent(GlLink);
  const findGlRemindButton = () => wrapper.findByTestId('alert-modal-remind-button');
  const findGlUpgradeButton = () => wrapper.findByTestId('alert-modal-upgrade-button');

  describe('template defaults', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders visible critical security alert modal', () => {
      expect(findGlModal().props('visible')).toBe(true);
    });

    it('renders the modal title correctly', () => {
      expect(findGlModalTitle().text()).toBe(wrapper.vm.$options.i18n.modalTitle);
    });

    it('renders modal body without suggested versions', () => {
      expect(findGlModalBody().text()).toBe(
        sprintf(wrapper.vm.$options.i18n.modalBodyNoStableVersions, {
          currentVersion: defaultProps.currentVersion,
        }),
      );
    });

    it('does not render modal details', () => {
      expect(findGlModalDetails().exists()).toBe(false);
    });

    it(`tracks render ${TRACKING_LABELS.MODAL} correctly`, () => {
      expectDispatchedTracking(TRACKING_ACTIONS.RENDER, TRACKING_LABELS.MODAL);
    });

    it(`tracks click ${TRACKING_LABELS.DISMISS} when close button clicked`, async () => {
      await findGlModal().vm.$emit('close');

      expectDispatchedTracking(TRACKING_ACTIONS.CLICK_BUTTON, TRACKING_LABELS.DISMISS);
    });

    describe('Learn more link', () => {
      it('renders with correct text and link', () => {
        expect(findGlLink().text()).toBe(wrapper.vm.$options.i18n.learnMore);
        expect(findGlLink().attributes('href')).toBe(ABOUT_RELEASES_PAGE);
      });

      it(`tracks click ${TRACKING_LABELS.LEARN_MORE_LINK} when clicked`, async () => {
        await findGlLink().vm.$emit('click');

        expectDispatchedTracking(TRACKING_ACTIONS.CLICK_LINK, TRACKING_LABELS.LEARN_MORE_LINK);
      });
    });

    describe('Remind me button', () => {
      beforeEach(() => {
        wrapper.vm.$refs.alertModal.hide = jest.fn();
      });

      it('renders with correct text', () => {
        expect(findGlRemindButton().text()).toBe(wrapper.vm.$options.i18n.secondaryButtonText);
      });

      it(`tracks click ${TRACKING_LABELS.REMIND_ME_BTN} when clicked`, async () => {
        await findGlRemindButton().vm.$emit('click');

        expectDispatchedTracking(TRACKING_ACTIONS.CLICK_BUTTON, TRACKING_LABELS.REMIND_ME_BTN);
      });

      it('calls setHideAlertModalCookie with the currentVersion when clicked', async () => {
        jest.spyOn(utils, 'setHideAlertModalCookie');
        await findGlRemindButton().vm.$emit('click');

        expect(utils.setHideAlertModalCookie).toHaveBeenCalledWith(defaultProps.currentVersion);
      });

      it('hides the modal', async () => {
        await findGlRemindButton().vm.$emit('click');

        expect(wrapper.vm.$refs.alertModal.hide).toHaveBeenCalled();
      });
    });

    describe('Upgrade button', () => {
      it('renders with correct text and link', () => {
        expect(findGlUpgradeButton().text()).toBe(wrapper.vm.$options.i18n.primaryButtonText);
        expect(findGlUpgradeButton().attributes('href')).toBe(UPGRADE_DOCS_URL);
      });

      it(`tracks click ${TRACKING_LABELS.UPGRADE_BTN_LINK} when clicked`, async () => {
        await findGlUpgradeButton().vm.$emit('click');

        expectDispatchedTracking(TRACKING_ACTIONS.CLICK_LINK, TRACKING_LABELS.UPGRADE_BTN_LINK);
      });

      it('calls setHideAlertModalCookie with the currentVersion when clicked', async () => {
        jest.spyOn(utils, 'setHideAlertModalCookie');
        await findGlUpgradeButton().vm.$emit('click');

        expect(utils.setHideAlertModalCookie).toHaveBeenCalledWith(defaultProps.currentVersion);
      });
    });
  });

  describe('template with latestStableVersions', () => {
    const latestStableVersions = ['88.8.3', '89.9.9', '90.0.0'];

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

    it('renders modal body with suggested versions', () => {
      expect(findGlModalBody().text()).toBe(
        sprintf(wrapper.vm.$options.i18n.modalBodyStableVersions, {
          currentVersion: defaultProps.currentVersion,
          latestStableVersions: latestStableVersions.join(', '),
        }),
      );
    });
  });

  describe('template with details', () => {
    const details = 'This is some details about the upgrade';

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

    it('renders modal details', () => {
      expect(findGlModalDetails().text()).toBe(
        sprintf(wrapper.vm.$options.i18n.modalDetails, { details }),
      );
    });
  });

  describe('when modal is hidden by cookie', () => {
    beforeEach(() => {
      jest.spyOn(utils, 'getHideAlertModalCookie').mockReturnValue(true);
      createComponent();
    });

    it('renders modal with visibility false', () => {
      expect(findGlModal().props('visible')).toBe(false);
    });

    it(`does not track render ${TRACKING_LABELS.MODAL} correctly`, () => {
      expect(trackingSpy).not.toHaveBeenCalledWith(undefined, TRACKING_ACTIONS.RENDER, {
        label: TRACKING_LABELS.MODAL,
        property: defaultProps.currentVersion,
      });
    });
  });
});