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