This commit is contained in:
Ulises Gascón
2024-02-07 12:29:54 +01:00
committed by Ulises Gascon
parent 91ba1f5054
commit 9dd233c371
143 changed files with 68070 additions and 1 deletions
+9
View File
@@ -0,0 +1,9 @@
const whispers = [{ id: 1, message: 'test' }, { id: 2, message: 'hello world' }]
const inventedId = 12345
const existingId = whispers[0].id
export {
whispers,
inventedId,
existingId
}
+106
View File
@@ -0,0 +1,106 @@
import supertest from 'supertest'
import { app } from '../server'
import { restoreDb, populateDb } from './utils.js'
import { whispers, inventedId, existingId } from './fixtures.js'
import { getById } from '../store'
describe('Server', () => {
beforeEach(() => populateDb(whispers))
afterAll(restoreDb)
describe('GET /api/v1/whisper', () => {
it("Should return an empty array when there's no data", async () => {
await restoreDb() // empty the db
const response = await supertest(app).get('/api/v1/whisper')
expect(response.status).toBe(200)
expect(response.body).toEqual([])
})
it('Should return all the whispers', async () => {
const response = await supertest(app).get('/api/v1/whisper')
expect(response.status).toBe(200)
expect(response.body).toEqual(whispers)
})
})
describe('GET /api/v1/whisper/:id', () => {
it("Should return a 404 when the whisper doesn't exist", async () => {
const response = await supertest(app).get(`/api/v1/whisper/${inventedId}`)
expect(response.status).toBe(404)
})
it('Should return a whisper details', async () => {
const response = await supertest(app).get(`/api/v1/whisper/${existingId}`)
expect(response.status).toBe(200)
expect(response.body).toEqual(whispers.find(w => w.id === existingId))
})
})
describe('POST /api/v1/whisper', () => {
it('Should return a 400 when the body is empty', async () => {
const response = await supertest(app)
.post('/api/v1/whisper')
.send({})
expect(response.status).toBe(400)
})
it('Should return a 400 when the body is invalid', async () => {
const response = await supertest(app)
.post('/api/v1/whisper')
.send({ invented: 'This is a new whisper' })
expect(response.status).toBe(400)
})
it('Should return a 201 when the whisper is created', async () => {
const newWhisper = { id: whispers.length + 1, message: 'This is a new whisper' }
const response = await supertest(app)
.post('/api/v1/whisper')
.send({ message: newWhisper.message })
// HTTP Response
expect(response.status).toBe(201)
expect(response.body).toEqual(newWhisper)
// Database changes
const storedWhisper = await getById(newWhisper.id)
expect(storedWhisper).toStrictEqual(newWhisper)
})
})
describe('PUT /api/v1/whisper/:id', () => {
it('Should return a 400 when the body is empty', async () => {
const response = await supertest(app)
.put(`/api/v1/whisper/${existingId}`)
.send({})
expect(response.status).toBe(400)
})
it('Should return a 400 when the body is invalid', async () => {
const response = await supertest(app)
.put(`/api/v1/whisper/${existingId}`)
.send({ invented: 'This a new field' })
expect(response.status).toBe(400)
})
it("Should return a 404 when the whisper doesn't exist", async () => {
const response = await supertest(app)
.put(`/api/v1/whisper/${inventedId}`)
.send({ message: 'Whisper updated' })
expect(response.status).toBe(404)
})
it('Should return a 200 when the whisper is updated', async () => {
const response = await supertest(app)
.put(`/api/v1/whisper/${existingId}`)
.send({ message: 'Whisper updated' })
expect(response.status).toBe(200)
// Database changes
const storedWhisper = await getById(existingId)
expect(storedWhisper).toStrictEqual({ id: existingId, message: 'Whisper updated' })
})
})
describe('DELETE /api/v1/whisper/:id', () => {
it("Should return a 404 when the whisper doesn't exist", async () => {
const response = await supertest(app).delete(`/api/v1/whisper/${inventedId}`)
expect(response.status).toBe(404)
})
it('Should return a 200 when the whisper is deleted', async () => {
const response = await supertest(app).delete(`/api/v1/whisper/${existingId}`)
expect(response.status).toBe(200)
// Database changes
const storedWhisper = await getById(existingId)
expect(storedWhisper).toBeUndefined()
})
})
})
+74
View File
@@ -0,0 +1,74 @@
import { getAll, getById, create, updateById, deleteById } from '../store.js'
import { restoreDb, populateDb } from './utils.js'
import { whispers, inventedId, existingId } from './fixtures.js'
describe('store', () => {
beforeEach(() => populateDb(whispers))
afterAll(restoreDb)
describe('getAll', () => {
it("Should return an empty array when there's no data", async () => {
restoreDb()
const data = await getAll()
expect(data).toEqual([])
})
it('Should return an array with one item when there is one item', async () => {
const data = await getAll()
expect(data).toEqual(whispers)
})
})
describe('getById', () => {
it('Should return undefined when there is no item with the given id', async () => {
const item = await getById(inventedId)
expect(item).toBeUndefined()
})
it('Should return the item with the given id', async () => {
const item = await getById(whispers[0].id)
expect(item).toEqual(whispers[0])
})
})
describe('create', () => {
it('Should return the created item', async () => {
const newItem = { id: whispers.length + 1, message: 'test 3' }
const item = await create(newItem.message)
expect(item).toEqual(newItem)
})
it('Should add the item to the db', async () => {
const newItem = { id: whispers.length + 1, message: 'test 3' }
const { id } = await create(newItem.message)
const item = await getById(id)
expect(item).toEqual(newItem)
})
})
describe('updateById', () => {
it('Should return undefined when there is no item with the given id', async () => {
const item = await updateById(inventedId)
expect(item).toBeUndefined()
})
it('Should not return the updated item', async () => {
const updatedItem = { id: existingId, message: 'updated' }
const item = await updateById(updatedItem.id, updatedItem.message)
expect(item).toBeUndefined()
})
it('Should update the item in the db', async () => {
const updatedItem = { id: existingId, message: 'updated' }
await updateById(updatedItem.id, updatedItem.message)
const item = await getById(existingId)
expect(item).toEqual(updatedItem)
})
})
describe('deleteById', () => {
it('Should return undefined when there is no item with the given id', async () => {
const item = await deleteById(inventedId)
expect(item).toBeUndefined()
})
it('Should not return the deleted item', async () => {
const item = await deleteById(existingId)
expect(item).toBeUndefined()
})
it('Should delete the item from the db', async () => {
await deleteById(existingId)
const items = await getAll()
expect(items).toEqual(whispers.filter(item => item.id !== existingId))
})
})
})
+8
View File
@@ -0,0 +1,8 @@
import { writeFileSync } from 'node:fs'
import { join } from 'node:path'
const dbPath = join(process.cwd(), 'db.json')
const restoreDb = () => writeFileSync(dbPath, JSON.stringify([]))
const populateDb = (data) => writeFileSync(dbPath, JSON.stringify(data))
export { restoreDb, populateDb }