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

sidebar_subscriptions_widget_spec.js « subscriptions « components « sidebar « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 430acf9f9e785e9080e7945e8047d272239ec929 (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
import { GlIcon, GlToggle } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import createFlash from '~/flash';
import SidebarEditableItem from '~/sidebar/components/sidebar_editable_item.vue';
import SidebarSubscriptionWidget from '~/sidebar/components/subscriptions/sidebar_subscriptions_widget.vue';
import issueSubscribedQuery from '~/sidebar/queries/issue_subscribed.query.graphql';
import updateMergeRequestSubscriptionMutation from '~/sidebar/queries/update_merge_request_subscription.mutation.graphql';
import toast from '~/vue_shared/plugins/global_toast';
import {
  issueSubscriptionsResponse,
  mergeRequestSubscriptionMutationResponse,
} from '../../mock_data';

jest.mock('~/flash');
jest.mock('~/vue_shared/plugins/global_toast');

Vue.use(VueApollo);

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

  const findEditableItem = () => wrapper.findComponent(SidebarEditableItem);
  const findToggle = () => wrapper.findComponent(GlToggle);
  const findIcon = () => wrapper.findComponent(GlIcon);

  const createComponent = ({
    subscriptionsQueryHandler = jest.fn().mockResolvedValue(issueSubscriptionsResponse()),
    issuableType = 'issue',
    movedMrSidebar = false,
  } = {}) => {
    subscriptionMutationHandler = jest
      .fn()
      .mockResolvedValue(mergeRequestSubscriptionMutationResponse);
    fakeApollo = createMockApollo([
      [issueSubscribedQuery, subscriptionsQueryHandler],
      [updateMergeRequestSubscriptionMutation, subscriptionMutationHandler],
    ]);

    wrapper = shallowMount(SidebarSubscriptionWidget, {
      apolloProvider: fakeApollo,
      provide: {
        canUpdate: true,
        glFeatures: {
          movedMrSidebar,
        },
      },
      propsData: {
        fullPath: 'group/project',
        iid: '1',
        issuableType,
      },
      stubs: {
        SidebarEditableItem,
      },
    });
  };

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

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

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

  describe('when user is not subscribed to the issue', () => {
    beforeEach(() => {
      createComponent();
      return waitForPromises();
    });

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

    it('toggle is unchecked', () => {
      expect(findToggle().props('value')).toBe(false);
    });

    it('emits `subscribedUpdated` event with a `false` payload', () => {
      expect(wrapper.emitted('subscribedUpdated')).toEqual([[false]]);
    });
  });

  describe('when user is subscribed to the issue', () => {
    beforeEach(() => {
      createComponent({
        subscriptionsQueryHandler: jest.fn().mockResolvedValue(issueSubscriptionsResponse(true)),
      });
      return waitForPromises();
    });

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

    it('toggle is checked', () => {
      expect(findToggle().props('value')).toBe(true);
    });

    it('emits `subscribedUpdated` event with a `true` payload', () => {
      expect(wrapper.emitted('subscribedUpdated')).toEqual([[true]]);
    });
  });

  describe('when emails are disabled', () => {
    it('toggle is disabled and off when user is subscribed', async () => {
      createComponent({
        subscriptionsQueryHandler: jest
          .fn()
          .mockResolvedValue(issueSubscriptionsResponse(true, true)),
      });
      await waitForPromises();

      expect(findIcon().props('name')).toBe('notifications-off');
      expect(findToggle().props('disabled')).toBe(true);
    });

    it('toggle is disabled and off when user is not subscribed', async () => {
      createComponent({
        subscriptionsQueryHandler: jest
          .fn()
          .mockResolvedValue(issueSubscriptionsResponse(false, true)),
      });
      await waitForPromises();

      expect(findIcon().props('name')).toBe('notifications-off');
      expect(findToggle().props('disabled')).toBe(true);
    });
  });

  it('displays a flash message when query is rejected', async () => {
    createComponent({
      subscriptionsQueryHandler: jest.fn().mockRejectedValue('Houston, we have a problem'),
    });
    await waitForPromises();

    expect(createFlash).toHaveBeenCalled();
  });

  describe('merge request', () => {
    it('displays toast when mutation is successful', async () => {
      createComponent({
        issuableType: 'merge_request',
        movedMrSidebar: true,
        subscriptionsQueryHandler: jest.fn().mockResolvedValue(issueSubscriptionsResponse(true)),
      });
      await waitForPromises();

      await wrapper.find('[data-testid="notifications-toggle"]').vm.$emit('change');

      await waitForPromises();

      expect(toast).toHaveBeenCalledWith('Notifications turned on.');
    });
  });
});