Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default function transformer(file, api) {
- const j = api.jscodeshift;
- const root = j(file.source);
- const getClockMocker = j.variableDeclaration('const', [
- j.variableDeclarator(
- j.identifier('clock'),
- j.callExpression(j.identifier('ClockMocker.getClock'), [])
- )
- ]);
- const nextClockMocker = j.expressionStatement(j.callExpression(j.identifier('clock.next'), []));
- const uninstallClockMocker = j.expressionStatement(
- j.callExpression(j.identifier('ClockMocker.uninstall'), [])
- );
- const tests = root.find(j.CallExpression, {
- callee: {
- name: 'it'
- }
- });
- tests.forEach(test => {
- const blockStatement = test.value.arguments[1].body;
- const waitForReactRerenderFunctions = j(blockStatement).find(j.CallExpression, {
- callee: {
- name: 'waitForReactRerender'
- }
- });
- if (waitForReactRerenderFunctions.paths().length > 0) {
- // remove waitForReactRerender import
- root
- .find(j.ImportDefaultSpecifier, { local: { name: 'waitForReactRerender' } })
- .forEach(path => j(path.parentPath.parentPath).remove());
- // add ClockMocker import if needed
- if (root.find(j.ImportDefaultSpecifier, { local: { name: 'ClockMocker' } }).length === 0) {
- const cmImport = j.importDeclaration(
- [j.importDefaultSpecifier(j.identifier('ClockMocker'))],
- j.literal('test/testUtilities/ClockMocker')
- );
- root
- .find(j.Program)
- .get('body')
- .get(0)
- .insertBefore(cmImport);
- }
- const blockBody = j(blockStatement).get('body');
- blockBody.get(0).insertBefore(getClockMocker);
- blockBody.get(blockBody.value.length - 1).insertAfter(uninstallClockMocker);
- //make test sync
- j(test)
- .find(j.Identifier, { name: 'done' })
- .remove();
- }
- let FunctionContents = new Map();
- waitForReactRerenderFunctions.forEach(path => {
- // add clock.next(); to the beginning of each function
- j(path.value.arguments).forEach(arg => {
- const funcBody = j(arg).get('body');
- if (funcBody.value) {
- const existingValue = FunctionContents.get(path.parentPath) || [];
- FunctionContents.set(path.parentPath, [
- ...existingValue,
- nextClockMocker,
- ...funcBody.value.body
- ]);
- }
- });
- });
- FunctionContents.forEach((values, key) =>
- values.reverse().forEach(value => key.insertAfter(value))
- );
- waitForReactRerenderFunctions.remove();
- });
- return root.toSource();
- }
Add Comment
Please, Sign In to add comment