@@ -1145,4 +1145,184 @@ describe('getPersistentState', () => {
1145
1145
expect ( visitorController . state . visitors ) . toHaveLength ( 0 ) ;
1146
1146
} ) ;
1147
1147
} ) ;
1148
+
1149
+ describe ( 'registerActionHandlers' , ( ) => {
1150
+ type TestControllerActions =
1151
+ | {
1152
+ type : 'TestController:testMethod' ;
1153
+ handler : ( ) => string ;
1154
+ }
1155
+ | { type : 'TestController:method1' ; handler : ( ) => string }
1156
+ | { type : 'TestController:method2' ; handler : ( ) => string }
1157
+ | { type : 'TestController:getInstanceValue' ; handler : ( ) => string } ;
1158
+
1159
+ type TestControllerMessenger = RestrictedMessenger <
1160
+ 'TestController' ,
1161
+ TestControllerActions ,
1162
+ never ,
1163
+ never ,
1164
+ never
1165
+ > ;
1166
+
1167
+ /**
1168
+ * Factory function to create a test controller with configurable action handler registration
1169
+ *
1170
+ * @param options - Configuration options for the test controller
1171
+ * @param options.methodsToRegister - Array of method names to register as action handlers
1172
+ * @param options.excludedMethods - Optional array of method names to exclude from registration
1173
+ * @param options.exceptions - Optional map of method names to custom handlers
1174
+ * @param options.instanceValue - Optional custom value for the controller instance
1175
+ * @returns Object containing the messenger and controller instances
1176
+ */
1177
+ function createTestController ( options : {
1178
+ methodsToRegister : readonly string [ ] ;
1179
+ excludedMethods ?: readonly string [ ] ;
1180
+ exceptions ?: Record < string , ( ...args : unknown [ ] ) => unknown > ;
1181
+ instanceValue ?: string ;
1182
+ } ) {
1183
+ const {
1184
+ methodsToRegister,
1185
+ excludedMethods = [ ] ,
1186
+ exceptions = { } ,
1187
+ instanceValue = 'controller instance' ,
1188
+ } = options ;
1189
+
1190
+ class TestController extends BaseController <
1191
+ 'TestController' ,
1192
+ CountControllerState ,
1193
+ TestControllerMessenger
1194
+ > {
1195
+ private readonly instanceValue = instanceValue ;
1196
+
1197
+ constructor ( messenger : TestControllerMessenger ) {
1198
+ super ( {
1199
+ messenger,
1200
+ name : 'TestController' ,
1201
+ state : { count : 0 } ,
1202
+ metadata : countControllerStateMetadata ,
1203
+ } ) ;
1204
+ this . registerActionHandlers (
1205
+ methodsToRegister as readonly ( keyof this & string ) [ ] ,
1206
+ excludedMethods ,
1207
+ exceptions as Partial <
1208
+ Record < keyof this & string , ( ...args : unknown [ ] ) => unknown >
1209
+ > ,
1210
+ ) ;
1211
+ }
1212
+
1213
+ testMethod ( ) {
1214
+ return 'test result' ;
1215
+ }
1216
+
1217
+ method1 ( ) {
1218
+ return 'method1 result' ;
1219
+ }
1220
+
1221
+ method2 ( ) {
1222
+ return 'method2 result' ;
1223
+ }
1224
+
1225
+ getInstanceValue ( ) {
1226
+ return this . instanceValue ;
1227
+ }
1228
+ }
1229
+
1230
+ const messenger = new Messenger < TestControllerActions , never > ( ) ;
1231
+ const controller = new TestController (
1232
+ messenger . getRestricted ( {
1233
+ name : 'TestController' ,
1234
+ allowedActions : [ ] ,
1235
+ allowedEvents : [ ] ,
1236
+ } ) ,
1237
+ ) ;
1238
+
1239
+ return { messenger, controller } ;
1240
+ }
1241
+
1242
+ it ( 'should register action handlers for specified methods using the simplified API' , ( ) => {
1243
+ const { messenger } = createTestController ( {
1244
+ methodsToRegister : [ 'testMethod' , 'method1' ] ,
1245
+ } ) ;
1246
+
1247
+ const testResult = messenger . call ( 'TestController:testMethod' ) ;
1248
+ expect ( testResult ) . toBe ( 'test result' ) ;
1249
+
1250
+ const method1Result = messenger . call ( 'TestController:method1' ) ;
1251
+ expect ( method1Result ) . toBe ( 'method1 result' ) ;
1252
+ } ) ;
1253
+
1254
+ it ( 'should register action handlers with exclusions and exceptions' , ( ) => {
1255
+ const customMethod1 = ( ) => 'custom method1 result' ;
1256
+
1257
+ const { messenger } = createTestController ( {
1258
+ methodsToRegister : [ 'method1' , 'method2' ] ,
1259
+ excludedMethods : [ 'method2' ] ,
1260
+ exceptions : { method1 : customMethod1 } ,
1261
+ } ) ;
1262
+
1263
+ // method1 should use the custom handler
1264
+ const result1 = messenger . call ( 'TestController:method1' ) ;
1265
+ expect ( result1 ) . toBe ( 'custom method1 result' ) ;
1266
+
1267
+ // method2 should not be registered due to exclusion
1268
+ expect ( ( ) => {
1269
+ messenger . call ( 'TestController:method2' ) ;
1270
+ } ) . toThrow (
1271
+ 'A handler for TestController:method2 has not been registered' ,
1272
+ ) ;
1273
+ } ) ;
1274
+
1275
+ it ( 'should properly bind methods to the controller instance' , ( ) => {
1276
+ const { messenger } = createTestController ( {
1277
+ methodsToRegister : [ 'getInstanceValue' ] ,
1278
+ instanceValue : 'custom instance value' ,
1279
+ } ) ;
1280
+
1281
+ // Verify the method is properly bound to the controller instance
1282
+ const result = messenger . call ( 'TestController:getInstanceValue' ) ;
1283
+ expect ( result ) . toBe ( 'custom instance value' ) ;
1284
+ } ) ;
1285
+
1286
+ it ( 'should handle empty method registration' , ( ) => {
1287
+ const { messenger } = createTestController ( {
1288
+ methodsToRegister : [ ] ,
1289
+ } ) ;
1290
+
1291
+ // None of the methods should be registered
1292
+ expect ( ( ) => {
1293
+ messenger . call ( 'TestController:testMethod' ) ;
1294
+ } ) . toThrow (
1295
+ 'A handler for TestController:testMethod has not been registered' ,
1296
+ ) ;
1297
+
1298
+ expect ( ( ) => {
1299
+ messenger . call ( 'TestController:method1' ) ;
1300
+ } ) . toThrow (
1301
+ 'A handler for TestController:method1 has not been registered' ,
1302
+ ) ;
1303
+ } ) ;
1304
+
1305
+ it ( 'should handle multiple exclusions' , ( ) => {
1306
+ const { messenger } = createTestController ( {
1307
+ methodsToRegister : [ 'testMethod' , 'method1' , 'method2' ] ,
1308
+ excludedMethods : [ 'method1' , 'method2' ] ,
1309
+ } ) ;
1310
+
1311
+ // Only testMethod should be registered
1312
+ const testResult = messenger . call ( 'TestController:testMethod' ) ;
1313
+ expect ( testResult ) . toBe ( 'test result' ) ;
1314
+
1315
+ expect ( ( ) => {
1316
+ messenger . call ( 'TestController:method1' ) ;
1317
+ } ) . toThrow (
1318
+ 'A handler for TestController:method1 has not been registered' ,
1319
+ ) ;
1320
+
1321
+ expect ( ( ) => {
1322
+ messenger . call ( 'TestController:method2' ) ;
1323
+ } ) . toThrow (
1324
+ 'A handler for TestController:method2 has not been registered' ,
1325
+ ) ;
1326
+ } ) ;
1327
+ } ) ;
1148
1328
} ) ;
0 commit comments