-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation.test.ts
198 lines (174 loc) · 6.63 KB
/
relation.test.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { Prisma } from '@prisma/client'
import { relationMiddleware } from './relation'
import type { RelationOptions } from './relation'
const getOpts = (): RelationOptions => ({
idField: 'organizationId',
idGetter: () => context.currentUser.organizationId,
models: ['Pallet', 'Product'],
relationModel: 'Organization',
})
const getParams = (): Prisma.MiddlewareParams => ({
action: 'create',
args: {
data: { name: 'Test' },
where: { id: '1' },
},
dataPath: [''],
runInTransaction: false,
model: 'Pallet',
})
describe('relation utilities', () => {
describe('relationMiddleware', () => {
// Re-usable variables, re-initalized before each test
let CB: Prisma.Middleware
let Params: Prisma.MiddlewareParams
let Next: <T>(params: Prisma.MiddlewareParams) => Promise<T>
let Options: RelationOptions
const organizationId = '1'
beforeEach(() => {
/// @ts-expect-error testing custom functionality
mockCurrentUser({ organizationId })
Params = getParams()
Options = getOpts()
Next = jest.fn()
CB = relationMiddleware(Options)
})
it('returns a function with two arguments', () => {
expect(typeof CB === 'function').toBeTruthy()
expect(CB.length === 2).toBeTruthy()
})
it('calls `next` only once', async () => {
await CB(Params, Next)
expect(Next).toHaveBeenCalledTimes(1)
})
it('returns the results of calling `next` with updated `params`', async () => {})
it('does not run for unconfigured models', async () => {
const params: Prisma.MiddlewareParams = { ...Params, model: 'User' }
await CB(params, Next)
expect(Next).toHaveBeenCalledWith(params)
expect(Next).not.toHaveBeenCalledWith({
...params,
args: { data: { ...params.args.data, organizationId } },
})
})
it('adds `idField` to `params.args`', async () => {
await CB(Params, Next)
expect(Params.args.data.organizationId).toBe(organizationId)
})
it('adds `idField` to `params.args` in the correct location', async () => {
await CB(Params, Next)
expect(Params.args.data.organizationId).toBe(organizationId)
expect(Params.args.where.organizationId).toBeUndefined()
Params = getParams()
const params: Prisma.MiddlewareParams = { ...Params, action: 'findFirst' }
await CB(params, Next)
expect(params.args.data.organizationId).toBeUndefined()
expect(params.args.where.organizationId).toBe(organizationId)
})
it('supports a custom `idField` option', async () => {
/// @ts-expect-error testing custom functionality
mockCurrentUser({ orange: '2' })
const idField = 'orange'
// @ts-expect-error testing custom functionality
const idGetter = () => context.currentUser.orange
const cb = relationMiddleware({ ...Options, idField, idGetter })
await cb(Params, Next)
expect(Params.args.data.orange).toBe('2')
expect(Params.args.data.organizationId).toBeUndefined()
})
it('supports a custom `idGetter` option', async () => {
const idGetter = jest.fn(() => '3')
const cb = relationMiddleware({ ...Options, idGetter })
await cb(Params, Next)
expect(idGetter).toHaveBeenCalledTimes(1)
expect(idGetter).toHaveBeenCalledWith()
expect(Params.args.data.organizationId).toBe('3')
expect(Params.args.data.organizationId).not.toBe(organizationId)
})
it('supports a custom `relationModel` option', async () => {
const cb = relationMiddleware({
...Options,
models: [...Options.models, 'Organization'],
relationModel: 'Pallet',
})
const params: Prisma.MiddlewareParams = {
...Params,
model: 'Organization',
}
await cb(params, Next)
expect(params.args.data.organizationId).toBe(organizationId)
})
it('does not run for `relationModel`', async () => {
const cb = relationMiddleware({ ...Options, relationModel: 'Pallet' })
await cb(Params, Next)
expect(Params.args.data.organizationId).toBeUndefined()
expect(Next).toHaveBeenCalledWith(Params)
})
it('removes `idField` from update queries', async () => {
const params: Prisma.MiddlewareParams = {
...Params,
args: { data: { ...Params.args.data, organizationId } },
action: 'update',
}
await CB(params, Next)
expect(params.args.data.organizationId).toBeUndefined()
expect(params.args.data.organizationId).not.toBe(organizationId)
})
it('can be configured to support updating `idField`', async () => {
const cb = relationMiddleware({ ...Options, disableUpdates: false })
const params: Prisma.MiddlewareParams = {
...Params,
args: { data: { ...Params.args.data, organizationId: 5 } },
action: 'update',
}
await cb(params, Next)
expect(params.args.data.organizationId).not.toBeUndefined()
expect(params.args.data.organizationId).toBe(5)
})
it('does not trigger when `idField` is defined', async () => {
let params: Prisma.MiddlewareParams = {
...Params,
args: { data: { organizationId: '4' } },
}
await CB(params, Next)
expect(params.args.data.organizationId).toBe('4')
// `null` is a value distinct from `undefined`
params = {
...Params,
args: { data: { organizationId: null } },
}
await CB(params, Next)
expect(params.args.data.organizationId).toBeNull()
// `undefined` should get the value returned by `idGetter`
params = {
...Params,
args: { data: { organizationId: undefined } },
}
await CB(params, Next)
expect(params.args.data.organizationId).toBe(organizationId)
})
// "manually drawn" = using `organization: { connect/create: ... }`
it('does not trigger when the relation is manually drawn', async () => {
const params: Prisma.MiddlewareParams = {
...Params,
args: { data: { organization: {} } },
}
await CB(params, Next)
expect(params.args.data.organizationId).toBeUndefined()
expect(params.args.data.organization).toEqual(expect.objectContaining({}))
})
it('adds `params.args` if it is undefined', async () => {
const params: Prisma.MiddlewareParams = { ...Params, args: undefined }
await CB(params, Next)
expect(params.args).not.toBeUndefined()
})
it('adds `params.args.{data|where}` if it is undefined', async () => {
const params: Prisma.MiddlewareParams = {
...Params,
args: { data: undefined },
}
await CB(params, Next)
expect(params.args.data).not.toBeUndefined()
})
})
})