Skip to content

Commit fb1c476

Browse files
committed
refactor: moved API functions into correct location, added tests
1 parent dab49be commit fb1c476

File tree

7 files changed

+703
-177
lines changed

7 files changed

+703
-177
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { describe, it, expect, vi } from 'vitest';
2+
import { deleteAttachments, refreshAttachments } from './attachment';
3+
4+
describe('deleteAttachments', () => {
5+
it('should delete attachment and return JSON response if successful', async () => {
6+
const mockResponse = { success: true, message: 'Attachment deleted' };
7+
global.fetch = vi.fn(() =>
8+
Promise.resolve({
9+
ok: true,
10+
json: () => Promise.resolve(mockResponse),
11+
} as Response)
12+
);
13+
14+
const attachmentId = '123';
15+
const csrfToken = 'csrfToken';
16+
const result = await deleteAttachments(attachmentId, csrfToken);
17+
18+
expect(result).toEqual(mockResponse);
19+
expect(global.fetch).toHaveBeenCalledWith('./api/content/attachments', {
20+
method: 'DELETE',
21+
headers: {
22+
Accept: 'application/json, text/plain, */*',
23+
'Content-Type': 'application/json',
24+
},
25+
body: JSON.stringify({ attId: attachmentId, csrf: csrfToken }),
26+
});
27+
});
28+
29+
it('should throw an error if fetch fails', async () => {
30+
const mockError = new Error('Fetch failed');
31+
global.fetch = vi.fn(() => Promise.reject(mockError));
32+
33+
const attachmentId = '123';
34+
const csrfToken = 'csrfToken';
35+
36+
await expect(deleteAttachments(attachmentId, csrfToken)).rejects.toThrow(mockError);
37+
});
38+
});
39+
40+
describe('refreshAttachments', () => {
41+
it('should refresh attachment and return JSON response if successful', async () => {
42+
const mockResponse = { success: true, message: 'Attachment refreshed' };
43+
global.fetch = vi.fn(() =>
44+
Promise.resolve({
45+
ok: true,
46+
json: () => Promise.resolve(mockResponse),
47+
} as Response)
48+
);
49+
50+
const attachmentId = '123';
51+
const csrfToken = 'csrfToken';
52+
const result = await refreshAttachments(attachmentId, csrfToken);
53+
54+
expect(result).toEqual(mockResponse);
55+
expect(global.fetch).toHaveBeenCalledWith('./api/content/attachments/refresh', {
56+
method: 'POST',
57+
headers: {
58+
Accept: 'application/json, text/plain, */*',
59+
'Content-Type': 'application/json',
60+
},
61+
body: JSON.stringify({ attId: attachmentId, csrf: csrfToken }),
62+
});
63+
});
64+
65+
it('should throw an error if fetch fails', async () => {
66+
const mockError = new Error('Fetch failed');
67+
global.fetch = vi.fn(() => Promise.reject(mockError));
68+
69+
const attachmentId = '123';
70+
const csrfToken = 'csrfToken';
71+
72+
await expect(refreshAttachments(attachmentId, csrfToken)).rejects.toThrow(mockError);
73+
});
74+
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const deleteAttachments = async (attachmentId: string, csrfToken: string)
2828

2929
return await response.json();
3030
} catch (error) {
31-
console.error(error);
31+
throw error;
3232
}
3333
};
3434

