Skip to content

Commit

Permalink
create all the spec.js for all the logic b00tc4mp#150
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnnyrc26 committed Jul 22, 2024
1 parent 158fba3 commit b4bd2bc
Show file tree
Hide file tree
Showing 8 changed files with 690 additions and 21 deletions.
10 changes: 5 additions & 5 deletions staff/johnny-rojas/socialcode/api/logic/createComment.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MatchError, SystemError } from 'com/errors.js'
import { NotFoundError, SystemError } from 'com/errors.js'
import { User, Post } from '../data/index.js'
import validate from 'com/validate.js'

const createPostComment = (userId, postId, comment) => {
const createComment = (userId, postId, comment) => {
validate.id(userId, 'userId')
validate.id(postId, 'postId')
validate.text(comment, 'comment', 150)
Expand All @@ -12,14 +12,14 @@ const createPostComment = (userId, postId, comment) => {
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) {
throw new NotFounError('user not found')
throw new NotFoundError('user not found')
}

return Post.findById(postId).lean()
.catch(error => callback(new SystemError(error.message)))
.then(post => {
if (!post) {
throw new MatchError('post not found')
throw new NotFoundError('post not found')
}

return Post.findByIdAndUpdate((postId), { $push: { comments: { author: userId, comment: comment, date: Date.now() } } }, { new: true })
Expand All @@ -29,4 +29,4 @@ const createPostComment = (userId, postId, comment) => {
})
}

export default createPostComment
export default createComment
142 changes: 142 additions & 0 deletions staff/johnny-rojas/socialcode/api/logic/createComment.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import 'dotenv'
import mongoose, { Types } from 'mongoose'
import bcrypt from 'bcryptjs'
import { expect } from 'chai'

import { User, Post } from '../data/index.js'
import createComment from '../logic/createComment.js'

import { NotFoundError, ContentError } from 'com/errors.js'

const { MONGODB_URL_TEST } = process.env
const { ObjectId } = Types

debugger

describe('createComment', () => {
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => {
return Promise.all([User.deleteMany(), Post.deleteMany()])
}))

beforeEach(() => Promise.all([User.deleteMany(), Post.deleteMany()]))

it('succeeds on new post', () => {
return bcrypt.hash('86873', 8)
.then(hash => {
const user = new User({
name: 'Chai',
surname: 'Mocha',
email: '[email protected]',
username: 'Chaimocha',
password: hash
})

const post = new Post({
author: user.id,
title: 'jeepeta',
image: 'https://media.giphy.com/media/KEh5kliRTSVJm/giphy.gif?cid=82a1493b9ouww5te0ehma0gz6ttcyxrxwbzxdhw5m99taqh1&ep=v1_gifs_trending&rid=giphy.gif&ct=g',
description: 'Test driving',
date: new Date,
likes: [],
comments: []
})

return Promise.all([user.save(), post.save()])
.then(([user, post]) => createComment(user.id, post.id, 'F&F')
.then(() => Post.findById(post.id))
.then(post => {
expect(post.comments[0].author.toString()).to.equal(user.id.toString())
expect(post.comments[0].comment).to.equal('F&F')
})
)
})
})

it('fails on non-existing user', () => {
let errorThrown

return Post.create({
author: new ObjectId(),
title: 'jeepeta',
image: 'https://media.giphy.com/media/KEh5kliRTSVJm/giphy.gif?cid=82a1493b9ouww5te0ehma0gz6ttcyxrxwbzxdhw5m99taqh1&ep=v1_gifs_trending&rid=giphy.gif&ct=g',
description: 'Test driving',
date: new Date,
likes: [],
comments: []
})
.then((post) => createComment(new ObjectId().toString(), post.id, 'F&F'))
.catch(error => errorThrown = error)
.finally(() => {
expect(errorThrown).to.be.an.instanceOf(NotFoundError)
expect(errorThrown.message).to.equal('user not found')
})
})

it('fails on non-existing post', () => {
let errorThrown

return bcrypt.hash('4321', 8)
.then((hash) => User.create({
name: 'Chai',
surname: 'Mocha',
email: '[email protected]',
username: 'Chaimocha',
password: hash
}))
.then((user) => createComment(user.id, new ObjectId().toString(), 'F&F'))
.catch(error => errorThrown = error)
.finally(() => {
expect(errorThrown).to.be.an.instanceOf(NotFoundError)
expect(errorThrown.message).to.equal('post not found')
})
})

it('fails on ivalid userId', () => {
let errorThrown

try {
createComment(96737, new ObjectId().toString(), 'F&F')

} catch (error) {
errorThrown = error

} finally {
expect(errorThrown).to.be.an.instanceOf(ContentError)
expect(errorThrown.message).to.equal('userId is not valid')

}
})

it('fails on ivalid postId', () => {
let errorThrown

try {
createComment(new ObjectId().toString(), 972367, 'F&F')

} catch (error) {
errorThrown = error

} finally {
expect(errorThrown).to.be.an.instanceOf(ContentError)
expect(errorThrown.message).to.equal('postId is not valid')
}
})

it('fails on ivalid comment', () => {
let errorThrown

try {
createComment(new ObjectId().toString(), new ObjectId().toString(), 8976327)

} catch (error) {
errorThrown = error

} finally {
expect(errorThrown).to.be.an.instanceOf(ContentError)
expect(errorThrown.message).to.equal('comment is not valid')

}
})

after(() => Post.deleteMany().then(()=> User.deleteMany().then(() => mongoose.disconnect())))
})
14 changes: 8 additions & 6 deletions staff/johnny-rojas/socialcode/api/logic/createPost.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@ describe('createPost', () => {
}))

