test_uploads.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import pytest
  2. import reflex as rx
  3. @pytest.fixture
  4. def upload_component():
  5. """A test upload component function.
  6. Returns:
  7. A test upload component function.
  8. """
  9. def upload_component():
  10. return rx.upload(
  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_component()
  16. @pytest.fixture
  17. def upload_component_with_props():
  18. """A test upload component with props function.
  19. Returns:
  20. A test upload component with props function.
  21. """
  22. def upload_component_with_props():
  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. no_drag=True,
  28. max_files=2,
  29. )
  30. return upload_component_with_props()
  31. def test_upload_component_render(upload_component):
  32. """Test that the render function is set correctly.
  33. Args:
  34. upload_component: component fixture
  35. """
  36. upload = upload_component.render()
  37. # upload
  38. assert upload["name"] == "ReactDropzone"
  39. assert upload["props"] == [
  40. "id={`default`}",
  41. "multiple={true}",
  42. "onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
  43. "ref={ref_default}",
  44. ]
  45. assert upload["args"] == ("getRootProps", "getInputProps")
  46. # box inside of upload
  47. [box] = upload["children"]
  48. assert box["name"] == "Box"
  49. assert box["props"] == [
  50. 'sx={{"border": "1px dotted black"}}',
  51. "{...getRootProps()}",
  52. ]
  53. # input, button and text inside of box
  54. [input, button, text] = box["children"]
  55. assert input["name"] == "Input"
  56. assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
  57. assert button["name"] == "RadixThemesButton"
  58. assert button["children"][0]["contents"] == "{`select file`}"
  59. assert text["name"] == "RadixThemesText"
  60. assert (
  61. text["children"][0]["contents"]
  62. == "{`Drag and drop files here or click to select files`}"
  63. )
  64. def test_upload_component_with_props_render(upload_component_with_props):
  65. """Test that the render function is set correctly.
  66. Args:
  67. upload_component_with_props: component fixture
  68. """
  69. upload = upload_component_with_props.render()
  70. assert upload["props"] == [
  71. "id={`default`}",
  72. "maxFiles={2}",
  73. "multiple={true}",
  74. "noDrag={true}",
  75. "onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
  76. "ref={ref_default}",
  77. ]