Skip to content

Commit cdaf4aa

Browse files
committed
refactor: moved code into correct methods, added more tests
1 parent 11544a9 commit cdaf4aa

File tree

6 files changed

+514
-70
lines changed

6 files changed

+514
-70
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
import { describe, it, expect, vi } from 'vitest';
2+
import {
3+
fetchActivateInput,
4+
fetchSetInputAsRequired,
5+
fetchEditTranslation,
6+
fetchDeleteTranslation,
7+
fetchAddTranslation,
8+
} from './forms';
9+
10+
describe('fetchActivateInput', () => {
11+
it('should activate input and return JSON response if successful', async () => {
12+
const mockResponse = { success: true };
13+
global.fetch = vi.fn(() =>
14+
Promise.resolve({
15+
status: 200,
16+
json: () => Promise.resolve(mockResponse),
17+
} as Response)
18+
);
19+
20+
const csrf = 'csrfToken';
21+
const formId = 'formId';
22+
const inputId = 'inputId';
23+
const checked = true;
24+
const result = await fetchActivateInput(csrf, formId, inputId, checked);
25+
26+
expect(result).toEqual(mockResponse);
27+
expect(global.fetch).toHaveBeenCalledWith('api/forms/activate', {
28+
method: 'POST',
29+
headers: {
30+
Accept: 'application/json, text/plain, */*',
31+
'Content-Type': 'application/json',
32+
},
33+
body: JSON.stringify({
34+
csrf: csrf,
35+
formid: formId,
36+
inputid: inputId,
37+
checked: checked,
38+
}),
39+
});
40+
});
41+
42+
it('should throw an error if the network response is not ok', async () => {
43+
global.fetch = vi.fn(() =>
44+
Promise.resolve({
45+
status: 500,
46+
} as Response)
47+
);
48+
49+
const csrf = 'csrfToken';
50+
const formId = 'formId';
51+
const inputId = 'inputId';
52+
const checked = true;
53+
54+
await expect(fetchActivateInput(csrf, formId, inputId, checked)).rejects.toThrow('Network response was not ok.');
55+
});
56+
});
57+
58+
describe('fetchSetInputAsRequired', () => {
59+
it('should set input as required and return JSON response if successful', async () => {
60+
const mockResponse = { success: true };
61+
global.fetch = vi.fn(() =>
62+
Promise.resolve({
63+
status: 200,
64+
json: () => Promise.resolve(mockResponse),
65+
} as Response)
66+
);
67+
68+
const csrf = 'csrfToken';
69+
const formId = 'formId';
70+
const inputId = 'inputId';
71+
const checked = true;
72+
const result = await fetchSetInputAsRequired(csrf, formId, inputId, checked);
73+
74+
expect(result).toEqual(mockResponse);
75+
expect(global.fetch).toHaveBeenCalledWith('api/forms/required', {
76+
method: 'POST',
77+
headers: {
78+
Accept: 'application/json, text/plain, */*',
79+
'Content-Type': 'application/json',
80+
},
81+
body: JSON.stringify({
82+
csrf: csrf,
83+
formid: formId,
84+
inputid: inputId,
85+
checked: checked,
86+
}),
87+
});
88+
});
89+
90+
it('should throw an error if the network response is not ok', async () => {
91+
global.fetch = vi.fn(() =>
92+
Promise.resolve({
93+
status: 500,
94+
} as Response)
95+
);
96+
97+
const csrf = 'csrfToken';
98+
const formId = 'formId';
99+
const inputId = 'inputId';
100+
const checked = true;
101+
102+
await expect(fetchSetInputAsRequired(csrf, formId, inputId, checked)).rejects.toThrow(
103+
'Network response was not ok.'
104+
);
105+
});
106+
});
107+
108+
describe('fetchEditTranslation', () => {
109+
it('should edit translation and return JSON response if successful', async () => {
110+
const mockResponse = { success: true };
111+
global.fetch = vi.fn(() =>
112+
Promise.resolve({
113+
status: 200,
114+
json: () => Promise.resolve(mockResponse),
115+
} as Response)
116+
);
117+
118+
const csrf = 'csrfToken';
119+
const formId = 'formId';
120+
const inputId = 'inputId';
121+
const label = 'label';
122+
const lang = 'en';
123+
const result = await fetchEditTranslation(csrf, formId, inputId, label, lang);
124+
125+
expect(result).toEqual(mockResponse);
126+
expect(global.fetch).toHaveBeenCalledWith('api/forms/translation-edit', {
127+
method: 'POST',
128+
headers: {
129+
Accept: 'application/json, text/plain, */*',
130+
'Content-Type': 'application/json',
131+
},
132+
body: JSON.stringify({
133+
csrf: csrf,
134+
formId: formId,
135+
inputId: inputId,
136+
lang: lang,
137+
label: label,
138+
}),
139+
});
140+
});
141+
142+
it('should throw an error if the network response is not ok', async () => {
143+
global.fetch = vi.fn(() =>
144+
Promise.resolve({
145+
status: 500,
146+
} as Response)
147+
);
148+
149+
const csrf = 'csrfToken';
150+
const formId = 'formId';
151+
const inputId = 'inputId';
152+
const label = 'label';
153+
const lang = 'en';
154+
155+
await expect(fetchEditTranslation(csrf, formId, inputId, label, lang)).rejects.toThrow(
156+
'Network response was not ok.'
157+
);
158+
});
159+
});
160+
161+
describe('fetchDeleteTranslation', () => {
162+
it('should delete translation and return JSON response if successful', async () => {
163+
const mockResponse = { success: true };
164+
global.fetch = vi.fn(() =>
165+
Promise.resolve({
166+
status: 200,
167+
json: () => Promise.resolve(mockResponse),
168+
} as Response)
169+
);
170+
171+
const csrf = 'csrfToken';
172+
const formId = 'formId';
173+
const inputId = 'inputId';
174+
const lang = 'en';
175+
const result = await fetchDeleteTranslation(csrf, formId, inputId, lang);
176+
177+
expect(result).toEqual(mockResponse);
178+
expect(global.fetch).toHaveBeenCalledWith('api/forms/translation-delete', {
179+
method: 'POST',
180+
headers: {
181+
Accept: 'application/json, text/plain, */*',
182+
'Content-Type': 'application/json',
183+
},
184+
body: JSON.stringify({
185+
csrf: csrf,
186+
formId: formId,
187+
inputId: inputId,
188+
lang: lang,
189+
}),
190+
});
191+
});
192+
193+
it('should throw an error if the network response is not ok', async () => {
194+
global.fetch = vi.fn(() =>
195+
Promise.resolve({
196+
status: 500,
197+
} as Response)
198+
);
199+
200+
const csrf = 'csrfToken';
201+
const formId = 'formId';
202+
const inputId = 'inputId';
203+
const lang = 'en';
204+
205+
await expect(fetchDeleteTranslation(csrf, formId, inputId, lang)).rejects.toThrow('Network response was not ok.');
206+
});
207+
});
208+
209+
describe('fetchAddTranslation', () => {
210+
it('should add translation and return JSON response if successful', async () => {
211+
const mockResponse = { success: true };
212+
global.fetch = vi.fn(() =>
213+
Promise.resolve({
214+
status: 200,
215+
json: () => Promise.resolve(mockResponse),
216+
} as Response)
217+
);
218+
219+
const csrf = 'csrfToken';
220+
const formId = 'formId';
221+
const inputId = 'inputId';
222+
const lang = 'en';
223+
const translation = 'translation';
224+
const result = await fetchAddTranslation(csrf, formId, inputId, lang, translation);
225+
226+
expect(result).toEqual(mockResponse);
227+
expect(global.fetch).toHaveBeenCalledWith('api/forms/translation-add', {
228+
method: 'POST',
229+
headers: {
230+
Accept: 'application/json, text/plain, */*',
231+
'Content-Type': 'application/json',
232+
},
233+
body: JSON.stringify({
234+
csrf: csrf,
235+
formId: formId,
236+
inputId: inputId,
237+
lang: lang,
238+
translation: translation,
239+
}),
240+
});
241+
});
242+
243+
it('should throw an error if the network response is not ok', async () => {
244+
global.fetch = vi.fn(() =>
245+
Promise.resolve({
246+
status: 500,
247+
} as Response)
248+
);
249+
250+
const csrf = 'csrfToken';
251+
const formId = 'formId';
252+
const inputId = 'inputId';
253+
const lang = 'en';
254+
const translation = 'translation';
255+
256+
await expect(fetchAddTranslation(csrf, formId, inputId, lang, translation)).rejects.toThrow(
257+
'Network response was not ok.'
258+
);
259+
});
260+
});

