Test infra: Support gate('enableFeatureFlag') (#30760)
Shortcut for the common case where only a single flag is checked. Same as `gate(flags => flags.enableFeatureFlag)`. Normally I don't care about these types of conveniences but I'm about to add a lot more inline flag checks these all over our tests and it gets noisy. This helps a bit.
Andrew Clark committed
Aug 20, 2024 at 16:40 UTC
e831c232787400474673051d63df4aaf6c01bdeb
2 files changed
+19
-4
scripts/babel/__tests__/transform-test-gate-pragma-test.js
+4
@@ -221,4 +221,8 @@ describe('dynamic gate method', () => {
221
it('returns same conditions as pragma', () => {
222
expect(gate(ctx => ctx.experimental && ctx.__DEV__)).toBe(true);
223
});
224
+
225
+ it('converts string conditions to accessor function', () => {
226
+ expect(gate('experimental')).toBe(gate(flags => flags.experimental));
227
+ });
228
});
scripts/jest/setupTests.js
+15
-4
@@ -205,8 +205,17 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
205
}
206
};
207
208
+ const coerceGateConditionToFunction = gateFnOrString => {
209
+ return typeof gateFnOrString === 'string'
210
+ ? // `gate('foo')` is treated as equivalent to `gate(flags => flags.foo)`
211
+ flags => flags[gateFnOrString]
212
+ : // Assume this is already a function
213
+ gateFnOrString;
214
+ };
215
+
216
const gatedErrorMessage = 'Gated test was expected to fail, but it passed.';
209
- global._test_gate = (gateFn, testName, callback, timeoutMS) => {
217
+ global._test_gate = (gateFnOrString, testName, callback, timeoutMS) => {
218
+ const gateFn = coerceGateConditionToFunction(gateFnOrString);
219
let shouldPass;
220
try {
221
const flags = getTestFlags();
@@ -230,7 +239,8 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
239
expectTestToFail(callback, error, timeoutMS));
240
}
241
};
233
- global._test_gate_focus = (gateFn, testName, callback, timeoutMS) => {
242
+ global._test_gate_focus = (gateFnOrString, testName, callback, timeoutMS) => {
243
+ const gateFn = coerceGateConditionToFunction(gateFnOrString);
244
let shouldPass;
245
try {
246
const flags = getTestFlags();
@@ -259,8 +269,9 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
269
};
270
271
// Dynamic version of @gate pragma
262
- global.gate = fn => {
272
+ global.gate = gateFnOrString => {
273
+ const gateFn = coerceGateConditionToFunction(gateFnOrString);
274
const flags = getTestFlags();
264
- return fn(flags);
275
+ return gateFn(flags);
276
};
277
}