|
| 1 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 2 | +import { addInstance, deleteInstance } from './instance'; |
| 3 | + |
| 4 | +describe('Instance API', () => { |
| 5 | + afterEach(() => { |
| 6 | + vi.restoreAllMocks(); |
| 7 | + }); |
| 8 | + |
| 9 | + describe('addInstance', () => { |
| 10 | + it('should add instance and return JSON response if successful', async () => { |
| 11 | + const mockResponse = { added: '123', url: 'http://example.com', deleted: '' }; |
| 12 | + global.fetch = vi.fn(() => |
| 13 | + Promise.resolve({ |
| 14 | + ok: true, |
| 15 | + json: () => Promise.resolve(mockResponse), |
| 16 | + } as Response) |
| 17 | + ); |
| 18 | + |
| 19 | + const csrf = 'csrfToken'; |
| 20 | + const url = 'http://example.com'; |
| 21 | + const instance = 'instanceName'; |
| 22 | + const comment = 'comment'; |
| 23 | + const email = '[email protected]'; |
| 24 | + const admin = 'admin'; |
| 25 | + const password = 'password'; |
| 26 | + const result = await addInstance(csrf, url, instance, comment, email, admin, password); |
| 27 | + |
| 28 | + expect(result).toEqual(mockResponse); |
| 29 | + expect(global.fetch).toHaveBeenCalledWith('./api/faq/search', { |
| 30 | + method: 'POST', |
| 31 | + headers: { |
| 32 | + Accept: 'application/json, text/plain, */*', |
| 33 | + 'Content-Type': 'application/json', |
| 34 | + }, |
| 35 | + body: JSON.stringify({ |
| 36 | + csrf: csrf, |
| 37 | + url: url, |
| 38 | + instance: instance, |
| 39 | + comment: comment, |
| 40 | + email: email, |
| 41 | + admin: admin, |
| 42 | + password: password, |
| 43 | + }), |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should throw an error if fetch fails', async () => { |
| 48 | + const mockError = new Error('Fetch failed'); |
| 49 | + global.fetch = vi.fn(() => Promise.reject(mockError)); |
| 50 | + |
| 51 | + const csrf = 'csrfToken'; |
| 52 | + const url = 'http://example.com'; |
| 53 | + const instance = 'instanceName'; |
| 54 | + const comment = 'comment'; |
| 55 | + const email = '[email protected]'; |
| 56 | + const admin = 'admin'; |
| 57 | + const password = 'password'; |
| 58 | + |
| 59 | + await expect(addInstance(csrf, url, instance, comment, email, admin, password)).rejects.toThrow(mockError); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('deleteInstance', () => { |
| 64 | + it('should delete instance and return JSON response if successful', async () => { |
| 65 | + const mockResponse = { added: '', url: '', deleted: '123' }; |
| 66 | + global.fetch = vi.fn(() => |
| 67 | + Promise.resolve({ |
| 68 | + ok: true, |
| 69 | + json: () => Promise.resolve(mockResponse), |
| 70 | + } as Response) |
| 71 | + ); |
| 72 | + |
| 73 | + const csrf = 'csrfToken'; |
| 74 | + const instanceId = '123'; |
| 75 | + const result = await deleteInstance(csrf, instanceId); |
| 76 | + |
| 77 | + expect(result).toEqual(mockResponse); |
| 78 | + expect(global.fetch).toHaveBeenCalledWith('./api/instance/delete', { |
| 79 | + method: 'DELETE', |
| 80 | + headers: { |
| 81 | + Accept: 'application/json, text/plain, */*', |
| 82 | + 'Content-Type': 'application/json', |
| 83 | + }, |
| 84 | + body: JSON.stringify({ |
| 85 | + csrf: csrf, |
| 86 | + instanceId: instanceId, |
| 87 | + }), |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should throw an error if fetch fails', async () => { |
| 92 | + const mockError = new Error('Fetch failed'); |
| 93 | + global.fetch = vi.fn(() => Promise.reject(mockError)); |
| 94 | + |
| 95 | + const csrf = 'csrfToken'; |
| 96 | + const instanceId = '123'; |
| 97 | + |
| 98 | + await expect(deleteInstance(csrf, instanceId)).rejects.toThrow(mockError); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments