Skip to content

Commit

Permalink
fix(comments): plebbit.createComment throws adds useComment().error
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanabaroa committed Jun 2, 2024
1 parent 6b7645f commit 8ff7aba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/hooks/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,26 @@ describe('comments', () => {
// restore mock
Comment.prototype.update = commentUpdate
})

test('plebbit.createComment throws adds useComment().error', async () => {
// mock update to save comment instance
const createComment = Plebbit.prototype.createComment
Plebbit.prototype.createComment = async function () {
throw Error('plebbit.createComment error')
}

const rendered = renderHook<any, any>((commentCid) => useComment({commentCid}))
const waitFor = testUtils.createWaitFor(rendered)
rendered.rerender('comment cid')

// first error
await waitFor(() => rendered.result.current.error.message === 'plebbit.createComment error')
expect(rendered.result.current.error.message).toBe('plebbit.createComment error')
expect(rendered.result.current.errors[0].message).toBe('plebbit.createComment error')
expect(rendered.result.current.errors.length).toBe(1)

// restore mock
Plebbit.prototype.createComment = createComment
})
})
})
5 changes: 5 additions & 0 deletions src/stores/comments/comments-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const commentsStore = createStore<CommentsState>((setState: Function, getState:
log('commentsStore.addCommentToStore', {commentCid, comment, account})
setState((state: CommentsState) => ({comments: {...state.comments, [commentCid]: utils.clone(comment)}}))
} catch (e) {
setState((state: CommentsState) => {
let commentErrors = state.errors[commentCid] || []
commentErrors = [...commentErrors, e]

Check failure on line 50 in src/stores/comments/comments-store.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Type 'unknown' is not assignable to type 'Error'.

Check failure on line 50 in src/stores/comments/comments-store.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Type 'unknown' is not assignable to type 'Error'.
return {...state, errors: {...state.errors, [commentCid]: commentErrors}}
})
throw e
} finally {
plebbitGetCommentPending[commentCid + account.id] = false
Expand Down

0 comments on commit 8ff7aba

Please sign in to comment.