|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { fetchByLanguage, postStopWord, removeStopWord } from './stop-words'; |
| 3 | + |
| 4 | +global.fetch = vi.fn(); |
| 5 | + |
| 6 | +describe('Stop Words API', () => { |
| 7 | + afterEach(() => { |
| 8 | + vi.clearAllMocks(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('fetchByLanguage should fetch stop words by language', async () => { |
| 12 | + const mockResponse = [{ id: 1, lang: 'en', stopword: 'example' }]; |
| 13 | + (fetch as vi.Mock).mockResolvedValue({ |
| 14 | + ok: true, |
| 15 | + json: async () => mockResponse, |
| 16 | + }); |
| 17 | + |
| 18 | + const result = await fetchByLanguage('en'); |
| 19 | + expect(fetch).toHaveBeenCalledWith('./api/stopwords?language=en', { |
| 20 | + method: 'GET', |
| 21 | + headers: { |
| 22 | + Accept: 'application/json, text/plain, */*', |
| 23 | + 'Content-Type': 'application/json', |
| 24 | + }, |
| 25 | + }); |
| 26 | + expect(result).toEqual(mockResponse); |
| 27 | + }); |
| 28 | + |
| 29 | + it('postStopWord should post a new stop word', async () => { |
| 30 | + const mockResponse = { success: true }; |
| 31 | + (fetch as vi.Mock).mockResolvedValue({ |
| 32 | + ok: true, |
| 33 | + json: async () => mockResponse, |
| 34 | + }); |
| 35 | + |
| 36 | + const result = await postStopWord('csrfToken', 'example', 1, 'en'); |
| 37 | + expect(fetch).toHaveBeenCalledWith('./api/stopword/save', { |
| 38 | + method: 'POST', |
| 39 | + headers: { |
| 40 | + Accept: 'application/json, text/plain, */*', |
| 41 | + 'Content-Type': 'application/json', |
| 42 | + }, |
| 43 | + body: JSON.stringify({ |
| 44 | + csrf: 'csrfToken', |
| 45 | + stopWord: 'example', |
| 46 | + stopWordId: 1, |
| 47 | + stopWordsLang: 'en', |
| 48 | + }), |
| 49 | + }); |
| 50 | + expect(result).toEqual(mockResponse); |
| 51 | + }); |
| 52 | + |
| 53 | + it('removeStopWord should delete a stop word', async () => { |
| 54 | + const mockResponse = { success: true }; |
| 55 | + (fetch as vi.Mock).mockResolvedValue({ |
| 56 | + ok: true, |
| 57 | + json: async () => mockResponse, |
| 58 | + }); |
| 59 | + |
| 60 | + const result = await removeStopWord('csrfToken', 1, 'en'); |
| 61 | + expect(fetch).toHaveBeenCalledWith('./api/stopword/delete', { |
| 62 | + method: 'POST', |
| 63 | + headers: { |
| 64 | + Accept: 'application/json, text/plain, */*', |
| 65 | + 'Content-Type': 'application/json', |
| 66 | + }, |
| 67 | + body: JSON.stringify({ |
| 68 | + csrf: 'csrfToken', |
| 69 | + stopWordId: 1, |
| 70 | + stopWordsLang: 'en', |
| 71 | + }), |
| 72 | + }); |
| 73 | + expect(result).toEqual(mockResponse); |
| 74 | + }); |
| 75 | +}); |
0 commit comments