summaryrefslogtreecommitdiff
path: root/spec/frontend/import_entities/components/group_dropdown_spec.js
blob: b44bc33de6f2f61de63ebcd6a8d675f37e757f56 (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
import { GlSearchBoxByType, GlDropdown } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import GroupDropdown from '~/import_entities/components/group_dropdown.vue';
import { DEBOUNCE_DELAY } from '~/vue_shared/components/filtered_search_bar/constants';
import searchNamespacesWhereUserCanCreateProjectsQuery from '~/projects/new/queries/search_namespaces_where_user_can_create_projects.query.graphql';

Vue.use(VueApollo);

const makeGroupMock = (fullPath) => ({
  id: `gid://gitlab/Group/${fullPath}`,
  fullPath,
  name: fullPath,
  visibility: 'public',
  webUrl: `http://gdk.test:3000/groups/${fullPath}`,
  __typename: 'Group',
});

const AVAILABLE_NAMESPACES = [
  makeGroupMock('match1'),
  makeGroupMock('unrelated'),
  makeGroupMock('match2'),
];

const SEARCH_NAMESPACES_MOCK = Promise.resolve({
  data: {
    currentUser: {
      id: 'gid://gitlab/User/1',
      groups: {
        nodes: AVAILABLE_NAMESPACES,
        __typename: 'GroupConnection',
      },
      namespace: {
        id: 'gid://gitlab/Namespaces::UserNamespace/1',
        fullPath: 'root',
        __typename: 'Namespace',
      },
      __typename: 'UserCore',
    },
  },
});

describe('Import entities group dropdown component', () => {
  let wrapper;
  let namespacesTracker;

  const createComponent = (propsData) => {
    const apolloProvider = createMockApollo([
      [searchNamespacesWhereUserCanCreateProjectsQuery, () => SEARCH_NAMESPACES_MOCK],
    ]);

    namespacesTracker = jest.fn();

    wrapper = shallowMount(GroupDropdown, {
      apolloProvider,
      scopedSlots: {
        default: namespacesTracker,
      },
      stubs: { GlDropdown },
      propsData,
    });
  };

  it('passes namespaces from graphql query to default slot', async () => {
    createComponent();
    jest.advanceTimersByTime(DEBOUNCE_DELAY);
    await nextTick();
    await waitForPromises();
    await nextTick();

    expect(namespacesTracker).toHaveBeenCalledWith({ namespaces: AVAILABLE_NAMESPACES });
  });

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

    namespacesTracker.mockReset();
    wrapper.findComponent(GlSearchBoxByType).vm.$emit('input', 'match');
    jest.advanceTimersByTime(DEBOUNCE_DELAY);
    await nextTick();
    await waitForPromises();
    await nextTick();

    expect(namespacesTracker).toHaveBeenCalledWith({
      namespaces: [
        expect.objectContaining({ fullPath: 'match1' }),
        expect.objectContaining({ fullPath: 'match2' }),
      ],
    });
  });
});