@drax/crud-back 3.4.1 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
- import { describe, it, beforeAll, afterAll, expect } from "vitest"
1
+ import { describe, it, beforeAll, afterAll, afterEach, expect, vi } from "vitest"
2
2
  import { PersonFastifyRoutes } from '../people/routes/PersonRoutes'
3
3
  import { PersonPermissions } from '../people/permissions/PersonPermissions'
4
+ import { PersonController } from '../people/controllers/PersonController.js'
4
5
 
5
6
  import type { IPersonBase } from '../people/interfaces/IPerson'
6
7
 
@@ -32,6 +33,10 @@ describe("Person Controller Test", function () {
32
33
  return
33
34
  })
34
35
 
36
+ afterEach(() => {
37
+ vi.restoreAllMocks()
38
+ })
39
+
35
40
  // Test users are logged in and get their details
36
41
  it("Me Admin Root", async () => {
37
42
  const { accessToken } = await testSetup.rootUserLogin()
@@ -105,6 +110,34 @@ describe("Person Controller Test", function () {
105
110
  expect(getEntity.fullname).toBe("Test Person")
106
111
  })
107
112
 
113
+ it("should run postCreate interceptor after creating a person", async () => {
114
+ const { accessToken } = await testSetup.rootUserLogin()
115
+ await testSetup.dropCollection('Person')
116
+
117
+ const postCreateSpy = vi
118
+ .spyOn(PersonController.prototype, 'postCreate')
119
+ .mockImplementation(async (_request, item) => ({
120
+ ...item,
121
+ fullname: `${item.fullname} [postCreate]`
122
+ }))
123
+
124
+ const resp = await testSetup.fastifyInstance.inject({
125
+ method: 'POST',
126
+ url: '/api/person',
127
+ payload: {
128
+ fullname: "Create Hook",
129
+ money: 100,
130
+ address: defaultAddress
131
+ },
132
+ headers: { Authorization: `Bearer ${accessToken}` }
133
+ })
134
+
135
+ const person = await resp.json()
136
+ expect(resp.statusCode).toBe(200)
137
+ expect(postCreateSpy).toHaveBeenCalledTimes(1)
138
+ expect(person.fullname).toBe("Create Hook [postCreate]")
139
+ })
140
+
108
141
  // 2. Create and Update (Full Update - PUT)
109
142
  it("should create and update a person and finally find by id", async () => {
110
143
  const { accessToken } = await testSetup.rootUserLogin()
@@ -152,6 +185,47 @@ describe("Person Controller Test", function () {
152
185
  expect(verifiedEntity.money).toBe(150)
153
186
  })
154
187
 
188
+ it("should run postUpdate interceptor after updating a person", async () => {
189
+ const { accessToken } = await testSetup.rootUserLogin()
190
+ await testSetup.dropCollection('Person')
191
+
192
+ const createResp = await testSetup.fastifyInstance.inject({
193
+ method: 'POST',
194
+ url: '/api/person',
195
+ payload: {
196
+ fullname: "Update Hook",
197
+ money: 100,
198
+ address: defaultAddress
199
+ },
200
+ headers: { Authorization: `Bearer ${accessToken}` }
201
+ })
202
+
203
+ const createdEntity = await createResp.json()
204
+
205
+ const postUpdateSpy = vi
206
+ .spyOn(PersonController.prototype, 'postUpdate')
207
+ .mockImplementation(async (_request, item) => ({
208
+ ...item,
209
+ fullname: `${item.fullname} [postUpdate]`
210
+ }))
211
+
212
+ const updateResp = await testSetup.fastifyInstance.inject({
213
+ method: 'PUT',
214
+ url: `/api/person/${createdEntity._id}`,
215
+ payload: {
216
+ fullname: "Updated Hook",
217
+ money: 250,
218
+ address: defaultAddress
219
+ },
220
+ headers: { Authorization: `Bearer ${accessToken}` }
221
+ })
222
+
223
+ const updatedEntity = await updateResp.json()
224
+ expect(updateResp.statusCode).toBe(200)
225
+ expect(postUpdateSpy).toHaveBeenCalledTimes(1)
226
+ expect(updatedEntity.fullname).toBe("Updated Hook [postUpdate]")
227
+ })
228
+
155
229
  // 3. Create and Partial Update (PATCH)
156
230
  it("should create and update partial a person and finally find by id", async () => {
157
231
  const { accessToken } = await testSetup.rootUserLogin()
@@ -192,6 +266,79 @@ describe("Person Controller Test", function () {
192
266
  expect(verifiedEntity.money).toBe(120)
193
267
  })
194
268
 
269
+ it("should run postUpdatePartial interceptor after patching a person", async () => {
270
+ const { accessToken } = await testSetup.rootUserLogin()
271
+ await testSetup.dropCollection('Person')
272
+
273
+ const createResp = await testSetup.fastifyInstance.inject({
274
+ method: 'POST',
275
+ url: '/api/person',
276
+ payload: {
277
+ fullname: "Patch Hook",
278
+ money: 120,
279
+ address: defaultAddress
280
+ },
281
+ headers: { Authorization: `Bearer ${accessToken}` }
282
+ })
283
+
284
+ const createdEntity = await createResp.json()
285
+
286
+ const postUpdatePartialSpy = vi
287
+ .spyOn(PersonController.prototype, 'postUpdatePartial')
288
+ .mockImplementation(async (_request, item) => ({
289
+ ...item,
290
+ fullname: `${item.fullname} [postUpdatePartial]`
291
+ }))
292
+
293
+ const patchResp = await testSetup.fastifyInstance.inject({
294
+ method: 'PATCH',
295
+ url: `/api/person/${createdEntity._id}`,
296
+ payload: { fullname: "Patched Hook" },
297
+ headers: { Authorization: `Bearer ${accessToken}` }
298
+ })
299
+
300
+ const patchedEntity = await patchResp.json()
301
+ expect(patchResp.statusCode).toBe(200)
302
+ expect(postUpdatePartialSpy).toHaveBeenCalledTimes(1)
303
+ expect(patchedEntity.fullname).toBe("Patched Hook [postUpdatePartial]")
304
+ })
305
+
306
+ it("should run postRead interceptor when reading a person", async () => {
307
+ const { accessToken } = await testSetup.rootUserLogin()
308
+ await testSetup.dropCollection('Person')
309
+
310
+ const createResp = await testSetup.fastifyInstance.inject({
311
+ method: 'POST',
312
+ url: '/api/person',
313
+ payload: {
314
+ fullname: "Read Hook",
315
+ money: 80,
316
+ address: defaultAddress
317
+ },
318
+ headers: { Authorization: `Bearer ${accessToken}` }
319
+ })
320
+
321
+ const createdEntity = await createResp.json()
322
+
323
+ const postReadSpy = vi
324
+ .spyOn(PersonController.prototype, 'postRead')
325
+ .mockImplementation(async (_request, item) => ({
326
+ ...item,
327
+ fullname: `${item.fullname} [postRead]`
328
+ }))
329
+
330
+ const getResp = await testSetup.fastifyInstance.inject({
331
+ method: 'GET',
332
+ url: `/api/person/${createdEntity._id}`,
333
+ headers: { Authorization: `Bearer ${accessToken}` }
334
+ })
335
+
336
+ const person = await getResp.json()
337
+ expect(getResp.statusCode).toBe(200)
338
+ expect(postReadSpy).toHaveBeenCalledTimes(1)
339
+ expect(person.fullname).toBe("Read Hook [postRead]")
340
+ })
341
+
195
342
  // 4. Create and Delete
196
343
  it("should create and delete a person", async () => {
197
344
  const { accessToken } = await testSetup.rootUserLogin()