summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/subscriptions_spec.js
blob: e7ae59e26cf3aa4272fa6eb803dc14f8a9ee1f29 (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
import { GlToggle } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import Subscriptions from '~/sidebar/components/subscriptions/subscriptions.vue';
import eventHub from '~/sidebar/event_hub';

describe('Subscriptions', () => {
  let wrapper;

  const findToggleButton = () => wrapper.findComponent(GlToggle);

  const mountComponent = (propsData) =>
    extendedWrapper(
      shallowMount(Subscriptions, {
        propsData,
      }),
    );

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

  it('shows loading spinner when loading', () => {
    wrapper = mountComponent({
      loading: true,
      subscribed: undefined,
    });

    expect(findToggleButton().props('isLoading')).toBe(true);
  });

  it('is toggled "off" when currently not subscribed', () => {
    wrapper = mountComponent({
      subscribed: false,
    });

    expect(findToggleButton().props('value')).toBe(false);
  });

  it('is toggled "on" when currently subscribed', () => {
    wrapper = mountComponent({
      subscribed: true,
    });

    expect(findToggleButton().props('value')).toBe(true);
  });

  it('toggleSubscription method emits `toggleSubscription` event on eventHub and Component', () => {
    const id = 42;
    wrapper = mountComponent({ subscribed: true, id });
    const eventHubSpy = jest.spyOn(eventHub, '$emit');
    const wrapperEmitSpy = jest.spyOn(wrapper.vm, '$emit');

    wrapper.vm.toggleSubscription();

    expect(eventHubSpy).toHaveBeenCalledWith('toggleSubscription', id);
    expect(wrapperEmitSpy).toHaveBeenCalledWith('toggleSubscription', id);
    eventHubSpy.mockRestore();
    wrapperEmitSpy.mockRestore();
  });

  it('tracks the event when toggled', () => {
    wrapper = mountComponent({ subscribed: true });

    const wrapperTrackSpy = jest.spyOn(wrapper.vm, 'track');

    wrapper.vm.toggleSubscription();

    expect(wrapperTrackSpy).toHaveBeenCalledWith('toggle_button', {
      property: 'notifications',
      value: 0,
    });
    wrapperTrackSpy.mockRestore();
  });

  it('onClickCollapsedIcon method emits `toggleSidebar` event on component', () => {
    wrapper = mountComponent({ subscribed: true });
    const spy = jest.spyOn(wrapper.vm, '$emit');

    wrapper.vm.onClickCollapsedIcon();

    expect(spy).toHaveBeenCalledWith('toggleSidebar');
    spy.mockRestore();
  });

  describe('given project emails are disabled', () => {
    const subscribeDisabledDescription = 'Notifications have been disabled';

    beforeEach(() => {
      wrapper = mountComponent({
        subscribed: false,
        projectEmailsDisabled: true,
        subscribeDisabledDescription,
      });
    });

    it('sets the correct display text', () => {
      expect(wrapper.findByTestId('subscription-title').text()).toContain(
        subscribeDisabledDescription,
      );
      expect(wrapper.find({ ref: 'tooltip' }).attributes('title')).toBe(
        subscribeDisabledDescription,
      );
    });

    it('does not render the toggle button', () => {
      expect(findToggleButton().exists()).toBe(false);
    });
  });
});