test_markdown.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from selenium.webdriver.common.by import By
  2. from nicegui import ui
  3. from nicegui.testing import Screen
  4. def test_markdown(screen: Screen):
  5. m = ui.markdown('This is **Markdown**')
  6. screen.open('/')
  7. element = screen.find('This is')
  8. assert element.text == 'This is Markdown'
  9. assert element.get_attribute('innerHTML') == 'This is <strong>Markdown</strong>'
  10. m.set_content('New **content**')
  11. element = screen.find('New')
  12. assert element.text == 'New content'
  13. assert element.get_attribute('innerHTML') == 'New <strong>content</strong>'
  14. def test_markdown_with_mermaid(screen: Screen):
  15. m = ui.markdown('''
  16. Mermaid:
  17. ```mermaid
  18. graph TD;
  19. Node_A --> Node_B;
  20. ```
  21. ''', extras=['mermaid', 'fenced-code-blocks'])
  22. screen.open('/')
  23. screen.wait(0.5) # wait for Mermaid to render
  24. screen.should_contain('Mermaid')
  25. assert screen.find_by_tag('svg').get_attribute('id') == f'{m.html_id}_mermaid_0'
  26. assert screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_A")]]').is_displayed()
  27. m.set_content('''
  28. New:
  29. ```mermaid
  30. graph TD;
  31. Node_C --> Node_D;
  32. ```
  33. ''')
  34. screen.should_contain('New')
  35. assert screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_C")]]').is_displayed()
  36. screen.should_not_contain('Node_A')
  37. def test_markdown_with_mermaid_on_demand(screen: Screen):
  38. ui.button('Create Mermaid', on_click=lambda: ui.markdown('''
  39. ```mermaid
  40. graph TD;
  41. Node_A --> Node_B;
  42. ```
  43. ''', extras=['mermaid', 'fenced-code-blocks']))
  44. screen.open('/')
  45. screen.click('Create Mermaid')
  46. assert screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_A")]]').is_displayed()
  47. assert screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_B")]]').is_displayed()
  48. def test_strip_indentation(screen: Screen):
  49. ui.markdown('''
  50. **This is Markdown.**
  51. ''')
  52. screen.open('/')
  53. screen.should_contain('This is Markdown.')
  54. screen.should_not_contain('**This is Markdown.**') # NOTE: '**' are translated to formatting and not visible
  55. def test_replace_markdown(screen: Screen):
  56. with ui.row() as container:
  57. ui.markdown('A')
  58. def replace():
  59. container.clear()
  60. with container:
  61. ui.markdown('B')
  62. ui.button('Replace', on_click=replace)
  63. screen.open('/')
  64. screen.should_contain('A')
  65. screen.click('Replace')
  66. screen.should_contain('B')
  67. screen.should_not_contain('A')