Add test for PUT /comments/:commentId

This commit is contained in:
Jerko Steiner 2019-03-11 22:11:53 +05:00
parent 051208753b
commit cec34260ce
7 changed files with 61 additions and 13 deletions

View File

@ -183,7 +183,7 @@ export interface IAPIDef {
} }
put: { put: {
response: IComment, response: IComment,
body: IComment, body: INewComment,
params: { params: {
commentId: number commentId: number
} }

View File

@ -1,5 +1,5 @@
import * as CommentTestUtils from './CommentTestUtils' import * as CommentTestUtils from './CommentTestUtils'
import {IStory} from '@rondo/common' import {IComment, IStory} from '@rondo/common'
import {createSite} from '../site/SiteTestUtils' import {createSite} from '../site/SiteTestUtils'
import {getStory} from '../story/StoryTestUtils' import {getStory} from '../story/StoryTestUtils'
import {test} from '../test' import {test} from '../test'
@ -78,13 +78,33 @@ describe('comment', () => {
}) })
describe('PUT /comments/:commentId', () => { describe('PUT /comments/:commentId', () => {
it('updates a comment', () => {
let comment: IComment
beforeEach(async () => {
comment = await CommentTestUtils.createRootComment(t, {
storyId: story.id,
message: 'test',
})
})
it('updates a comment', async () => {
await t.put('/comments/:commentId', {
params: {
commentId: comment.id,
},
})
.send({
message: 'test2',
})
.expect(200)
const c = await CommentTestUtils.getCommentById(t, comment.id)
expect(c.message).toEqual('test2')
// TODO save edit history
}) })
it('fails to update a comment if user is not the owner') // it('fails to update a comment if user is not the owner')
it('updates a comment if user is site moderator') // TODO later // it('updates a comment if user is site moderator') // TODO later
}) })
describe('DELETE /comments/:commentId', () => { describe('DELETE /comments/:commentId', () => {

View File

@ -53,9 +53,13 @@ export class CommentRoutes extends BaseRoute<IAPIDef> {
}) })
t.put('/comments/:commentId', async req => { t.put('/comments/:commentId', async req => {
const comment = req.body const commentId = Number(req.params.commentId)
comment.id = req.params.commentId const {message} = req.body
return this.commentService.edit(comment, req.user!.id) return this.commentService.edit({
id: commentId,
message,
userId: req.user!.id,
})
}) })
t.delete('/comments/:commentId', async req => { t.delete('/comments/:commentId', async req => {

View File

@ -1,6 +1,7 @@
import {BaseService} from '../services/BaseService' import {BaseService} from '../services/BaseService'
import {Comment} from '../entities/Comment' import {Comment} from '../entities/Comment'
import {IComment, ICommentTree} from '@rondo/common' import {ICommentTree} from '@rondo/common'
import {IEditCommentParams} from './IEditCommentParams'
import {ICommentService} from './ICommentService' import {ICommentService} from './ICommentService'
import {INewCommentParams} from './INewCommentParams' import {INewCommentParams} from './INewCommentParams'
import {INewRootCommentParams} from './INewRootCommentParams' import {INewRootCommentParams} from './INewRootCommentParams'
@ -92,22 +93,26 @@ export class CommentService extends BaseService implements ICommentService {
}) })
} }
async edit(comment: IComment, userId: number) { async edit(comment: IEditCommentParams) {
new Validator(comment) new Validator(comment)
.ensure('id') .ensure('id')
.ensure('message') .ensure('message')
.ensure('userId')
.throw() .throw()
const {id, message, userId} = comment
await this.getRepository(Comment) await this.getRepository(Comment)
.update({ .update({
id: comment.id, id,
userId, userId,
}, { }, {
message: comment.message, message,
}) })
const editedComment = await this.findOne(comment.id) const editedComment = await this.findOne(comment.id)
if (!editedComment) { if (!editedComment) {
// TODO 400 or 404
throw new Error('Comment not found') throw new Error('Comment not found')
} }
return editedComment return editedComment

View File

@ -52,3 +52,16 @@ export async function getComments(
return response.body! return response.body!
} }
export async function getCommentById(
t: RequestTester<IAPIDef>,
commentId: number,
) {
const response = await t
.get('/comments/:commentId', {
params: {commentId},
})
.expect(200)
return response.body!
}

View File

@ -1,4 +1,5 @@
import {IComment, ICommentTree} from '@rondo/common' import {IComment, ICommentTree} from '@rondo/common'
import {IEditCommentParams} from './IEditCommentParams'
import {INewCommentParams} from './INewCommentParams' import {INewCommentParams} from './INewCommentParams'
import {INewRootCommentParams} from './INewRootCommentParams' import {INewRootCommentParams} from './INewRootCommentParams'
@ -11,7 +12,7 @@ export interface ICommentService {
save(comment: INewCommentParams): Promise<IComment> save(comment: INewCommentParams): Promise<IComment>
edit(comment: IComment, userId: number): Promise<IComment> edit(comment: IEditCommentParams): Promise<IComment>
delete(commentId: number, userId: number): Promise<IComment | undefined> delete(commentId: number, userId: number): Promise<IComment | undefined>

View File

@ -0,0 +1,5 @@
export interface IEditCommentParams {
id: number
message: string
userId: number
}