@@ -419,3 +419,116 @@ def my_handler(self):
419419
420420 # Should be the minified name directly
421421 assert event_name == "d"
422+
423+
424+ class TestDynamicHandlerMinification :
425+ """Tests for dynamic event handler minification (setvar, auto-setters)."""
426+
427+ def test_setvar_registered_with_config (self , temp_minify_json ):
428+ """Test that setvar is registered in _event_id_to_name when config exists."""
429+ expected_module = "tests.units.test_minification"
430+ expected_state_path = f"{ expected_module } .State.TestStateWithSetvar"
431+
432+ config : MinifyConfig = {
433+ "version" : SCHEMA_VERSION ,
434+ "states" : {
435+ "reflex.state.State" : "a" ,
436+ expected_state_path : "b" ,
437+ },
438+ "events" : {
439+ expected_state_path : {"setvar" : "s" },
440+ },
441+ }
442+ save_minify_config (config )
443+ clear_config_cache ()
444+ State .get_name .cache_clear ()
445+ State .get_full_name .cache_clear ()
446+ State .get_class_substate .cache_clear ()
447+
448+ class TestStateWithSetvar (State ):
449+ pass
450+
451+ # Verify setvar is registered for minification
452+ assert "s" in TestStateWithSetvar ._event_id_to_name
453+ assert TestStateWithSetvar ._event_id_to_name ["s" ] == "setvar"
454+
455+ def test_auto_setter_registered_with_config (self , temp_minify_json ):
456+ """Test that auto-setters (set_*) are registered in _event_id_to_name when config exists."""
457+ expected_module = "tests.units.test_minification"
458+ expected_state_path = f"{ expected_module } .State.TestStateWithAutoSetter"
459+
460+ config : MinifyConfig = {
461+ "version" : SCHEMA_VERSION ,
462+ "states" : {
463+ "reflex.state.State" : "a" ,
464+ expected_state_path : "b" ,
465+ },
466+ "events" : {
467+ expected_state_path : {"set_count" : "c" , "setvar" : "v" },
468+ },
469+ }
470+ save_minify_config (config )
471+ clear_config_cache ()
472+ State .get_name .cache_clear ()
473+ State .get_full_name .cache_clear ()
474+ State .get_class_substate .cache_clear ()
475+
476+ class TestStateWithAutoSetter (State ):
477+ count : int = 0
478+
479+ # Verify auto-setter is registered for minification
480+ assert "c" in TestStateWithAutoSetter ._event_id_to_name
481+ assert TestStateWithAutoSetter ._event_id_to_name ["c" ] == "set_count"
482+
483+ def test_dynamic_handlers_not_registered_without_config (self , temp_minify_json ):
484+ """Test that dynamic handlers are NOT registered when no config exists."""
485+ # No config saved - temp_minify_json fixture ensures clean state
486+
487+ class TestStateNoConfig (State ):
488+ count : int = 0
489+
490+ # Without config, _event_id_to_name should be empty
491+ assert TestStateNoConfig ._event_id_to_name == {}
492+
493+ def test_add_event_handler_registered_with_config (self , temp_minify_json ):
494+ """Test that dynamically added event handlers via _add_event_handler are registered."""
495+ import reflex as rx
496+
497+ expected_module = "tests.units.test_minification"
498+ expected_state_path = f"{ expected_module } .State.TestStateWithDynamicHandler"
499+
500+ config : MinifyConfig = {
501+ "version" : SCHEMA_VERSION ,
502+ "states" : {
503+ "reflex.state.State" : "a" ,
504+ expected_state_path : "b" ,
505+ },
506+ "events" : {
507+ expected_state_path : {"dynamic_handler" : "d" , "setvar" : "v" },
508+ },
509+ }
510+ save_minify_config (config )
511+ clear_config_cache ()
512+ State .get_name .cache_clear ()
513+ State .get_full_name .cache_clear ()
514+ State .get_class_substate .cache_clear ()
515+
516+ class TestStateWithDynamicHandler (State ):
517+ pass
518+
519+ # Dynamically add an event handler after class creation
520+ @rx .event
521+ def dynamic_handler (self ):
522+ pass
523+
524+ from reflex .event import EventHandler
525+
526+ handler = EventHandler (
527+ fn = dynamic_handler ,
528+ state_full_name = TestStateWithDynamicHandler .get_full_name (),
529+ )
530+ TestStateWithDynamicHandler ._add_event_handler ("dynamic_handler" , handler )
531+
532+ # Verify dynamic handler is registered for minification
533+ assert "d" in TestStateWithDynamicHandler ._event_id_to_name
534+ assert TestStateWithDynamicHandler ._event_id_to_name ["d" ] == "dynamic_handler"
0 commit comments