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

organization_switcher_spec.js « components « super_sidebar « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 094cb4baedb3c79df141ae6206ea6a5ae23b1a51 (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
import { GlAvatar, GlDisclosureDropdown, GlLoadingIcon } from '@gitlab/ui';
import VueApollo from 'vue-apollo';
import Vue from 'vue';

import { mountExtended } from 'helpers/vue_test_utils_helper';
import OrganizationSwitcher from '~/super_sidebar/components/organization_switcher.vue';
import {
  defaultOrganization as currentOrganization,
  organizations as nodes,
  pageInfo,
  pageInfoEmpty,
} from '~/organizations/mock_data';
import organizationsQuery from '~/organizations/shared/graphql/queries/organizations.query.graphql';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';

Vue.use(VueApollo);

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

  const [, secondOrganization, thirdOrganization] = nodes;

  const organizations = {
    nodes,
    pageInfo,
  };

  const successHandler = jest.fn().mockResolvedValue({
    data: {
      currentUser: {
        id: 'gid://gitlab/User/1',
        organizations,
      },
    },
  });

  const createComponent = (handler = successHandler) => {
    mockApollo = createMockApollo([[organizationsQuery, handler]]);

    wrapper = mountExtended(OrganizationSwitcher, {
      apolloProvider: mockApollo,
    });
  };

  const findDropdownItemByIndex = (index) =>
    wrapper.findAllByTestId('disclosure-dropdown-item').at(index);
  const showDropdown = () => wrapper.findComponent(GlDisclosureDropdown).vm.$emit('shown');

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

  it('renders disclosure dropdown with current organization selected', () => {
    createComponent();

    const toggleButton = wrapper.findByTestId('toggle-button');
    const dropdownItem = findDropdownItemByIndex(0);

    expect(toggleButton.text()).toContain(currentOrganization.name);
    expect(toggleButton.findComponent(GlAvatar).props()).toMatchObject({
      src: currentOrganization.avatar_url,
      entityId: currentOrganization.id,
      entityName: currentOrganization.name,
    });
    expect(dropdownItem.text()).toContain(currentOrganization.name);
    expect(dropdownItem.findComponent(GlAvatar).props()).toMatchObject({
      src: currentOrganization.avatar_url,
      entityId: currentOrganization.id,
      entityName: currentOrganization.name,
    });
  });

  it('does not call GraphQL query', () => {
    createComponent();

    expect(successHandler).not.toHaveBeenCalled();
  });

  describe('when dropdown is shown', () => {
    it('calls GraphQL query and renders organizations that are available to switch to', async () => {
      createComponent();
      showDropdown();

      expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);

      await waitForPromises();

      expect(findDropdownItemByIndex(1).text()).toContain(secondOrganization.name);
      expect(findDropdownItemByIndex(1).element.firstChild.getAttribute('href')).toBe(
        secondOrganization.webUrl,
      );
      expect(findDropdownItemByIndex(1).findComponent(GlAvatar).props()).toMatchObject({
        src: secondOrganization.avatarUrl,
        entityId: getIdFromGraphQLId(secondOrganization.id),
        entityName: secondOrganization.name,
      });

      expect(findDropdownItemByIndex(2).text()).toContain(thirdOrganization.name);
      expect(findDropdownItemByIndex(2).element.firstChild.getAttribute('href')).toBe(
        thirdOrganization.webUrl,
      );
      expect(findDropdownItemByIndex(2).findComponent(GlAvatar).props()).toMatchObject({
        src: thirdOrganization.avatarUrl,
        entityId: getIdFromGraphQLId(thirdOrganization.id),
        entityName: thirdOrganization.name,
      });
    });

    describe('when there are no organizations to switch to', () => {
      beforeEach(async () => {
        createComponent(
          jest.fn().mockResolvedValue({
            data: {
              currentUser: {
                id: 'gid://gitlab/User/1',
                organizations: {
                  nodes: [],
                  pageInfo: pageInfoEmpty,
                },
              },
            },
          }),
        );
        showDropdown();
        await waitForPromises();
      });

      it('renders empty message', () => {
        expect(findDropdownItemByIndex(1).text()).toBe('No organizations available to switch to.');
      });
    });

    describe('when there is an error fetching organizations', () => {
      beforeEach(async () => {
        createComponent(jest.fn().mockRejectedValue());
        showDropdown();
        await waitForPromises();
      });

      it('renders empty message', () => {
        expect(findDropdownItemByIndex(1).text()).toBe('No organizations available to switch to.');
      });
    });
  });
});