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

dropdown_filter_spec.js « components « search « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffac038e1c5aeae26f02399b6f88efaad96dd496 (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
import { shallowMount } from '@vue/test-utils';
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import DropdownFilter from '~/search/components/dropdown_filter.vue';
import {
  FILTER_STATES,
  FILTER_STATES_BY_SCOPE,
  FILTER_HEADER,
  SCOPES,
} from '~/search/state_filter/constants';
import * as urlUtils from '~/lib/utils/url_utility';

jest.mock('~/lib/utils/url_utility', () => ({
  visitUrl: jest.fn(),
  setUrlParams: jest.fn(),
}));

function createComponent(props = { scope: 'issues' }) {
  return shallowMount(DropdownFilter, {
    propsData: {
      filtersArray: FILTER_STATES_BY_SCOPE.issues,
      filters: FILTER_STATES,
      header: FILTER_HEADER,
      param: 'state',
      supportedScopes: Object.values(SCOPES),
      ...props,
    },
  });
}

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

  beforeEach(() => {
    wrapper = createComponent();
  });

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

  const findGlDropdown = () => wrapper.find(GlDropdown);
  const findGlDropdownItems = () => findGlDropdown().findAll(GlDropdownItem);
  const findDropdownItemsText = () => findGlDropdownItems().wrappers.map(w => w.text());
  const firstDropDownItem = () => findGlDropdownItems().at(0);

  describe('template', () => {
    describe.each`
      scope               | showDropdown
      ${'issues'}         | ${true}
      ${'merge_requests'} | ${true}
      ${'projects'}       | ${false}
      ${'milestones'}     | ${false}
      ${'users'}          | ${false}
      ${'notes'}          | ${false}
      ${'wiki_blobs'}     | ${false}
      ${'blobs'}          | ${false}
    `(`dropdown`, ({ scope, showDropdown }) => {
      beforeEach(() => {
        wrapper = createComponent({ scope });
      });

      it(`does${showDropdown ? '' : ' not'} render when scope is ${scope}`, () => {
        expect(findGlDropdown().exists()).toBe(showDropdown);
      });
    });

    describe.each`
      initialFilter                 | label
      ${FILTER_STATES.ANY.value}    | ${`Any ${FILTER_HEADER}`}
      ${FILTER_STATES.OPEN.value}   | ${FILTER_STATES.OPEN.label}
      ${FILTER_STATES.CLOSED.value} | ${FILTER_STATES.CLOSED.label}
    `(`filter text`, ({ initialFilter, label }) => {
      describe(`when initialFilter is ${initialFilter}`, () => {
        beforeEach(() => {
          wrapper = createComponent({ scope: 'issues', initialFilter });
        });

        it(`sets dropdown label to ${label}`, () => {
          expect(findGlDropdown().attributes('text')).toBe(label);
        });
      });
    });

    describe('Filter options', () => {
      it('renders a dropdown item for each filterOption', () => {
        expect(findDropdownItemsText()).toStrictEqual(
          FILTER_STATES_BY_SCOPE[SCOPES.ISSUES].map(v => {
            return v.label;
          }),
        );
      });

      it('clicking a dropdown item calls setUrlParams', () => {
        const state = FILTER_STATES[Object.keys(FILTER_STATES)[0]].value;
        firstDropDownItem().vm.$emit('click');

        expect(urlUtils.setUrlParams).toHaveBeenCalledWith({ state });
      });

      it('clicking a dropdown item calls visitUrl', () => {
        firstDropDownItem().vm.$emit('click');

        expect(urlUtils.visitUrl).toHaveBeenCalled();
      });
    });
  });
});