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

step_spec.js « components « code_quality_walkthrough « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c397faf1f35421d3bfbc7d0c05665698302a795d (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
import { GlButton, GlPopover } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Cookies from 'js-cookie';
import Step from '~/code_quality_walkthrough/components/step.vue';
import { EXPERIMENT_NAME, STEPS } from '~/code_quality_walkthrough/constants';
import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants';
import { getParameterByName } from '~/lib/utils/common_utils';
import Tracking from '~/tracking';

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

let wrapper;

function factory({ step, link }) {
  wrapper = shallowMount(Step, {
    propsData: { step, link },
  });
}

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

const dummyLink = '/group/project/-/jobs/:id?code_quality_walkthrough=true';
const dummyContext = 'experiment_context';

const findButton = () => wrapper.findComponent(GlButton);
const findPopover = () => wrapper.findComponent(GlPopover);

describe('When the code_quality_walkthrough URL parameter is missing', () => {
  beforeEach(() => {
    getParameterByName.mockReturnValue(false);
  });

  it('does not render the component', () => {
    factory({
      step: STEPS.commitCiFile,
    });

    expect(findPopover().exists()).toBe(false);
  });
});

describe('When the code_quality_walkthrough URL parameter is present', () => {
  beforeEach(() => {
    getParameterByName.mockReturnValue(true);
    Cookies.set(EXPERIMENT_NAME, { data: dummyContext });
  });

  afterEach(() => {
    Cookies.remove(EXPERIMENT_NAME);
  });

  describe('When mounting the component', () => {
    beforeEach(() => {
      jest.spyOn(Tracking, 'event');

      factory({
        step: STEPS.commitCiFile,
      });
    });

    it('tracks an event', () => {
      expect(Tracking.event).toHaveBeenCalledWith(
        EXPERIMENT_NAME,
        `${STEPS.commitCiFile}_displayed`,
        {
          context: {
            schema: TRACKING_CONTEXT_SCHEMA,
            data: dummyContext,
          },
        },
      );
    });
  });

  describe('When updating the component', () => {
    beforeEach(() => {
      factory({
        step: STEPS.runningPipeline,
      });

      jest.spyOn(Tracking, 'event');

      wrapper.setProps({ step: STEPS.successPipeline });
    });

    it('tracks an event', () => {
      expect(Tracking.event).toHaveBeenCalledWith(
        EXPERIMENT_NAME,
        `${STEPS.successPipeline}_displayed`,
        {
          context: {
            schema: TRACKING_CONTEXT_SCHEMA,
            data: dummyContext,
          },
        },
      );
    });
  });

  describe('When dismissing a popover', () => {
    beforeEach(() => {
      factory({
        step: STEPS.commitCiFile,
      });

      jest.spyOn(Cookies, 'set');
      jest.spyOn(Tracking, 'event');

      findButton().vm.$emit('click');
    });

    it('sets a cookie', () => {
      expect(Cookies.set).toHaveBeenCalledWith(
        EXPERIMENT_NAME,
        { commit_ci_file: true, data: dummyContext },
        { expires: 365 },
      );
    });

    it('removes the popover', () => {
      expect(findPopover().exists()).toBe(false);
    });

    it('tracks an event', () => {
      expect(Tracking.event).toHaveBeenCalledWith(
        EXPERIMENT_NAME,
        `${STEPS.commitCiFile}_dismissed`,
        {
          context: {
            schema: TRACKING_CONTEXT_SCHEMA,
            data: dummyContext,
          },
        },
      );
    });
  });

  describe('Code Quality Walkthrough Step component', () => {
    describe.each(Object.values(STEPS))('%s step', (step) => {
      it(`renders ${step === STEPS.troubleshootJob ? 'an alert' : 'a popover'}`, () => {
        const options = { step };
        if ([STEPS.successPipeline, STEPS.failedPipeline].includes(step)) {
          options.link = dummyLink;
        }
        factory(options);

        expect(wrapper.element).toMatchSnapshot();
      });
    });
  });
});