test_table.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from typing import List
  2. from selenium.webdriver.common.by import By
  3. from nicegui import ui
  4. from .screen import Screen
  5. def columns() -> List:
  6. return [
  7. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True},
  8. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  9. ]
  10. def rows() -> List:
  11. return [
  12. {'id': 0, 'name': 'Alice', 'age': 18},
  13. {'id': 1, 'name': 'Bob', 'age': 21},
  14. {'id': 2, 'name': 'Lionel', 'age': 19},
  15. ]
  16. def test_table(screen: Screen):
  17. ui.table(title='My Team', columns=columns(), rows=rows())
  18. screen.open('/')
  19. screen.should_contain('My Team')
  20. screen.should_contain('Name')
  21. screen.should_contain('Alice')
  22. screen.should_contain('Bob')
  23. screen.should_contain('Lionel')
  24. def test_pagination(screen: Screen):
  25. ui.table(columns=columns(), rows=rows(), pagination=2)
  26. screen.open('/')
  27. screen.should_contain('Alice')
  28. screen.should_contain('Bob')
  29. screen.should_not_contain('Lionel')
  30. screen.should_contain('1-2 of 3')
  31. def test_filter(screen: Screen):
  32. table = ui.table(columns=columns(), rows=rows())
  33. ui.input('Search by name').bind_value(table, 'filter')
  34. screen.open('/')
  35. screen.should_contain('Alice')
  36. screen.should_contain('Bob')
  37. screen.should_contain('Lionel')
  38. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Search by name"]')
  39. element.send_keys('e')
  40. screen.should_contain('Alice')
  41. screen.should_not_contain('Bob')
  42. screen.should_contain('Lionel')
  43. def test_add_remove(screen: Screen):
  44. table = ui.table(columns=columns(), rows=rows())
  45. ui.button('Add', on_click=lambda: table.add_rows({'id': 3, 'name': 'Carol', 'age': 32}))
  46. ui.button('Remove', on_click=lambda: table.remove_rows(table.rows[0]))
  47. screen.open('/')
  48. screen.click('Add')
  49. screen.should_contain('Carol')
  50. screen.click('Remove')
  51. screen.wait(0.5)
  52. screen.should_not_contain('Alice')
  53. def test_slots(screen: Screen):
  54. with ui.table(columns=columns(), rows=rows()) as table:
  55. with table.add_slot('top-row'):
  56. with table.row():
  57. with table.cell():
  58. ui.label('This is the top slot.')
  59. table.add_slot('body', '''
  60. <q-tr :props="props">
  61. <q-td key="name" :props="props">overridden</q-td>
  62. <q-td key="age" :props="props">
  63. <q-badge color="green">{{ props.row.age }}</q-badge>
  64. </q-td>
  65. </q-tr>
  66. ''')
  67. screen.open('/')
  68. screen.should_contain('This is the top slot.')
  69. screen.should_not_contain('Alice')
  70. screen.should_contain('overridden')
  71. screen.should_contain('21')
  72. def test_single_selection(screen: Screen):
  73. ui.table(columns=columns(), rows=rows(), selection='single')
  74. screen.open('/')
  75. screen.find('Alice').find_element(By.XPATH, 'preceding-sibling::td').click()
  76. screen.wait(0.5)
  77. screen.should_contain('1 record selected.')
  78. screen.find('Bob').find_element(By.XPATH, 'preceding-sibling::td').click()
  79. screen.wait(0.5)
  80. screen.should_contain('1 record selected.')
  81. def test_dynamic_column_attributes(screen: Screen):
  82. ui.table(columns=[{'name': 'age', 'label': 'Age', 'field': 'age', ':format': 'value => value + " years"'}],
  83. rows=[{'name': 'Alice', 'age': 18}])
  84. screen.open('/')
  85. screen.should_contain('18 years')
  86. def test_remove_selection(screen: Screen):
  87. t = ui.table(columns=columns(), rows=rows(), selection='single')
  88. ui.button('Remove first row', on_click=lambda: t.remove_rows(t.rows[0]))
  89. screen.open('/')
  90. screen.find('Alice').find_element(By.XPATH, 'preceding-sibling::td').click()
  91. screen.should_contain('1 record selected.')
  92. screen.click('Remove first row')
  93. screen.wait(0.5)
  94. screen.should_not_contain('Alice')
  95. screen.should_not_contain('1 record selected.')