Skip to content

Commit 8c99ec4

Browse files
committed
Update code example to match with current state of app
1 parent 6a4cf1b commit 8c99ec4

File tree

2 files changed

+31
-59
lines changed

2 files changed

+31
-59
lines changed

src/content/4/en/part4b.md

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,18 +1054,17 @@ Our test coverage is currently lacking. Some requests like <i>GET /api/notes/:id
10541054
Below is an example of the test file after making some minor improvements:
10551055
10561056
```js
1057-
const { test, after, beforeEach, describe } = require('node:test')
10581057
const assert = require('node:assert')
1058+
const { test, after, beforeEach, describe } = require('node:test')
10591059
const mongoose = require('mongoose')
10601060
const supertest = require('supertest')
10611061
const app = require('../app')
1062-
const api = supertest(app)
1063-
10641062
const helper = require('./test_helper')
1065-
10661063
const Note = require('../models/note')
10671064

1068-
describe('when there are some notes saved initially', () => {
1065+
const api = supertest(app)
1066+
1067+
describe('when there is initially some notes saved', () => {
10691068
beforeEach(async () => {
10701069
await Note.deleteMany({})
10711070
await Note.insertMany(helper.initialNotes)
@@ -1079,23 +1078,21 @@ describe('when there are some notes saved initially', () => {
10791078
})
10801079

10811080
test('all notes are returned', async () => {
1082-
const response = await api.get('/api/notes')
1081+
const notes = await helper.notesInDb()
10831082

1084-
assert.strictEqual(response.body.length, helper.initialNotes.length)
1083+
assert.strictEqual(notes.length, helper.initialNotes.length)
10851084
})
10861085

10871086
test('a specific note is within the returned notes', async () => {
1088-
const response = await api.get('/api/notes')
1087+
const notes = await helper.notesInDb()
10891088

1090-
const contents = response.body.map(r => r.content)
1089+
const contents = notes.map(n => n.content)
10911090
assert(contents.includes('HTML is easy'))
10921091
})
10931092

10941093
describe('viewing a specific note', () => {
1095-
10961094
test('succeeds with a valid id', async () => {
10971095
const notesAtStart = await helper.notesInDb()
1098-
10991096
const noteToView = notesAtStart[0]
11001097

11011098
const resultNote = await api
@@ -1109,17 +1106,13 @@ describe('when there are some notes saved initially', () => {
11091106
test('fails with statuscode 404 if note does not exist', async () => {
11101107
const validNonexistingId = await helper.nonExistingId()
11111108

1112-
await api
1113-
.get(`/api/notes/${validNonexistingId}`)
1114-
.expect(404)
1109+
await api.get(`/api/notes/${validNonexistingId}`).expect(404)
11151110
})
11161111

11171112
test('fails with statuscode 400 id is invalid', async () => {
11181113
const invalidId = '5a3d5da59070081a82a3445'
11191114

1120-
await api
1121-
.get(`/api/notes/${invalidId}`)
1122-
.expect(400)
1115+
await api.get(`/api/notes/${invalidId}`).expect(400)
11231116
})
11241117
})
11251118

@@ -1144,14 +1137,9 @@ describe('when there are some notes saved initially', () => {
11441137
})
11451138

11461139
test('fails with status code 400 if data invalid', async () => {
1147-
const newNote = {
1148-
important: true
1149-
}
1140+
const newNote = { important: true }
11501141

1151-
await api
1152-
.post('/api/notes')
1153-
.send(newNote)
1154-
.expect(400)
1142+
await api.post('/api/notes').send(newNote).expect(400)
11551143

11561144
const notesAtEnd = await helper.notesInDb()
11571145

@@ -1164,16 +1152,14 @@ describe('when there are some notes saved initially', () => {
11641152
const notesAtStart = await helper.notesInDb()
11651153
const noteToDelete = notesAtStart[0]
11661154

1167-
await api
1168-
.delete(`/api/notes/${noteToDelete.id}`)
1169-
.expect(204)
1155+
await api.delete(`/api/notes/${noteToDelete.id}`).expect(204)
11701156

11711157
const notesAtEnd = await helper.notesInDb()
11721158

1173-
assert.strictEqual(notesAtEnd.length, helper.initialNotes.length - 1)
1174-
1175-
const contents = notesAtEnd.map(r => r.content)
1159+
const contents = notesAtEnd.map(n => n.content)
11761160
assert(!contents.includes(noteToDelete.content))
1161+
1162+
assert.strictEqual(notesAtEnd.length, helper.initialNotes.length - 1)
11771163
})
11781164
})
11791165
})

src/content/4/fi/osa4b.md

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,17 +1044,16 @@ Testit ovat tällä hetkellä osittain epätäydelliset, sillä esim. reittejä
10441044
Jossain määrin parannellut testit ovat seuraavassa:
10451045
10461046
```js
1047-
const { test, after, beforeEach, describe } = require('node:test')
10481047
const assert = require('node:assert')
1048+
const { test, after, beforeEach, describe } = require('node:test')
10491049
const mongoose = require('mongoose')
10501050
const supertest = require('supertest')
10511051
const app = require('../app')
1052-
const api = supertest(app)
1053-
10541052
const helper = require('./test_helper')
1055-
10561053
const Note = require('../models/note')
10571054

