test_table.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from nicegui import ui
  2. from .screen import Screen
  3. def test_update_table(screen: Screen):
  4. table = ui.table({
  5. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  6. 'rowData': [{'name': 'Alice', 'age': 18}],
  7. })
  8. screen.open('/')
  9. screen.should_contain('Name')
  10. screen.should_contain('Age')
  11. screen.should_contain('Alice')
  12. screen.should_contain('18')
  13. table.options['rowData'][0]['age'] = 42
  14. table.update()
  15. screen.should_contain('42')
  16. def test_add_row(screen: Screen):
  17. table = ui.table({
  18. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  19. 'rowData': [],
  20. })
  21. ui.button('Update', on_click=table.update)
  22. screen.open('/')
  23. table.options['rowData'].append({'name': 'Alice', 'age': 18})
  24. screen.click('Update')
  25. screen.should_contain('Alice')
  26. screen.should_contain('18')
  27. table.options['rowData'].append({'name': 'Bob', 'age': 21})
  28. screen.click('Update')
  29. screen.should_contain('Alice')
  30. screen.should_contain('18')
  31. screen.should_contain('Bob')
  32. screen.should_contain('21')
  33. def test_click_cell(screen: Screen):
  34. table = ui.table({
  35. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  36. 'rowData': [{'name': 'Alice', 'age': 18}],
  37. })
  38. table.on('cellClicked', lambda msg: ui.label(f'{msg["args"]["data"]["name"]} has been clicked!'))
  39. screen.open('/')
  40. screen.click('Alice')
  41. screen.should_contain('Alice has been clicked!')