Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/configure-tasks-app/ui/Subtemplates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
}

debounceMutate(cacheKey)
}, [activeTemplate?.subTaskTemplates])

Check warning on line 69 in src/app/configure-tasks-app/ui/Subtemplates.tsx

View workflow job for this annotation

GitHub Actions / Run linters and tests

React Hook useEffect has missing dependencies: 'activeTemplate', 'cacheKey', and 'debounceMutate'. Either include them or remove the dependency array

const handleSubtemplateCreation = (payload: CreateTemplateRequest) => {
const tempId = generateRandomString('temp-template')
Expand All @@ -90,7 +90,7 @@
const optimisticData = sortTaskByDescendingOrder([...currentSubtemplates, tempSubtemplate])

try {
mutate(
await mutate(
cacheKey,
async () => {
const subTask = await createSubTemplate(token, template_id, payload)
Expand Down
2 changes: 1 addition & 1 deletion src/app/detail/ui/ActivityWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
}
setLastUpdated(task?.lastActivityLogUpdated)
}
}, [task?.lastActivityLogUpdated])

Check warning on line 80 in src/app/detail/ui/ActivityWrapper.tsx

View workflow job for this annotation

GitHub Actions / Run linters and tests

React Hook useEffect has missing dependencies: 'cacheKey', 'debounceMutate', 'lastUpdated', and 'task'. Either include them or remove the dependency array

const currentUserId = tokenPayload.internalUserId ?? tokenPayload.clientId

Expand All @@ -102,7 +102,7 @@
const optimisticData = getOptimisticData(postCommentPayload, activities.data, tempLog)

try {
mutate(
await mutate(
cacheKey,
async () => {
shouldRefetchRef.current = false
Expand Down
2 changes: 1 addition & 1 deletion src/app/detail/ui/Subtasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
debounceMutate(cacheKey)
}
setLastUpdated(activeTask?.lastSubtaskUpdated)
}, [activeTask?.lastSubtaskUpdated])

Check warning on line 85 in src/app/detail/ui/Subtasks.tsx

View workflow job for this annotation

GitHub Actions / Run linters and tests

React Hook useEffect has missing dependencies: 'activeTask', 'cacheKey', 'debounceMutate', and 'lastUpdated'. Either include them or remove the dependency array

const handleSubTaskCreation = (payload: CreateTaskRequest) => {
const tempId = generateRandomString('temp-task')
Expand All @@ -104,7 +104,7 @@
)
const optimisticData = subTasks?.tasks ? sortSubtasksByPriority([...subTasks.tasks, tempSubtask]) : [tempSubtask]
try {
mutate(
await mutate(
cacheKey,
async () => {
const subTask = await handleCreate(token, payload, { disableSubtaskTemplates: true })
Expand Down
35 changes: 35 additions & 0 deletions src/utils/fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { fetcher } from '@/utils/fetcher'

describe('fetcher', () => {
const originalFetch = global.fetch

afterEach(() => {
global.fetch = originalFetch
jest.restoreAllMocks()
})

it('returns parsed JSON for successful responses', async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: async () => ({ tasks: [] }),
})

await expect(fetcher('/api/tasks/?token=test')).resolves.toEqual({ tasks: [] })
})

it('throws an error with status, url, and response body for failed responses', async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: false,
status: 500,
text: async () => 'Internal server error',
})

await expect(fetcher('/api/tasks/?token=test')).rejects.toThrow(
'An error occurred while fetching the data. [500] /api/tasks/?token=test: Internal server error',
)
})

it('returns undefined when url is null', async () => {
await expect(fetcher(null)).resolves.toBeUndefined()
})
})
5 changes: 3 additions & 2 deletions src/utils/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ export const fetcher = async (url: string | null) => {
const res = await fetch(url)

if (!res.ok) {
const error = new Error('An error occurred while fetching the data.')
throw error
const responseText = await res.text().catch(() => '')
const detail = responseText ? `: ${responseText.slice(0, 200)}` : ''
throw new Error(`An error occurred while fetching the data. [${res.status}] ${url}${detail}`)
}

return res.json()
Expand Down
Loading