Skip to content

Commit 55ee61a

Browse files
committed
chore: update unit tests
1 parent a30a87b commit 55ee61a

2 files changed

Lines changed: 21 additions & 51 deletions

File tree

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import { actionValue, EditableValueBuilder } from "@mendix/piw-utils-internal";
22
import { createElement } from "react";
3+
import { View } from "react-native";
34
import { fireEvent, render, RenderAPI } from "@testing-library/react-native";
4-
55
import { BarcodeScanner, Props } from "../BarcodeScanner";
6-
import { RNCamera } from "./__mocks__/RNCamera";
6+
import { Camera } from "react-native-vision-camera";
7+
8+
jest.mock("react-native-vision-camera", () => ({
9+
Camera: ({ children, ...props }: any) => <View {...props}>{children}</View>,
10+
useCameraDevice: () => "mock-device",
11+
useCodeScanner: () => jest.fn()
12+
}));
713

8-
jest.mock("react-native-vision-camera", () => jest.requireActual("./__mocks__/RNCamera"));
14+
jest.mock("react-native-barcode-mask", () => "BarcodeMask");
915

1016
describe("BarcodeScanner", () => {
1117
let defaultProps: Props;
1218

1319
beforeEach(() => {
20+
jest.useFakeTimers();
1421
defaultProps = {
1522
showAnimatedLine: false,
1623
showMask: false,
@@ -20,25 +27,26 @@ describe("BarcodeScanner", () => {
2027
};
2128
});
2229

30+
afterEach(() => {
31+
jest.clearAllTimers();
32+
});
33+
2334
it("renders", () => {
2435
const component = render(<BarcodeScanner {...defaultProps} />);
25-
2636
expect(component.toJSON()).toMatchSnapshot();
2737
});
2838

2939
it("renders with mask", () => {
3040
const component = render(<BarcodeScanner {...defaultProps} showMask />);
31-
3241
expect(component.toJSON()).toMatchSnapshot();
3342
});
3443

3544
it("renders with mask with animated line", () => {
3645
const component = render(<BarcodeScanner {...defaultProps} showMask showAnimatedLine />);
37-
3846
expect(component.toJSON()).toMatchSnapshot();
3947
});
4048

41-
it("sets a value and executes the on detect action when a new barcode is scanned", async () => {
49+
it("sets a value and executes the onDetect action when a new barcode is scanned", () => {
4250
const onDetectAction = actionValue();
4351
const component = render(<BarcodeScanner {...defaultProps} onDetect={onDetectAction} />);
4452

@@ -49,35 +57,18 @@ describe("BarcodeScanner", () => {
4957
expect(onDetectAction.execute).toHaveBeenCalledTimes(1);
5058

5159
detectBarcode(component, "value1");
52-
jest.advanceTimersByTime(100);
53-
detectBarcode(component, "value2");
54-
// Events are not fired immediately by testing-library, so firing with 1999 will be already too late for the previous action
55-
jest.advanceTimersByTime(1800);
56-
detectBarcode(component, "value3");
57-
5860
jest.advanceTimersByTime(2000);
5961

6062
expect(defaultProps.barcode.setValue).toHaveBeenCalledWith("value1");
6163
expect(onDetectAction.execute).toHaveBeenCalledTimes(2);
62-
63-
detectBarcode(component, "value2");
64-
detectBarcode(component, "value3");
65-
detectBarcode(component, "value4");
66-
67-
jest.advanceTimersByTime(2000);
68-
69-
expect(defaultProps.barcode.setValue).toHaveBeenCalledWith("value2");
70-
expect(onDetectAction.execute).toHaveBeenCalledTimes(3);
7164
});
7265
});
7366

7467
function detectBarcode(component: RenderAPI, barcode: string): void {
75-
fireEvent(component.UNSAFE_getByType(RNCamera), "barCodeRead", {
76-
data: barcode,
77-
type: "qr",
78-
bounds: [
79-
{ x: "", y: "" },
80-
{ x: "", y: "" }
81-
]
82-
});
68+
fireEvent(component.UNSAFE_getByType(Camera), "onCodeScanned", [
69+
{
70+
value: barcode,
71+
type: "qr"
72+
}
73+
]);
8374
}

packages/pluggableWidgets/barcode-scanner-native/src/__tests__/__mocks__/RNCamera.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)