test_echart.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from nicegui import ui
  2. from nicegui.testing import Screen
  3. def test_create_dynamically(screen: Screen):
  4. def create():
  5. ui.echart({
  6. 'xAxis': {'type': 'value'},
  7. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  8. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  9. })
  10. ui.button('Create', on_click=create)
  11. screen.open('/')
  12. screen.click('Create')
  13. assert screen.find_by_tag('canvas')
  14. def test_update(screen: Screen):
  15. def update():
  16. chart.options['xAxis'] = {'type': 'value'}
  17. chart.options['yAxis'] = {'type': 'category', 'data': ['A', 'B', 'C']}
  18. chart.options['series'] = [{'type': 'line', 'data': [0.1, 0.2, 0.3]}]
  19. chart.update()
  20. chart = ui.echart({})
  21. ui.button('Update', on_click=update)
  22. screen.open('/')
  23. assert not screen.find_all_by_tag('canvas')
  24. screen.click('Update')
  25. assert screen.find_by_tag('canvas')
  26. def test_nested_card(screen: Screen):
  27. with ui.card().style('height: 200px; width: 600px'):
  28. ui.echart({
  29. 'xAxis': {'type': 'value'},
  30. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  31. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  32. })
  33. screen.open('/')
  34. canvas = screen.find_by_tag('canvas')
  35. assert canvas.rect['height'] == 168
  36. assert canvas.rect['width'] == 568
  37. def test_nested_expansion(screen: Screen):
  38. with ui.expansion() as expansion:
  39. with ui.card().style('height: 200px; width: 600px'):
  40. ui.echart({
  41. 'xAxis': {'type': 'value'},
  42. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  43. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  44. })
  45. ui.button('Open', on_click=expansion.open)
  46. screen.open('/')
  47. screen.click('Open')
  48. canvas = screen.find_by_tag('canvas')
  49. assert canvas.rect['height'] == 168
  50. assert canvas.rect['width'] == 568
  51. def test_run_method(screen: Screen):
  52. echart = ui.echart({
  53. 'xAxis': {'type': 'value'},
  54. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  55. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  56. }).classes('w-[600px]')
  57. async def get_width():
  58. ui.label(f'Width: {await echart.run_chart_method("getWidth")}px')
  59. ui.button('Get Width', on_click=get_width)
  60. screen.open('/')
  61. screen.click('Get Width')
  62. screen.should_contain('Width: 600px')