-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathgenerate.js
More file actions
97 lines (87 loc) · 2.21 KB
/
generate.js
File metadata and controls
97 lines (87 loc) · 2.21 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import faker from 'faker'
import {getUserToken, getSaltAndHash} from '../../src/utils/auth'
// passwords must have at least these kinds of characters to be valid, so we'll
// prefex all of the ones we generate with `!0_Oo` to ensure it's valid.
const getPassword = (...args) => `!0_Oo${faker.internet.password(...args)}`
const getUsername = faker.internet.userName
const getId = faker.datatype.uuid
const getSynopsis = faker.lorem.paragraph
const getNotes = faker.lorem.paragraph
function buildUser({password = getPassword(), ...overrides} = {}) {
return {
id: getId(),
username: getUsername(),
...getSaltAndHash(password),
...overrides,
}
}
function buildBook(overrides) {
return {
id: getId(),
title: faker.lorem.words(),
author: faker.name.findName(),
coverImageUrl: faker.image.imageUrl(),
pageCount: faker.datatype.number(400),
publisher: faker.company.companyName(),
synopsis: faker.lorem.paragraph(),
...overrides,
}
}
function buildListItem(overrides = {}) {
const {
bookId = overrides.book ? overrides.book.id : getId(),
startDate = faker.date.past(2),
finishDate = faker.date.between(startDate, new Date()),
owner = {ownerId: getId()},
} = overrides
return {
id: getId(),
bookId,
ownerId: owner.id,
rating: faker.datatype.number(5),
notes: faker.datatype.boolean() ? '' : getNotes(),
finishDate,
startDate,
...overrides,
}
}
function token(user) {
return getUserToken(buildUser(user))
}
function loginForm(overrides) {
return {
username: getUsername(),
password: getPassword(),
...overrides,
}
}
function buildReq({user = buildUser(), ...overrides} = {}) {
const req = {user, body: {}, params: {}, ...overrides}
return req
}
function buildRes(overrides = {}) {
const res = {
json: jest.fn(() => res).mockName('json'),
status: jest.fn(() => res).mockName('status'),
...overrides,
}
return res
}
function buildNext(impl) {
return jest.fn(impl).mockName('next')
}
export {
buildReq,
buildRes,
buildNext,
buildUser,
buildListItem,
buildBook,
token,
loginForm,
getPassword as password,
getUsername as username,
getId as id,
getSynopsis as synopsis,
getNotes as notes,
}