test_markdown.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from nicegui import ui
  2. from nicegui.testing import Screen
  3. def test_markdown(screen: Screen):
  4. m = ui.markdown('This is **Markdown**')
  5. screen.open('/')
  6. element = screen.find('This is')
  7. assert element.text == 'This is Markdown'
  8. assert element.get_attribute('innerHTML') == 'This is <strong>Markdown</strong>'
  9. m.set_content('New **content**')
  10. element = screen.find('New')
  11. assert element.text == 'New content'
  12. assert element.get_attribute('innerHTML') == 'New <strong>content</strong>'
  13. def test_markdown_with_mermaid(screen: Screen):
  14. m = ui.markdown('''
  15. Mermaid:
  16. ```mermaid
  17. graph TD;
  18. Node_A --> Node_B;
  19. ```
  20. ''', extras=['mermaid', 'fenced-code-blocks'])
  21. screen.open('/')
  22. screen.wait(0.5) # wait for Mermaid to render
  23. screen.should_contain('Mermaid')
  24. assert screen.find_by_tag('svg').get_attribute('id').startswith('mermaid-')
  25. assert screen.find('Node_A').get_attribute('class') == 'nodeLabel'
  26. m.set_content('''
  27. New:
  28. ```mermaid
  29. graph TD;
  30. Node_C --> Node_D;
  31. ```
  32. ''')
  33. screen.should_contain('New')
  34. assert screen.find('Node_C').get_attribute('class') == 'nodeLabel'
  35. screen.should_not_contain('Node_A')
  36. def test_strip_indentation(screen: Screen):
  37. ui.markdown('''
  38. **This is Markdown.**
  39. ''')
  40. screen.open('/')
  41. screen.should_contain('This is Markdown.')
  42. screen.should_not_contain('**This is Markdown.**') # NOTE: '**' are translated to formatting and not visible
  43. def test_replace_markdown(screen: Screen):
  44. with ui.row() as container:
  45. ui.markdown('A')
  46. def replace():
  47. container.clear()
  48. with container:
  49. ui.markdown('B')
  50. ui.button('Replace', on_click=replace)
  51. screen.open('/')
  52. screen.should_contain('A')
  53. screen.click('Replace')
  54. screen.should_contain('B')
  55. screen.should_not_contain('A')