Skip to content

Commit

Permalink
implement toggle add game logic and testing in core b00tc4mp#84
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden23 committed Aug 13, 2024
1 parent 13dbc73 commit 06c0ac4
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
2 changes: 2 additions & 0 deletions staff/marti-herms/project/V-HUB/app/src/home/Library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useState, useEffect } from 'react'
export default function Library() {
const [games, setGames] = useState(null)

useEffect()

return <div>
{games.map()}
</div>
Expand Down
2 changes: 0 additions & 2 deletions staff/marti-herms/project/V-HUB/core/logic/searchGame.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dotenv/config'
import mongoose from 'mongoose'

import { User } from '../data/models.js'

import searchGame from './searchGame.js'

mongoose.connect(process.env.MONGODB_URI)
Expand Down
19 changes: 19 additions & 0 deletions staff/marti-herms/project/V-HUB/core/logic/toggleAddGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,23 @@ const { SystemError, NotFoundError } = errors
export default (userId, gameId) => {
validate.string(userId, 'userId')
validate.string(gameId, 'gameId')

return User.findById(userId).lean()
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) throw new NotFoundError('user not found')

return Game.findById(gameId).lean()
.catch(error => { throw new SystemError(error.message) })
.then(game => {
if (!game) throw new NotFoundError('game not found')

if (user.library.some(gameObjectId => gameObjectId.toString() === gameId))
return User.findByIdAndUpdate(userId, { $pull: { library: game._id } })
.catch(error => { throw new SystemError(error.message) })
else
return User.findByIdAndUpdate(userId, { $push: { library: game._id } })
.catch(error => { throw new SystemError(error.message) })
})
})
}
103 changes: 103 additions & 0 deletions staff/marti-herms/project/V-HUB/core/logic/toggleAddGame.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'dotenv/config'
import toggleAddGame from './toggleAddGame.js'
import mongoose from 'mongoose'

import { expect } from 'chai'
import { User, Game } from '../data/models.js'

import { errors } from 'com'

const { NotFoundError, ValidationError } = errors

const img = 'https://store-images.s-microsoft.com/image/apps.54354.13510798882606697.7a42c472-75d7-487e-9538-ebb5ce1657e6.372723d8-dd1a-450a-9fed-d420e7705e4e?mode=scale&q=90&h=300&w=200'

const link = 'https://www.microsoft.com/en-us/p/candy-crush-saga/9nblggh18846?activetab=pivot:overviewtab'

describe('toggleAddGame', () => {
before(() => mongoose.connect(process.env.MONGODB_URI))

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

it('succeeds on existing user and game and game is in user library', () => {
return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' })
.then(user =>
Game.create({ author: user.id, name: 'candy crush', image: img, description: 'candy crush game broh', link: link })
.then(game =>
toggleAddGame(user.id, game.id)
.then(() => User.findOne({ username: 'monoloco' }))
.then(user => {
expect(user.library).to.include(game._id)
})
)
)
})

it('succeeds on existing user and game and game is not in user library', () => {
return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' })
.then(user =>
Game.create({ author: user.id, name: 'candy crush', image: img, description: 'candy crush game broh', link: link })
.then(game =>
toggleAddGame(user.id, game.id)
.then(() => toggleAddGame(user.id, game.id))
.then(() => User.findOne({ username: 'monoloco' }))
.then(user => {
expect(user.library).to.not.include(game._id)
expect(user.library.length).to.equal(0)
})
)
)
})

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

return toggleAddGame('66ba313a881fabd96394b179', '66ba007f874aa7b84ec54491')
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(NotFoundError)
expect(_error.message).to.equal('user not found')
})
})

it('fails on non-existing game', () => {
let _error

return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' })
.then(user => toggleAddGame(user.id, '66ba007f874aa7b84ec54491'))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(NotFoundError)
expect(_error.message).to.equal('game not found')
})
})

it('fails on non-string userId', () => {
let error

try {
toggleAddGame(123, '66ba007f874aa7b84ec54491')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('userId is not a string')
}
})

it('fails on non-string gameId', () => {
let error

try {
toggleAddGame('66ba313a881fabd96394b179', 123)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('gameId is not a string')
}
})

afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()]))

after(() => mongoose.disconnect())
})
10 changes: 10 additions & 0 deletions staff/marti-herms/project/V-HUB/core/logic/toggleAddGame.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dotenv/config'
import mongoose from 'mongoose'

import toggleAddGame from './toggleAddGame.js'

mongoose.connect(process.env.MONGODB_URI)
.then(() => toggleAddGame('66ba007f874aa7b84ec54491', '66ba313a881fabd96394b179'))
.then(games => console.log(games))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())

0 comments on commit 06c0ac4

Please sign in to comment.