test_table.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_int(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_pagination_dict(screen: Screen):
  32. ui.table(columns=columns(), rows=rows(), pagination={'rowsPerPage': 2})
  33. screen.open('/')
  34. screen.should_contain('Alice')
  35. screen.should_contain('Bob')
  36. screen.should_not_contain('Lionel')
  37. screen.should_contain('1-2 of 3')
  38. def test_filter(screen: Screen):
  39. table = ui.table(columns=columns(), rows=rows())
  40. ui.input('Search by name').bind_value(table, 'filter')
  41. screen.open('/')
  42. screen.should_contain('Alice')
  43. screen.should_contain('Bob')
  44. screen.should_contain('Lionel')
  45. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Search by name"]')
  46. element.send_keys('e')
  47. screen.should_contain('Alice')
  48. screen.should_not_contain('Bob')
  49. screen.should_contain('Lionel')
  50. def test_add_remove(screen: Screen):
  51. table = ui.table(columns=columns(), rows=rows())
  52. ui.button('Add', on_click=lambda: table.add_rows({'id': 3, 'name': 'Carol', 'age': 32}))
  53. ui.button('Remove', on_click=lambda: table.remove_rows(table.rows[0]))
  54. screen.open('/')
  55. screen.click('Add')
  56. screen.should_contain('Carol')
  57. screen.click('Remove')
  58. screen.wait(0.5)
  59. screen.should_not_contain('Alice')
  60. def test_slots(screen: Screen):
  61. with ui.table(columns=columns(), rows=rows()) as table:
  62. with table.add_slot('top-row'):
  63. with table.row():
  64. with table.cell():
  65. ui.label('This is the top slot.')
  66. table.add_slot('body', '''
  67. <q-tr :props="props">
  68. <q-td key="name" :props="props">overridden</q-td>
  69. <q-td key="age" :props="props">
  70. <q-badge color="green">{{ props.row.age }}</q-badge>
  71. </q-td>
  72. </q-tr>
  73. ''')
  74. screen.open('/')
  75. screen.should_contain('This is the top slot.')
  76. screen.should_not_contain('Alice')
  77. screen.should_contain('overridden')
  78. screen.should_contain('21')
  79. def test_single_selection(screen: Screen):
  80. ui.table(columns=columns(), rows=rows(), selection='single')
  81. screen.open('/')
  82. screen.find('Alice').find_element(By.XPATH, 'preceding-sibling::td').click()
  83. screen.wait(0.5)
  84. screen.should_contain('1 record selected.')
  85. screen.find('Bob').find_element(By.XPATH, 'preceding-sibling::td').click()
  86. screen.wait(0.5)
  87. screen.should_contain('1 record selected.')
  88. def test_dynamic_column_attributes(screen: Screen):
  89. ui.table(columns=[{'name': 'age', 'label': 'Age', 'field': 'age', ':format': 'value => value + " years"'}],
  90. rows=[{'name': 'Alice', 'age': 18}])
  91. screen.open('/')
  92. screen.should_contain('18 years')
  93. def test_remove_selection(screen: Screen):
  94. t = ui.table(columns=columns(), rows=rows(), selection='single')
  95. ui.button('Remove first row', on_click=lambda: t.remove_rows(t.rows[0]))
  96. screen.open('/')
  97. screen.find('Alice').find_element(By.XPATH, 'preceding-sibling::td').click()
  98. screen.should_contain('1 record selected.')
  99. screen.click('Remove first row')
  100. screen.wait(0.5)
  101. screen.should_not_contain('Alice')
  102. screen.should_not_contain('1 record selected.')