test_uploads.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_id_special():
  31. def upload_component():
  32. return rx.upload(
  33. rx.button("select file"),
  34. rx.text("Drag and drop files here or click to select files"),
  35. border="1px dotted black",
  36. id="#spec!`al-_98ID",
  37. )
  38. return upload_component()
  39. @pytest.fixture
  40. def upload_component_with_props():
  41. """A test upload component with props function.
  42. Returns:
  43. A test upload component with props function.
  44. """
  45. def upload_component_with_props():
  46. return rx.upload(
  47. rx.button("select file"),
  48. rx.text("Drag and drop files here or click to select files"),
  49. border="1px dotted black",
  50. no_drag=True,
  51. max_files=2,
  52. )
  53. return upload_component_with_props()
  54. def test_upload_root_component_render(upload_root_component):
  55. """Test that the render function is set correctly.
  56. Args:
  57. upload_root_component: component fixture
  58. """
  59. upload = upload_root_component.render()
  60. # upload
  61. assert upload["name"] == "ReactDropzone"
  62. assert upload["props"] == [
  63. "id={`default`}",
  64. "multiple={true}",
  65. "onDrop={e => setFilesById(filesById => {\n"
  66. " const updatedFilesById = Object.assign({}, filesById);\n"
  67. " updatedFilesById[`default`] = e;\n"
  68. " return updatedFilesById;\n"
  69. " })\n"
  70. " }",
  71. "ref={ref_default}",
  72. ]
  73. assert upload["args"] == ("getRootProps", "getInputProps")
  74. # box inside of upload
  75. [box] = upload["children"]
  76. assert box["name"] == "RadixThemesBox"
  77. assert box["props"] == [
  78. "className={`rx-Upload`}",
  79. 'css={{"border": "1px dotted black"}}',
  80. "{...getRootProps()}",
  81. ]
  82. # input, button and text inside of box
  83. [input, button, text] = box["children"]
  84. assert input["name"] == "input"
  85. assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
  86. assert button["name"] == "RadixThemesButton"
  87. assert button["children"][0]["contents"] == "{`select file`}"
  88. assert text["name"] == "RadixThemesText"
  89. assert (
  90. text["children"][0]["contents"]
  91. == "{`Drag and drop files here or click to select files`}"
  92. )
  93. def test_upload_component_render(upload_component):
  94. """Test that the render function is set correctly.
  95. Args:
  96. upload_component: component fixture
  97. """
  98. upload = upload_component.render()
  99. # upload
  100. assert upload["name"] == "ReactDropzone"
  101. assert upload["props"] == [
  102. "id={`default`}",
  103. "multiple={true}",
  104. "onDrop={e => setFilesById(filesById => {\n"
  105. " const updatedFilesById = Object.assign({}, filesById);\n"
  106. " updatedFilesById[`default`] = e;\n"
  107. " return updatedFilesById;\n"
  108. " })\n"
  109. " }",
  110. "ref={ref_default}",
  111. ]
  112. assert upload["args"] == ("getRootProps", "getInputProps")
  113. # box inside of upload
  114. [box] = upload["children"]
  115. assert box["name"] == "RadixThemesBox"
  116. assert box["props"] == [
  117. "className={`rx-Upload`}",
  118. 'css={{"border": "1px dotted black", "padding": "5em", "textAlign": "center"}}',
  119. "{...getRootProps()}",
  120. ]
  121. # input, button and text inside of box
  122. [input, button, text] = box["children"]
  123. assert input["name"] == "input"
  124. assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
  125. assert button["name"] == "RadixThemesButton"
  126. assert button["children"][0]["contents"] == "{`select file`}"
  127. assert text["name"] == "RadixThemesText"
  128. assert (
  129. text["children"][0]["contents"]
  130. == "{`Drag and drop files here or click to select files`}"
  131. )
  132. def test_upload_component_with_props_render(upload_component_with_props):
  133. """Test that the render function is set correctly.
  134. Args:
  135. upload_component_with_props: component fixture
  136. """
  137. upload = upload_component_with_props.render()
  138. assert upload["props"] == [
  139. "id={`default`}",
  140. "maxFiles={2}",
  141. "multiple={true}",
  142. "noDrag={true}",
  143. "onDrop={e => setFilesById(filesById => {\n"
  144. " const updatedFilesById = Object.assign({}, filesById);\n"
  145. " updatedFilesById[`default`] = e;\n"
  146. " return updatedFilesById;\n"
  147. " })\n"
  148. " }",
  149. "ref={ref_default}",
  150. ]
  151. def test_upload_component_id_with_special_chars(upload_component_id_special):
  152. upload = upload_component_id_special.render()
  153. assert upload["props"] == [
  154. r"id={`#spec!\`al-_98ID`}",
  155. "multiple={true}",
  156. "onDrop={e => setFilesById(filesById => {\n"
  157. " const updatedFilesById = Object.assign({}, filesById);\n"
  158. " updatedFilesById[`#spec!\\`al-_98ID`] = e;\n"
  159. " return updatedFilesById;\n"
  160. " })\n"
  161. " }",
  162. "ref={ref__spec_al__98ID}",
  163. ]