phpmyfaq/admin/assets/src/api/forms.ts

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
* @since 2014-03-21
1515
*/
1616

17-
import { pushNotification } from '../../../../assets/src/utils';
18-
import { Response } from '../interfaces';
19-
2017
export const fetchActivateInput = async (
2118
csrf: string,
2219
formId: string,
@@ -38,18 +35,12 @@ export const fetchActivateInput = async (
3835
}),
3936
});
4037

41-
if (response.ok) {
42-
const result: Response = await response.json();
43-
if (result.success) {
44-
pushNotification(result.success); // @todo move that to the forms.ts file in the content folder
45-
} else {
46-
console.error(result.error);
47-
}
38+
if (response.status === 200) {
39+
return await response.json();
4840
} else {
49-
throw new Error('Network response was not ok: ' + (await response.text()));
41+
throw new Error('Network response was not ok.');
5042
}
5143
} catch (error) {
52-
console.error('Error activating/deactivating input:', error);
5344
throw error;
5445
}
5546
};
@@ -75,18 +66,12 @@ export const fetchSetInputAsRequired = async (
7566
}),
7667
});
7768

78-
if (response.ok) {
79-
const result: Response = await response.json();
80-
if (result.success) {
81-
pushNotification(result.success); // @todo move that to the forms.ts file in the content folder
82-
} else {
83-
console.error(result.error);
84-
}
69+
if (response.status === 200) {
70+
return await response.json();
8571
} else {
86-
throw new Error('Network response was not ok: ' + (await response.text()));
72+
throw new Error('Network response was not ok.');
8773
}
8874
} catch (error) {
89-
console.error('Error setting input as required:', error);
9075
throw error;
9176
}
9277
};
@@ -114,18 +99,12 @@ export const fetchEditTranslation = async (
11499
}),
115100
});
116101

