瀏覽代碼

on_action params grouped (#2047)

* on_action params grouped
resolves #2045

* with tests

---------

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Fred Lefévère-Laoide 7 月之前
父節點
當前提交
48c86e9556
共有 2 個文件被更改,包括 23 次插入2 次删除
  1. 2 2
      taipy/gui/gui.py
  2. 21 0
      tests/gui/gui_specific/test_gui.py

+ 2 - 2
taipy/gui/gui.py

@@ -1506,7 +1506,7 @@ class Gui:
                 return
                 return
             else:  # pragma: no cover
             else:  # pragma: no cover
                 _warn(f"on_action(): '{action}' is not a valid function.")
                 _warn(f"on_action(): '{action}' is not a valid function.")
-        if hasattr(self, "on_action"):
+        if getattr(self, "on_action", None) is not None:
             self.__call_function_with_args(action_function=self.on_action, id=id, payload=payload)
             self.__call_function_with_args(action_function=self.on_action, id=id, payload=payload)
 
 
     def __call_function_with_args(self, **kwargs):
     def __call_function_with_args(self, **kwargs):
@@ -1520,7 +1520,7 @@ class Gui:
                     args = [self._get_real_var_name(id)[0], payload]
                     args = [self._get_real_var_name(id)[0], payload]
                 except Exception:
                 except Exception:
                     args = [id, payload]
                     args = [id, payload]
-                self._call_function_with_state(t.cast(t.Callable, action_function), [args])
+                self._call_function_with_state(t.cast(t.Callable, action_function), args)
                 return True
                 return True
             except Exception as e:  # pragma: no cover
             except Exception as e:  # pragma: no cover
                 if not self._call_on_exception(action_function, e):
                 if not self._call_on_exception(action_function, e):

+ 21 - 0
tests/gui/gui_specific/test_gui.py

@@ -89,3 +89,24 @@ def test__get_valid_adapter_result(gui: Gui):
         res = gui._get_valid_adapter_result(("id", "label"))
         res = gui._get_valid_adapter_result(("id", "label"))
         assert isinstance(res, tuple)
         assert isinstance(res, tuple)
         assert res[0] == "id"
         assert res[0] == "id"
+
+def test_on_action_call(gui:Gui):
+    an_id = "my_id"
+
+    a_non_action_payload = {"a": "b"}
+    def on_action(state, id, payload):
+        assert id == an_id
+        assert payload is a_non_action_payload
+
+    an_action_payload = {"action": "on_an_action"}
+    def on_an_action(state, id, payload):
+        assert id == an_id
+        assert payload is an_action_payload
+
+    # set gui frame
+    gui._set_frame(inspect.currentframe())
+
+    gui.run(run_server=False)
+    with gui.get_flask_app().app_context():
+        gui._Gui__on_action(an_id, a_non_action_payload) # type: ignore[reportAttributeAccessIssue]
+        gui._Gui__on_action(an_id, an_action_payload) # type: ignore[reportAttributeAccessIssue]