1
0

test_uploads.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import pytest
  2. import reflex as rx
  3. @pytest.fixture
  4. def upload_root_component():
  5. """A test upload component function.
  6. Returns:
  7. A test upload component function.
  8. """
  9. def upload_root_component():
  10. return rx.upload.root(
  11. rx.button("select file"),
  12. rx.text("Drag and drop files here or click to select files"),
  13. border="1px dotted black",
  14. )
  15. return upload_root_component()
  16. @pytest.fixture
  17. def upload_component():
  18. """A test upload component function.
  19. Returns:
  20. A test upload component function.
  21. """
  22. def upload_component():
  23. return rx.upload(
  24. rx.button("select file"),
  25. rx.text("Drag and drop files here or click to select files"),
  26. border="1px dotted black",
  27. )
  28. return upload_component()
  29. @pytest.fixture
  30. def upload_component_with_props():
  31. """A test upload component with props function.
  32. Returns:
  33. A test upload component with props function.
  34. """
  35. def upload_component_with_props():
  36. return rx.upload(
  37. rx.button("select file"),
  38. rx.text("Drag and drop files here or click to select files"),
  39. border="1px dotted black",
  40. no_drag=True,
  41. max_files=2,
  42. )
  43. return upload_component_with_props()
  44. def test_upload_root_component_render(upload_root_component):
  45. """Test that the render function is set correctly.
  46. Args:
  47. upload_root_component: component fixture
  48. """
  49. upload = upload_root_component.render()
  50. # upload
  51. assert upload["name"] == "ReactDropzone"
  52. assert upload["props"] == [
  53. "id={`default`}",
  54. "multiple={true}",
  55. "onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
  56. "ref={ref_default}",
  57. ]
  58. assert upload["args"] == ("getRootProps", "getInputProps")
  59. # box inside of upload
  60. [box] = upload["children"]
  61. assert box["name"] == "Box"
  62. assert box["props"] == [
  63. 'sx={{"border": "1px dotted black"}}',
  64. "{...getRootProps()}",
  65. ]
  66. # input, button and text inside of box
  67. [input, button, text] = box["children"]
  68. assert input["name"] == "Input"
  69. assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
  70. assert button["name"] == "RadixThemesButton"
  71. assert button["children"][0]["contents"] == "{`select file`}"
  72. assert text["name"] == "RadixThemesText"
  73. assert (
  74. text["children"][0]["contents"]
  75. == "{`Drag and drop files here or click to select files`}"
  76. )
  77. def test_upload_component_render(upload_component):
  78. """Test that the render function is set correctly.
  79. Args:
  80. upload_component: component fixture
  81. """
  82. upload = upload_component.render()
  83. # upload
  84. assert upload["name"] == "ReactDropzone"
  85. assert upload["props"] == [
  86. "id={`default`}",
  87. "multiple={true}",
  88. "onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
  89. "ref={ref_default}",
  90. ]
  91. assert upload["args"] == ("getRootProps", "getInputProps")
  92. # box inside of upload
  93. [box] = upload["children"]
  94. assert box["name"] == "Box"
  95. assert box["props"] == [
  96. 'sx={{"border": "1px dotted black", "padding": "5em", "textAlign": "center"}}',
  97. "{...getRootProps()}",
  98. ]
  99. # input, button and text inside of box
  100. [input, button, text] = box["children"]
  101. assert input["name"] == "Input"
  102. assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
  103. assert button["name"] == "RadixThemesButton"
  104. assert button["children"][0]["contents"] == "{`select file`}"
  105. assert text["name"] == "RadixThemesText"
  106. assert (
  107. text["children"][0]["contents"]
  108. == "{`Drag and drop files here or click to select files`}"
  109. )
  110. def test_upload_component_with_props_render(upload_component_with_props):
  111. """Test that the render function is set correctly.
  112. Args:
  113. upload_component_with_props: component fixture
  114. """
  115. upload = upload_component_with_props.render()
  116. assert upload["props"] == [
  117. "id={`default`}",
  118. "maxFiles={2}",
  119. "multiple={true}",
  120. "noDrag={true}",
  121. "onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
  122. "ref={ref_default}",
  123. ]