Skip to content

Commit 16508f2

Browse files
committed
fix: types
1 parent 55b7150 commit 16508f2

File tree

8 files changed

+37
-136
lines changed

8 files changed

+37
-136
lines changed

@types/global.d.ts

-5
This file was deleted.

package.json

+6-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"license": "MIT",
66
"source": "src/index.ts",
77
"main": "./lib/commonjs/index.js",
8+
"react-native": "src/index.ts",
89
"module": "./lib/module/index.js",
10+
"types": "lib/typescript/index.d.ts",
911
"scripts": {
1012
"typecheck": "tsc --noEmit --project tsconfig.test.json",
1113
"test": "jest",
@@ -61,7 +63,7 @@
6163
"codegenConfig": {
6264
"name": "RNHapticFeedbackSpec",
6365
"type": "modules",
64-
"jsSrcsDir": "src",
66+
"jsSrcsDir": "./src/codegenSpec",
6567
"android": {
6668
"javaPackageName": "com.mkuczera"
6769
}
@@ -100,25 +102,9 @@
100102
"source": "src",
101103
"output": "lib",
102104
"targets": [
103-
[
104-
"commonjs",
105-
{
106-
"esm": true
107-
}
108-
],
109-
[
110-
"module",
111-
{
112-
"esm": true
113-
}
114-
],
115-
[
116-
"typescript",
117-
{
118-
"project": "tsconfig.build.json",
119-
"esm": true
120-
}
121-
]
105+
"commonjs",
106+
"module",
107+
"typescript"
122108
]
123109
}
124110
}

setupTests.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
global.__DEV__ = true;
2-
global.__turboModuleProxy = null;
32

