@@ -22,51 +22,108 @@ type App = typeof app
2222type AppCtx = HookContext < App >
2323type UserCtx = HookContext < App , MemoryService < User > >
2424
25- const context = { } as UserCtx
26- const appContext = { } as AppCtx
27-
2825it ( 'options overload accepts valid options' , ( ) => {
29- checkContext ( context , { type : 'before' } )
30- checkContext ( context , { method : 'create' } )
31- checkContext ( context , {
26+ const ctx1 = { } as UserCtx
27+ checkContext ( ctx1 , { type : 'before' } )
28+ const ctx2 = { } as UserCtx
29+ checkContext ( ctx2 , { method : 'create' } )
30+ const ctx3 = { } as UserCtx
31+ checkContext ( ctx3 , {
3232 type : [ 'before' , 'around' ] ,
3333 method : [ 'create' , 'patch' ] ,
3434 } )
35- checkContext ( context , { type : 'before' , label : 'myHook' } )
36- checkContext ( context , { path : 'users' } )
35+ const ctx4 = { } as UserCtx
36+ checkContext ( ctx4 , { type : 'before' , label : 'myHook' } )
37+ const ctx5 = { } as UserCtx
38+ checkContext ( ctx5 , { path : 'users' } )
3739} )
3840
3941it ( 'options overload rejects invalid type' , ( ) => {
42+ const ctx = { } as UserCtx
4043 // @ts -expect-error "invalid" is not a valid HookType
41- checkContext ( context , { type : 'invalid' } )
44+ checkContext ( ctx , { type : 'invalid' } )
4245} )
4346
4447it ( 'options overload accepts valid path for app-level context' , ( ) => {
45- checkContext ( appContext , { path : 'users' } )
46- checkContext ( appContext , { path : 'messages' } )
47- checkContext ( appContext , { path : [ 'users' , 'messages' ] } )
48+ const ctx1 = { } as AppCtx
49+ checkContext ( ctx1 , { path : 'users' } )
50+ const ctx2 = { } as AppCtx
51+ checkContext ( ctx2 , { path : 'messages' } )
52+ const ctx3 = { } as AppCtx
53+ checkContext ( ctx3 , { path : [ 'users' , 'messages' ] } )
4854} )
4955
5056it ( 'options overload rejects invalid path for service-specific context' , ( ) => {
57+ const ctx = { } as UserCtx
5158 // @ts -expect-error "messages" is not valid when context is narrowed to MemoryService<User>
52- checkContext ( context , { path : 'messages' } )
59+ checkContext ( ctx , { path : 'messages' } )
5360} )
5461
5562it ( 'options overload rejects invalid path for app-level context' , ( ) => {
63+ const ctx = { } as AppCtx
5664 // @ts -expect-error "nonExistent" is not a valid service path
57- checkContext ( appContext , { path : 'nonExistent' } )
65+ checkContext ( ctx , { path : 'nonExistent' } )
5866} )
5967
6068it ( 'positional overload accepts valid args' , ( ) => {
61- checkContext ( context , 'before' )
62- checkContext ( context , [ 'before' , 'after' ] )
63- checkContext ( context , 'before' , 'create' )
64- checkContext ( context , [ 'before' , 'around' ] , [ 'create' , 'patch' ] , 'myHook' )
65- checkContext ( context , null , 'create' )
66- checkContext ( context , undefined , 'create' )
69+ const ctx1 = { } as UserCtx
70+ checkContext ( ctx1 , 'before' )
71+ const ctx2 = { } as UserCtx
72+ checkContext ( ctx2 , [ 'before' , 'after' ] )
73+ const ctx3 = { } as UserCtx
74+ checkContext ( ctx3 , 'before' , 'create' )
75+ const ctx4 = { } as UserCtx
76+ checkContext ( ctx4 , [ 'before' , 'around' ] , [ 'create' , 'patch' ] , 'myHook' )
77+ const ctx5 = { } as UserCtx
78+ checkContext ( ctx5 , null , 'create' )
79+ const ctx6 = { } as UserCtx
80+ checkContext ( ctx6 , undefined , 'create' )
6781} )
6882
6983it ( 'positional overload rejects invalid type' , ( ) => {
7084 // @ts -expect-error "invalid" is not a valid HookType
7185 checkContext ( context , 'invalid' )
7286} )
87+
88+ it ( 'narrows path with options overload' , ( ) => {
89+ const ctx = { } as AppCtx
90+ checkContext ( ctx , { path : [ 'users' , 'messages' ] } )
91+ expectTypeOf ( ctx . path ) . toEqualTypeOf < 'users' | 'messages' > ( )
92+ } )
93+
94+ it ( 'narrows path with single value' , ( ) => {
95+ const ctx = { } as AppCtx
96+ checkContext ( ctx , { path : 'users' } )
97+ expectTypeOf ( ctx . path ) . toEqualTypeOf < 'users' > ( )
98+ } )
99+
100+ it ( 'narrows type with options overload' , ( ) => {
101+ const ctx = { } as AppCtx
102+ checkContext ( ctx , { type : [ 'before' , 'around' ] } )
103+ expectTypeOf ( ctx . type ) . toEqualTypeOf < 'before' | 'around' > ( )
104+ } )
105+
106+ it ( 'narrows method with options overload' , ( ) => {
107+ const ctx = { } as AppCtx
108+ checkContext ( ctx , { method : [ 'create' , 'patch' ] } )
109+ expectTypeOf ( ctx . method ) . toEqualTypeOf < 'create' | 'patch' > ( )
110+ } )
111+
112+ it ( 'narrows type with positional overload' , ( ) => {
113+ const ctx = { } as AppCtx
114+ checkContext ( ctx , 'before' )
115+ expectTypeOf ( ctx . type ) . toEqualTypeOf < 'before' > ( )
116+ } )
117+
118+ it ( 'narrows type and method with positional overload' , ( ) => {
119+ const ctx = { } as AppCtx
120+ checkContext ( ctx , [ 'before' , 'around' ] , [ 'create' , 'patch' ] )
121+ expectTypeOf ( ctx . type ) . toEqualTypeOf < 'before' | 'around' > ( )
122+ expectTypeOf ( ctx . method ) . toEqualTypeOf < 'create' | 'patch' > ( )
123+ } )
124+
125+ it ( 'does not narrow when null is passed in positional overload' , ( ) => {
126+ const ctx = { } as AppCtx
127+ checkContext ( ctx , null , 'create' )
128+ expectTypeOf ( ctx . method ) . toEqualTypeOf < 'create' > ( )
129+ } )
0 commit comments