test_binding.py 2.8 KB

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