test_navigate.py 4.1 KB

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