|
| 1 | +import { beforeEach, describe, expect, test, vi } from 'vitest'; |
| 2 | +import { createBookmark, deleteBookmark, deleteAllBookmarks } from './bookmarks'; |
| 3 | +import createFetchMock, { FetchMock } from 'vitest-fetch-mock'; |
| 4 | +import { BookmarkResponse } from '../interfaces'; |
| 5 | + |
| 6 | +const fetchMocker: FetchMock = createFetchMock(vi); |
| 7 | + |
| 8 | +fetchMocker.enableMocks(); |
| 9 | + |
| 10 | +describe('Bookmark API', (): void => { |
| 11 | + beforeEach((): void => { |
| 12 | + fetchMocker.resetMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + test('createBookmark should return response when successful', async (): Promise<void> => { |
| 16 | + const mockResponse = { success: 'Bookmark created' }; |
| 17 | + fetchMocker.mockResponseOnce(JSON.stringify(mockResponse)); |
| 18 | + |
| 19 | + const faqId = '123'; |
| 20 | + const csrf = 'csrfToken'; |
| 21 | + const data: BookmarkResponse | undefined = await createBookmark(faqId, csrf); |
| 22 | + |
| 23 | + expect(data).toEqual(mockResponse); |
| 24 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 25 | + expect(fetch).toHaveBeenCalledWith('api/bookmark/create', { |
| 26 | + method: 'POST', |
| 27 | + cache: 'no-cache', |
| 28 | + body: JSON.stringify({ id: faqId, csrfToken: csrf }), |
| 29 | + headers: { 'Content-Type': 'application/json' }, |
| 30 | + redirect: 'follow', |
| 31 | + referrerPolicy: 'no-referrer', |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + test('createBookmark should handle error', async (): Promise<void> => { |
| 36 | + fetchMocker.mockResponseOnce(null, { status: 500 }); |
| 37 | + |
| 38 | + const faqId = '123'; |
| 39 | + const csrf = 'csrfToken'; |
| 40 | + |
| 41 | + await expect(createBookmark(faqId, csrf)).rejects.toThrow('HTTP 500'); |
| 42 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 43 | + }); |
| 44 | + |
| 45 | + test('deleteBookmark should return response when successful', async (): Promise<void> => { |
| 46 | + const mockResponse = { success: 'Bookmark deleted' }; |
| 47 | + fetchMocker.mockResponseOnce(JSON.stringify(mockResponse)); |
| 48 | + |
| 49 | + const faqId = '123'; |
| 50 | + const csrf = 'csrfToken'; |
| 51 | + const data: BookmarkResponse | undefined = await deleteBookmark(faqId, csrf); |
| 52 | + |
| 53 | + expect(data).toEqual(mockResponse); |
| 54 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 55 | + expect(fetch).toHaveBeenCalledWith('api/bookmark/delete', { |
| 56 | + method: 'DELETE', |
| 57 | + cache: 'no-cache', |
| 58 | + body: JSON.stringify({ id: faqId, csrfToken: csrf }), |
| 59 | + headers: { 'Content-Type': 'application/json' }, |
| 60 | + redirect: 'follow', |
| 61 | + referrerPolicy: 'no-referrer', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test('deleteBookmark should handle error', async (): Promise<void> => { |
| 66 | + fetchMocker.mockResponseOnce(null, { status: 500 }); |
| 67 | + |
| 68 | + const faqId = '123'; |
| 69 | + const csrf = 'csrfToken'; |
| 70 | + |
| 71 | + await expect(deleteBookmark(faqId, csrf)).rejects.toThrow('HTTP 500'); |
| 72 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 73 | + }); |
| 74 | + |
| 75 | + test('deleteAllBookmarks should return response when successful', async (): Promise<void> => { |
| 76 | + const mockResponse = { success: 'All bookmarks deleted' }; |
| 77 | + fetchMocker.mockResponseOnce(JSON.stringify(mockResponse)); |
| 78 | + |
| 79 | + const csrf = 'csrfToken'; |
| 80 | + const data: BookmarkResponse | undefined = await deleteAllBookmarks(csrf); |
| 81 | + |
| 82 | + expect(data).toEqual(mockResponse); |
| 83 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 84 | + expect(fetch).toHaveBeenCalledWith('api/bookmark/delete-all', { |
| 85 | + method: 'DELETE', |
| 86 | + cache: 'no-cache', |
| 87 | + body: JSON.stringify({ csrfToken: csrf }), |
| 88 | + headers: { 'Content-Type': 'application/json' }, |
| 89 | + redirect: 'follow', |
| 90 | + referrerPolicy: 'no-referrer', |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test('deleteAllBookmarks should handle error', async (): Promise<void> => { |
| 95 | + fetchMocker.mockResponseOnce(null, { status: 500 }); |
| 96 | + |
| 97 | + const csrf = 'csrfToken'; |
| 98 | + |
| 99 | + await expect(deleteAllBookmarks(csrf)).rejects.toThrow('HTTP 500'); |
| 100 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | +}); |
0 commit comments