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
|
import { nextTick } from 'vue';
import { GlLoadingIcon, GlAlert } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import AxiosMockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND } from '~/lib/utils/http_status';
import CustomEmail from '~/projects/settings_service_desk/components/custom_email.vue';
import CustomEmailForm from '~/projects/settings_service_desk/components/custom_email_form.vue';
import {
FEEDBACK_ISSUE_URL,
I18N_GENERIC_ERROR,
I18N_TOAST_SAVED,
} from '~/projects/settings_service_desk/custom_email_constants';
import {
MOCK_CUSTOM_EMAIL_EMPTY,
MOCK_CUSTOM_EMAIL_STARTED,
MOCK_CUSTOM_EMAIL_FORM_SUBMIT,
} from './mock_data';
describe('CustomEmail', () => {
let axiosMock;
let wrapper;
const defaultProps = {
incomingEmail: 'incoming@example.com',
customEmailEndpoint: '/flightjs/Flight/-/service_desk/custom_email',
};
const showToast = jest.fn();
const createWrapper = (props = {}) => {
wrapper = extendedWrapper(
mount(CustomEmail, {
propsData: { ...defaultProps, ...props },
mocks: {
$toast: {
show: showToast,
},
},
}),
);
};
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findAlert = () => wrapper.findComponent(GlAlert);
const findFeedbackLink = () => wrapper.findByTestId('feedback-link');
beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios);
});
afterEach(() => {
axiosMock.restore();
showToast.mockReset();
});
it('displays link to feedback issue', () => {
createWrapper();
expect(findFeedbackLink().attributes('href')).toEqual(FEEDBACK_ISSUE_URL);
});
describe('when initial resource loading returns no configured custom email', () => {
beforeEach(() => {
axiosMock
.onGet(defaultProps.customEmailEndpoint)
.replyOnce(HTTP_STATUS_OK, MOCK_CUSTOM_EMAIL_EMPTY);
createWrapper();
});
it('displays loading icon while fetching data', async () => {
// while loading
expect(findLoadingIcon().exists()).toBe(true);
await waitForPromises();
// loading completed
expect(findLoadingIcon().exists()).toBe(false);
});
it('displays form', async () => {
await waitForPromises();
expect(wrapper.findComponent(CustomEmailForm).exists()).toBe(true);
});
describe('when CustomEmailForm emits submit event with valid params', () => {
beforeEach(() => {
axiosMock
.onPost(defaultProps.customEmailEndpoint)
.replyOnce(HTTP_STATUS_OK, MOCK_CUSTOM_EMAIL_STARTED);
});
it('creates custom email', async () => {
createWrapper();
await nextTick();
const spy = jest.spyOn(axios, 'post');
wrapper.findComponent(CustomEmailForm).vm.$emit('submit', MOCK_CUSTOM_EMAIL_FORM_SUBMIT);
expect(wrapper.findComponent(CustomEmailForm).emitted('submit')).toEqual([
[MOCK_CUSTOM_EMAIL_FORM_SUBMIT],
]);
await waitForPromises();
expect(spy).toHaveBeenCalledWith(
defaultProps.customEmailEndpoint,
MOCK_CUSTOM_EMAIL_FORM_SUBMIT,
);
expect(showToast).toHaveBeenCalledWith(I18N_TOAST_SAVED);
});
});
});
describe('when initial resource loading returns 404', () => {
beforeEach(async () => {
axiosMock.onGet(defaultProps.customEmailEndpoint).reply(HTTP_STATUS_NOT_FOUND);
createWrapper();
await waitForPromises();
});
it('displays error alert with correct text', () => {
expect(findLoadingIcon().exists()).toBe(false);
expect(findAlert().exists()).toBe(true);
expect(findAlert().text()).toBe(I18N_GENERIC_ERROR);
});
it('dismissing the alert removes it', async () => {
expect(findAlert().exists()).toBe(true);
findAlert().vm.$emit('dismiss');
await nextTick();
expect(findAlert().exists()).toBe(false);
});
});
});
|