|
| 1 | +import { createMessage } from "@appsmith/ads-old"; |
| 2 | +import "@testing-library/jest-dom"; |
| 3 | +import { fireEvent, render } from "@testing-library/react"; |
| 4 | +import { |
| 5 | + REQUEST_BUILDING_BLOCK, |
| 6 | + REQUEST_TEMPLATE, |
| 7 | +} from "ee/constants/messages"; |
| 8 | +import AnalyticsUtil from "ee/utils/AnalyticsUtil"; |
| 9 | +import { unitTestBaseMockStore } from "layoutSystems/common/dropTarget/unitTestUtils"; |
| 10 | +import React from "react"; |
| 11 | +import { Provider } from "react-redux"; |
| 12 | +import configureStore from "redux-mock-store"; |
| 13 | +import { lightTheme } from "selectors/themeSelectors"; |
| 14 | +import { ThemeProvider } from "styled-components"; |
| 15 | +import RequestTemplate, { |
| 16 | + REQUEST_TEMPLATE_URL, |
| 17 | + type RequestTemplateProps, |
| 18 | +} from "./RequestTemplate"; |
| 19 | +const mockStore = configureStore([]); |
| 20 | + |
| 21 | +const BaseComponentRender = ( |
| 22 | + props: RequestTemplateProps, |
| 23 | + storeToUse = unitTestBaseMockStore, |
| 24 | +) => ( |
| 25 | + <Provider store={mockStore(storeToUse)}> |
| 26 | + <ThemeProvider theme={lightTheme}> |
| 27 | + <RequestTemplate {...props} /> |
| 28 | + </ThemeProvider> |
| 29 | + </Provider> |
| 30 | +); |
| 31 | + |
| 32 | +describe("RequestTemplate", () => { |
| 33 | + it("should display correct message based on isBuildingBlock prop", () => { |
| 34 | + const { getByText } = render( |
| 35 | + BaseComponentRender({ isBuildingBlock: true }), |
| 36 | + ); |
| 37 | + |
| 38 | + expect( |
| 39 | + getByText(createMessage(REQUEST_BUILDING_BLOCK)), |
| 40 | + ).toBeInTheDocument(); |
| 41 | + }); |
| 42 | + it("should open REQUEST_TEMPLATE_URL in a new window when button is clicked", () => { |
| 43 | + const openSpy = jest.spyOn(window, "open"); |
| 44 | + const { getByText } = render( |
| 45 | + BaseComponentRender({ isBuildingBlock: false }), |
| 46 | + ); |
| 47 | + const button = getByText(createMessage(REQUEST_TEMPLATE)); |
| 48 | + |
| 49 | + fireEvent.click(button); |
| 50 | + expect(openSpy).toHaveBeenCalledWith(REQUEST_TEMPLATE_URL); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should trigger AnalyticsUtil logEvent with "REQUEST_NEW_TEMPLATE" when button is clicked', () => { |
| 54 | + const logEventSpy = jest.spyOn(AnalyticsUtil, "logEvent"); |
| 55 | + const { getByText } = render( |
| 56 | + BaseComponentRender({ isBuildingBlock: false }), |
| 57 | + ); |
| 58 | + const button = getByText(createMessage(REQUEST_TEMPLATE)); |
| 59 | + |
| 60 | + fireEvent.click(button); |
| 61 | + expect(logEventSpy).toHaveBeenCalledWith("REQUEST_NEW_TEMPLATE"); |
| 62 | + }); |
| 63 | +}); |
0 commit comments