11/* eslint-disable @typescript-eslint/no-this-alias */
22import type { HookContext } from '@feathersjs/feathers'
3- import { assert } from 'vitest'
3+ import { assert , expect , vi } from 'vitest'
44import { iffElse } from './iff-else.hook.js'
55import { or } from '../../predicates/or/or.predicate.js'
66import { and } from '../../predicates/and/and.predicate.js'
@@ -53,9 +53,9 @@ const hookFcn = function (this: any, hook: any, _cb: any) {
5353const predicateTrue = function (
5454 this : any ,
5555 hook : any ,
56- more2 : any ,
57- more3 : any ,
58- more4 : any ,
56+ more2 ? : any ,
57+ more3 ? : any ,
58+ more4 ? : any ,
5959) : true {
6060 predicateTrueContext = this
6161
@@ -128,28 +128,26 @@ describe('services iffElse', () => {
128128 } )
129129
130130 it ( 'when false' , ( ) => {
131- return (
132- // @ts -expect-error TODO
133- iffElse ( false , null , [ hookFcnSync , hookFcnAsync , hookFcn ] ) ( hook ) . then (
134- ( hook : any ) => {
135- assert . deepEqual ( hook , hookAfter )
136- assert . equal ( hookFcnSyncCalls , 1 )
137- assert . equal ( hookFcnAsyncCalls , 1 )
138- assert . equal ( hookFcnCalls , 1 )
139- assert . deepEqual ( hook , hookAfter )
140- } ,
141- )
142- )
131+ return iffElse (
132+ false ,
133+ undefined ,
134+ [ hookFcnSync , hookFcnAsync , hookFcn ] ,
135+ ) ( hook ) . then ( ( hook : any ) => {
136+ assert . deepEqual ( hook , hookAfter )
137+ assert . equal ( hookFcnSyncCalls , 1 )
138+ assert . equal ( hookFcnAsyncCalls , 1 )
139+ assert . equal ( hookFcnCalls , 1 )
140+ assert . deepEqual ( hook , hookAfter )
141+ } )
143142 } )
144143 } )
145144
146145 describe ( 'predicate gets right params' , ( ) => {
147146 it ( 'when true' , ( ) => {
148147 return iffElse (
149- // @ts -expect-error TODO
150148 predicateTrue ,
151149 [ hookFcnSync , hookFcnAsync , hookFcn ] ,
152- null ,
150+ undefined ,
153151 ) ( hook ) . then ( ( ) => {
154152 assert . deepEqual ( predicateParam1 , hook , 'param1' )
155153 assert . strictEqual ( predicateParam2 , undefined , 'param2' )
@@ -160,7 +158,6 @@ describe('services iffElse', () => {
160158
161159 it ( 'and passes on correct params' , ( ) => {
162160 return iffElse (
163- // @ts -expect-error TODO
164161 and ( predicateTrue ) ,
165162 [ hookFcnSync , hookFcnAsync , hookFcn ] ,
166163 [ ] ,
@@ -174,7 +171,6 @@ describe('services iffElse', () => {
174171
175172 it ( 'or passes on correct params' , ( ) => {
176173 return iffElse (
177- // @ts -expect-error TODO
178174 or ( predicateTrue ) ,
179175 [ hookFcnSync , hookFcnAsync , hookFcn ] ,
180176 [ ] ,
@@ -197,23 +193,81 @@ describe('services iffElse', () => {
197193 } )
198194
199195 it ( 'services' , ( ) => {
200- return (
201- // @ts -expect-error TODO
202- iffElse ( predicateTrue , [ hookFcnSync , hookFcnAsync , hookFcn ] , null )
203- . call ( context , hook )
204- . then ( ( hook : any ) => {
205- assert . deepEqual ( hook , hookAfter )
206- assert . equal ( hookFcnSyncCalls , 1 )
207- assert . equal ( hookFcnAsyncCalls , 1 )
208- assert . equal ( hookFcnCalls , 1 )
209- assert . deepEqual ( hook , hookAfter )
210-
211- assert . deepEqual ( predicateTrueContext , { service : 'abc' } )
212- assert . deepEqual ( hookFcnSyncContext , { service : 'abc' } )
213- assert . deepEqual ( hookFcnAsyncContext , { service : 'abc' } )
214- assert . deepEqual ( hookFcnContext , { service : 'abc' } )
215- } )
196+ return iffElse (
197+ predicateTrue ,
198+ [ hookFcnSync , hookFcnAsync , hookFcn ] ,
199+ undefined ,
216200 )
201+ . call ( context , hook )
202+ . then ( ( hook : any ) => {
203+ assert . deepEqual ( hook , hookAfter )
204+ assert . equal ( hookFcnSyncCalls , 1 )
205+ assert . equal ( hookFcnAsyncCalls , 1 )
206+ assert . equal ( hookFcnCalls , 1 )
207+ assert . deepEqual ( hook , hookAfter )
208+
209+ assert . deepEqual ( predicateTrueContext , { service : 'abc' } )
210+ assert . deepEqual ( hookFcnSyncContext , { service : 'abc' } )
211+ assert . deepEqual ( hookFcnAsyncContext , { service : 'abc' } )
212+ assert . deepEqual ( hookFcnContext , { service : 'abc' } )
213+ } )
214+ } )
215+ } )
216+
217+ describe ( 'around hooks' , ( ) => {
218+ it ( 'runs trueHooks and then next() when predicate is truthy' , async ( ) => {
219+ const next = vi . fn ( )
220+
221+ await iffElse ( true , hookFcnSync , hookFcnAsync ) ( hook , next )
222+
223+ assert . equal ( hookFcnSyncCalls , 1 )
224+ assert . equal ( hookFcnAsyncCalls , 0 )
225+ expect ( next ) . toHaveBeenCalledOnce ( )
226+ } )
227+
228+ it ( 'runs falseHooks and then next() when predicate is falsy' , async ( ) => {
229+ const next = vi . fn ( )
230+
231+ await iffElse ( false , hookFcnSync , hookFcnAsync ) ( hook , next )
232+
233+ assert . equal ( hookFcnSyncCalls , 0 )
234+ assert . equal ( hookFcnAsyncCalls , 1 )
235+ expect ( next ) . toHaveBeenCalledOnce ( )
236+ } )
237+
238+ it ( 'calls next() even when no hooks are provided for the branch' , async ( ) => {
239+ const next = vi . fn ( )
240+
241+ await iffElse ( true , undefined , undefined ) ( hook , next )
242+
243+ expect ( next ) . toHaveBeenCalledOnce ( )
244+ } )
245+
246+ it ( 'awaits async predicate then runs trueHooks and next()' , async ( ) => {
247+ const next = vi . fn ( )
248+ const asyncTrue = ( ) => Promise . resolve ( true )
249+
250+ await iffElse ( asyncTrue , hookFcnSync , hookFcnAsync ) ( hook , next )
251+
252+ assert . equal ( hookFcnSyncCalls , 1 )
253+ assert . equal ( hookFcnAsyncCalls , 0 )
254+ expect ( next ) . toHaveBeenCalledOnce ( )
255+ } )
256+
257+ it ( 'calls next() after inner hooks finish, not before' , async ( ) => {
258+ const order : string [ ] = [ ]
259+ const slowHook = async ( h : any ) => {
260+ await new Promise ( ( r ) => setTimeout ( r , 5 ) )
261+ order . push ( 'inner' )
262+ return h
263+ }
264+ const next = vi . fn ( async ( ) => {
265+ order . push ( 'next' )
266+ } )
267+
268+ await iffElse ( true , slowHook , undefined ) ( hook , next )
269+
270+ assert . deepEqual ( order , [ 'inner' , 'next' ] )
217271 } )
218272 } )
219273} )
0 commit comments