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

organization_select_spec.js « entity_select « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea029ba4f27a2329080156d6fdaecc4021952c93 (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
import VueApollo from 'vue-apollo';
import Vue, { nextTick } from 'vue';
import { GlCollapsibleListbox } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import OrganizationSelect from '~/vue_shared/components/entity_select/organization_select.vue';
import EntitySelect from '~/vue_shared/components/entity_select/entity_select.vue';
import {
  ORGANIZATION_TOGGLE_TEXT,
  ORGANIZATION_HEADER_TEXT,
  FETCH_ORGANIZATIONS_ERROR,
  FETCH_ORGANIZATION_ERROR,
} from '~/vue_shared/components/entity_select/constants';
import resolvers from '~/organizations/shared/graphql/resolvers';
import organizationsQuery from '~/organizations/index/graphql/organizations.query.graphql';
import { organizations as organizationsMock } from '~/organizations/mock_data';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';

Vue.use(VueApollo);

jest.useFakeTimers();

describe('OrganizationSelect', () => {
  let wrapper;
  let mockApollo;

  // Mocks
  const [organizationMock] = organizationsMock;

  // Stubs
  const GlAlert = {
    template: '<div><slot /></div>',
  };

  // Props
  const label = 'label';
  const description = 'description';
  const inputName = 'inputName';
  const inputId = 'inputId';
  const toggleClass = 'foo-bar';

  // Finders
  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);
  const findEntitySelect = () => wrapper.findComponent(EntitySelect);
  const findAlert = () => wrapper.findComponent(GlAlert);

  const handleInput = jest.fn();

  // Helpers
  const createComponent = ({ props = {}, mockResolvers = resolvers, handlers } = {}) => {
    mockApollo = createMockApollo(
      handlers || [
        [
          organizationsQuery,
          jest.fn().mockResolvedValueOnce({
            data: { currentUser: { id: 1, organizations: { nodes: organizationsMock } } },
          }),
        ],
      ],
      mockResolvers,
    );

    wrapper = shallowMountExtended(OrganizationSelect, {
      apolloProvider: mockApollo,
      propsData: {
        label,
        description,
        inputName,
        inputId,
        toggleClass,
        ...props,
      },
      stubs: {
        GlAlert,
        EntitySelect,
      },
      listeners: {
        input: handleInput,
      },
    });
  };
  const openListbox = () => findListbox().vm.$emit('shown');

  afterEach(() => {
    mockApollo = null;
  });

  describe('entity_select props', () => {
    beforeEach(() => {
      createComponent();
    });

    it.each`
      prop                   | expectedValue
      ${'label'}             | ${label}
      ${'description'}       | ${description}
      ${'inputName'}         | ${inputName}
      ${'inputId'}           | ${inputId}
      ${'defaultToggleText'} | ${ORGANIZATION_TOGGLE_TEXT}
      ${'headerText'}        | ${ORGANIZATION_HEADER_TEXT}
      ${'toggleClass'}       | ${toggleClass}
    `('passes the $prop prop to entity-select', ({ prop, expectedValue }) => {
      expect(findEntitySelect().props(prop)).toBe(expectedValue);
    });
  });

  describe('on mount', () => {
    it('fetches organizations when the listbox is opened', async () => {
      createComponent();
      await nextTick();
      jest.runAllTimers();
      await waitForPromises();

      openListbox();
      jest.runAllTimers();
      await waitForPromises();
      expect(findListbox().props('items')).toEqual([
        { text: organizationsMock[0].name, value: 1 },
        { text: organizationsMock[1].name, value: 2 },
        { text: organizationsMock[2].name, value: 3 },
      ]);
    });

    describe('with an initial selection', () => {
      it("fetches the initially selected value's name", async () => {
        createComponent({ props: { initialSelection: organizationMock.id } });
        await nextTick();
        jest.runAllTimers();
        await waitForPromises();

        expect(findListbox().props('toggleText')).toBe(organizationMock.name);
      });

      it('show an error if fetching initially selected fails', async () => {
        const mockResolvers = {
          Query: {
            organization: jest.fn().mockRejectedValueOnce(new Error()),
          },
        };

        createComponent({ props: { initialSelection: organizationMock.id }, mockResolvers });
        await nextTick();
        jest.runAllTimers();

        expect(findAlert().exists()).toBe(false);

        await waitForPromises();

        expect(findAlert().exists()).toBe(true);
        expect(findAlert().text()).toBe(FETCH_ORGANIZATION_ERROR);
      });
    });
  });

  it('shows an error when fetching organizations fails', async () => {
    createComponent({
      handlers: [[organizationsQuery, jest.fn().mockRejectedValueOnce(new Error())]],
    });
    await nextTick();
    jest.runAllTimers();
    await waitForPromises();

    openListbox();
    expect(findAlert().exists()).toBe(false);

    jest.runAllTimers();
    await waitForPromises();

    expect(findAlert().exists()).toBe(true);
    expect(findAlert().text()).toBe(FETCH_ORGANIZATIONS_ERROR);
  });

  it('forwards events to the parent scope via `v-on="$listeners"`', () => {
    createComponent();
    findEntitySelect().vm.$emit('input');

    expect(handleInput).toHaveBeenCalledTimes(1);
  });
});