test(source): linkItem() tested (#211)

* linkItem() tested

* set line coverage to 1
This commit is contained in:
Anton Tranelis 2025-04-22 00:19:01 +01:00 committed by GitHub
parent edb0172a8e
commit e68ca0817a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 3 deletions

View File

@ -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')
})
})
})

View File

@ -16,9 +16,6 @@ export default defineConfig({
reporter: ['html', 'json-summary'],
thresholds: {
lines: 1,
functions: 56,
branches: 58,
statements: 1,
},
},
},