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

log_spec.js « log « components « job_details « ci « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de02c7aad6d22d7470545c19615214a6fd380b31 (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
import { mount } from '@vue/test-utils';
import Vue from 'vue';
// eslint-disable-next-line no-restricted-imports
import Vuex from 'vuex';
import waitForPromises from 'helpers/wait_for_promises';
import { scrollToElement } from '~/lib/utils/common_utils';
import Log from '~/ci/job_details/components/log/log.vue';
import LogLineHeader from '~/ci/job_details/components/log/line_header.vue';
import LineNumber from '~/ci/job_details/components/log/line_number.vue';
import { logLinesParser } from '~/ci/job_details/store/utils';
import { mockJobLog, mockJobLogLineCount } from './mock_data';

const mockPagePath = 'project/-/jobs/99';

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

describe('Job Log', () => {
  let wrapper;
  let actions;
  let state;
  let store;
  let toggleCollapsibleLineMock;

  Vue.use(Vuex);

  const createComponent = (props) => {
    store = new Vuex.Store({ actions, state });

    wrapper = mount(Log, {
      provide: {
        pagePath: mockPagePath,
      },
      propsData: {
        ...props,
      },
      store,
    });
  };

  beforeEach(() => {
    toggleCollapsibleLineMock = jest.fn();
    actions = {
      toggleCollapsibleLine: toggleCollapsibleLineMock,
      setupFullScreenListeners: jest.fn(),
    };

    const { lines, sections } = logLinesParser(mockJobLog);

    state = {
      jobLog: lines,
      jobLogSections: sections,
    };
  });

  const findLineNumbers = () => wrapper.findAllComponents(LineNumber);
  const findLineHeader = () => wrapper.findComponent(LogLineHeader);
  const findLineHeaders = () => wrapper.findAllComponents(LogLineHeader);

  describe('line numbers', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders a line number for each line %d with an href', () => {
      for (let i = 0; i < mockJobLogLineCount; i += 1) {
        const w = findLineNumbers().at(i);

        expect(w.text()).toBe(`${i + 1}`);
        expect(w.attributes('href')).toBe(`${mockPagePath}#L${i + 1}`);
      }
    });
  });

  describe('collapsible sections', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders a clickable header section', () => {
      expect(findLineHeader().attributes('role')).toBe('button');
    });

    it('renders an icon with the open state', () => {
      expect(findLineHeader().find('[data-testid="chevron-lg-down-icon"]').exists()).toBe(true);
    });

    describe('on click header section', () => {
      it('calls toggleCollapsibleLine', () => {
        findLineHeader().trigger('click');

        expect(toggleCollapsibleLineMock).toHaveBeenCalled();
      });
    });

    describe('duration', () => {
      it('shows duration', () => {
        expect(findLineHeader().props('duration')).toBe('00:00');
        expect(findLineHeader().props('hideDuration')).toBe(false);
      });

      it('hides duration', () => {
        state.jobLogSections['resolve-secrets'].hideDuration = true;
        createComponent();

        expect(findLineHeader().props('duration')).toBe('00:00');
        expect(findLineHeader().props('hideDuration')).toBe(true);
      });
    });

    describe('when a section is collapsed', () => {
      beforeEach(() => {
        state.jobLogSections['prepare-executor'].isClosed = true;

        createComponent();
      });

      it('hides lines in section', () => {
        expect(findLineNumbers().wrappers.map((w) => w.text())).toEqual([
          '1',
          '2',
          '3',
          '4',
          // closed section not shown
          '7',
        ]);
      });
    });
  });

  describe('anchor scrolling', () => {
    afterEach(() => {
      window.location.hash = '';
    });

    describe('when hash is not present', () => {
      it('does not scroll to line number', async () => {
        createComponent();

        await waitForPromises();

        expect(wrapper.find('#L9').exists()).toBe(false);
        expect(scrollToElement).not.toHaveBeenCalled();
      });
    });

    describe('when hash is present', () => {
      beforeEach(() => {
        window.location.hash = '#L6';
      });

      it('scrolls to line number', async () => {
        createComponent();

        state.jobLog = logLinesParser(mockJobLog, [], '#L6').lines;
        await waitForPromises();

        expect(scrollToElement).toHaveBeenCalledTimes(1);

        state.jobLog = logLinesParser(mockJobLog, [], '#L6').lines;
        await waitForPromises();

        expect(scrollToElement).toHaveBeenCalledTimes(1);
      });

      it('line number within collapsed section is visible', () => {
        state.jobLog = logLinesParser(mockJobLog, [], '#L6').lines;

        createComponent();

        expect(wrapper.find('#L6').exists()).toBe(true);
      });
    });

    describe('with search results', () => {
      it('passes isHighlighted prop correctly', () => {
        const mockSearchResults = [
          {
            offset: 1002,
            content: [
              {
                text: 'Using Docker executor with image dev.gitlab.org3',
              },
            ],
            section: 'prepare-executor',
            lineNumber: 3,
          },
        ];

        createComponent({ searchResults: mockSearchResults });

        expect(findLineHeaders().at(0).props('isHighlighted')).toBe(true);
        expect(findLineHeaders().at(1).props('isHighlighted')).toBe(false);
      });
    });
  });
});