summaryrefslogtreecommitdiff
path: root/spec/frontend/issues/list/utils_spec.js
blob: e8ffba9bc80de3744418016a5aafd2f69e19054a (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
import setWindowLocation from 'helpers/set_window_location_helper';
import { TEST_HOST } from 'helpers/test_constants';
import {
  apiParams,
  apiParamsWithSpecialValues,
  filteredTokens,
  filteredTokensWithSpecialValues,
  locationSearch,
  locationSearchWithSpecialValues,
  urlParams,
  urlParamsWithSpecialValues,
} from 'jest/issues/list/mock_data';
import {
  PAGE_SIZE,
  PAGE_SIZE_MANUAL,
  RELATIVE_POSITION_ASC,
  urlSortParams,
} from '~/issues/list/constants';
import {
  convertToApiParams,
  convertToSearchQuery,
  convertToUrlParams,
  getFilterTokens,
  getInitialPageParams,
  getSortKey,
  getSortOptions,
  isSortKey,
} from '~/issues/list/utils';
import { FILTERED_SEARCH_TERM } from '~/vue_shared/components/filtered_search_bar/constants';

describe('getInitialPageParams', () => {
  it.each(Object.keys(urlSortParams))(
    'returns the correct page params for sort key %s',
    (sortKey) => {
      const firstPageSize = sortKey === RELATIVE_POSITION_ASC ? PAGE_SIZE_MANUAL : PAGE_SIZE;

      expect(getInitialPageParams(sortKey)).toEqual({ firstPageSize });
    },
  );

  it.each(Object.keys(urlSortParams))(
    'returns the correct page params for sort key %s with afterCursor',
    (sortKey) => {
      const firstPageSize = sortKey === RELATIVE_POSITION_ASC ? PAGE_SIZE_MANUAL : PAGE_SIZE;
      const lastPageSize = undefined;
      const afterCursor = 'randomCursorString';
      const beforeCursor = undefined;
      const pageParams = getInitialPageParams(
        sortKey,
        firstPageSize,
        lastPageSize,
        afterCursor,
        beforeCursor,
      );

      expect(pageParams).toEqual({ firstPageSize, afterCursor });
    },
  );

  it.each(Object.keys(urlSortParams))(
    'returns the correct page params for sort key %s with beforeCursor',
    (sortKey) => {
      const firstPageSize = undefined;
      const lastPageSize = PAGE_SIZE;
      const afterCursor = undefined;
      const beforeCursor = 'anotherRandomCursorString';
      const pageParams = getInitialPageParams(
        sortKey,
        firstPageSize,
        lastPageSize,
        afterCursor,
        beforeCursor,
      );

      expect(pageParams).toEqual({ lastPageSize, beforeCursor });
    },
  );
});

describe('getSortKey', () => {
  it.each(Object.keys(urlSortParams))('returns %s given the correct inputs', (sortKey) => {
    const sort = urlSortParams[sortKey];
    expect(getSortKey(sort)).toBe(sortKey);
  });
});

describe('isSortKey', () => {
  it.each(Object.keys(urlSortParams))('returns true given %s', (sort) => {
    expect(isSortKey(sort)).toBe(true);
  });

  it.each(['', 'asdf', null, undefined])('returns false given %s', (sort) => {
    expect(isSortKey(sort)).toBe(false);
  });
});

describe('getSortOptions', () => {
  describe.each`
    hasIssueWeightsFeature | hasBlockedIssuesFeature | length | containsWeight | containsBlocking
    ${false}               | ${false}                | ${9}   | ${false}       | ${false}
    ${true}                | ${false}                | ${10}  | ${true}        | ${false}
    ${false}               | ${true}                 | ${10}  | ${false}       | ${true}
    ${true}                | ${true}                 | ${11}  | ${true}        | ${true}
  `(
    'when hasIssueWeightsFeature=$hasIssueWeightsFeature and hasBlockedIssuesFeature=$hasBlockedIssuesFeature',
    ({
      hasIssueWeightsFeature,
      hasBlockedIssuesFeature,
      length,
      containsWeight,
      containsBlocking,
    }) => {
      const sortOptions = getSortOptions(hasIssueWeightsFeature, hasBlockedIssuesFeature);

      it('returns the correct length of sort options', () => {
        expect(sortOptions).toHaveLength(length);
      });

      it(`${containsWeight ? 'contains' : 'does not contain'} weight option`, () => {
        expect(sortOptions.some((option) => option.title === 'Weight')).toBe(containsWeight);
      });

      it(`${containsBlocking ? 'contains' : 'does not contain'} blocking option`, () => {
        expect(sortOptions.some((option) => option.title === 'Blocking')).toBe(containsBlocking);
      });
    },
  );
});

describe('getFilterTokens', () => {
  it('returns filtered tokens given "window.location.search"', () => {
    expect(getFilterTokens(locationSearch)).toEqual(filteredTokens);
  });

  it('returns filtered tokens given "window.location.search" with special values', () => {
    expect(getFilterTokens(locationSearchWithSpecialValues)).toEqual(
      filteredTokensWithSpecialValues,
    );
  });

  it.each`
    description              | argument
    ${'an undefined value'}  | ${undefined}
    ${'an irrelevant value'} | ${'?unrecognised=parameter'}
  `('returns an empty filtered search term given $description', ({ argument }) => {
    expect(getFilterTokens(argument)).toEqual([
      {
        id: expect.any(String),
        type: FILTERED_SEARCH_TERM,
        value: { data: '' },
      },
    ]);
  });
});

describe('convertToApiParams', () => {
  beforeEach(() => {
    setWindowLocation(TEST_HOST);
  });

  it('returns api params given filtered tokens', () => {
    expect(convertToApiParams(filteredTokens)).toEqual(apiParams);
  });

  it('returns api params given filtered tokens with special values', () => {
    setWindowLocation('?assignee_id=123');

    expect(convertToApiParams(filteredTokensWithSpecialValues)).toEqual(apiParamsWithSpecialValues);
  });
});

describe('convertToUrlParams', () => {
  beforeEach(() => {
    setWindowLocation(TEST_HOST);
  });

  it('returns url params given filtered tokens', () => {
    expect(convertToUrlParams(filteredTokens)).toEqual(urlParams);
  });

  it('returns url params given filtered tokens with special values', () => {
    setWindowLocation('?assignee_id=123');

    expect(convertToUrlParams(filteredTokensWithSpecialValues)).toEqual(urlParamsWithSpecialValues);
  });
});

describe('convertToSearchQuery', () => {
  it('returns search string given filtered tokens', () => {
    expect(convertToSearchQuery(filteredTokens)).toBe('find issues');
  });
});