Skip to content

Commit 4154615

Browse files
author
Mariusz Pasinski
committed
feat: update tests for 3 arg requireNodeAddon()
1 parent 9bb9eec commit 4154615

File tree

2 files changed

+23
-54
lines changed

2 files changed

+23
-54
lines changed

packages/react-native-node-api-modules/src/node/babel-plugin/plugin.test.ts

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import path from "node:path";
44

55
import { transformFileSync } from "@babel/core";
66

7-
import { plugin } from "./plugin.js";
7+
import { plugin, type PluginOptions } from "./plugin.js";
88
import { setupTempDirectory } from "../test-utils.js";
9-
import { getLibraryName } from "../path-utils.js";
109

1110
describe("plugin", () => {
1211
it("transforms require calls, regardless", (context) => {
@@ -38,66 +37,36 @@ describe("plugin", () => {
3837
`,
3938
});
4039

41-
const ADDON_1_REQUIRE_ARG = getLibraryName(
42-
path.join(tempDirectoryPath, "addon-1"),
43-
{ stripPathSuffix: false }
44-
);
45-
const ADDON_2_REQUIRE_ARG = getLibraryName(
46-
path.join(tempDirectoryPath, "addon-2"),
47-
{ stripPathSuffix: false }
48-
);
40+
const EXPECTED_PKG_NAME = "my-package";
4941

50-
{
51-
const result = transformFileSync(
52-
path.join(tempDirectoryPath, "./addon-1.js"),
53-
{ plugins: [[plugin, { stripPathSuffix: false }]] }
54-
);
55-
assert(result);
56-
const { code } = result;
57-
assert(
58-
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
59-
`Unexpected code: ${code}`
60-
);
61-
}
62-
63-
{
64-
const result = transformFileSync(
65-
path.join(tempDirectoryPath, "./addon-2.js"),
66-
{ plugins: [[plugin, { naming: "hash" }]] }
67-
);
68-
assert(result);
69-
const { code } = result;
70-
assert(
71-
code && code.includes(`requireNodeAddon("${ADDON_2_REQUIRE_ARG}")`),
72-
`Unexpected code: ${code}`
73-
);
74-
}
75-
76-
{
42+
type TestCaseParams = {
43+
resolvedPath: string;
44+
originalPath: string;
45+
inputFile: string;
46+
options?: PluginOptions;
47+
};
48+
const runTestCase = ({
49+
resolvedPath,
50+
originalPath,
51+
inputFile,
52+
options,
53+
}: TestCaseParams) => {
7754
const result = transformFileSync(
78-
path.join(tempDirectoryPath, "./sub-directory/addon-1.js"),
79-
{ plugins: [[plugin, { naming: "hash" }]] }
55+
path.join(tempDirectoryPath, inputFile),
56+
{ plugins: [[plugin, options]] }
8057
);
8158
assert(result);
8259
const { code } = result;
8360
assert(
84-
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
61+
code && code.includes(`requireNodeAddon("${resolvedPath}", "${EXPECTED_PKG_NAME}", "${originalPath}")`),
8562
`Unexpected code: ${code}`
8663
);
87-
}
64+
};
8865

89-
{
90-
const result = transformFileSync(
91-
path.join(tempDirectoryPath, "./addon-1-bindings.js"),
92-
{ plugins: [[plugin, { naming: "hash" }]] }
93-
);
94-
assert(result);
95-
const { code } = result;
96-
assert(
97-
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
98-
`Unexpected code: ${code}`
99-
);
100-
}
66+
runTestCase({ resolvedPath: "./addon-1.node", originalPath: "./addon-1.node", inputFile: "./addon-1.js" });
67+
runTestCase({ resolvedPath: "./addon-2.node", originalPath: "./addon-2.node", inputFile: "./addon-2.js", options: { naming: "hash" } });
68+
runTestCase({ resolvedPath: "./addon-1.node", originalPath: "../addon-1.node", inputFile: "./sub-directory/addon-1.js", options: { naming: "hash" } });
69+
runTestCase({ resolvedPath: "./addon-1.node", originalPath: "addon-1", inputFile: "./addon-1-bindings.js", options: { naming: "hash" } });
10170

10271
{
10372
const result = transformFileSync(

packages/react-native-node-api-modules/src/node/babel-plugin/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { readPackageSync } from "read-pkg";
88

99
import { isNodeApiModule } from "../path-utils";
1010

11-
type PluginOptions = {
11+
export type PluginOptions = {
1212
stripPathSuffix?: boolean;
1313
};
1414

0 commit comments

Comments
 (0)