From cb005d58eb09050315ac29d99810cc1bae961873 Mon Sep 17 00:00:00 2001 From: Richard van Heuven van Staereling Date: Thu, 2 Mar 2023 17:05:08 +0100 Subject: [PATCH] chore: adds test cases for chaining hooks --- test/hooks/int.spec.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/hooks/int.spec.ts b/test/hooks/int.spec.ts index 4d0f12070..01db8ab10 100644 --- a/test/hooks/int.spec.ts +++ b/test/hooks/int.spec.ts @@ -5,6 +5,7 @@ import payload from '../../src'; import { RESTClient } from '../helpers/rest'; import { transformSlug } from './collections/Transform'; import { hooksSlug } from './collections/Hook'; +import { chainingHooksSlug } from './collections/ChainingHooks'; import { generatedAfterReadText, nestedAfterReadHooksSlug } from './collections/NestedAfterReadHooks'; import { relationsSlug } from './collections/Relations'; import type { NestedAfterReadHook } from './payload-types'; @@ -119,7 +120,39 @@ describe('Hooks', () => { expect(retrievedDoc.group.array[0].shouldPopulate.title).toEqual(relation.title); expect(retrievedDoc.group.subGroup.shouldPopulate.title).toEqual(relation.title); }); + + it('should pass result from previous hook into next hook with findByID', async () => { + const document = await payload.create({ + collection: chainingHooksSlug, + data: { + text: 'ok', + }, + }); + + const retrievedDoc = await payload.findByID({ + collection: chainingHooksSlug, + id: document.id, + }); + + expect(retrievedDoc.text).toEqual('ok!!'); + }); + + it('should pass result from previous hook into next hook with find', async () => { + const document = await payload.create({ + collection: chainingHooksSlug, + data: { + text: 'ok', + }, + }); + + const { docs: retrievedDocs } = await payload.find({ + collection: chainingHooksSlug, + }); + + expect(retrievedDocs[0].text).toEqual('ok!!'); + }); }); + describe('auth collection hooks', () => { it('allow admin login', async () => { const { user } = await payload.login({