|
1 | 1 | // __tests__/BarcodeScanner.spec.tsx
|
2 | 2 | import { actionValue, EditableValueBuilder } from "@mendix/piw-utils-internal";
|
| 3 | +import { render } from "@testing-library/react-native"; |
3 | 4 | import { createElement } from "react";
|
4 | 5 | import { View } from "react-native";
|
5 |
| -import { fireEvent, render, RenderAPI } from "@testing-library/react-native"; |
6 | 6 | import { BarcodeScanner, Props } from "../BarcodeScanner";
|
7 |
| -import { Camera } from "react-native-vision-camera"; |
| 7 | + |
| 8 | +let mockOnCodeScanned: ((codes: { value: string }[]) => void) | undefined; |
8 | 9 |
|
9 | 10 | jest.mock("react-native-vision-camera", () => ({
|
10 | 11 | Camera: ({ children, ...props }: any) => <View {...props}>{children}</View>,
|
11 | 12 | useCameraDevice: () => "mock-device",
|
12 |
| - useCodeScanner: () => jest.fn() |
| 13 | + useCodeScanner: (options: any) => { |
| 14 | + mockOnCodeScanned = options.onCodeScanned; |
| 15 | + return "mockCodeScanner"; |
| 16 | + } |
13 | 17 | }));
|
14 | 18 |
|
15 | 19 | jest.mock("react-native-barcode-mask", () => "BarcodeMask");
|
@@ -49,27 +53,20 @@ describe("BarcodeScanner", () => {
|
49 | 53 |
|
50 | 54 | it("sets a value and executes the onDetect action when a new barcode is scanned", () => {
|
51 | 55 | const onDetectAction = actionValue();
|
52 |
| - const component = render(<BarcodeScanner {...defaultProps} onDetect={onDetectAction} />); |
| 56 | + render(<BarcodeScanner {...defaultProps} onDetect={onDetectAction} />); |
53 | 57 |
|
54 |
| - detectBarcode(component, "value"); |
| 58 | + // Simulate scanning |
| 59 | + mockOnCodeScanned?.([{ value: "value" }]); |
55 | 60 | jest.advanceTimersByTime(2000);
|
56 | 61 |
|
57 | 62 | expect(defaultProps.barcode.setValue).toHaveBeenCalledWith("value");
|
58 | 63 | expect(onDetectAction.execute).toHaveBeenCalledTimes(1);
|
59 | 64 |
|
60 |
| - detectBarcode(component, "value1"); |
| 65 | + // Another scan |
| 66 | + mockOnCodeScanned?.([{ value: "value1" }]); |
61 | 67 | jest.advanceTimersByTime(2000);
|
62 | 68 |
|
63 | 69 | expect(defaultProps.barcode.setValue).toHaveBeenCalledWith("value1");
|
64 | 70 | expect(onDetectAction.execute).toHaveBeenCalledTimes(2);
|
65 | 71 | });
|
66 | 72 | });
|
67 |
| - |
68 |
| -function detectBarcode(component: RenderAPI, barcode: string): void { |
69 |
| - fireEvent(component.UNSAFE_getByType(Camera), "onCodeScanned", [ |
70 |
| - { |
71 |
| - value: barcode, |
72 |
| - type: "qr" |
73 |
| - } |
74 |
| - ]); |
75 |
| -} |
0 commit comments