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

sort_dropdown_spec.js « filter_sort « components « members « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 357fad741e96e5c6e8ddf7ee77bbcb1d724c0dbd (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
import { GlSorting, GlSortingItem } from '@gitlab/ui';
import { mount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import * as urlUtilities from '~/lib/utils/url_utility';
import SortDropdown from '~/members/components/filter_sort/sort_dropdown.vue';

const localVue = createLocalVue();
localVue.use(Vuex);

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

  const URL_HOST = 'https://localhost/';

  const createComponent = (state) => {
    const store = new Vuex.Store({
      state: {
        sourceId: 1,
        tableSortableFields: ['account', 'granted', 'expires', 'maxRole', 'lastSignIn'],
        filteredSearchBar: {
          show: true,
          tokens: ['two_factor'],
          searchParam: 'search',
          placeholder: 'Filter members',
          recentSearchesStorageKey: 'group_members',
        },
        ...state,
      },
    });

    wrapper = mount(SortDropdown, {
      localVue,
      store,
    });
  };

  const findSortingComponent = () => wrapper.find(GlSorting);
  const findSortDirectionToggle = () =>
    findSortingComponent().find('button[title="Sort direction"]');
  const findDropdownToggle = () => wrapper.find('button[aria-haspopup="true"]');
  const findDropdownItemByText = (text) =>
    wrapper
      .findAll(GlSortingItem)
      .wrappers.find((dropdownItemWrapper) => dropdownItemWrapper.text() === text);

  describe('dropdown options', () => {
    beforeEach(() => {
      delete window.location;
      window.location = new URL(URL_HOST);
    });

    it('adds dropdown items for all the sortable fields', () => {
      const URL_FILTER_PARAMS = '?two_factor=enabled&search=foobar';
      const EXPECTED_BASE_URL = `${URL_HOST}${URL_FILTER_PARAMS}&sort=`;

      window.location.search = URL_FILTER_PARAMS;

      const expectedDropdownItems = [
        {
          label: 'Account',
          url: `${EXPECTED_BASE_URL}name_asc`,
        },
        {
          label: 'Access granted',
          url: `${EXPECTED_BASE_URL}last_joined`,
        },
        {
          label: 'Max role',
          url: `${EXPECTED_BASE_URL}access_level_asc`,
        },
        {
          label: 'Last sign-in',
          url: `${EXPECTED_BASE_URL}recent_sign_in`,
        },
      ];

      createComponent();

      expectedDropdownItems.forEach((expectedDropdownItem) => {
        const dropdownItem = findDropdownItemByText(expectedDropdownItem.label);

        expect(dropdownItem).not.toBe(null);
        expect(dropdownItem.find('a').attributes('href')).toBe(expectedDropdownItem.url);
      });
    });

    it('checks selected sort option', () => {
      window.location.search = '?sort=access_level_asc';

      createComponent();

      expect(findDropdownItemByText('Max role').vm.$attrs.active).toBe(true);
    });
  });

  describe('dropdown toggle', () => {
    beforeEach(() => {
      delete window.location;
      window.location = new URL(URL_HOST);
    });

    it('defaults to sorting by "Account" in ascending order', () => {
      createComponent();

      expect(findSortingComponent().props('isAscending')).toBe(true);
      expect(findDropdownToggle().text()).toBe('Account');
    });

    it('sets text as selected sort option', () => {
      window.location.search = '?sort=access_level_asc';

      createComponent();

      expect(findDropdownToggle().text()).toBe('Max role');
    });
  });

  describe('sort direction toggle', () => {
    beforeEach(() => {
      delete window.location;
      window.location = new URL(URL_HOST);

      jest.spyOn(urlUtilities, 'visitUrl');
    });

    describe('when current sort direction is ascending', () => {
      beforeEach(() => {
        window.location.search = '?sort=access_level_asc';

        createComponent();
      });

      describe('when sort direction toggle is clicked', () => {
        beforeEach(() => {
          findSortDirectionToggle().trigger('click');
        });

        it('sorts in descending order', () => {
          expect(urlUtilities.visitUrl).toHaveBeenCalledWith(`${URL_HOST}?sort=access_level_desc`);
        });
      });
    });

    describe('when current sort direction is descending', () => {
      beforeEach(() => {
        window.location.search = '?sort=access_level_desc';

        createComponent();
      });

      describe('when sort direction toggle is clicked', () => {
        beforeEach(() => {
          findSortDirectionToggle().trigger('click');
        });

        it('sorts in ascending order', () => {
          expect(urlUtilities.visitUrl).toHaveBeenCalledWith(`${URL_HOST}?sort=access_level_asc`);
        });
      });
    });
  });
});