test_uploads.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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"] == "RadixThemesBox"
  62. assert box["props"] == [
  63. "className={`rx-Upload`}",
  64. 'css={{"border": "1px dotted black"}}',
  65. "{...getRootProps()}",
  66. ]
  67. # input, button and text inside of box
  68. [input, button, text] = box["children"]
  69. assert input["name"] == "input"
  70. assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
  71. assert button["name"] == "RadixThemesButton"
  72. assert button["children"][0]["contents"] == "{`select file`}"
  73. assert text["name"] == "RadixThemesText"
  74. assert (
  75. text["children"][0]["contents"]
  76. == "{`Drag and drop files here or click to select files`}"
  77. )
  78. def test_upload_component_render(upload_component):
  79. """Test that the render function is set correctly.
  80. Args:
  81. upload_component: component fixture
  82. """
  83. upload = upload_component.render()
  84. # upload
  85. assert upload["name"] == "ReactDropzone"
  86. assert upload["props"] == [
  87. "id={`default`}",
  88. "multiple={true}",
  89. "onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
  90. "ref={ref_default}",
  91. ]
  92. assert upload["args"] == ("getRootProps", "getInputProps")
  93. # box inside of upload
  94. [box] = upload["children"]
  95. assert box["name"] == "RadixThemesBox"
  96. assert box["props"] == [
  97. "className={`rx-Upload`}",
  98. 'css={{"border": "1px dotted black", "padding": "5em", "textAlign": "center"}}',
  99. "{...getRootProps()}",
  100. ]
  101. # input, button and text inside of box
  102. [input, button, text] = box["children"]
  103. assert input["name"] == "input"
  104. assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
  105. assert button["name"] == "RadixThemesButton"
  106. assert button["children"][0]["contents"] == "{`select file`}"
  107. assert text["name"] == "RadixThemesText"
  108. assert (
  109. text["children"][0]["contents"]
  110. == "{`Drag and drop files here or click to select files`}"
  111. )
  112. def test_upload_component_with_props_render(upload_component_with_props):
  113. """Test that the render function is set correctly.
  114. Args:
  115. upload_component_with_props: component fixture
  116. """
  117. upload = upload_component_with_props.render()
  118. assert upload["props"] == [
  119. "id={`default`}",
  120. "maxFiles={2}",
  121. "multiple={true}",
  122. "noDrag={true}",
  123. "onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
  124. "ref={ref_default}",
  125. ]