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

chore: adds automation for request templates component #37140

Merged
merged 1 commit into from
Nov 1, 2024
Merged
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
63 changes: 63 additions & 0 deletions app/client/src/pages/Templates/Template/RequestTemplate.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { createMessage } from "@appsmith/ads-old";
import "@testing-library/jest-dom";
import { fireEvent, render } from "@testing-library/react";
import {
REQUEST_BUILDING_BLOCK,
REQUEST_TEMPLATE,
} from "ee/constants/messages";
import AnalyticsUtil from "ee/utils/AnalyticsUtil";
import { unitTestBaseMockStore } from "layoutSystems/common/dropTarget/unitTestUtils";
import React from "react";
import { Provider } from "react-redux";
import configureStore from "redux-mock-store";
import { lightTheme } from "selectors/themeSelectors";
import { ThemeProvider } from "styled-components";
import RequestTemplate, {
REQUEST_TEMPLATE_URL,
type RequestTemplateProps,
} from "./RequestTemplate";
const mockStore = configureStore([]);

const BaseComponentRender = (
props: RequestTemplateProps,
storeToUse = unitTestBaseMockStore,
) => (
<Provider store={mockStore(storeToUse)}>
<ThemeProvider theme={lightTheme}>
<RequestTemplate {...props} />
</ThemeProvider>
</Provider>
);

describe("RequestTemplate", () => {
it("should display correct message based on isBuildingBlock prop", () => {
const { getByText } = render(
BaseComponentRender({ isBuildingBlock: true }),
);

expect(
getByText(createMessage(REQUEST_BUILDING_BLOCK)),
).toBeInTheDocument();
});
it("should open REQUEST_TEMPLATE_URL in a new window when button is clicked", () => {
const openSpy = jest.spyOn(window, "open");
const { getByText } = render(
BaseComponentRender({ isBuildingBlock: false }),
);
const button = getByText(createMessage(REQUEST_TEMPLATE));

fireEvent.click(button);
expect(openSpy).toHaveBeenCalledWith(REQUEST_TEMPLATE_URL);
});
Comment on lines +42 to +51
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add cleanup for window.open spy.

The test should clean up the spy after execution to prevent interference with other tests.

 it("should open REQUEST_TEMPLATE_URL in a new window when button is clicked", () => {
   const openSpy = jest.spyOn(window, "open");
+  afterEach(() => {
+    openSpy.mockRestore();
+  });
   const { getByText } = render(
     BaseComponentRender({ isBuildingBlock: false }),
   );
   const button = getByText(createMessage(REQUEST_TEMPLATE));

   fireEvent.click(button);
-  expect(openSpy).toHaveBeenCalledWith(REQUEST_TEMPLATE_URL);
+  expect(openSpy).toHaveBeenCalledWith(REQUEST_TEMPLATE_URL, "_blank");
 });

Committable suggestion was skipped due to low confidence.


it('should trigger AnalyticsUtil logEvent with "REQUEST_NEW_TEMPLATE" when button is clicked', () => {
const logEventSpy = jest.spyOn(AnalyticsUtil, "logEvent");
const { getByText } = render(
BaseComponentRender({ isBuildingBlock: false }),
);
const button = getByText(createMessage(REQUEST_TEMPLATE));

fireEvent.click(button);
expect(logEventSpy).toHaveBeenCalledWith("REQUEST_NEW_TEMPLATE");
});
});
Comment on lines +32 to +63
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding error scenario test cases.

The test suite could benefit from additional test cases:

  1. Error handling when URL is invalid
  2. Component behavior with undefined props
  3. Multiple button clicks handling

Would you like me to provide example implementations for these test cases?

14 changes: 7 additions & 7 deletions app/client/src/pages/Templates/Template/RequestTemplate.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from "react";
import styled from "styled-components";
import { Text, TextType } from "@appsmith/ads-old";
import { Button } from "@appsmith/ads";
import { Text, TextType } from "@appsmith/ads-old";
import RequestTemplateSvg from "assets/images/request-template.svg";
import {
COULDNT_FIND_TEMPLATE,
createMessage,
COULDNT_FIND_TEMPLATE_DESCRIPTION,
REQUEST_TEMPLATE,
createMessage,
REQUEST_BUILDING_BLOCK,
REQUEST_TEMPLATE,
} from "ee/constants/messages";
import AnalyticsUtil from "ee/utils/AnalyticsUtil";
import React from "react";
import styled from "styled-components";

const Wrapper = styled.div`
border: 1px solid var(--ads-v2-color-border);
Expand Down Expand Up @@ -48,10 +48,10 @@ const StyledImage = styled.img`
border-radius: var(--ads-v2-border-radius);
`;

const REQUEST_TEMPLATE_URL =
export const REQUEST_TEMPLATE_URL =
"https://app.appsmith.com/app/request-templates/request-list-6241c12fc99df2369931a714";

Comment on lines +51 to 52
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider moving the URL to environment configuration.

Hardcoding the URL with a specific app ID might cause issues if the environment or app ID changes. Consider moving this to an environment configuration file.

-export const REQUEST_TEMPLATE_URL =
-  "https://app.appsmith.com/app/request-templates/request-list-6241c12fc99df2369931a714";
+export const REQUEST_TEMPLATE_URL = process.env.REACT_APP_REQUEST_TEMPLATE_URL ||
+  "https://app.appsmith.com/app/request-templates/request-list-6241c12fc99df2369931a714";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const REQUEST_TEMPLATE_URL =
"https://app.appsmith.com/app/request-templates/request-list-6241c12fc99df2369931a714";
export const REQUEST_TEMPLATE_URL = process.env.REACT_APP_REQUEST_TEMPLATE_URL ||
"https://app.appsmith.com/app/request-templates/request-list-6241c12fc99df2369931a714";

interface RequestTemplateProps {
export interface RequestTemplateProps {
isBuildingBlock?: boolean;
}

Expand Down
Loading