@@ -45,6 +45,6 @@ export const refreshAttachments = async (attachmentId: string, csrfToken: string
4545

4646
return await response.json();
4747
} catch (error) {
48-
console.error(error);
48+
throw error;
4949
}
5050
};
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { describe, it, expect, vi } from 'vitest';
2+
import { fetchCategoryTranslations, deleteCategory, setCategoryTree } from './category';
3+
4+
describe('fetchCategoryTranslations', () => {
5+
it('should fetch category translations and return JSON response if successful', async () => {
6+
const mockResponse = { success: true, data: 'Translations data' };
7+
global.fetch = vi.fn(() =>
8+
Promise.resolve({
9+
ok: true,
10+
json: () => Promise.resolve(mockResponse),
11+
} as Response)
12+
);
13+
14+
const categoryId = '123';
15+
const result = await fetchCategoryTranslations(categoryId);
16+
17+
expect(result).toEqual(mockResponse);
18+
expect(global.fetch).toHaveBeenCalledWith(`./api/category/translations/${categoryId}`, {
19+
method: 'GET',
20+
cache: 'no-cache',
21+
headers: {
22+
'Content-Type': 'application/json',
23+
},
24+
redirect: 'follow',
25+
referrerPolicy: 'no-referrer',
26+
});
27+
});
28+
29+
it('should throw an error if fetch fails', async () => {
30+
const mockError = new Error('Fetch failed');
31+
global.fetch = vi.fn(() => Promise.reject(mockError));
32+
33+
const categoryId = '123';
34+
35+
await expect(fetchCategoryTranslations(categoryId)).rejects.toThrow(mockError);
36+
});
37+
});
38+
39+
describe('deleteCategory', () => {
40+
it('should delete category and return JSON response if successful', async () => {
41+
const mockResponse = { success: true, message: 'Category deleted' };
42+
global.fetch = vi.fn(() =>
43+
Promise.resolve({
44+
ok: true,
45+
json: () => Promise.resolve(mockResponse),
46+
} as Response)
47+
);
48+
49+
const categoryId = '123';
50+
const language = 'en';
51+
const csrfToken = 'csrfToken';
52+
const result = await deleteCategory(categoryId, language, csrfToken);
53+
54+
expect(result).toEqual(mockResponse);
55+
expect(global.fetch).toHaveBeenCalledWith('./api/category/delete', {
56+
method: 'DELETE',
57+
cache: 'no-cache',
58+
headers: {
59+
'Content-Type': 'application/json',
60+
},
61+
body: JSON.stringify({
62+
categoryId: categoryId,
63+
language: language,
64+
csrfToken: csrfToken,
65+
}),
66+
redirect: 'follow',
67+
referrerPolicy: 'no-referrer',
68+
});
69+
});
70+
71+
it('should throw an error if fetch fails', async () => {
72+
const mockError = new Error('Fetch failed');
73+
global.fetch = vi.fn(() => Promise.reject(mockError));
74+
75+
const categoryId = '123';
76+
const language = 'en';
77+
const csrfToken = 'csrfToken';
78+
79+
await expect(deleteCategory(categoryId, language, csrfToken)).rejects.toThrow(mockError);
80+
});
81+
});
82+
83+
describe('setCategoryTree', () => {
84+
it('should set category tree and return JSON response if successful', async () => {
85+
const mockResponse = { success: true, message: 'Category tree updated' };
86+
global.fetch = vi.fn(() =>
87+
Promise.resolve({
88+
ok: true,
89+
json: () => Promise.resolve(mockResponse),
90+
} as Response)
91+
);
92+
93+
const categoryTree = { id: '123', name: 'Category' };
94+
const categoryId = '123';
95+
const csrfToken = 'csrfToken';
96+
const result = await setCategoryTree(categoryTree, categoryId, csrfToken);
97+
98+
expect(result).toEqual(mockResponse);
99+
expect(global.fetch).toHaveBeenCalledWith('./api/category/update-order', {
100+
method: 'POST',
101+
cache: 'no-cache',
102+
headers: {
103+
'Content-Type': 'application/json',
104+
},
105+
body: JSON.stringify({
106+
categoryTree: categoryTree,
107+
categoryId: categoryId,
108+
csrfToken: csrfToken,
109+
}),
110+
redirect: 'follow',
111+
referrerPolicy: 'no-referrer',
112+
});
113+
});
114+
115+
it('should throw an error if fetch fails', async () => {
116+
const mockError = new Error('Fetch failed');
117+
global.fetch = vi.fn(() => Promise.reject(mockError));
118+
119+
const categoryTree = { id: '123', name: 'Category' };
120+
const categoryId = '123';
121+
const csrfToken = 'csrfToken';
122+
123+
await expect(setCategoryTree(categoryTree, categoryId, csrfToken)).rejects.toThrow(mockError);
124+
});
125+
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const fetchCategoryTranslations = async (categoryId: string): Promise<Res
2828

2929
return await response.json();
3030
} catch (error) {
31-
console.error(error);
31+
throw error;
3232
}
3333
};
3434

@@ -55,7 +55,7 @@ export const deleteCategory = async (
5555

5656
return await response.json();
5757
} catch (error) {
58-
console.error(error);
58+
throw error;
5959
}
6060
};
6161

@@ -82,6 +82,6 @@ export const setCategoryTree = async (
8282

8383
return await response.json();
8484
} catch (error) {
85-
console.error(error);
85+
throw error;
8686
}
8787
};

0 commit comments

Comments
 (0)