Skip to content

Commit 801be0a

Browse files
test(metro): cover the globalClassNamePolyfill gate
The metro resolver rewrites react-native imports only when globalClassNamePolyfill is on, and nothing tested either side of that gate.
1 parent 068dd89 commit 801be0a

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import { resolve, sep } from "node:path";
2+
3+
import type { MetroConfig } from "metro-config";
4+
import type {
5+
CustomResolutionContext,
6+
CustomResolver,
7+
Resolution,
8+
} from "metro-resolver";
9+
10+
import { withReactNativeCSS } from "../../metro";
11+
12+
// globalClassNamePolyfill decides whether the resolvers run at all; this covers the dispatch into them
13+
14+
// The resolver reads two fields off the context, so the fixture bridges the rest of ResolutionContext once
15+
function makeContext(
16+
originModulePath: string,
17+
resolveRequest: CustomResolver,
18+
): CustomResolutionContext {
19+
return {
20+
originModulePath,
21+
resolveRequest,
22+
} as unknown as CustomResolutionContext;
23+
}
24+
25+
function makeRecorder(): { calls: string[]; resolver: CustomResolver } {
26+
const calls: string[] = [];
27+
28+
const resolver: CustomResolver = (_context, moduleName): Resolution => {
29+
calls.push(moduleName);
30+
return {
31+
type: "sourceFile",
32+
filePath: resolve("/app/node_modules", moduleName, "index.js"),
33+
};
34+
};
35+
36+
return { calls, resolver };
37+
}
38+
39+
function makeConfig(
40+
options?: Parameters<typeof withReactNativeCSS>[1],
41+
): MetroConfig {
42+
return withReactNativeCSS<MetroConfig>(
43+
{},
44+
{ disableTypeScriptGeneration: true, ...options },
45+
);
46+
}
47+
48+
function resolveThrough(
49+
config: MetroConfig,
50+
moduleName: string,
51+
platform: string | null,
52+
resolver: CustomResolver,
53+
): Resolution {
54+
const resolveRequest = config.resolver?.resolveRequest;
55+
56+
if (!resolveRequest) {
57+
throw new Error("withReactNativeCSS did not install a resolveRequest");
58+
}
59+
60+
return resolveRequest(
61+
makeContext(resolve("/app/index.js"), resolver),
62+
moduleName,
63+
platform,
64+
);
65+
}
66+
67+
describe("globalClassNamePolyfill", () => {
68+
test("is off by default, so react-native resolves untouched", () => {
69+
const { calls, resolver } = makeRecorder();
70+
71+
const resolution = resolveThrough(
72+
makeConfig(),
73+
"react-native",
74+
"ios",
75+
resolver,
76+
);
77+
78+
expect(calls).toStrictEqual(["react-native"]);
79+
expect(resolution).toStrictEqual({
80+
type: "sourceFile",
81+
filePath: resolve("/app/node_modules/react-native/index.js"),
82+
});
83+
});
84+
85+
test("routes react-native to the components barrel when on", () => {
86+
const { calls, resolver } = makeRecorder();
87+
88+
const resolution = resolveThrough(
89+
makeConfig({ globalClassNamePolyfill: true }),
90+
"react-native",
91+
"ios",
92+
resolver,
93+
);
94+
95+
expect(calls).toStrictEqual([
96+
"react-native",
97+
"react-native-css/components",
98+
]);
99+
expect(resolution).toStrictEqual({
100+
type: "sourceFile",
101+
filePath: resolve(
102+
"/app/node_modules/react-native-css/components/index.js",
103+
),
104+
});
105+
});
106+
107+
test("dispatches to the web resolver on the web platform when on", () => {
108+
const { calls, resolver } = makeRecorder();
109+
110+
const resolution = resolveThrough(
111+
makeConfig({ globalClassNamePolyfill: true }),
112+
"react-native-web/dist/exports/View",
113+
"web",
114+
resolver,
115+
);
116+
117+
// Only the web resolver rewrites a resolved react-native-web path; the native one keys on the module name
118+
expect(calls).toStrictEqual([
119+
"react-native-web/dist/exports/View",
120+
"react-native-css/components/View",
121+
]);
122+
expect(resolution).toStrictEqual({
123+
type: "sourceFile",
124+
filePath: resolve(
125+
"/app/node_modules/react-native-css/components/View/index.js",
126+
),
127+
});
128+
});
129+
130+
test("leaves the same web module alone when off", () => {
131+
const { calls, resolver } = makeRecorder();
132+
133+
const resolution = resolveThrough(
134+
makeConfig(),
135+
"react-native-web/dist/exports/View",
136+
"web",
137+
resolver,
138+
);
139+
140+
expect(calls).toStrictEqual(["react-native-web/dist/exports/View"]);
141+
expect(resolution).toStrictEqual({
142+
type: "sourceFile",
143+
filePath: resolve(
144+
"/app/node_modules/react-native-web/dist/exports/View/index.js",
145+
),
146+
});
147+
});
148+
149+
test.each([true, false])(
150+
"short-circuits the metro override without consulting the parent (polyfill: %s)",
151+
(globalClassNamePolyfill) => {
152+
const { calls, resolver } = makeRecorder();
153+
154+
const resolution = resolveThrough(
155+
makeConfig({ globalClassNamePolyfill }),
156+
"react-native-css-metro-override",
157+
"ios",
158+
resolver,
159+
);
160+
161+
expect(calls).toStrictEqual([]);
162+
expect(resolution).toStrictEqual({
163+
type: "sourceFile",
164+
filePath: expect.stringContaining(`${sep}override.`),
165+
});
166+
},
167+
);
168+
169+
test("prefers an existing resolveRequest over the one on the context", () => {
170+
const existing = makeRecorder();
171+
const fromContext = makeRecorder();
172+
173+
const config = withReactNativeCSS<MetroConfig>(
174+
{ resolver: { resolveRequest: existing.resolver } },
175+
{ disableTypeScriptGeneration: true, globalClassNamePolyfill: true },
176+
);
177+
178+
resolveThrough(config, "react-native", "ios", fromContext.resolver);
179+
180+
expect(existing.calls).toStrictEqual([
181+
"react-native",
182+
"react-native-css/components",
183+
]);
184+
expect(fromContext.calls).toStrictEqual([]);
185+
});
186+
187+
test("appends the css source extension regardless of the gate", () => {
188+
expect(makeConfig().resolver?.sourceExts).toStrictEqual(["css"]);
189+
});
190+
});

0 commit comments

Comments
 (0)