|
| 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 | +}); |
0 commit comments