reflex/tests/components/forms/test_uploads.py
Elijah Ahianyo 34bf25071a
[REF-2821]Improve Dynamic Imports (#3345)
* Improve import times

* add lazy loading to rx.el

* add lazy loading to reflex core components

* minor refactor

* Get imports working with reflex web

* get imports to work with all reflex examples

* refactor to define imports only in the root.

* lint

* deadcode remove

* update poetry deps

* unit tests fix

* app_harness fix

* app_harness fix

* pyi file generate

* pyi file generate

* sort pyi order

* fix pyi

* fix docker ci

* rework pyi-generator

* generate pyi for __init__ files

* test pyright

* test pyright ci

* partial pyright fix

* more pyright fix

* pyright fix

* fix pyi_generator

* add rx.serializer and others

* add future annotation import which fixes container CI, then also load recharts lazily

* add new pyi files

* pyright fix

* minor fixes for reflex-web and flexdown

* forward references for py38

* ruff fix

* pyi fix

* unit tests fix

* reduce coverage to 68%

* reduce coverage to 67%

* reduce coverage to 66%as a workaround to coverage's rounding issue

* reduce coverage to 66%as a workaround to coverage's rounding issue

* exclude lazy_loader dependency review checks.

* its lazy-loader

* Add docstrings and regenerate pyi files

* add link

* address Pr comments

* CI fix

* partially address PR comments.

* edit docstrings and fix integration tests

* fix typo in docstring

* pyi fix
2024-05-31 16:43:10 +00:00

162 lines
4.3 KiB
Python

import pytest
import reflex as rx
@pytest.fixture
def upload_root_component():
"""A test upload component function.
Returns:
A test upload component function.
"""
def upload_root_component():
return rx.upload.root(
rx.button("select file"),
rx.text("Drag and drop files here or click to select files"),
border="1px dotted black",
)
return upload_root_component()
@pytest.fixture
def upload_component():
"""A test upload component function.
Returns:
A test upload component function.
"""
def upload_component():
return rx.upload(
rx.button("select file"),
rx.text("Drag and drop files here or click to select files"),
border="1px dotted black",
)
return upload_component()
@pytest.fixture
def upload_component_with_props():
"""A test upload component with props function.
Returns:
A test upload component with props function.
"""
def upload_component_with_props():
return rx.upload(
rx.button("select file"),
rx.text("Drag and drop files here or click to select files"),
border="1px dotted black",
no_drag=True,
max_files=2,
)
return upload_component_with_props()
def test_upload_root_component_render(upload_root_component):
"""Test that the render function is set correctly.
Args:
upload_root_component: component fixture
"""
upload = upload_root_component.render()
# upload
assert upload["name"] == "ReactDropzone"
assert upload["props"] == [
"id={`default`}",
"multiple={true}",
"onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
"ref={ref_default}",
]
assert upload["args"] == ("getRootProps", "getInputProps")
# box inside of upload
[box] = upload["children"]
assert box["name"] == "RadixThemesBox"
assert box["props"] == [
"className={`rx-Upload`}",
'css={{"border": "1px dotted black"}}',
"{...getRootProps()}",
]
# input, button and text inside of box
[input, button, text] = box["children"]
assert input["name"] == "input"
assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
assert button["name"] == "RadixThemesButton"
assert button["children"][0]["contents"] == "{`select file`}"
assert text["name"] == "RadixThemesText"
assert (
text["children"][0]["contents"]
== "{`Drag and drop files here or click to select files`}"
)
def test_upload_component_render(upload_component):
"""Test that the render function is set correctly.
Args:
upload_component: component fixture
"""
upload = upload_component.render()
# upload
assert upload["name"] == "ReactDropzone"
assert upload["props"] == [
"id={`default`}",
"multiple={true}",
"onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
"ref={ref_default}",
]
assert upload["args"] == ("getRootProps", "getInputProps")
# box inside of upload
[box] = upload["children"]
assert box["name"] == "RadixThemesBox"
assert box["props"] == [
"className={`rx-Upload`}",
'css={{"border": "1px dotted black", "padding": "5em", "textAlign": "center"}}',
"{...getRootProps()}",
]
# input, button and text inside of box
[input, button, text] = box["children"]
assert input["name"] == "input"
assert input["props"] == ["type={`file`}", "{...getInputProps()}"]
assert button["name"] == "RadixThemesButton"
assert button["children"][0]["contents"] == "{`select file`}"
assert text["name"] == "RadixThemesText"
assert (
text["children"][0]["contents"]
== "{`Drag and drop files here or click to select files`}"
)
def test_upload_component_with_props_render(upload_component_with_props):
"""Test that the render function is set correctly.
Args:
upload_component_with_props: component fixture
"""
upload = upload_component_with_props.render()
assert upload["props"] == [
"id={`default`}",
"maxFiles={2}",
"multiple={true}",
"noDrag={true}",
"onDrop={e => setFilesById(filesById => ({...filesById, default: e}))}",
"ref={ref_default}",
]