From c070f7e17f4149e9cb8f5c0e59141dfc27fda67d Mon Sep 17 00:00:00 2001 From: Kaustav Das Modak Date: Thu, 11 Feb 2016 16:50:30 +0530 Subject: [PATCH] Support refactoring of async calls. Refs #14 Signed-off-by: Kaustav Das Modak --- src/refactor.js | 2 ++ test/refactor-test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/refactor.js b/src/refactor.js index 94e424d8..e418f965 100644 --- a/src/refactor.js +++ b/src/refactor.js @@ -8,12 +8,14 @@ var processNode = function (node, req) { node.arguments[0].value = req.newStepValue.parameterizedStepValue; node.arguments[0].raw = "\"" + req.newStepValue.parameterizedStepValue + "\""; if (node.arguments[1] && node.arguments[1].type === "FunctionExpression") { + var asyncparams = node.arguments[1].params.slice(req.oldStepValue.parameters.length); node.arguments[1].params = req.newStepValue.parameters.map(function (param) { return { type: "Identifier", name: param }; }); + node.arguments[1].params = node.arguments[1].params.concat(asyncparams); } return node; }; diff --git a/test/refactor-test.js b/test/refactor-test.js index 38b320b9..53484ed7 100644 --- a/test/refactor-test.js +++ b/test/refactor-test.js @@ -226,4 +226,40 @@ describe( "Refactor", function () { assert.strictEqual( contentOutput, "gauge('The word has vowels and ends with .', function (word, number, end_letter) {\n});"); }); + + it( "Should perform refactoring while retaining callbacks for async step implementation calls", function () { + contentInput = "gauge('The word has vowels.', function (word, number, done) {\n});"; + request = { + refactorRequest: { + oldStepValue: { + stepValue: "The word {} has {} vowels.", + parameterizedStepValue: "The word has vowels.", + parameters: [ "word", "number" ] + }, + newStepValue: { + stepValue: "This English word {} has {} vowels.", + parameterizedStepValue: "This English word has vowels.", + parameters: [ "word", "numbers" ] + }, + paramPositions: [ { + oldPosition: -1, + newPosition: 1 + }, { + oldPosition: 0, + newPosition: 0 + } ] + } + }; + + info = { + fn: function ( word, number, done ) { word = "such"; number = "wow"; done = "phew."; }, + stepText: "The word has vowels.", + generalisedText: "The word {} has {} vowels.", + filePath: "test/data/refactor-output.js" + }; + + response = refactor( request, response ); + + assert.strictEqual( contentOutput, "gauge('This English word has vowels.', function (word, numbers, done) {\n});"); + }); });