forked from kentcdodds/testing-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtil-server-test-utils.js
More file actions
39 lines (35 loc) · 1013 Bytes
/
Copy pathtil-server-test-utils.js
File metadata and controls
39 lines (35 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// in a real app, a file like this would connect to a real database
// and calls into that database would be async, which is why
// these functions are set as async functions even though they
// actually can run synchronously.
import * as generate from 'til-shared/generate'
import db from '../src/utils/db'
async function initDb({
users = Array.from({length: 10}, () =>
generate.userData({id: generate.id()}),
),
posts = users.map(u =>
generate.postData({authorId: u.id, id: generate.id()}),
),
} = {}) {
db.users = users
db.posts = posts
return {users, posts}
}
async function insertTestUser() {
const testUser = generate.userData({
id: generate.id(),
username: 'til',
password: 'til',
})
db.users.push(testUser)
return testUser
}
async function resetDb({users, posts, testUser} = {}) {
await initDb({users, posts})
if (testUser) {
db.users.push(testUser)
}
return {users: db.users, posts: db.posts}
}
export {resetDb, initDb, insertTestUser, generate}