summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/components/participants/sidebar_participants_widget_spec.js
blob: 859e63b3df620ecee3f435ee1657914f68fc0c09 (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
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 Participants from '~/sidebar/components/participants/participants.vue';
import SidebarParticipantsWidget from '~/sidebar/components/participants/sidebar_participants_widget.vue';
import epicParticipantsQuery from '~/sidebar/queries/epic_participants.query.graphql';
import { epicParticipantsResponse } from '../../mock_data';

Vue.use(VueApollo);

describe('Sidebar Participants Widget', () => {
  let wrapper;
  let fakeApollo;

  const findParticipants = () => wrapper.findComponent(Participants);

  const createComponent = ({
    participantsQueryHandler = jest.fn().mockResolvedValue(epicParticipantsResponse()),
  } = {}) => {
    fakeApollo = createMockApollo([[epicParticipantsQuery, participantsQueryHandler]]);

    wrapper = shallowMount(SidebarParticipantsWidget, {
      apolloProvider: fakeApollo,
      propsData: {
        fullPath: 'group',
        iid: '1',
        issuableType: 'epic',
      },
      stubs: {
        Participants,
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
    fakeApollo = null;
  });

  it('passes a `loading` prop as true to child component when query is loading', () => {
    createComponent();

    expect(findParticipants().props('loading')).toBe(true);
  });

  it('emits toggleSidebar event when participants child component emits toggleSidebar', async () => {
    createComponent();
    findParticipants().vm.$emit('toggleSidebar');

    await nextTick();
    expect(wrapper.emitted('toggleSidebar')).toEqual([[]]);
  });

  describe('when participants are loaded', () => {
    beforeEach(() => {
      createComponent({
        participantsQueryHandler: jest.fn().mockResolvedValue(epicParticipantsResponse()),
      });
      return waitForPromises();
    });

    it('passes a `loading` prop as false to editable item', () => {
      expect(findParticipants().props('loading')).toBe(false);
    });

    it('passes participants to child component', () => {
      expect(findParticipants().props('participants')).toEqual(
        epicParticipantsResponse().data.workspace.issuable.participants.nodes,
      );
    });
  });

  describe('when error occurs', () => {
    it('emits error event with correct parameters', async () => {
      const mockError = new Error('mayday');

      createComponent({
        participantsQueryHandler: jest.fn().mockRejectedValue(mockError),
      });

      await waitForPromises();

      const [
        [
          {
            message,
            error: { networkError },
          },
        ],
      ] = wrapper.emitted('fetch-error');
      expect(message).toBe(wrapper.vm.$options.i18n.fetchingError);
      expect(networkError).toEqual(mockError);
    });
  });
});