@@ -10,6 +10,11 @@ import { devUser, regularUser } from '../credentials.js'
1010import { initPayloadInt } from '../helpers/initPayloadInt.js'
1111import { isMongoose } from '../helpers/isMongoose.js'
1212import { afterOperationSlug } from './collections/AfterOperation/index.js'
13+ import {
14+ beforeOperationSlug ,
15+ clearLastOperation ,
16+ getLastOperation ,
17+ } from './collections/BeforeOperation/index.js'
1318import { chainingHooksSlug } from './collections/ChainingHooks/index.js'
1419import { contextHooksSlug } from './collections/ContextHooks/index.js'
1520import { dataHooksSlug } from './collections/Data/index.js'
@@ -743,4 +748,241 @@ describe('Hooks', () => {
743748 expect ( updateResult ) . toBeDefined ( )
744749 } )
745750 } )
751+
752+ describe ( 'beforeOperation' , ( ) => {
753+ afterEach ( ( ) => {
754+ clearLastOperation ( )
755+ } )
756+
757+ it ( 'should pass correct operation arg on create' , async ( ) => {
758+ await payload . create ( {
759+ collection : beforeOperationSlug ,
760+ data : { } ,
761+ } )
762+
763+ expect ( getLastOperation ( ) ) . toEqual ( 'create' )
764+ } )
765+
766+ it ( 'should pass correct operation arg on update' , async ( ) => {
767+ const doc = await payload . create ( {
768+ collection : beforeOperationSlug ,
769+ data : { } ,
770+ } )
771+
772+ await payload . update ( {
773+ id : doc . id ,
774+ collection : beforeOperationSlug ,
775+ data : { } ,
776+ } )
777+
778+ expect ( getLastOperation ( ) ) . toEqual ( 'update' )
779+ } )
780+
781+ it ( 'should pass correct operation arg on updateByID' , async ( ) => {
782+ const doc = await payload . create ( {
783+ collection : beforeOperationSlug ,
784+ data : { } ,
785+ } )
786+
787+ await payload . update ( {
788+ id : doc . id ,
789+ collection : beforeOperationSlug ,
790+ data : { } ,
791+ } )
792+
793+ expect ( getLastOperation ( ) ) . toEqual ( 'update' )
794+ } )
795+
796+ it ( 'should pass correct operation arg on read (findByID)' , async ( ) => {
797+ const doc = await payload . create ( {
798+ collection : beforeOperationSlug ,
799+ data : { } ,
800+ } )
801+
802+ await payload . findByID ( {
803+ id : doc . id ,
804+ collection : beforeOperationSlug ,
805+ } )
806+
807+ expect ( getLastOperation ( ) ) . toEqual ( 'read' )
808+ } )
809+
810+ it ( 'should pass correct operation arg on read (find)' , async ( ) => {
811+ await payload . create ( {
812+ collection : beforeOperationSlug ,
813+ data : { } ,
814+ } )
815+
816+ clearLastOperation ( )
817+
818+ await payload . find ( {
819+ collection : beforeOperationSlug ,
820+ } )
821+
822+ expect ( getLastOperation ( ) ) . toEqual ( 'read' )
823+ } )
824+
825+ it ( 'should pass correct operation arg on readDistinct (findDistinct)' , async ( ) => {
826+ await payload . create ( {
827+ collection : beforeOperationSlug ,
828+ data : { category : 'test1' } ,
829+ } )
830+ await payload . create ( {
831+ collection : beforeOperationSlug ,
832+ data : { category : 'test2' } ,
833+ } )
834+ await payload . create ( {
835+ collection : beforeOperationSlug ,
836+ data : { category : 'test1' } ,
837+ } )
838+
839+ await payload . findDistinct ( {
840+ collection : beforeOperationSlug ,
841+ field : 'category' ,
842+ } )
843+
844+ expect ( getLastOperation ( ) ) . toEqual ( 'readDistinct' )
845+ } )
846+
847+ it ( 'should pass correct operation arg on delete' , async ( ) => {
848+ const doc = await payload . create ( {
849+ collection : beforeOperationSlug ,
850+ data : { } ,
851+ } )
852+
853+ await payload . delete ( {
854+ id : doc . id ,
855+ collection : beforeOperationSlug ,
856+ } )
857+
858+ expect ( getLastOperation ( ) ) . toEqual ( 'delete' )
859+ } )
860+
861+ it ( 'should pass correct operation arg on deleteByID' , async ( ) => {
862+ const doc = await payload . create ( {
863+ collection : beforeOperationSlug ,
864+ data : { } ,
865+ } )
866+
867+ await payload . delete ( {
868+ id : doc . id ,
869+ collection : beforeOperationSlug ,
870+ } )
871+
872+ expect ( getLastOperation ( ) ) . toEqual ( 'delete' )
873+ } )
874+
875+ it ( 'should pass correct operation arg on count' , async ( ) => {
876+ await payload . create ( {
877+ collection : beforeOperationSlug ,
878+ data : { } ,
879+ } )
880+
881+ await payload . count ( {
882+ collection : beforeOperationSlug ,
883+ } )
884+
885+ expect ( getLastOperation ( ) ) . toEqual ( 'count' )
886+ } )
887+
888+ it ( 'should pass correct operation arg on countVersions' , async ( ) => {
889+ const doc = await payload . create ( {
890+ collection : beforeOperationSlug ,
891+ data : { } ,
892+ } )
893+
894+ await payload . countVersions ( {
895+ collection : beforeOperationSlug ,
896+ where : {
897+ parent : {
898+ equals : doc . id ,
899+ } ,
900+ } ,
901+ } )
902+
903+ expect ( getLastOperation ( ) ) . toEqual ( 'countVersions' )
904+ } )
905+
906+ it ( 'should pass correct operation arg on findVersions' , async ( ) => {
907+ const doc = await payload . create ( {
908+ collection : beforeOperationSlug ,
909+ data : { } ,
910+ } )
911+
912+ await payload . findVersions ( {
913+ collection : beforeOperationSlug ,
914+ where : {
915+ parent : {
916+ equals : doc . id ,
917+ } ,
918+ } ,
919+ } )
920+
921+ expect ( getLastOperation ( ) ) . toEqual ( 'read' )
922+ } )
923+
924+ it ( 'should pass correct operation arg on findVersionByID' , async ( ) => {
925+ const doc = await payload . create ( {
926+ collection : beforeOperationSlug ,
927+ data : { category : 'v1' } ,
928+ } )
929+
930+ // Update to create a version
931+ await payload . update ( {
932+ id : doc . id ,
933+ collection : beforeOperationSlug ,
934+ data : { category : 'v2' } ,
935+ } )
936+
937+ const versions = await payload . findVersions ( {
938+ collection : beforeOperationSlug ,
939+ where : {
940+ parent : {
941+ equals : doc . id ,
942+ } ,
943+ } ,
944+ } )
945+
946+ expect ( versions . docs . length ) . toBeGreaterThan ( 0 )
947+
948+ await payload . findVersionByID ( {
949+ collection : beforeOperationSlug ,
950+ id : versions . docs [ 0 ] ! . id ,
951+ } )
952+
953+ expect ( getLastOperation ( ) ) . toEqual ( 'read' )
954+ } )
955+
956+ it ( 'should pass correct operation arg on restoreVersion' , async ( ) => {
957+ const doc = await payload . create ( {
958+ collection : beforeOperationSlug ,
959+ data : { category : 'v1' } ,
960+ } )
961+
962+ // Update to create a version
963+ await payload . update ( {
964+ id : doc . id ,
965+ collection : beforeOperationSlug ,
966+ data : { category : 'v2' } ,
967+ } )
968+
969+ const versions = await payload . findVersions ( {
970+ collection : beforeOperationSlug ,
971+ where : {
972+ parent : {
973+ equals : doc . id ,
974+ } ,
975+ } ,
976+ } )
977+
978+ expect ( versions . docs . length ) . toBeGreaterThan ( 0 )
979+
980+ await payload . restoreVersion ( {
981+ collection : beforeOperationSlug ,
982+ id : versions . docs [ 0 ] ! . id ,
983+ } )
984+
985+ expect ( getLastOperation ( ) ) . toEqual ( 'restoreVersion' )
986+ } )
987+ } )
746988} )
0 commit comments