test_actions.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import inspect
  12. from taipy.gui import Gui, Markdown
  13. def test_actions(gui: Gui, helpers):
  14. def an_action(state):
  15. pass
  16. # set gui frame
  17. gui._set_frame(inspect.currentframe())
  18. gui.add_page("test", Markdown("<|action button|button|on_action={an_action}|>"))
  19. gui.run(run_server=False)
  20. server_client = gui._server.test_client()
  21. cid = helpers.create_scope_and_get_sid(gui)
  22. # Get the jsx once so that the page will be evaluated -> variable will be registered
  23. response = server_client.get(f"/taipy-jsx/test?client_id={cid}")
  24. assert response.status_code == 200, f"response.status_code {response.status_code} != 200"
  25. response_data = helpers.get_response_data(response)
  26. assert isinstance(response_data, dict), "response_data is not Dict"
  27. assert "jsx" in response_data, "jsx not in response_data"
  28. jsx = response_data["jsx"]
  29. function_name = ""
  30. for part in jsx.split(" "):
  31. if part.startswith("onAction="):
  32. function_name = part.split('"')[1]
  33. break
  34. assert function_name
  35. scope = gui._bindings()._get_all_scopes().get(cid)
  36. assert getattr(scope, function_name) is an_action