43
jest.mock("react-native/Libraries/BatchedBridge/NativeModules", () => ({
54
RNHapticFeedback: {

src/NativeHapticFeedback.ts

-14
This file was deleted.

src/__tests__/index.test.tsx

+7-56
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
1-
import { NativeModules } from "react-native";
2-
import { trigger } from "../index";
1+
import RNHapticFeedback from "../index";
32
import { HapticFeedbackTypes } from "../types";
43

5-
jest.mock("../NativeHapticFeedback", () => ({
6-
default: {
7-
trigger: jest.fn(),
8-
},
9-
}));
10-
11-
const NativeHapticFeedbackMock = require("../NativeHapticFeedback").default;
4+
const NativeHapticFeedbackMock = require("../codegenSpec/NativeHapticFeedback").default;
125

136
describe("RNReactNativeHapticFeedback", () => {
14-
beforeAll(() => {
15-
global.__turboModuleProxy = null;
16-
NativeModules.RNHapticFeedback = {
17-
trigger: jest.fn(),
18-
};
19-
});
20-
21-
afterEach(() => {
22-
jest.clearAllMocks();
23-
});
24-
257
it("should trigger haptic feedback with default options using NativeModules when turbo module is not used", () => {
26-
trigger(HapticFeedbackTypes.selection);
8+
RNHapticFeedback.trigger(HapticFeedbackTypes.selection);
279

28-
expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith(
10+
expect(NativeHapticFeedbackMock.trigger).toHaveBeenCalledWith(
2911
"selection",
3012
{
3113
enableVibrateFallback: false,
@@ -35,16 +17,12 @@ describe("RNReactNativeHapticFeedback", () => {
3517
});
3618

3719
it("should trigger haptic feedback with turbo module when enabled", () => {
38-
global.__turboModuleProxy = true;
39-
40-
trigger(HapticFeedbackTypes.selection);
20+
RNHapticFeedback.trigger(HapticFeedbackTypes.selection);
4121

4222
expect(NativeHapticFeedbackMock.trigger).toHaveBeenCalledWith("selection", {
4323
enableVibrateFallback: false,
4424
ignoreAndroidSystemSettings: false,
4525
});
46-
47-
global.__turboModuleProxy = null;
4826
});
4927

5028
it("should pass the correct options", () => {
@@ -53,38 +31,11 @@ describe("RNReactNativeHapticFeedback", () => {
5331
ignoreAndroidSystemSettings: true,
5432
};
5533

56-
trigger(HapticFeedbackTypes.selection, options);
34+
RNHapticFeedback.trigger(HapticFeedbackTypes.selection, options);
5735

58-
expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith(
36+
expect(NativeHapticFeedbackMock.trigger).toHaveBeenCalledWith(
5937
"selection",
6038
options,
6139
);
6240
});
63-
64-
it("should handle the case when options is a boolean", () => {
65-
// @ts-expect-error - we're testing the case when options is a boolean for deprecated behavior
66-
trigger(HapticFeedbackTypes.selection, true);
67-
68-
expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith(
69-
"selection",
70-
{
71-
enableVibrateFallback: true,
72-
ignoreAndroidSystemSettings: false,
73-
},
74-
);
75-
});
76-
77-
it("should warn when haptic feedback module is not available", () => {
78-
delete NativeModules.RNHapticFeedback;
79-
80-
const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
81-
82-
trigger(HapticFeedbackTypes.selection);
83-
84-
expect(warnSpy).toHaveBeenCalledWith(
85-
"RNReactNativeHapticFeedback is not available",
86-
);
87-
88-
warnSpy.mockRestore();
89-
});
9041
});
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { TurboModule } from 'react-native';
2+
import { TurboModuleRegistry } from 'react-native';
3+
4+
export interface Spec extends TurboModule {
5+
trigger(
6+
type: string,
7+
options?: {
8+
enableVibrateFallback?: boolean;
9+
ignoreAndroidSystemSettings?: boolean;
10+
},
11+
): void;
12+
}
13+
14+
export default TurboModuleRegistry.getEnforcing<Spec>("RNHapticFeedback");

src/index.ts

+8-31
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,26 @@
1-
import { NativeModules } from "react-native";
1+
import NativeHapticFeedback from './codegenSpec/NativeHapticFeedback';
22
import { HapticFeedbackTypes } from "./types";
33
import type { HapticOptions } from "./types";
44

5-
import type { Spec } from "./NativeHapticFeedback";
6-
export * from "./types";
7-
85
const defaultOptions = {
96
enableVibrateFallback: false,
107
ignoreAndroidSystemSettings: false,
118
};
129

13-
class RNReactNativeHapticFeedback {
14-
static trigger = (
10+
const RNHapticFeedback = {
11+
trigger(
1512
type:
1613
| keyof typeof HapticFeedbackTypes
1714
| HapticFeedbackTypes = HapticFeedbackTypes.selection,
1815
options: HapticOptions = {},
19-
) => {
20-
const triggerOptions = createTriggerOptions(options);
21-
16+
) {
2217
try {
23-
const isTurboModuleEnabled = global.__turboModuleProxy != null;
24-
const hapticFeedback = isTurboModuleEnabled
25-
? (require("./NativeHapticFeedback").default as Spec)
26-
: NativeModules.RNHapticFeedback;
27-
28-
hapticFeedback.trigger(type, triggerOptions);
18+
NativeHapticFeedback.trigger(type, { ...defaultOptions, ...options });
2919
} catch {
3020
console.warn("RNReactNativeHapticFeedback is not available");
3121
}
32-
};
33-
}
34-
35-
const createTriggerOptions = (options: HapticOptions) => {
36-
// if options is a boolean we're using an api <=1.6 and we should pass use it to set the enableVibrateFallback option
37-
if (typeof options === "boolean") {
38-
return {
39-
...defaultOptions,
40-
enableVibrateFallback: options,
41-
};
42-
} else {
43-
return { ...defaultOptions, ...options };
4422
}
45-
};
46-
47-
export const trigger = RNReactNativeHapticFeedback.trigger;
23+
}
4824

49-
export default RNReactNativeHapticFeedback;
25+
export const { trigger } = RNHapticFeedback;
26+
export default RNHapticFeedback;

tsconfig.json

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
{
22
"compilerOptions": {
3-
"rootDir": ".",
4-
"paths": {
5-
"react-native-haptic-feedback": ["./src/index"]
6-
},
73
"allowUnreachableCode": false,
84
"allowUnusedLabels": false,
95
"esModuleInterop": true,
106
"forceConsistentCasingInFileNames": true,
117
"jsx": "react-jsx",
12-
"lib": ["ESNext"],
138
"module": "ESNext",
14-
"moduleResolution": "Bundler",
9+
"moduleResolution": "node",
1510
"noEmit": true,
1611
"noFallthroughCasesInSwitch": true,
1712
"noImplicitReturns": true,
@@ -25,8 +20,6 @@
2520
"strict": true,
2621
"target": "ESNext",
2722
"verbatimModuleSyntax": true,
28-
"types": ["node"],
29-
"typeRoots": ["./node_modules/@types", "./@types"],
3023
},
31-
"exclude": ["node_modules"]
24+
"include": ["./src/"]
3225
}

0 commit comments

Comments
 (0)