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

abuse_reports_filtered_search_bar_spec.js « components « abuse_reports « admin « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f3f2caa995ad4d98553641c73cfb97b7f30a9e0 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { shallowMount } from '@vue/test-utils';
import setWindowLocation from 'helpers/set_window_location_helper';
import { redirectTo, updateHistory } from '~/lib/utils/url_utility'; // eslint-disable-line import/no-deprecated
import AbuseReportsFilteredSearchBar from '~/admin/abuse_reports/components/abuse_reports_filtered_search_bar.vue';
import {
  FILTERED_SEARCH_TOKENS,
  FILTERED_SEARCH_TOKEN_USER,
  FILTERED_SEARCH_TOKEN_REPORTER,
  FILTERED_SEARCH_TOKEN_STATUS,
  FILTERED_SEARCH_TOKEN_CATEGORY,
  DEFAULT_SORT,
  SORT_OPTIONS,
} from '~/admin/abuse_reports/constants';
import FilteredSearchBar from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import { FILTERED_SEARCH_TERM } from '~/vue_shared/components/filtered_search_bar/constants';
import { buildFilteredSearchCategoryToken } from '~/admin/abuse_reports/utils';

jest.mock('~/lib/utils/url_utility', () => {
  const urlUtility = jest.requireActual('~/lib/utils/url_utility');

  return {
    __esModule: true,
    ...urlUtility,
    redirectTo: jest.fn(),
    updateHistory: jest.fn(),
  };
});

