test_navigate.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2021-2024 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. import warnings
  13. from taipy.gui import Gui, Markdown, State, navigate
  14. def test_navigate(gui: Gui, helpers):
  15. def navigate_to(state: State):
  16. navigate(state, "test")
  17. with warnings.catch_warnings(record=True):
  18. gui._set_frame(inspect.currentframe())
  19. gui.add_page("test", Markdown("#This is a page"))
  20. gui.run(run_server=False)
  21. client = gui._server.test_client()
  22. # WS client and emit
  23. ws_client = gui._server._ws.test_client(gui._server.get_flask())
  24. # Get the jsx once so that the page will be evaluated -> variable will be registered
  25. sid = helpers.create_scope_and_get_sid(gui)
  26. client.get(f"/taipy-jsx/test/?client_id={sid}")
  27. ws_client.emit("message", {"client_id": sid, "type": "A", "name": "my_button", "payload": "navigate_to"})
  28. # assert for received message (message that would be sent to the front-end client)
  29. assert ws_client.get_received()
  30. def test_navigate_to_no_route(gui: Gui, helpers):
  31. def navigate_to(state: State):
  32. navigate(state, "toto")
  33. with warnings.catch_warnings(record=True):
  34. gui._set_frame(inspect.currentframe())
  35. gui.add_page("test", Markdown("#This is a page"))
  36. gui.run(run_server=False)
  37. client = gui._server.test_client()
  38. # WS client and emit
  39. ws_client = gui._server._ws.test_client(gui._server.get_flask())
  40. # Get the jsx once so that the page will be evaluated -> variable will be registered
  41. sid = helpers.create_scope_and_get_sid(gui)
  42. client.get(f"/taipy-jsx/test/?client_id={sid}")
  43. ws_client.emit("message", {"client_id": sid, "type": "A", "name": "my_button", "payload": "navigate_to"})
  44. # assert for received message (message that would be sent to the front-end client)
  45. assert not ws_client.get_received()
  46. def test_on_navigate_to_inexistant(gui: Gui, helpers):
  47. def on_navigate(state: State, page: str):
  48. return "test2" if page == "test" else page
  49. with warnings.catch_warnings(record=True) as records:
  50. gui._set_frame(inspect.currentframe())
  51. gui.add_page("test", Markdown("#This is a page"))
  52. gui.run(run_server=False)
  53. client = gui._server.test_client()
  54. # Get the jsx once so that the page will be evaluated -> variable will be registered
  55. sid = helpers.create_scope_and_get_sid(gui)
  56. client.get(f"/taipy-jsx/test?client_id={sid}")
  57. warns = helpers.get_taipy_warnings(records)
  58. assert len(warns) == 1
  59. text = warns[0].message.args[0] if isinstance(warns[0].message, Warning) else warns[0].message
  60. assert text == 'Cannot navigate to "test2": unknown page.'
  61. def test_on_navigate_to_existant(gui: Gui, helpers):
  62. def on_navigate(state: State, page: str):
  63. return "test2" if page == "test1" else page
  64. with warnings.catch_warnings(record=True):
  65. gui._set_frame(inspect.currentframe())
  66. gui.add_page("test1", Markdown("#This is a page test1"))
  67. gui.add_page("test2", Markdown("#This is a page test2"))
  68. gui.run(run_server=False)
  69. client = gui._server.test_client()
  70. # Get the jsx once so that the page will be evaluated -> variable will be registered
  71. sid = helpers.create_scope_and_get_sid(gui)
  72. content = client.get(f"/taipy-jsx/test1?client_id={sid}")
  73. assert content.status_code == 302