Skip to content

Commit 6f051ab

Browse files
committed
Test infra: Support gate('enableFeatureFlag')
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.
1 parent 92d26c8 commit 6f051ab

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

scripts/babel/__tests__/transform-test-gate-pragma-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,8 @@ describe('dynamic gate method', () => {
221221
it('returns same conditions as pragma', () => {
222222
expect(gate(ctx => ctx.experimental && ctx.__DEV__)).toBe(true);
223223
});
224+
225+
it('converts string conditions to accessor function', () => {
226+
expect(gate('experimental')).toBe(gate(flags => flags.experimental));
227+
});
224228
});

scripts/jest/setupTests.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,17 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
205205
}
206206
};
207207

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+
208216
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);
210219
let shouldPass;
211220
try {
212221
const flags = getTestFlags();
@@ -230,7 +239,8 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
230239
expectTestToFail(callback, error, timeoutMS));
231240
}
232241
};
233-
global._test_gate_focus = (gateFn, testName, callback, timeoutMS) => {
242+
global._test_gate_focus = (gateFnOrString, testName, callback, timeoutMS) => {
243+
const gateFn = coerceGateConditionToFunction(gateFnOrString);
234244
let shouldPass;
235245
try {
236246
const flags = getTestFlags();
@@ -259,8 +269,9 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
259269
};
260270

261271
// Dynamic version of @gate pragma
262-
global.gate = fn => {
272+
global.gate = gateFnOrString => {
273+
const gateFn = coerceGateConditionToFunction(gateFnOrString);
263274
const flags = getTestFlags();
264-
return fn(flags);
275+
return gateFn(flags);
265276
};
266277
}

0 commit comments

Comments
 (0)