Skip to content

Commit

Permalink
Merge pull request #135 from huextrat/fix/types
Browse files Browse the repository at this point in the history
fix(ts): export types
  • Loading branch information
mkuczera authored Sep 17, 2024
2 parents 65c8b10 + 16508f2 commit d1516d5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 136 deletions.
5 changes: 0 additions & 5 deletions @types/global.d.ts

This file was deleted.

26 changes: 6 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"license": "MIT",
"source": "src/index.ts",
"main": "./lib/commonjs/index.js",
"react-native": "src/index.ts",
"module": "./lib/module/index.js",
"types": "lib/typescript/index.d.ts",
"scripts": {
"typecheck": "tsc --noEmit --project tsconfig.test.json",
"test": "jest",
Expand Down Expand Up @@ -61,7 +63,7 @@
"codegenConfig": {
"name": "RNHapticFeedbackSpec",
"type": "modules",
"jsSrcsDir": "src",
"jsSrcsDir": "./src/codegenSpec",
"android": {
"javaPackageName": "com.mkuczera"
}
Expand Down Expand Up @@ -100,25 +102,9 @@
"source": "src",
"output": "lib",
"targets": [
[
"commonjs",
{
"esm": true
}
],
[
"module",
{
"esm": true
}
],
[
"typescript",
{
"project": "tsconfig.build.json",
"esm": true
}
]
"commonjs",
"module",
"typescript"
]
}
}
1 change: 0 additions & 1 deletion setupTests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
global.__DEV__ = true;
global.__turboModuleProxy = null;

jest.mock("react-native/Libraries/BatchedBridge/NativeModules", () => ({
RNHapticFeedback: {
Expand Down
14 changes: 0 additions & 14 deletions src/NativeHapticFeedback.ts

This file was deleted.

63 changes: 7 additions & 56 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
import { NativeModules } from "react-native";
import { trigger } from "../index";
import RNHapticFeedback from "../index";
import { HapticFeedbackTypes } from "../types";

jest.mock("../NativeHapticFeedback", () => ({
default: {
trigger: jest.fn(),
},
}));

const NativeHapticFeedbackMock = require("../NativeHapticFeedback").default;
const NativeHapticFeedbackMock = require("../codegenSpec/NativeHapticFeedback").default;

describe("RNReactNativeHapticFeedback", () => {
beforeAll(() => {
global.__turboModuleProxy = null;
NativeModules.RNHapticFeedback = {
trigger: jest.fn(),
};
});

afterEach(() => {
jest.clearAllMocks();
});

it("should trigger haptic feedback with default options using NativeModules when turbo module is not used", () => {
trigger(HapticFeedbackTypes.selection);
RNHapticFeedback.trigger(HapticFeedbackTypes.selection);

expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith(
expect(NativeHapticFeedbackMock.trigger).toHaveBeenCalledWith(
"selection",
{
enableVibrateFallback: false,
Expand All @@ -35,16 +17,12 @@ describe("RNReactNativeHapticFeedback", () => {
});

it("should trigger haptic feedback with turbo module when enabled", () => {
global.__turboModuleProxy = true;

trigger(HapticFeedbackTypes.selection);
RNHapticFeedback.trigger(HapticFeedbackTypes.selection);

expect(NativeHapticFeedbackMock.trigger).toHaveBeenCalledWith("selection", {
enableVibrateFallback: false,
ignoreAndroidSystemSettings: false,
});

global.__turboModuleProxy = null;
});

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

trigger(HapticFeedbackTypes.selection, options);
RNHapticFeedback.trigger(HapticFeedbackTypes.selection, options);

expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith(
expect(NativeHapticFeedbackMock.trigger).toHaveBeenCalledWith(
"selection",
options,
);
});

it("should handle the case when options is a boolean", () => {
// @ts-expect-error - we're testing the case when options is a boolean for deprecated behavior
trigger(HapticFeedbackTypes.selection, true);

expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith(
"selection",
{
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false,
},
);
});

it("should warn when haptic feedback module is not available", () => {
delete NativeModules.RNHapticFeedback;

const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});

trigger(HapticFeedbackTypes.selection);

expect(warnSpy).toHaveBeenCalledWith(
"RNReactNativeHapticFeedback is not available",
);

warnSpy.mockRestore();
});
});
14 changes: 14 additions & 0 deletions src/codegenSpec/NativeHapticFeedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
trigger(
type: string,
options?: {
enableVibrateFallback?: boolean;
ignoreAndroidSystemSettings?: boolean;
},
): void;
}

export default TurboModuleRegistry.getEnforcing<Spec>("RNHapticFeedback");
39 changes: 8 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,26 @@
import { NativeModules } from "react-native";
import NativeHapticFeedback from './codegenSpec/NativeHapticFeedback';
import { HapticFeedbackTypes } from "./types";
import type { HapticOptions } from "./types";

import type { Spec } from "./NativeHapticFeedback";
export * from "./types";

const defaultOptions = {
enableVibrateFallback: false,
ignoreAndroidSystemSettings: false,
};

class RNReactNativeHapticFeedback {
static trigger = (
const RNHapticFeedback = {
trigger(
type:
| keyof typeof HapticFeedbackTypes
| HapticFeedbackTypes = HapticFeedbackTypes.selection,
options: HapticOptions = {},
) => {
const triggerOptions = createTriggerOptions(options);

) {
try {
const isTurboModuleEnabled = global.__turboModuleProxy != null;
const hapticFeedback = isTurboModuleEnabled
? (require("./NativeHapticFeedback").default as Spec)
: NativeModules.RNHapticFeedback;

hapticFeedback.trigger(type, triggerOptions);
NativeHapticFeedback.trigger(type, { ...defaultOptions, ...options });
} catch {
console.warn("RNReactNativeHapticFeedback is not available");
}
};
}

const createTriggerOptions = (options: HapticOptions) => {
// if options is a boolean we're using an api <=1.6 and we should pass use it to set the enableVibrateFallback option
if (typeof options === "boolean") {
return {
...defaultOptions,
enableVibrateFallback: options,
};
} else {
return { ...defaultOptions, ...options };
}
};

export const trigger = RNReactNativeHapticFeedback.trigger;
}

export default RNReactNativeHapticFeedback;
export const { trigger } = RNHapticFeedback;
export default RNHapticFeedback;
11 changes: 2 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
{
"compilerOptions": {
"rootDir": ".",
"paths": {
"react-native-haptic-feedback": ["./src/index"]
},
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react-jsx",
"lib": ["ESNext"],
"module": "ESNext",
"moduleResolution": "Bundler",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
Expand All @@ -25,8 +20,6 @@
"strict": true,
"target": "ESNext",
"verbatimModuleSyntax": true,
"types": ["node"],
"typeRoots": ["./node_modules/@types", "./@types"],
},
"exclude": ["node_modules"]
"include": ["./src/"]
}

0 comments on commit d1516d5

Please sign in to comment.