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

import_target_dropdown_spec.js « components « import_entities « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba0bb0b0f743c0f323a8553cd628c4e65846cd96 (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
import { GlCollapsibleListbox } from '@gitlab/ui';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { shallowMount } from '@vue/test-utils';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';

import ImportTargetDropdown from '~/import_entities/components/import_target_dropdown.vue';
import { DEBOUNCE_DELAY } from '~/vue_shared/components/filtered_search_bar/constants';
import searchNamespacesWhereUserCanImportProjectsQuery from '~/import_entities/import_projects/graphql/queries/search_namespaces_where_user_can_import_projects.query.graphql';

import { mockAvailableNamespaces, mockNamespacesResponse, mockUserNamespace } from '../mock_data';

Vue.use(VueApollo);

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

  const defaultProps = {
    selected: mockUserNamespace,
  };

  const createComponent = ({ props = {} } = {}) => {
    const apolloProvider = createMockApollo([
      [
        searchNamespacesWhereUserCanImportProjectsQuery,
        jest.fn().mockResolvedValue(mockNamespacesResponse),
      ],
    ]);

    wrapper = shallowMount(ImportTargetDropdown, {
      apolloProvider,
      propsData: {
        ...defaultProps,
        ...props,
      },
    });
  };

  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);
  const findListboxFirstGroupItems = () => findListbox().props('items')[0].options;
  const findListboxGroupsItems = () => findListbox().props('items')[1].options;

  const waitForQuery = async () => {
    jest.advanceTimersByTime(DEBOUNCE_DELAY);
    await waitForPromises();
  };

  it('renders listbox', () => {
    createComponent();

    expect(findListbox().exists()).toBe(true);
  });

  it('truncates "toggle-text" when "selected" is too long', () => {
    const mockSelected = 'a-group-path-that-is-longer-than-24-characters';

    createComponent({
      props: { selected: mockSelected },
    });

    expect(findListbox().props('toggleText')).toBe('a-group-path-that-is-lo…');
  });

  describe('when used on group import', () => {
    beforeEach(() => {
      createComponent();
    });

    it('adds "No parent" in "Parent" group', () => {
      expect(findListboxFirstGroupItems()).toEqual([{ text: 'No parent', value: '' }]);
    });

    it('emits "select" event with { fullPath: "", id: null } when "No parent" is selected', () => {
      findListbox().vm.$emit('select', '');

      expect(wrapper.emitted('select')[0]).toEqual([{ fullPath: '', id: null }]);
    });

    it('emits "select" event with { fullPath, id } when a group is selected', async () => {
      await waitForQuery();

      const mockGroupPath = 'match1';

      findListbox().vm.$emit('select', mockGroupPath);

      expect(wrapper.emitted('select')[0]).toEqual([
        { fullPath: mockGroupPath, id: `gid://gitlab/Group/${mockGroupPath}` },
      ]);
    });
  });

  describe('when used on project import', () => {
    beforeEach(() => {
      createComponent({
        props: { userNamespace: mockUserNamespace },
      });
    });

    it('passes userNamespace as "Users" group item', () => {
      expect(findListboxFirstGroupItems()).toEqual([
        { text: mockUserNamespace, value: mockUserNamespace },
      ]);
    });

    it('emits "select" event with path as value', () => {
      const mockProjectPath = 'mock-project';

      findListbox().vm.$emit('select', mockProjectPath);

      expect(wrapper.emitted('select')[0]).toEqual([mockProjectPath]);
    });
  });

  it('passes namespaces from GraphQL as "Groups" group item', async () => {
    createComponent();

    await waitForQuery();

    expect(findListboxGroupsItems()).toEqual(
      mockAvailableNamespaces.map((namespace) => ({
        text: namespace.fullPath,
        value: namespace.fullPath,
      })),
    );
  });

  it('filters namespaces based on user input', async () => {
    createComponent();

    findListbox().vm.$emit('search', 'match');

    await waitForQuery();

    expect(findListboxGroupsItems()).toEqual([
      { text: 'match1', value: 'match1' },
      { text: 'match2', value: 'match2' },
    ]);
  });
});