Skip to content
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

fix/bug-snowflake-key-pair-auth-private-key-does-not-get-removed-upon… #36270

Open
wants to merge 1 commit into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions app/client/src/components/formControls/FilePickerControl.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import React from "react";
import "@testing-library/jest-dom/extend-expect";
import { act, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Provider } from "react-redux";
import { combineReducers, createStore } from "redux";
import { ThemeProvider } from "styled-components";
import { reducer as formReducer } from "redux-form";
import { theme } from "constants/DefaultTheme"; // Adjust the path as necessary
import { BrowserRouter as Router } from "react-router-dom";
import FilePickerControl, { RenderFilePicker } from "./FilePickerControl"; // Adjust the path as necessary
import { Field, reduxForm } from "redux-form";

const rootReducer = combineReducers({
form: formReducer,
});

const mockStore = createStore(rootReducer);

const mockInput = {
value: {},
onChange: jest.fn(),
};

const mockProps = {
input: mockInput,
meta: {},
disabled: false,
onChange: jest.fn(),
};

const TestForm = reduxForm({ form: "testForm" })(() => (
<Field component={RenderFilePicker} name="filePicker" {...mockProps} />
));

const renderComponent = () =>
render(
<Provider store={mockStore}>
<Router>
<ThemeProvider theme={theme}>
<TestForm />
</ThemeProvider>
</Router>
</Provider>
);

describe("FilePickerControl Component", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("renders the component", () => {
renderComponent();
expect(screen.getByText("Select")).toBeInTheDocument();
});

test("selects a file and triggers onChange", async () => {
renderComponent();

const file = new File(["file contents"], "example.txt", {
type: "text/plain",
});

userEvent.click(screen.getByText("Select"));
userEvent.click(screen.getByText("Browse"));

const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using as, can we define type for fileInput?


await act(async () => {
if (fileInput) {
console.log('File input found, uploading file...');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these console logs needed here?

await userEvent.upload(fileInput, file);
console.log('File uploaded.');
} else {
throw new Error("File input not found");
}
});

await waitFor(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add a test case for when we remove the file from file picker? Asserting that input is blank?

console.log('Waiting for file name to appear...');
expect(screen.getByText("example.txt")).toBeInTheDocument();
});

});
});
8 changes: 5 additions & 3 deletions app/client/src/components/formControls/FilePickerControl.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { useState } from "react";
import { useState, useEffect, useCallback } from "react";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these useEffect and useCallback imports needed?

import styled from "styled-components";
import type { ControlProps } from "./BaseControl";
import BaseControl from "./BaseControl";
Expand All @@ -8,7 +8,6 @@ import type { SetProgress } from "@appsmith/ads-old";
import { FilePickerV2, FileType } from "@appsmith/ads-old";
import type { WrappedFieldInputProps, WrappedFieldMetaProps } from "redux-form";
import { Field } from "redux-form";
import { useEffect, useCallback } from "react";
import { replayHighlightClass } from "globalStyles/portals";
import { Button, Modal, ModalBody, ModalContent } from "@appsmith/ads";

Expand Down Expand Up @@ -48,7 +47,7 @@ type RenderFilePickerProps = FilePickerControlProps & {
onChange: (event: any) => void;
};

function RenderFilePicker(props: RenderFilePickerProps) {
export function RenderFilePicker(props: RenderFilePickerProps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jagadeesh-90 Is this export needed here? As we are already exporting this whole component at the bottom

const [isOpen, setIsOpen] = useState(false);
const [appFileToBeUploaded, setAppFileToBeUploaded] = useState<{
file: File;
Expand Down Expand Up @@ -84,6 +83,9 @@ function RenderFilePicker(props: RenderFilePickerProps) {
});
};
}
else{
props.input?.onChange("");
}
}, [appFileToBeUploaded]);

return (
Expand Down
Loading