Skip to content

Commit

Permalink
per Fred
Browse files Browse the repository at this point in the history
  • Loading branch information
namnguyen20999 committed Jul 12, 2024
1 parent ee4e3e3 commit 2c74165
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
41 changes: 16 additions & 25 deletions frontend/taipy-gui/src/components/Taipy/FileSelector.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe("FileSelector Component", () => {
});

it("resets dropLabel and dropSx on drag leave", async () => {
const { getByRole, getByTestId } = render(<FileSelector />);
const { getByRole } = render(<FileSelector />);
const elt = getByRole("button");

// Create a mock DragEvent
Expand All @@ -153,26 +153,23 @@ describe("FileSelector Component", () => {
fireEvent(elt, mockEvent);

// Add assertions to check if dropLabel and dropSx have been reset
const dropLabelElement = getByTestId("file-selector");
expect(dropLabelElement.textContent).toBe(" ");
const buttonElement = getByTestId("upload-button");
expect(buttonElement).toHaveStyle("min-width: 0px");
const labelElement = elt.querySelector("span");
expect(labelElement!.textContent).toBe("");
expect(elt).toHaveStyle("min-width: 0px");
});

it("checks if notification is dispatched on file upload completion", async () => {
const mockDispatch = jest.fn();
const { getByTestId } = render(
const { getByLabelText } = render(
<TaipyContext.Provider value={{ state: INITIAL_STATE, dispatch: mockDispatch }}>
<FileSelector label="FileSelector" notify={true} />
</TaipyContext.Provider>,
);

// Simulate file upload
const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
const inputElement = getByTestId("file-selector").querySelector("input");
if (inputElement) {
fireEvent.change(inputElement, { target: { files: [file] } });
}
const inputElement = getByLabelText("FileSelector");
fireEvent.change(inputElement, { target: { files: [file] } });

// Wait for the upload to complete
await waitFor(() => expect(mockDispatch).toHaveBeenCalled());
Expand All @@ -199,18 +196,16 @@ describe("FileSelector Component", () => {
});

const mockDispatch = jest.fn();
const { getByTestId } = render(
const { getByLabelText } = render(
<TaipyContext.Provider value={{ state: INITIAL_STATE, dispatch: mockDispatch }}>
<FileSelector label="FileSelector" notify={true} />
</TaipyContext.Provider>,
);

// Simulate file upload
const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
const inputElement = getByTestId("file-selector").querySelector("input");
if (inputElement) {
fireEvent.change(inputElement, { target: { files: [file] } });
}
const inputElement = getByLabelText("FileSelector");
fireEvent.change(inputElement, { target: { files: [file] } });

// Wait for the upload to complete
await waitFor(() => expect(mockDispatch).toHaveBeenCalled());
Expand All @@ -232,18 +227,16 @@ describe("FileSelector Component", () => {
(uploadFile as jest.Mock).mockImplementation(() => Promise.resolve("mocked response"));

const mockDispatch = jest.fn();
const { getByTestId, queryByRole } = render(
const { getByLabelText, queryByRole } = render(
<TaipyContext.Provider value={{ state: INITIAL_STATE, dispatch: mockDispatch }}>
<FileSelector label="FileSelector" notify={true} onAction="testAction" />
</TaipyContext.Provider>,
);

// Simulate file upload
const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
const inputElement = getByTestId("file-selector").querySelector("input");
if (inputElement) {
fireEvent.change(inputElement, { target: { files: [file] } });
}
const inputElement = getByLabelText("FileSelector");
fireEvent.change(inputElement, { target: { files: [file] } });

// Check if the progress bar is displayed during the upload process
expect(queryByRole("progressbar")).toBeInTheDocument();
Expand All @@ -266,17 +259,15 @@ describe("FileSelector Component", () => {

it("checks if no action is taken when no file is uploaded", async () => {
const mockDispatch = jest.fn();
const { getByTestId } = render(
const { getByLabelText } = render(
<TaipyContext.Provider value={{ state: INITIAL_STATE, dispatch: mockDispatch }}>
<FileSelector label="FileSelector" notify={true} />
</TaipyContext.Provider>,
);

// Simulate file upload without providing a file
const inputElement = getByTestId("file-selector").querySelector("input");
if (inputElement) {
fireEvent.change(inputElement, { target: { files: [] } });
}
const inputElement = getByLabelText("FileSelector");
fireEvent.change(inputElement, { target: { files: [] } });

// Check if the dispatch function has not been called
expect(mockDispatch).not.toHaveBeenCalled();
Expand Down
3 changes: 1 addition & 2 deletions frontend/taipy-gui/src/components/Taipy/FileSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const FileSelector = (props: FileSelectorProps) => {
}, [handleDrop, handleDragLeave, handleDragOverWithLabel]);

return (
<label data-testid="file-selector" htmlFor={inputId} className={className}>
<label htmlFor={inputId} className={className}>
<input
style={noDisplayStyle}
id={inputId}
Expand All @@ -157,7 +157,6 @@ const FileSelector = (props: FileSelectorProps) => {
<Tooltip title={hover || ""}>
<Button
id={id}
data-testid="upload-button"
component="span"
aria-label="upload"
variant="outlined"
Expand Down

0 comments on commit 2c74165

Please sign in to comment.