test_binding.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from selenium.webdriver.common.keys import Keys
  2. from nicegui import ui
  3. from nicegui.testing import Screen
  4. def test_ui_select_with_tuple_as_key(screen: Screen):
  5. class Model:
  6. selection = None
  7. data = Model()
  8. options = {
  9. (2, 1): 'option A',
  10. (1, 2): 'option B',
  11. }
  12. data.selection = list(options.keys())[0]
  13. ui.select(options).bind_value(data, 'selection')
  14. screen.open('/')
  15. screen.should_not_contain('option B')
  16. element = screen.click('option A')
  17. screen.click_at_position(element, x=20, y=100)
  18. screen.wait(0.3)
  19. screen.should_contain('option B')
  20. screen.should_not_contain('option A')
  21. assert data.selection == (1, 2)
  22. def test_ui_select_with_list_of_tuples(screen: Screen):
  23. class Model:
  24. selection = None
  25. data = Model()
  26. options = [(1, 1), (2, 2), (3, 3)]
  27. data.selection = options[0]
  28. ui.select(options).bind_value(data, 'selection')
  29. screen.open('/')
  30. screen.should_not_contain('2,2')
  31. element = screen.click('1,1')
  32. screen.click_at_position(element, x=20, y=100)
  33. screen.wait(0.3)
  34. screen.should_contain('2,2')
  35. screen.should_not_contain('1,1')
  36. assert data.selection == (2, 2)
  37. def test_ui_select_with_list_of_lists(screen: Screen):
  38. class Model:
  39. selection = None
  40. data = Model()
  41. options = [[1, 1], [2, 2], [3, 3]]
  42. data.selection = options[0]
  43. ui.select(options).bind_value(data, 'selection')
  44. screen.open('/')
  45. screen.should_not_contain('2,2')
  46. element = screen.click('1,1')
  47. screen.click_at_position(element, x=20, y=100)
  48. screen.wait(0.3)
  49. screen.should_contain('2,2')
  50. screen.should_not_contain('1,1')
  51. assert data.selection == [2, 2]
  52. def test_binding_to_input(screen: Screen):
  53. class Model:
  54. text = 'one'
  55. data = Model()
  56. element = ui.input().bind_value(data, 'text')
  57. screen.open('/')
  58. screen.should_contain_input('one')
  59. screen.type(Keys.TAB)
  60. screen.type('two')
  61. screen.should_contain_input('two')
  62. assert data.text == 'two'
  63. data.text = 'three'
  64. screen.should_contain_input('three')
  65. element.set_value('four')
  66. screen.should_contain_input('four')
  67. assert data.text == 'four'
  68. element.value = 'five'
  69. screen.should_contain_input('five')
  70. assert data.text == 'five'
  71. def test_binding_refresh_before_page_delivery(screen: Screen):
  72. state = {'count': 0}
  73. @ui.page('/')
  74. def main_page() -> None:
  75. ui.label().bind_text_from(state, 'count')
  76. state['count'] += 1
  77. screen.open('/')
  78. screen.should_contain('1')