Skip to content

Commit 15bd792

Browse files
committed
fix: unit test
1 parent f8c019e commit 15bd792

5 files changed

Lines changed: 54 additions & 18 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
"react-dom": ">=18.0.0 <19.0.0",
8282
"rollup": "catalog:",
8383
"ts-node": "10.9.2",
84-
"typescript": ">5.8.0"
84+
"typescript": ">5.8.0",
85+
"flatted": ">=3.4.2"
8586
},
8687
"patchedDependencies": {
8788
"mime-types": "patches/mime-types.patch",

packages/pluggableWidgets/barcode-generator-web/src/__tests__/BarcodeGenerator.spec.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import "@testing-library/jest-dom";
22
import { fireEvent, render, screen } from "@testing-library/react";
33
import userEvent from "@testing-library/user-event";
44
import { EditableValueBuilder } from "@mendix/widget-plugin-test-utils";
5+
import { forwardRef } from "react";
56

67
// Mock JsBarcode
78
const mockJsBarcode = jest.fn();
89
jest.mock("jsbarcode", () => mockJsBarcode);
910

1011
// Mock the QRCodeSVG component
1112
jest.mock("qrcode.react", () => ({
12-
QRCodeSVG: ({ value, size, level, marginSize, title, imageSettings }: any) => (
13+
QRCodeSVG: forwardRef<SVGSVGElement, any>(({ value, size, level, marginSize, title, imageSettings }, ref) => (
1314
<div
15+
ref={ref as any}
1416
data-testid="qr-code"
1517
data-value={value}
1618
data-size={size}
@@ -21,7 +23,7 @@ jest.mock("qrcode.react", () => ({
2123
>
2224
QR Code: {value}
2325
</div>
24-
)
26+
))
2527
}));
2628

2729
// Mock download functionality
@@ -66,6 +68,7 @@ const createBarcodeProps = (overrides: any = {}): any => ({
6668
qrMargin: 2,
6769
qrTitle: "",
6870
qrLevel: "L" as any,
71+
showTitle: false,
6972
qrImage: false,
7073
qrImageSrc: createMockWebImage(),
7174
qrImageCenter: true,
@@ -358,7 +361,8 @@ describe("BarcodeGenerator", () => {
358361

359362
it("renders QR code with title", () => {
360363
const props = createBarcodeProps({
361-
qrTitle: "QR Code Title",
364+
qrTitle: { value: "QR Code Title", status: "available" } as any,
365+
showTitle: true,
362366
codeValue: { value: "test", status: "available" } as any
363367
});
364368

@@ -881,7 +885,8 @@ describe("BarcodeGenerator", () => {
881885
describe("accessibility", () => {
882886
it("renders QR code title as semantic element when provided", () => {
883887
const props = createBarcodeProps({
884-
qrTitle: "Invoice QR Code",
888+
qrTitle: { value: "Invoice QR Code", status: "available" } as any,
889+
showTitle: true,
885890
codeValue: { value: "test", status: "available" } as any
886891
});
887892

@@ -894,7 +899,8 @@ describe("BarcodeGenerator", () => {
894899

895900
it("does not render title when qrTitle is empty", () => {
896901
const props = createBarcodeProps({
897-
qrTitle: "",
902+
qrTitle: { value: "", status: "available" } as any,
903+
showTitle: false,
898904
codeValue: { value: "test", status: "available" } as any
899905
});
900906

@@ -973,7 +979,8 @@ describe("BarcodeGenerator", () => {
973979
it("renders QR code with download, title, and image overlay", () => {
974980
const props = createBarcodeProps({
975981
allowDownload: true,
976-
qrTitle: "Secure QR",
982+
qrTitle: { value: "Secure QR", status: "available" } as any,
983+
showTitle: true,
977984
qrImage: true,
978985
qrImageSrc: createMockWebImage("available"),
979986
downloadButtonCaption: { status: "available" as const, value: "Save QR" } as any,

packages/pluggableWidgets/barcode-generator-web/src/components/QRCode.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
import { QRCodeSVG } from "qrcode.react";
2-
import { ReactElement, useRef } from "react";
2+
import { forwardRef, ReactElement, useRef } from "react";
3+
import { QRCodeTypeConfig } from "../config/Barcode.config";
34
import { downloadCode } from "../utils/download-code";
45
import { DownloadButton } from "./DownloadButton";
5-
import { QRCodeTypeConfig } from "../config/Barcode.config";
66

77
interface QRCodeRendererProps {
88
config: QRCodeTypeConfig;
99
}
10+
interface QRCodeSVGWrapperProps {
11+
value: string;
12+
size?: number;
13+
level?: string;
14+
marginSize?: number;
15+
title?: string;
16+
imageSettings?: any;
17+
}
18+
19+
const QRCodeSVGWrapper = forwardRef<SVGSVGElement, QRCodeSVGWrapperProps>(
20+
({ value, size, level, marginSize, title, imageSettings }, ref) => (
21+
<QRCodeSVG
22+
ref={ref as any}
23+
value={value}
24+
size={size}
25+
level={level as any}
26+
marginSize={marginSize}
27+
title={title}
28+
imageSettings={imageSettings}
29+
/>
30+
)
31+
);
32+
QRCodeSVGWrapper.displayName = "QRCodeSVGWrapper";
1033

1134
export function QRCodeRenderer({ config }: QRCodeRendererProps): ReactElement {
1235
const ref = useRef<SVGSVGElement>(null);
@@ -26,7 +49,7 @@ export function QRCodeRenderer({ config }: QRCodeRendererProps): ReactElement {
2649
<div className="qrcode-renderer">
2750
{config.showTitle && <h3 className="qrcode-renderer-title">{title}</h3>}
2851
{buttonPosition === "top" && button}
29-
<QRCodeSVG
52+
<QRCodeSVGWrapper
3053
ref={ref}
3154
value={codeValue}
3255
size={size}

packages/pluggableWidgets/barcode-generator-web/src/config/Barcode.config.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,20 @@ export function barcodeConfig(props: BarcodeGeneratorContainerProps): BarcodeCon
7272
: undefined;
7373

7474
const baseConfig: CodeBaseTypeConfig = {
75-
type: format === "QRCode" ? "qrcode" : "barcode",
75+
type: "barcode",
7676
codeValue,
7777
margin: props.codeMargin ?? 2,
7878
logLevel: props.logLevel,
7979
downloadButton: downloadButtonConfig
8080
};
8181

8282
if (format === "QRCode") {
83-
return {
83+
const qrConfig: QRCodeTypeConfig = {
8484
...baseConfig,
85+
margin: props.qrMargin ?? 2,
8586
type: "qrcode",
8687
size: props.qrSize ?? 128,
87-
showTitle: props.showTitle,
88+
showTitle: props.showTitle ?? false,
8889
title: props.qrTitle.status === "available" ? props.qrTitle.value : "QR Code",
8990
level: props.qrLevel ?? "L",
9091
overlay:
@@ -100,9 +101,10 @@ export function barcodeConfig(props: BarcodeGeneratorContainerProps): BarcodeCon
100101
}
101102
: undefined
102103
};
104+
return qrConfig;
103105
}
104106

105-
return {
107+
const barcodeConfig: BarcodeTypeConfig = {
106108
...baseConfig,
107109
type: "barcode",
108110
width: props.codeWidth ?? 128,
@@ -119,6 +121,8 @@ export function barcodeConfig(props: BarcodeGeneratorContainerProps): BarcodeCon
119121
addonFormat: props.addonFormat,
120122
addonSpacing: props.addonSpacing ?? 20
121123
};
124+
125+
return barcodeConfig;
122126
}
123127

124128
function generateFileName(customFileName: string | undefined, format: string, codeValue: string): string {

pnpm-lock.yaml

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)