117-
if (response.ok) {
118-
const result: Response = await response.json();
119-
if (result.success) {
120-
pushNotification(result.success); // @todo move that to the forms.ts file in the content folder
121-
} else {
122-
console.error(result.error);
123-
}
102+
if (response.status === 200) {
103+
return await response.json();
124104
} else {
125-
throw new Error('Network response was not ok: ' + (await response.text()));
105+
throw new Error('Network response was not ok.');
126106
}
127107
} catch (error) {
128-
console.error('Error editing translation:', error);
129108
throw error;
130109
}
131110
};
@@ -134,8 +113,7 @@ export const fetchDeleteTranslation = async (
134113
csrf: string,
135114
formId: string,
136115
inputId: string,
137-
lang: string,
138-
element: HTMLElement
116+
lang: string
139117
): Promise<void> => {
140118
try {
141119
const response = await fetch('api/forms/translation-delete', {
@@ -152,23 +130,12 @@ export const fetchDeleteTranslation = async (
152130
}),
153131
});
154132

155-
if (response.ok) {
156-
const result: Response = await response.json();
157-
if (result.success) {
158-
// @todo move that to the forms.ts file in the content folder
159-
pushNotification(result.success);
160-
document.getElementById('item_' + element.getAttribute('data-pmf-lang'))?.remove();
161-
const option = document.createElement('option');
162-
option.innerText = element.getAttribute('data-pmf-langname')!;
163-
document.getElementById('languageSelect')?.appendChild(option);
164-
} else {
165-
console.error(result.error);
166-
}
133+
if (response.status === 200) {
134+
return await response.json();
167135
} else {
168-
throw new Error('Network response was not ok: ' + (await response.text()));
136+
throw new Error('Network response was not ok.');
169137
}
170138
} catch (error) {
171-
console.error('Error deleting translation:', error);
172139
throw error;
173140
}
174141
};
@@ -196,22 +163,12 @@ export const fetchAddTranslation = async (
196163
}),
197164
});
198165

199-
if (response.ok) {
200-
const result: Response = await response.json();
201-
if (result.success) {
202-
// @todo move that to the forms.ts file in the content folder
203-
pushNotification(result.success);
204-
setTimeout(function () {
205-
window.location.reload();
206-
}, 3000);
207-
} else {
208-
console.error(result.error);
209-
}
166+
if (response.status === 200) {
167+
return await response.json();
210168
} else {
211-
throw new Error('Network response was not ok: ' + (await response.text()));
169+
throw new Error('Network response was not ok.');
212170
}
213171
} catch (error) {
214-
console.error('Error adding translation:', error);
215172
throw error;
216173
}
217174
};

0 commit comments

Comments
 (0)