summaryrefslogtreecommitdiff
path: root/spec/frontend/issuable/components/issuable_by_email_spec.js
blob: b04a6c0b8fda4d360ab974e876cd3a1b4fcc5ebe (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
import { GlModal, GlSprintf, GlFormInputGroup, GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import IssuableByEmail from '~/issuable/components/issuable_by_email.vue';
import { HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK } from '~/lib/utils/http_status';

const initialEmail = 'user@gitlab.com';

const mockToastShow = jest.fn();

describe('IssuableByEmail', () => {
  let wrapper;
  let mockAxios;
  let glModalDirective;

  function createComponent(injectedProperties = {}) {
    glModalDirective = jest.fn();

    return extendedWrapper(
      shallowMount(IssuableByEmail, {
        stubs: {
          GlModal,
          GlSprintf,
          GlFormInputGroup,
          GlButton,
        },
        directives: {
          glModal: {
            bind(_, { value }) {
              glModalDirective(value);
            },
          },
        },
        mocks: {
          $toast: {
            show: mockToastShow,
          },
        },
        provide: {
          issuableType: 'issue',
          initialEmail,
          ...injectedProperties,
        },
      }),
    );
  }

  beforeEach(() => {
    mockAxios = new MockAdapter(axios);
  });

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

  const findButton = () => wrapper.findComponent(GlButton);
  const findFormInputGroup = () => wrapper.findComponent(GlFormInputGroup);

  const clickResetEmail = async () => {
    wrapper.findAllComponents(GlButton).at(2).trigger('click');

    await waitForPromises();
  };

  describe('modal button', () => {
    it.each`
      issuableType       | buttonText
      ${'issue'}         | ${'Email a new issue to this project'}
      ${'merge_request'} | ${'Email a new merge request to this project'}
    `(
      'renders a link with "$buttonText" when type is "$issuableType"',
      ({ issuableType, buttonText }) => {
        wrapper = createComponent({ issuableType });
        expect(findButton().text()).toBe(buttonText);
      },
    );

    it('opens the modal when the user clicks the button', () => {
      wrapper = createComponent();

      findButton().trigger('click');

      expect(glModalDirective).toHaveBeenCalled();
    });
  });

  describe('modal', () => {
    it('renders a read-only email input field', () => {
      wrapper = createComponent();

      expect(findFormInputGroup().props('value')).toBe('user@gitlab.com');
    });

    it.each`
      issuableType       | subject                            | body
      ${'issue'}         | ${'Enter the issue title'}         | ${'Enter the issue description'}
      ${'merge_request'} | ${'Enter the merge request title'} | ${'Enter the merge request description'}
    `('renders a mailto button when type is "$issuableType"', ({ issuableType, subject, body }) => {
      wrapper = createComponent({
        issuableType,
        initialEmail,
      });

      expect(wrapper.findAllComponents(GlButton).at(1).attributes('href')).toBe(
        `mailto:${initialEmail}?subject=${subject}&body=${body}`,
      );
    });

    describe('reset email', () => {
      const resetPath = 'gitlab-test/new_issuable_address?issuable_type=issue';

      beforeEach(() => {
        jest.spyOn(axios, 'put');
      });
      it('should send request to reset email token', async () => {
        wrapper = createComponent({
          issuableType: 'issue',
          initialEmail,
          resetPath,
        });

        await clickResetEmail();

        expect(axios.put).toHaveBeenCalledWith(resetPath);
      });

      it('should update the email when the request succeeds', async () => {
        mockAxios.onPut(resetPath).reply(HTTP_STATUS_OK, { new_address: 'foo@bar.com' });

        wrapper = createComponent({
          issuableType: 'issue',
          initialEmail,
          resetPath,
        });

        await clickResetEmail();

        expect(findFormInputGroup().props('value')).toBe('foo@bar.com');
      });

      it('should show a toast message when the request fails', async () => {
        mockAxios.onPut(resetPath).reply(HTTP_STATUS_NOT_FOUND, {});

        wrapper = createComponent({
          issuableType: 'issue',
          initialEmail,
          resetPath,
        });

        await clickResetEmail();

        expect(mockToastShow).toHaveBeenCalledWith('There was an error when reseting email token.');
        expect(findFormInputGroup().props('value')).toBe('user@gitlab.com');
      });
    });
  });
});