|
| 1 | +from dash import Dash, Input, Output, dcc, html |
| 2 | + |
| 3 | + |
| 4 | +def test_upfd001_folder_upload_with_enable_folder_selection(dash_dcc): |
| 5 | + """ |
| 6 | + Test that folder upload is enabled when enable_folder_selection=True. |
| 7 | +
|
| 8 | + Note: Full end-to-end testing of folder upload functionality is limited |
| 9 | + by Selenium's capabilities. This test verifies the component renders |
| 10 | + correctly with enable_folder_selection=True which enables folder support. |
| 11 | + """ |
| 12 | + app = Dash(__name__) |
| 13 | + |
| 14 | + app.layout = html.Div( |
| 15 | + [ |
| 16 | + html.Div("Folder Upload Test", id="title"), |
| 17 | + dcc.Upload( |
| 18 | + id="upload-folder", |
| 19 | + children=html.Div( |
| 20 | + ["Drag and Drop or ", html.A("Select Folder")] |
| 21 | + ), |
| 22 | + style={ |
| 23 | + "width": "100%", |
| 24 | + "height": "60px", |
| 25 | + "lineHeight": "60px", |
| 26 | + "borderWidth": "1px", |
| 27 | + "borderStyle": "dashed", |
| 28 | + "borderRadius": "5px", |
| 29 | + "textAlign": "center", |
| 30 | + }, |
| 31 | + multiple=True, |
| 32 | + enable_folder_selection=True, # Enables folder selection |
| 33 | + accept=".txt,.csv", # Test accept filtering |
| 34 | + ), |
| 35 | + html.Div(id="output"), |
| 36 | + ] |
| 37 | + ) |
| 38 | + |
| 39 | + @app.callback( |
| 40 | + Output("output", "children"), |
| 41 | + [Input("upload-folder", "contents")], |
| 42 | + ) |
| 43 | + def update_output(contents_list): |
| 44 | + if contents_list is not None: |
| 45 | + return html.Div(f"Uploaded {len(contents_list)} file(s)", id="file-count") |
| 46 | + return html.Div("No files uploaded") |
| 47 | + |
| 48 | + dash_dcc.start_server(app) |
| 49 | + |
| 50 | + # Verify the component renders |
| 51 | + dash_dcc.wait_for_text_to_equal("#title", "Folder Upload Test") |
| 52 | + |
| 53 | + # Verify the upload component and input are present |
| 54 | + dash_dcc.wait_for_element("#upload-folder") |
| 55 | + |
| 56 | + # Verify the input has folder selection attributes when enable_folder_selection=True |
| 57 | + upload_input = dash_dcc.wait_for_element("#upload-folder input[type=file]") |
| 58 | + webkitdir_attr = upload_input.get_attribute("webkitdirectory") |
| 59 | + |
| 60 | + assert webkitdir_attr == "true", ( |
| 61 | + f"webkitdirectory attribute should be 'true' when enable_folder_selection=True, " |
| 62 | + f"but got '{webkitdir_attr}'" |
| 63 | + ) |
| 64 | + |
| 65 | + assert dash_dcc.get_logs() == [], "browser console should contain no error" |
| 66 | + |
| 67 | + |
| 68 | +def test_upfd002_multiple_files_without_folder_selection(dash_dcc): |
| 69 | + """ |
| 70 | + Test that multiple file upload does NOT enable folder selection |
| 71 | + when enable_folder_selection=False (default). |
| 72 | + """ |
| 73 | + app = Dash(__name__) |
| 74 | + |
| 75 | + app.layout = html.Div( |
| 76 | + [ |
| 77 | + html.Div("Multiple Files Test", id="title"), |
| 78 | + dcc.Upload( |
| 79 | + id="upload-multiple", |
| 80 | + children=html.Div(["Drag and Drop or ", html.A("Select Multiple Files")]), |
| 81 | + style={ |
| 82 | + "width": "100%", |
| 83 | + "height": "60px", |
| 84 | + "lineHeight": "60px", |
| 85 | + "borderWidth": "1px", |
| 86 | + "borderStyle": "dashed", |
| 87 | + "borderRadius": "5px", |
| 88 | + "textAlign": "center", |
| 89 | + }, |
| 90 | + multiple=True, # Allows multiple files |
| 91 | + enable_folder_selection=False, # But NOT folder selection |
| 92 | + accept=".txt,.csv", # Accept should work with file picker |
| 93 | + ), |
| 94 | + html.Div(id="output", children="Upload ready"), |
| 95 | + ] |
| 96 | + ) |
| 97 | + |
| 98 | + dash_dcc.start_server(app) |
| 99 | + |
| 100 | + # Wait for the component to render |
| 101 | + dash_dcc.wait_for_text_to_equal("#title", "Multiple Files Test") |
| 102 | + dash_dcc.wait_for_text_to_equal("#output", "Upload ready") |
| 103 | + |
| 104 | + # Verify the input does NOT have folder selection attributes |
| 105 | + upload_input = dash_dcc.wait_for_element("#upload-multiple input[type=file]") |
| 106 | + webkitdir_attr = upload_input.get_attribute("webkitdirectory") |
| 107 | + |
| 108 | + # webkitdirectory should not be set when enable_folder_selection=False |
| 109 | + assert webkitdir_attr in [None, "", "false"], ( |
| 110 | + f"webkitdirectory attribute should not be 'true' when enable_folder_selection=False, " |
| 111 | + f"but got '{webkitdir_attr}'" |
| 112 | + ) |
| 113 | + |
| 114 | + # Verify multiple attribute is set |
| 115 | + multiple_attr = upload_input.get_attribute("multiple") |
| 116 | + assert multiple_attr == "true", ( |
| 117 | + f"multiple attribute should be 'true' when multiple=True, " |
| 118 | + f"but got '{multiple_attr}'" |
| 119 | + ) |
| 120 | + |
| 121 | + assert dash_dcc.get_logs() == [], "browser console should contain no error" |
| 122 | + |
| 123 | + |
| 124 | +def test_upfd003_single_file_upload(dash_dcc): |
| 125 | + """ |
| 126 | + Test that single file upload does NOT enable folder selection. |
| 127 | + """ |
| 128 | + app = Dash(__name__) |
| 129 | + |
| 130 | + app.layout = html.Div( |
| 131 | + [ |
| 132 | + html.Div("Single File Test", id="title"), |
| 133 | + dcc.Upload( |
| 134 | + id="upload-single", |
| 135 | + children=html.Div(["Drag and Drop or ", html.A("Select File")]), |
| 136 | + style={ |
| 137 | + "width": "100%", |
| 138 | + "height": "60px", |
| 139 | + "lineHeight": "60px", |
| 140 | + "borderWidth": "1px", |
| 141 | + "borderStyle": "dashed", |
| 142 | + "borderRadius": "5px", |
| 143 | + "textAlign": "center", |
| 144 | + }, |
| 145 | + multiple=False, # Single file only |
| 146 | + accept="application/pdf", |
| 147 | + ), |
| 148 | + html.Div(id="output", children="Upload ready"), |
| 149 | + ] |
| 150 | + ) |
| 151 | + |
| 152 | + dash_dcc.start_server(app) |
| 153 | + |
| 154 | + # Wait for the component to render |
| 155 | + dash_dcc.wait_for_text_to_equal("#title", "Single File Test") |
| 156 | + dash_dcc.wait_for_text_to_equal("#output", "Upload ready") |
| 157 | + |
| 158 | + # Verify the input does NOT have folder selection attributes when multiple=False |
| 159 | + upload_input = dash_dcc.wait_for_element("#upload-single input[type=file]") |
| 160 | + webkitdir_attr = upload_input.get_attribute("webkitdirectory") |
| 161 | + |
| 162 | + # webkitdirectory should not be set when multiple=False |
| 163 | + assert webkitdir_attr in [None, "", "false"], ( |
| 164 | + f"webkitdirectory attribute should not be 'true' when multiple=False, " |
| 165 | + f"but got '{webkitdir_attr}'" |
| 166 | + ) |
| 167 | + |
| 168 | + assert dash_dcc.get_logs() == [], "browser console should contain no error" |
0 commit comments