diff --git a/src/Components/Profile/ItemFunctions.spec.tsx b/src/Components/Profile/ItemFunctions.spec.tsx new file mode 100644 index 00000000..a36498c5 --- /dev/null +++ b/src/Components/Profile/ItemFunctions.spec.tsx @@ -0,0 +1,44 @@ +import { describe, it, expect, vi } from 'vitest' + +import { linkItem } from './itemFunctions' + +const toastErrorMock: (t: string) => void = vi.fn() +const toastSuccessMock: (t: string) => void = vi.fn() + +vi.mock('react-toastify', () => ({ + toast: { + error: (t: string) => toastErrorMock(t), + success: (t: string) => toastSuccessMock(t), + }, +})) + +describe('linkItem', () => { + const id = 'some-id' + let updateApi: () => void = vi.fn() + const item = { layer: { api: { updateItem: () => updateApi() } } } + const updateItem = vi.fn() + + beforeEach(() => { + updateApi = vi.fn() + vi.clearAllMocks() + }) + + describe('api rejects', () => { + it('toasts an error', async () => { + updateApi = vi.fn().mockRejectedValue('autsch') + await linkItem(id, item, updateItem) + expect(toastErrorMock).toHaveBeenCalledWith('autsch') + expect(updateItem).not.toHaveBeenCalled() + expect(toastSuccessMock).not.toHaveBeenCalled() + }) + }) + + describe('api resolves', () => { + it('toasts success and calls updateItem()', async () => { + await linkItem(id, item, updateItem) + expect(toastErrorMock).not.toHaveBeenCalled() + expect(updateItem).toHaveBeenCalledTimes(1) + expect(toastSuccessMock).toHaveBeenCalledWith('Item linked') + }) + }) +}) diff --git a/vite.config.ts b/vite.config.ts index 6745b480..e8b122b2 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -16,9 +16,6 @@ export default defineConfig({ reporter: ['html', 'json-summary'], thresholds: { lines: 1, - functions: 56, - branches: 58, - statements: 1, }, }, },