-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add file input dialog #90
Changes from 9 commits
6d24bbe
379654e
8f8ddb8
d5724eb
f4cc39e
9437fa3
aae6520
5ea62b0
c761096
b68b3ea
d6386d3
aeef171
01e22d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,10 @@ import { | |
Tooltip, | ||
MenuItem, | ||
Stack, | ||
Modal, | ||
Paper, | ||
Card, | ||
CardContent, | ||
} from "@mui/material"; | ||
import DownloadJSONButton from "./DownloadJSONButton"; | ||
import MemoryModelsMenu from "./MemoryModelsMenu"; | ||
|
@@ -45,6 +49,10 @@ type MemoryModelsUserInputPropTypes = MemoryModelsFileInputPropTypes & | |
|
||
function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) { | ||
const [uploadedFileString, setUploadedFileString] = useState(""); | ||
const [open, setOpen] = useState(false); | ||
|
||
const handleOpen = () => setOpen(true); | ||
const handleClose = () => setOpen(false); | ||
|
||
const onChange = (event) => { | ||
try { | ||
|
@@ -54,7 +62,6 @@ function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) { | |
fileReader.onload = (event) => { | ||
const fileString = event.target.result as string; | ||
setUploadedFileString(fileString); | ||
props.setTextData(fileString); | ||
}; | ||
} catch (error) { | ||
const errorMessage = `Error reading uploaded file as text. Please ensure it's in UTF-8 encoding: ${error.message}`; | ||
|
@@ -66,29 +73,55 @@ function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) { | |
|
||
const onLoadButtonClick = () => { | ||
props.setTextData(uploadedFileString); | ||
setOpen(false); | ||
}; | ||
|
||
return ( | ||
<Stack direction={"row"} spacing={2}> | ||
<Input | ||
type="file" | ||
onChange={onChange} | ||
inputProps={{ | ||
accept: "application/JSON", | ||
"data-testid": "file-input", | ||
}} | ||
disableUnderline={true} | ||
/> | ||
<Button | ||
data-testid="file-input-reapply-button" | ||
variant="contained" | ||
disabled={!uploadedFileString} | ||
onClick={onLoadButtonClick} | ||
sx={{ textTransform: "none" }} | ||
> | ||
Load file data | ||
<div> | ||
<Button onClick={handleOpen} sx={{ textTransform: "none" }}> | ||
File Input | ||
</Button> | ||
</Stack> | ||
<Modal | ||
open={open} | ||
onClose={handleClose} | ||
data-testid="file-input-modal" | ||
> | ||
<Paper | ||
sx={{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why any of these custom styles are necessary, doesn't the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default styling for the modal was pretty much unusable: As far as I can tell, the MUI There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yoonieaj aha, thanks for clarifying. This prompted me to do some more digging in the MUI documentation, since this seemed counter-intuitive to me. If you look at the documentation for
and
So, I think we should be using |
||
position: "absolute", | ||
top: "40%", | ||
left: "20%", | ||
width: "50%", | ||
padding: 2, | ||
}} | ||
> | ||
<div> | ||
<Input | ||
type="file" | ||
onChange={onChange} | ||
inputProps={{ | ||
accept: "application/JSON", | ||
"data-testid": "file-input", | ||
}} | ||
disableUnderline={true} | ||
sx={{ alignSelf: "center" }} | ||
/> | ||
</div> | ||
<div> | ||
<Button | ||
data-testid="file-input-reapply-button" | ||
variant="contained" | ||
color="primary" | ||
disabled={!uploadedFileString} | ||
onClick={onLoadButtonClick} | ||
sx={{ textTransform: "none" }} | ||
> | ||
Load file data | ||
</Button> | ||
</div> | ||
</Paper> | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import React from "react"; | ||
import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
import { | ||
fireEvent, | ||
getByText, | ||
render, | ||
screen, | ||
waitFor, | ||
} from "@testing-library/react"; | ||
import MemoryModelsUserInput from "../MemoryModelsUserInput"; | ||
|
||
describe("MemoryModelsUserInput", () => { | ||
|
@@ -127,7 +133,16 @@ describe("MemoryModelsUserInput", () => { | |
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("does not render the modal when the page first loads", () => { | ||
const modal = screen.queryByTestId("file-input-modal"); | ||
expect(modal).toBeNull(); | ||
|
||
const input: HTMLInputElement = screen.queryByTestId("file-input"); | ||
expect(input).toBeNull(); | ||
}); | ||
|
||
it("renders an enabled input and disabled reapply button", () => { | ||
fireEvent.click(screen.getByText("File Input")); | ||
const input: HTMLInputElement = screen.getByTestId("file-input"); | ||
expect(input).toHaveProperty("disabled", false); | ||
|
||
|
@@ -149,6 +164,7 @@ describe("MemoryModelsUserInput", () => { | |
type: "application/json", | ||
} | ||
); | ||
fireEvent.click(screen.getByText("File Input")); | ||
const input: HTMLInputElement = screen.getByTestId("file-input"); | ||
await waitFor(() => { | ||
// this needs to be awaited because of fileReader.onload being async | ||
|
@@ -167,6 +183,7 @@ describe("MemoryModelsUserInput", () => { | |
let input: HTMLInputElement; | ||
|
||
beforeEach(async () => { | ||
fireEvent.click(screen.getByText("File Input")); | ||
const file = new File([fileString], "test.json", { | ||
type: "application/json", | ||
}); | ||
|
@@ -195,13 +212,9 @@ describe("MemoryModelsUserInput", () => { | |
}); | ||
|
||
await waitFor(() => { | ||
// once from reapplyBtn onChange, once from MemoryModelsTextInput handleTextFieldChange | ||
// if put within the same waitFor block as fireEvent.click(reapplyBtn), this test always passes | ||
// even with the wrong expect | ||
expect(setTextDataMock).toHaveBeenNthCalledWith( | ||
2, | ||
fileString | ||
); | ||
expect(setTextDataMock).toHaveBeenCalledWith(fileString); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, |
||
}); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to "Upload JSON File"