Skip to content

Commit

Permalink
fix: refactor zone patch test method
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Mar 13, 2020
1 parent 78b96a3 commit ab14e6b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 59 deletions.
16 changes: 16 additions & 0 deletions example/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ describe('AppComponent', () => {
expect(arg2).toBe(2);
done();
});

it.each`
foo | bar
${1} | ${2}
`('it.each should work with table as a tagged template literal', ({foo, bar}) => {
expect(foo).toBe(1);
expect(bar).toBe(2);
});

it.each`
foo | bar
${1} | ${2}
`('(async) it.each should work with table as a tagged template literal', async ({foo, bar}) => {
expect(foo).toBe(1);
expect(bar).toBe(2);
});
});

test.todo('a sample todo');
71 changes: 12 additions & 59 deletions src/zone-patch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,21 @@ function wrapTestInZone(testBody, eachArgs) {
return;
}

if (!eachArgs || eachArgs.length === 0 || eachArgs[0].length === 0) {
// If we are not handling `test.each`, then the parameter of `testBody`
// will be 0 or 1, if it is 1, then we need to return a function with
// done parameter
const wrappedFunc = function() {
return testProxyZone.run(testBody, null, arguments);
};
try {
Object.defineProperty(wrappedFunc, 'length', {
configurable: true,
writable: true
});
wrappedFunc.length = testBody.length;
} catch (e) {
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
} else {
// Dynamically create a Function to contain the same length
// of the parameters as the testBody
// For example:
// ```
// test.each([[1, 2]])('test.each', (arg1, arg2) => {});
// ```
// In this case we need to return a function like this
// ```
// return function(arg1, arg2) {
// return testProxyZone.run(testBody, null, [arg1, arg2]);
// }
// ```
const len = eachArgs[0].length;
const args = [];
let argString = '';
for (let i = 0; i < len; i++) {
args.push('arg' + i);
argString += 'arg' + i;
if (i !== len - 1) {
argString += ', ';
}
}
args.push('testBody');
args.push('testProxyZone');
if (len < testBody.length) {
args.push('done');
argString += ', done';
}
const funcBody = `
return testProxyZone.run(testBody, null, [${argString}])`;
return new Function(args, funcBody);
}
return wrappedFunc;
}

/**
Expand All @@ -108,29 +83,7 @@ const bindDescribe = originalJestFn =>
const bindTest = originalJestFn =>
function(...eachArgs) {
return function(...args) {
const testBody = args[1];
if (
testBody.length > 0 &&
Array.isArray(eachArgs) &&
eachArgs.length > 0 &&
eachArgs[0].length > 0
) {
// check whether eachArgs is a 1D array
if (!Array.isArray(eachArgs[0][0])) {
// transfer eachArgs from 1D to 2D array
eachArgs = eachArgs.map(row => row.map(a => [a]));
}
}
args[1] = wrapTestInZone(args[1], ...eachArgs);
if (testBody.length > 0 || (eachArgs.length > 0 && eachArgs[0].length > 0)) {
eachArgs.forEach(row => {
const modifiedRow = row.map(a => {
a.push(testBody);
a.push(testProxyZone);
});
return modifiedRow;
});
}
args[1] = wrapTestInZone(args[1]);
return originalJestFn.apply(this, eachArgs).apply(this, args);
};
};
Expand Down

0 comments on commit ab14e6b

Please sign in to comment.