|
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { ReactElement } from "react"; |
| 4 | +import { SignatureContainerProps } from "../../typings/SignatureProps"; |
| 5 | +import Signature from "../Signature"; |
| 6 | + |
| 7 | +const mockSignatureComponent = jest.fn((_props: unknown) => <div data-testid="signature-component" />); |
| 8 | + |
| 9 | +jest.mock("../components/Signature", () => ({ |
| 10 | + SignatureComponent: (props: unknown): ReactElement => mockSignatureComponent(props) |
| 11 | +})); |
| 12 | + |
| 13 | +describe("Signature", () => { |
| 14 | + const imageSource = { |
| 15 | + setValue: jest.fn() |
| 16 | + } as unknown as SignatureContainerProps["imageSource"]; |
| 17 | + |
| 18 | + const defaultProps: SignatureContainerProps = { |
| 19 | + name: "signature", |
| 20 | + class: "mx-signature", |
| 21 | + tabIndex: 0, |
| 22 | + imageSource, |
| 23 | + hasSignatureAttribute: undefined, |
| 24 | + penType: "ballpoint", |
| 25 | + penColor: "#000000", |
| 26 | + widthUnit: "pixels", |
| 27 | + width: 300, |
| 28 | + heightUnit: "pixels", |
| 29 | + height: 200, |
| 30 | + minHeightUnit: "none", |
| 31 | + minHeight: 0, |
| 32 | + maxHeightUnit: "none", |
| 33 | + maxHeight: 0, |
| 34 | + OverflowY: "auto", |
| 35 | + showGrid: true, |
| 36 | + gridBorderColor: "#cccccc", |
| 37 | + gridCellHeight: 20, |
| 38 | + gridCellWidth: 20, |
| 39 | + gridBorderWidth: 1 |
| 40 | + }; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + mockSignatureComponent.mockClear(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("renders SignatureComponent", () => { |
| 47 | + render(<Signature {...defaultProps} />); |
| 48 | + |
| 49 | + expect(screen.getByTestId("signature-component")).toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("passes derived and default props to SignatureComponent", () => { |
| 53 | + render(<Signature {...defaultProps} />); |
| 54 | + |
| 55 | + expect(mockSignatureComponent).toHaveBeenCalledTimes(1); |
| 56 | + expect(mockSignatureComponent).toHaveBeenCalledWith( |
| 57 | + expect.objectContaining({ |
| 58 | + className: defaultProps.class, |
| 59 | + readOnly: false, |
| 60 | + clearSignature: false, |
| 61 | + imageSource |
| 62 | + }) |
| 63 | + ); |
| 64 | + |
| 65 | + const passedProps = mockSignatureComponent.mock.calls[0][0] as { |
| 66 | + onSignEndAction?: () => void; |
| 67 | + }; |
| 68 | + expect(typeof passedProps.onSignEndAction).toBe("function"); |
| 69 | + }); |
| 70 | +}); |
0 commit comments