1055+
const api = supertest(app)
1056+
10581057
describe('when there is initially some notes saved', () => {
10591058
beforeEach(async () => {
10601059
await Note.deleteMany({})
@@ -1069,23 +1068,21 @@ describe('when there is initially some notes saved', () => {
10691068
})
10701069

10711070
test('all notes are returned', async () => {
1072-
const response = await api.get('/api/notes')
1071+
const notes = await helper.notesInDb()
10731072

1074-
assert.strictEqual(response.body.length, helper.initialNotes.length)
1073+
assert.strictEqual(notes.length, helper.initialNotes.length)
10751074
})
10761075

10771076
test('a specific note is within the returned notes', async () => {
1078-
const response = await api.get('/api/notes')
1077+
const notes = await helper.notesInDb()
10791078

1080-
const contents = response.body.map(r => r.content)
1079+
const contents = notes.map(n => n.content)
10811080
assert(contents.includes('HTML is easy'))
10821081
})
10831082

10841083
describe('viewing a specific note', () => {
1085-
10861084
test('succeeds with a valid id', async () => {
10871085
const notesAtStart = await helper.notesInDb()
1088-
10891086
const noteToView = notesAtStart[0]
10901087

10911088
const resultNote = await api
@@ -1099,17 +1096,13 @@ describe('when there is initially some notes saved', () => {
10991096
test('fails with statuscode 404 if note does not exist', async () => {
11001097
const validNonexistingId = await helper.nonExistingId()
11011098

1102-
await api
1103-
.get(`/api/notes/${validNonexistingId}`)
1104-
.expect(404)
1099+
await api.get(`/api/notes/${validNonexistingId}`).expect(404)
11051100
})
11061101

11071102
test('fails with statuscode 400 id is invalid', async () => {
11081103
const invalidId = '5a3d5da59070081a82a3445'
11091104

1110-
await api
1111-
.get(`/api/notes/${invalidId}`)
1112-
.expect(400)
1105+
await api.get(`/api/notes/${invalidId}`).expect(400)
11131106
})
11141107
})
11151108

@@ -1134,14 +1127,9 @@ describe('when there is initially some notes saved', () => {
11341127
})
11351128

11361129
test('fails with status code 400 if data invalid', async () => {
1137-
const newNote = {
1138-
important: true
1139-
}
1130+
const newNote = { important: true }
11401131

1141-
await api
1142-
.post('/api/notes')
1143-
.send(newNote)
1144-
.expect(400)
1132+
await api.post('/api/notes').send(newNote).expect(400)
11451133

11461134
const notesAtEnd = await helper.notesInDb()
11471135

@@ -1154,16 +1142,14 @@ describe('when there is initially some notes saved', () => {
11541142
const notesAtStart = await helper.notesInDb()
11551143
const noteToDelete = notesAtStart[0]
11561144

1157-
await api
1158-
.delete(`/api/notes/${noteToDelete.id}`)
1159-
.expect(204)
1145+
await api.delete(`/api/notes/${noteToDelete.id}`).expect(204)
11601146

11611147
const notesAtEnd = await helper.notesInDb()
11621148

1163-
assert.strictEqual(notesAtEnd.length, helper.initialNotes.length - 1)
1164-
1165-
const contents = notesAtEnd.map(r => r.content)
1149+
const contents = notesAtEnd.map(n => n.content)
11661150
assert(!contents.includes(noteToDelete.content))
1151+
1152+
assert.strictEqual(notesAtEnd.length, helper.initialNotes.length - 1)
11671153
})
11681154
})
11691155
})

0 commit comments

Comments
 (0)