2021-03-11 19:13:27 +05:30
|
|
|
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';
|
2023-03-17 16:20:25 +05:30
|
|
|
import { HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
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(() => {
|
|
|
|
mockAxios.restore();
|
|
|
|
});
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
const findButton = () => wrapper.findComponent(GlButton);
|
|
|
|
const findFormInputGroup = () => wrapper.findComponent(GlFormInputGroup);
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
const clickResetEmail = async () => {
|
2021-09-04 01:27:46 +05:30
|
|
|
wrapper.findAllComponents(GlButton).at(2).trigger('click');
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
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 });
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(findButton().text()).toBe(buttonText);
|
2021-03-11 19:13:27 +05:30
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('opens the modal when the user clicks the button', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
findButton().trigger('click');
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(wrapper.findAllComponents(GlButton).at(1).attributes('href')).toBe(
|
2021-03-11 19:13:27 +05:30
|
|
|
`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 () => {
|
2023-03-17 16:20:25 +05:30
|
|
|
mockAxios.onPut(resetPath).reply(HTTP_STATUS_OK, { new_address: 'foo@bar.com' });
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
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 () => {
|
2023-03-17 16:20:25 +05:30
|
|
|
mockAxios.onPut(resetPath).reply(HTTP_STATUS_NOT_FOUND, {});
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
wrapper = createComponent({
|
|
|
|
issuableType: 'issue',
|
|
|
|
initialEmail,
|
|
|
|
resetPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
await clickResetEmail();
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(mockToastShow).toHaveBeenCalledWith('There was an error when reseting email token.');
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(findFormInputGroup().props('value')).toBe('user@gitlab.com');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|