.then(user =>
createPost(user.id, 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [])
createPost(user.id, 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [], [])
.then(() => Post.findOne())
.then(post => {
expect(post.author.toString()).to.equal(user.id)
expect(post.title).to.equal('SaxoGuy')
expect(post.image).to.equal('https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g')
expect(post.description).to.equal('PlaySomeMusic')
expect(post.likes).to.be.an('array')
expect(post.likes).to.be.an('array')
})
)
)

it('fails on non-existing user', () => {
let errorThrown

return createPost(new ObjectId().toString(), 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [])
return createPost(new ObjectId().toString(), 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [], [])
.catch(error => errorThrown = error)
.finally(() => {
expect(errorThrown).to.be.an.instanceOf(NotFoundError)
Expand All @@ -58,7 +60,7 @@ describe('createPost', () => {
let errorThrown

try {
createPost(1111, 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [])
createPost(1111, 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [], [])
} catch (error) {
errorThrown = error
} finally {
Expand All @@ -71,7 +73,7 @@ describe('createPost', () => {
let errorThrown

try {
createPost(new ObjectId().toString(), 1234, 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [])
createPost(new ObjectId().toString(), 1234, 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 'PlaySomeMusic', [], [])
} catch (error) {
errorThrown = error
} finally {
Expand All @@ -84,7 +86,7 @@ describe('createPost', () => {
let errorThrown

try {
createPost(new ObjectId().toString(), 'SaxoGuy', 1234, 'PlaySomeMusic', [])
createPost(new ObjectId().toString(), 'SaxoGuy', 1234, 'PlaySomeMusic', [], [])
} catch (error) {
errorThrown = error
} finally {
Expand All @@ -97,7 +99,7 @@ describe('createPost', () => {
let errorThrown

try {
createPost(new ObjectId().toString(), 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 981687, [])
createPost(new ObjectId().toString(), 'SaxoGuy', 'https://media.giphy.com/media/7vAwVEdJS5cKxediyK/giphy.gif?cid=82a1493bp3k0u2y3xvspwzh4uvd8j21fsl4ursis3uruy526&ep=v1_gifs_trending&rid=giphy.gif&ct=g', 981687, [], [])
} catch (error) {
errorThrown = error
} finally {
Expand Down
Loading

0 comments on commit b4bd2bc

Please sign in to comment.