'use strict';
describe('transform-test-gate-pragma', () => {
const _test_gate = (gateFn, testName, cb) => {
it(testName, (...args) => {
shouldPass = gateFn(context);
return cb(...args);
});
};
const _test_gate_focus = (gateFn, testName, cb) => {
it(testName, (...args) => {
shouldPass = gateFn(context);
isFocused = true;
return cb(...args);
});
};
const context = {
flagThatIsOff: false,
flagThatIsOn: true,
environment: 'fake-environment',
};
let shouldPass;
let isFocused;
beforeEach(() => {
shouldPass = null;
isFocused = false;
});
it('no pragma', () => {
expect(shouldPass).toBe(null);
});
it('no pragma, unrelated comment', () => {
expect(shouldPass).toBe(null);
});
it('basic positive test', () => {
expect(shouldPass).toBe(true);
});
it('basic negative test', () => {
expect(shouldPass).toBe(false);
});
it('method', () => {
expect(shouldPass).toBe(true);
});
it.only('test.only', () => {
expect(isFocused).toBe(true);
expect(shouldPass).toBe(true);
});
it.only('it.only', () => {
expect(isFocused).toBe(true);
expect(shouldPass).toBe(false);
});
it.only('fit', () => {
expect(isFocused).toBe(true);
expect(shouldPass).toBe(true);
});
it('flag negation', () => {
expect(shouldPass).toBe(true);
});
it('multiple gates', () => {
expect(shouldPass).toBe(true);
});
it('multiple gates 2', () => {
expect(shouldPass).toBe(false);
});
it('&&', () => {
expect(shouldPass).toBe(true);
});
it('||', () => {
expect(shouldPass).toBe(true);
});
it('groups', () => {
expect(shouldPass).toBe(true);
});
it('==', () => {
expect(shouldPass).toBe(true);
});
it('===', () => {
expect(shouldPass).toBe(true);
});
it('!=', () => {
expect(shouldPass).toBe(false);
});
it('!==', () => {
expect(shouldPass).toBe(false);
});
it('true', () => {
expect(shouldPass).toBe(true);
});
it('false', () => {
expect(shouldPass).toBe(true);
});
it('double quoted strings', () => {
expect(shouldPass).toBe(true);
});
it('single quoted strings', () => {
expect(shouldPass).toBe(true);
});
it('line comment', () => {
expect(shouldPass).toBe(true);
});
});
describe('transform test-gate-pragma: actual runtime', () => {
it('__DEV__', () => {
if (!__DEV__) {
throw Error("Doesn't work in production!");
}
});
it('strings', () => {
if (!__DEV__) {
throw Error("Doesn't work in production!");
}
});
it('works with console.error tracking', () => {
console.error('Should cause test to fail');
});
it('works with console.warn tracking', () => {
console.warn('Should cause test to fail');
});
it('works with console tracking if error is thrown before end of test', () => {
console.warn('Please stop that!');
console.error('Stop that!');
throw Error('I told you to stop!');
});
it('a global error event is treated as a test failure', () => {
dispatchEvent(
new ErrorEvent('error', {
error: new Error('Oops!'),
})
);
});
});
describe('dynamic gate method', () => {
it('returns same conditions as pragma', () => {
expect(gate(ctx => ctx.experimental && ctx.__DEV__)).toBe(true);
});
it('converts string conditions to accessor function', () => {
expect(gate('experimental')).toBe(gate(flags => flags.experimental));
});
});