Skip to content

Commit 575d62b

Browse files
authored
Merge pull request #894 from tkhq/ethan/utils-unit-tests-v1
started adding unit tests for util functions
2 parents 5f2bc26 + bda604e commit 575d62b

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

packages/core/jest.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import("@jest/types").Config.InitialOptions} */
2+
const config = {
3+
transform: {
4+
"\\.[jt]sx?$": "@turnkey/jest-config/transformer.js",
5+
},
6+
testMatch: ["**/__tests__/**/*-(spec|test).[jt]s?(x)"],
7+
testPathIgnorePatterns: ["<rootDir>/dist/", "<rootDir>/node_modules/"],
8+
testTimeout: 30 * 1000, // For slow CI machines
9+
};
10+
11+
module.exports = config;

packages/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"build": "rollup -c",
1515
"clean": "rimraf ./dist ./.cache",
1616
"typecheck": "tsc -p tsconfig.typecheck.json",
17-
"prepublishOnly": "pnpm run clean && pnpm run build"
17+
"prepublishOnly": "pnpm run clean && pnpm run build",
18+
"test": "jest"
1819
},
1920
"keywords": [
2021
"turnkey"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { isWeb, isReactNative } from "../utils";
2+
import {
3+
describe,
4+
expect,
5+
jest,
6+
beforeEach,
7+
afterEach,
8+
it,
9+
} from "@jest/globals";
10+
11+
describe("platform detection", () => {
12+
const g = globalThis as any;
13+
14+
const saveGlobals = () => ({
15+
window: g.window,
16+
document: g.document,
17+
navigator: g.navigator,
18+
});
19+
20+
const restoreGlobals = (orig: any) => {
21+
if (typeof orig.window === "undefined") delete g.window;
22+
else g.window = orig.window;
23+
24+
if (typeof orig.document === "undefined") delete g.document;
25+
else g.document = orig.document;
26+
27+
if (typeof orig.navigator === "undefined") delete g.navigator;
28+
else g.navigator = orig.navigator;
29+
};
30+
31+
let orig: any;
32+
33+
beforeEach(() => {
34+
orig = saveGlobals();
35+
// Start clean: no browser-like globals unless a test sets them.
36+
delete g.window;
37+
delete g.document;
38+
delete g.navigator;
39+
jest.resetModules(); // ensures fresh imports if needed later
40+
});
41+
42+
afterEach(() => {
43+
restoreGlobals(orig);
44+
jest.restoreAllMocks();
45+
});
46+
47+
it("returns false for both on plain Node (no globals)", () => {
48+
expect(isWeb()).toBe(false);
49+
expect(isReactNative()).toBe(false);
50+
});
51+
52+
it("isWeb() true when window and document exist", () => {
53+
g.window = {}; // minimal stubs are fine since you only check typeof
54+
g.document = {};
55+
expect(isWeb()).toBe(true);
56+
expect(isReactNative()).toBe(false);
57+
});
58+
59+
it("isWeb() false if only window exists", () => {
60+
g.window = {};
61+
expect(isWeb()).toBe(false);
62+
});
63+
64+
it("isWeb() false if only document exists", () => {
65+
g.document = {};
66+
expect(isWeb()).toBe(false);
67+
});
68+
69+
it("isReactNative() true when navigator.product === 'ReactNative'", () => {
70+
g.navigator = { product: "ReactNative" };
71+
expect(isReactNative()).toBe(true);
72+
expect(isWeb()).toBe(false);
73+
});
74+
75+
it("isReactNative() false when navigator exists but product differs", () => {
76+
g.navigator = { product: "Gecko" };
77+
expect(isReactNative()).toBe(false);
78+
});
79+
80+
it("isReactNative() false when navigator missing", () => {
81+
expect(isReactNative()).toBe(false);
82+
});
83+
84+
it("does not accidentally treat web + RN at the same time", () => {
85+
g.window = {};
86+
g.document = {};
87+
g.navigator = { product: "ReactNative" };
88+
// Your current logic would report both true; assert current behavior or
89+
// change requirements. Here we assert the current behavior explicitly.
90+
expect(isWeb()).toBe(true);
91+
expect(isReactNative()).toBe(true);
92+
});
93+
});

packages/core/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
EvmChainInfo,
2727
SolanaChainInfo,
2828
Curve,
29-
} from "@types";
29+
} from "./__types__/base";
3030
import bs58 from "bs58";
3131

3232
// Import all defaultAccountAtIndex functions for each address format

0 commit comments

Comments
 (0)