test_mermaid.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import pytest
  2. from nicegui import ui
  3. from nicegui.testing import Screen
  4. def test_mermaid(screen: Screen):
  5. m = ui.mermaid('''
  6. graph TD;
  7. Node_A --> Node_B;
  8. ''')
  9. screen.open('/')
  10. assert screen.find('Node_A').get_attribute('class') == 'nodeLabel'
  11. m.set_content('''
  12. graph TD;
  13. Node_C --> Node_D;
  14. ''')
  15. assert screen.find('Node_C').get_attribute('class') == 'nodeLabel'
  16. screen.should_not_contain('Node_A')
  17. def test_mermaid_with_line_breaks(screen: Screen):
  18. ui.mermaid('''
  19. requirementDiagram
  20. requirement test_req {
  21. id: 1
  22. text: some test text
  23. risk: high
  24. verifymethod: test
  25. }
  26. ''')
  27. screen.open('/')
  28. screen.should_contain('<<Requirement>>')
  29. screen.should_contain('Id: 1')
  30. screen.should_contain('Text: some test text')
  31. screen.should_contain('Risk: High')
  32. screen.should_contain('Verification: Test')
  33. def test_replace_mermaid(screen: Screen):
  34. with ui.row() as container:
  35. ui.mermaid('graph LR; Node_A')
  36. def replace():
  37. container.clear()
  38. with container:
  39. ui.mermaid('graph LR; Node_B')
  40. ui.button('Replace', on_click=replace)
  41. screen.open('/')
  42. screen.should_contain('Node_A')
  43. screen.click('Replace')
  44. screen.wait(0.5)
  45. screen.should_contain('Node_B')
  46. screen.should_not_contain('Node_A')
  47. def test_create_dynamically(screen: Screen):
  48. ui.button('Create', on_click=lambda: ui.mermaid('graph LR; Node'))
  49. screen.open('/')
  50. screen.click('Create')
  51. screen.should_contain('Node')
  52. def test_error(screen: Screen):
  53. ui.mermaid('''
  54. graph LR;
  55. A --> B;
  56. A -> C;
  57. ''').on('error', lambda e: ui.label(e.args['message']))
  58. screen.open('/')
  59. screen.should_contain('Syntax error in text')
  60. screen.should_contain('Parse error on line 3')
  61. @pytest.mark.parametrize('security_level', ['loose', 'strict'])
  62. def test_click_mermaid_node(security_level: str, screen: Screen):
  63. ui.mermaid('''
  64. flowchart TD;
  65. A;
  66. click A call document.write("Success")
  67. ''', config={'securityLevel': security_level})
  68. screen.open('/')
  69. screen.click('A')
  70. screen.wait(0.5)
  71. if security_level == 'loose':
  72. screen.should_contain('Success')
  73. else:
  74. screen.should_not_contain('Success')