summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/components/attention_required_toggle_spec.js
blob: 8555068cdd83ee79fc6019d3614b088c35901ea4 (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
import { GlButton } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import AttentionRequestedToggle from '~/sidebar/components/attention_requested_toggle.vue';

let wrapper;

function factory(propsData = {}) {
  wrapper = mount(AttentionRequestedToggle, { propsData });
}

const findToggle = () => wrapper.findComponent(GlButton);

describe('Attention require toggle', () => {
  afterEach(() => {
    wrapper.destroy();
  });

  it('renders button', () => {
    factory({ type: 'reviewer', user: { attention_requested: false } });

    expect(findToggle().exists()).toBe(true);
  });

  it.each`
    attentionRequested | icon
    ${true}            | ${'star'}
    ${false}           | ${'star-o'}
  `(
    'renders $icon icon when attention_requested is $attentionRequested',
    ({ attentionRequested, icon }) => {
      factory({ type: 'reviewer', user: { attention_requested: attentionRequested } });

      expect(findToggle().props('icon')).toBe(icon);
    },
  );

  it.each`
    attentionRequested | variant
    ${true}            | ${'warning'}
    ${false}           | ${'default'}
  `(
    'renders button with variant $variant when attention_requested is $attentionRequested',
    ({ attentionRequested, variant }) => {
      factory({ type: 'reviewer', user: { attention_requested: attentionRequested } });

      expect(findToggle().props('variant')).toBe(variant);
    },
  );

  it('emits toggle-attention-requested on click', async () => {
    factory({ type: 'reviewer', user: { attention_requested: true } });

    await findToggle().trigger('click');

    expect(wrapper.emitted('toggle-attention-requested')[0]).toEqual([
      {
        user: { attention_requested: true },
        callback: expect.anything(),
      },
    ]);
  });

  it('sets loading on click', async () => {
    factory({ type: 'reviewer', user: { attention_requested: true } });

    await findToggle().trigger('click');

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

  it.each`
    type          | attentionRequested | tooltip
    ${'reviewer'} | ${true}            | ${AttentionRequestedToggle.i18n.removeAttentionRequested}
    ${'reviewer'} | ${false}           | ${AttentionRequestedToggle.i18n.attentionRequestedReviewer}
    ${'assignee'} | ${false}           | ${AttentionRequestedToggle.i18n.attentionRequestedAssignee}
  `(
    'sets tooltip as $tooltip when attention_requested is $attentionRequested and type is $type',
    ({ type, attentionRequested, tooltip }) => {
      factory({ type, user: { attention_requested: attentionRequested } });

      expect(findToggle().attributes('aria-label')).toBe(tooltip);
    },
  );
});