describe('AbuseReportsFilteredSearchBar', () => {
  let wrapper;

  const CATEGORIES = ['spam', 'phishing'];

  const createComponent = () => {
    wrapper = shallowMount(AbuseReportsFilteredSearchBar, {
      provide: { categories: CATEGORIES },
    });
  };

  const findFilteredSearchBar = () => wrapper.findComponent(FilteredSearchBar);

  beforeEach(() => {
    setWindowLocation('https://localhost');
  });

  it('passes correct props to `FilteredSearchBar` component', () => {
    createComponent();

    const categoryToken = buildFilteredSearchCategoryToken(CATEGORIES);

    expect(findFilteredSearchBar().props()).toMatchObject({
      namespace: 'abuse_reports',
      recentSearchesStorageKey: 'abuse_reports',
      searchInputPlaceholder: 'Filter reports',
      tokens: [...FILTERED_SEARCH_TOKENS, categoryToken],
      initialSortBy: DEFAULT_SORT,
      sortOptions: SORT_OPTIONS,
    });
  });

  it.each([undefined, 'invalid'])(
    'sets status=open query when initial status query is %s',
    (status) => {
      if (status) {
        setWindowLocation(`?status=${status}`);
      }

      createComponent();

      expect(updateHistory).toHaveBeenCalledWith({
        url: 'https://localhost/?status=open',
        replace: true,
      });

      expect(findFilteredSearchBar().props('initialFilterValue')).toMatchObject([
        {
          type: FILTERED_SEARCH_TOKEN_STATUS.type,
          value: { data: 'open', operator: '=' },
        },
      ]);
    },
  );

  it('parses and passes search param to `FilteredSearchBar` component as `initialFilterValue` prop', () => {
    setWindowLocation('?status=closed&user=mr_abuser&reporter=ms_nitch');

    createComponent();

    expect(findFilteredSearchBar().props('initialFilterValue')).toMatchObject([
      {
        type: FILTERED_SEARCH_TOKEN_USER.type,
        value: { data: 'mr_abuser', operator: '=' },
      },
      {
        type: FILTERED_SEARCH_TOKEN_REPORTER.type,
        value: { data: 'ms_nitch', operator: '=' },
      },
      {
        type: FILTERED_SEARCH_TOKEN_STATUS.type,
        value: { data: 'closed', operator: '=' },
      },
    ]);
  });

  describe('initial sort', () => {
    it.each(
      SORT_OPTIONS.flatMap(({ sortDirection: { descending, ascending } }) => [
        descending,
        ascending,
      ]),
    )(
      'parses sort=%s query and passes it to `FilteredSearchBar` component as initialSortBy',
      (sortBy) => {
        setWindowLocation(`?sort=${sortBy}`);

        createComponent();

        expect(findFilteredSearchBar().props('initialSortBy')).toEqual(sortBy);
      },
    );

    it(`uses ${DEFAULT_SORT} as initialSortBy when sort query param is invalid`, () => {
      setWindowLocation(`?sort=unknown`);

      createComponent();

      expect(findFilteredSearchBar().props('initialSortBy')).toEqual(DEFAULT_SORT);
    });
  });

  describe('onFilter', () => {
    const USER_FILTER_TOKEN = {
      type: FILTERED_SEARCH_TOKEN_USER.type,
      value: { data: 'mr_abuser', operator: '=' },
    };
    const REPORTER_FILTER_TOKEN = {
      type: FILTERED_SEARCH_TOKEN_REPORTER.type,
      value: { data: 'ms_nitch', operator: '=' },
    };
    const STATUS_FILTER_TOKEN = {
      type: FILTERED_SEARCH_TOKEN_STATUS.type,
      value: { data: 'open', operator: '=' },
    };
    const CATEGORY_FILTER_TOKEN = {
      type: FILTERED_SEARCH_TOKEN_CATEGORY.type,
      value: { data: 'spam', operator: '=' },
    };

    const createComponentAndFilter = (filterTokens, initialLocation) => {
      if (initialLocation) {
        setWindowLocation(initialLocation);
      }

      createComponent();

      findFilteredSearchBar().vm.$emit('onFilter', filterTokens);
    };

    it.each([USER_FILTER_TOKEN, REPORTER_FILTER_TOKEN, STATUS_FILTER_TOKEN, CATEGORY_FILTER_TOKEN])(
      'redirects with $type query param',
      (filterToken) => {
        createComponentAndFilter([filterToken]);
        const { type, value } = filterToken;
        expect(redirectTo).toHaveBeenCalledWith(`https://localhost/?${type}=${value.data}`); // eslint-disable-line import/no-deprecated
      },
    );

    it('ignores search query param', () => {
      const searchFilterToken = { type: FILTERED_SEARCH_TERM, value: { data: 'ignored' } };
      createComponentAndFilter([USER_FILTER_TOKEN, searchFilterToken]);
      expect(redirectTo).toHaveBeenCalledWith('https://localhost/?user=mr_abuser'); // eslint-disable-line import/no-deprecated
    });

    it('redirects without page query param', () => {
      createComponentAndFilter([USER_FILTER_TOKEN], '?page=2');
      expect(redirectTo).toHaveBeenCalledWith('https://localhost/?user=mr_abuser'); // eslint-disable-line import/no-deprecated
    });

    it('redirects with existing sort query param', () => {
      createComponentAndFilter([USER_FILTER_TOKEN], `?sort=${DEFAULT_SORT}`);
      // eslint-disable-next-line import/no-deprecated
      expect(redirectTo).toHaveBeenCalledWith(
        `https://localhost/?user=mr_abuser&sort=${DEFAULT_SORT}`,
      );
    });
  });

  describe('onSort', () => {
    const SORT_VALUE = 'updated_at_asc';
    const EXISTING_QUERY = 'status=closed&user=mr_abuser';

    const createComponentAndSort = (initialLocation) => {
      setWindowLocation(initialLocation);
      createComponent();
      findFilteredSearchBar().vm.$emit('onSort', SORT_VALUE);
    };

    it('redirects to URL with existing query params and the sort query param', () => {
      createComponentAndSort(`?${EXISTING_QUERY}`);

      // eslint-disable-next-line import/no-deprecated
      expect(redirectTo).toHaveBeenCalledWith(
        `https://localhost/?${EXISTING_QUERY}&sort=${SORT_VALUE}`,
      );
    });

    it('redirects without page query param', () => {
      createComponentAndSort(`?${EXISTING_QUERY}&page=2`);

      // eslint-disable-next-line import/no-deprecated
      expect(redirectTo).toHaveBeenCalledWith(
        `https://localhost/?${EXISTING_QUERY}&sort=${SORT_VALUE}`,
      );
    });

    it('redirects with existing sort query param replaced with the new one', () => {
      createComponentAndSort(`?${EXISTING_QUERY}&sort=created_at_desc`);

      // eslint-disable-next-line import/no-deprecated
      expect(redirectTo).toHaveBeenCalledWith(
        `https://localhost/?${EXISTING_QUERY}&sort=${SORT_VALUE}`,
      );
    });
  });
});