summaryrefslogtreecommitdiff
path: root/spec/frontend/diffs/components/diff_row_utils_spec.js
blob: 394b6cb1914065f8f510acdb66f9cd7d0b8b72d8 (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
import * as utils from '~/diffs/components/diff_row_utils';
import {
  MATCH_LINE_TYPE,
  CONTEXT_LINE_TYPE,
  OLD_NO_NEW_LINE_TYPE,
  NEW_NO_NEW_LINE_TYPE,
  EMPTY_CELL_TYPE,
} from '~/diffs/constants';

const LINE_CODE = 'abc123';

describe('isHighlighted', () => {
  it('should return true if line is highlighted', () => {
    const state = { diffs: { highlightedRow: LINE_CODE } };
    const line = { line_code: LINE_CODE };
    const isCommented = false;
    expect(utils.isHighlighted(state, line, isCommented)).toBe(true);
  });

  it('should return false if line is not highlighted', () => {
    const state = { diffs: { highlightedRow: 'xxx' } };
    const line = { line_code: LINE_CODE };
    const isCommented = false;
    expect(utils.isHighlighted(state, line, isCommented)).toBe(false);
  });

  it('should return true if isCommented is true', () => {
    const state = { diffs: { highlightedRow: 'xxx' } };
    const line = { line_code: LINE_CODE };
    const isCommented = true;
    expect(utils.isHighlighted(state, line, isCommented)).toBe(true);
  });
});

describe('isContextLine', () => {
  it('return true if line type is context', () => {
    expect(utils.isContextLine(CONTEXT_LINE_TYPE)).toBe(true);
  });

  it('return false if line type is not context', () => {
    expect(utils.isContextLine('xxx')).toBe(false);
  });
});

describe('isMatchLine', () => {
  it('return true if line type is match', () => {
    expect(utils.isMatchLine(MATCH_LINE_TYPE)).toBe(true);
  });

  it('return false if line type is not match', () => {
    expect(utils.isMatchLine('xxx')).toBe(false);
  });
});

describe('isMetaLine', () => {
  it.each`
    type                    | expectation
    ${OLD_NO_NEW_LINE_TYPE} | ${true}
    ${NEW_NO_NEW_LINE_TYPE} | ${true}
    ${EMPTY_CELL_TYPE}      | ${true}
    ${'xxx'}                | ${false}
  `('should return $expectation if type is $type', ({ type, expectation }) => {
    expect(utils.isMetaLine(type)).toBe(expectation);
  });
});

describe('shouldRenderCommentButton', () => {
  it('should return false if comment button is not rendered', () => {
    expect(utils.shouldRenderCommentButton(true, false)).toBe(false);
  });

  it('should return false if not logged in', () => {
    expect(utils.shouldRenderCommentButton(false, true)).toBe(false);
  });

  it('should return true logged in and rendered', () => {
    expect(utils.shouldRenderCommentButton(true, true)).toBe(true);
  });
});

describe('hasDiscussions', () => {
  it('should return false if line is undefined', () => {
    expect(utils.hasDiscussions()).toBe(false);
  });

  it('should return false if discussions is undefined', () => {
    expect(utils.hasDiscussions({})).toBe(false);
  });

  it('should return false if discussions has legnth of 0', () => {
    expect(utils.hasDiscussions({ discussions: [] })).toBe(false);
  });

  it('should return true if discussions has legnth > 0', () => {
    expect(utils.hasDiscussions({ discussions: [1] })).toBe(true);
  });
});

describe('lineHref', () => {
  it(`should return #${LINE_CODE}`, () => {
    expect(utils.lineHref({ line_code: LINE_CODE })).toEqual(`#${LINE_CODE}`);
  });

  it(`should return '#' if line is undefined`, () => {
    expect(utils.lineHref()).toEqual('#');
  });

  it(`should return '#' if line_code is undefined`, () => {
    expect(utils.lineHref({})).toEqual('#');
  });
});

describe('lineCode', () => {
  it(`should return undefined if line_code is undefined`, () => {
    expect(utils.lineCode()).toEqual(undefined);
    expect(utils.lineCode({ left: {} })).toEqual(undefined);
    expect(utils.lineCode({ right: {} })).toEqual(undefined);
  });

  it(`should return ${LINE_CODE}`, () => {
    expect(utils.lineCode({ line_code: LINE_CODE })).toEqual(LINE_CODE);
    expect(utils.lineCode({ left: { line_code: LINE_CODE } })).toEqual(LINE_CODE);
    expect(utils.lineCode({ right: { line_code: LINE_CODE } })).toEqual(LINE_CODE);
  });
});

describe('classNameMapCell', () => {
  it.each`
    line               | hll      | loggedIn | hovered  | expectation
    ${undefined}       | ${true}  | ${true}  | ${true}  | ${[]}
    ${{ type: 'new' }} | ${false} | ${false} | ${false} | ${['new', { hll: false, 'is-over': false }]}
    ${{ type: 'new' }} | ${true}  | ${true}  | ${false} | ${['new', { hll: true, 'is-over': false }]}
    ${{ type: 'new' }} | ${true}  | ${false} | ${true}  | ${['new', { hll: true, 'is-over': false }]}
    ${{ type: 'new' }} | ${true}  | ${true}  | ${true}  | ${['new', { hll: true, 'is-over': true }]}
  `('should return $expectation', ({ line, hll, loggedIn, hovered, expectation }) => {
    const classes = utils.classNameMapCell(line, hll, loggedIn, hovered);
    expect(classes).toEqual(expectation);
  });
});

describe('addCommentTooltip', () => {
  const brokenSymLinkTooltip =
    'Commenting on symbolic links that replace or are replaced by files is currently not supported.';
  const brokenRealTooltip =
    'Commenting on files that replace or are replaced by symbolic links is currently not supported.';
  it('should return default tooltip', () => {
    expect(utils.addCommentTooltip()).toBeUndefined();
  });

  it('should return broken symlink tooltip', () => {
    expect(utils.addCommentTooltip({ commentsDisabled: { wasSymbolic: true } })).toEqual(
      brokenSymLinkTooltip,
    );
    expect(utils.addCommentTooltip({ commentsDisabled: { isSymbolic: true } })).toEqual(
      brokenSymLinkTooltip,
    );
  });

  it('should return broken real tooltip', () => {
    expect(utils.addCommentTooltip({ commentsDisabled: { wasReal: true } })).toEqual(
      brokenRealTooltip,
    );
    expect(utils.addCommentTooltip({ commentsDisabled: { isReal: true } })).toEqual(
      brokenRealTooltip,
    );
  });
});

describe('parallelViewLeftLineType', () => {
  it(`should return ${OLD_NO_NEW_LINE_TYPE}`, () => {
    expect(utils.parallelViewLeftLineType({ right: { type: NEW_NO_NEW_LINE_TYPE } })).toEqual(
      OLD_NO_NEW_LINE_TYPE,
    );
  });

  it(`should return 'new'`, () => {
    expect(utils.parallelViewLeftLineType({ left: { type: 'new' } })).toContain('new');
  });

  it(`should return ${EMPTY_CELL_TYPE}`, () => {
    expect(utils.parallelViewLeftLineType({})).toContain(EMPTY_CELL_TYPE);
  });

  it(`should return hll:true`, () => {
    expect(utils.parallelViewLeftLineType({}, true)[1]).toEqual({ hll: true });
  });
});

describe('shouldShowCommentButton', () => {
  it.each`
    hover    | context  | meta     | discussions | expectation
    ${true}  | ${false} | ${false} | ${false}    | ${true}
    ${false} | ${false} | ${false} | ${false}    | ${false}
    ${true}  | ${true}  | ${false} | ${false}    | ${false}
    ${true}  | ${true}  | ${true}  | ${false}    | ${false}
    ${true}  | ${true}  | ${true}  | ${true}     | ${false}
  `(
    'should return $expectation when hover is $hover',
    ({ hover, context, meta, discussions, expectation }) => {
      expect(utils.shouldShowCommentButton(hover, context, meta, discussions)).toBe(expectation);
    },
  );
});