diff --git a/packages/codemods/src/__test__/method-codemods.test.js b/packages/codemods/src/__test__/method-codemods.test.js index 0155076e..c44d0014 100644 --- a/packages/codemods/src/__test__/method-codemods.test.js +++ b/packages/codemods/src/__test__/method-codemods.test.js @@ -135,11 +135,56 @@ describe('codemods operating on methods', () => { }); }); - // - // .lastOptions() => .callHistory.lastCall()?.options - // .lastResponse() => .callHistory.lastCall()?.response + ['get', 'post', 'put', 'delete', 'head', 'patch'].forEach((method) => { + describe(`${method}Any() -> any()`, () => { + it('when only has response', () => { + expectCodemodResult( + `fetchMock.${method}Any(200)`, + `fetchMock.any(200, {method: '${method}'})`, + ); + }); + it('when has additional options', () => { + expectCodemodResult( + `fetchMock.${method}Any(200, {name: "my-route"})`, + `fetchMock.any(200, { + name: "my-route", + method: '${method}' +})`, + ); + }); + it('when has name', () => { + expectCodemodResult( + `fetchMock.${method}Any(200, "my-route")`, + `fetchMock.any(200, {name: "my-route", method: '${method}'})`, + ); + }); + }); + describe(`${method}AnyOnce() -> anyOnce()`, () => { + it('when only has response', () => { + expectCodemodResult( + `fetchMock.${method}AnyOnce(200)`, + `fetchMock.anyOnce(200, {method: '${method}'})`, + ); + }); + it('when has additional options', () => { + expectCodemodResult( + `fetchMock.${method}AnyOnce(200, {name: "my-route"})`, + `fetchMock.anyOnce(200, { + name: "my-route", + method: '${method}' +})`, + ); + }); + it('when has name', () => { + expectCodemodResult( + `fetchMock.${method}AnyOnce(200, "my-route")`, + `fetchMock.anyOnce(200, {name: "my-route", method: '${method}'})`, + ); + }); + }); + }); + // .sandbox() => .fetchHandler(and maybe a comment about.createInstance()) - // .getAny(), .postAny(), .putAny(), .deleteAny(), .headAny(), .patchAny(), .getAnyOnce(), .postAnyOnce(), .putAnyOnce(), .deleteAnyOnce(), .headAnyOnce(), .patchAnyOnce() => calls to the underlying method + options // restore() / reset()... once I've decided how to implement these // lastCall() => try to change uses of this to expect a callLog, but probably just insert a commemnt / error }); diff --git a/packages/codemods/src/codemods/methods.js b/packages/codemods/src/codemods/methods.js index affa512c..4e1517e7 100644 --- a/packages/codemods/src/codemods/methods.js +++ b/packages/codemods/src/codemods/methods.js @@ -24,6 +24,38 @@ export function simpleMethods(fetchMockVariableName, root, j) { if (method === 'mock') { path.value.callee.property.name = 'route'; } + ['get', 'post', 'put', 'delete', 'head', 'patch'].some((httpMethod) => { + let applyMethod = false; + if (method === `${httpMethod}Any`) { + applyMethod = true; + path.value.callee.property.name = 'any'; + } else if (method === `${httpMethod}AnyOnce`) { + applyMethod = true; + path.value.callee.property.name = 'anyOnce'; + } + if (applyMethod) { + const options = path.value.arguments[1]; + if (!options) { + path.value.arguments.push( + j(`const options = {method: '${httpMethod}'}`) + .find(j.ObjectExpression) + .get().value, + ); + } else if (options.type === 'Literal') { + path.value.arguments[1] = j( + `const options = {name: ${options.raw}, method: '${httpMethod}'}`, + ) + .find(j.ObjectExpression) + .get().value; + } else if (options.type === 'ObjectExpression') { + options.properties.push( + j(`const options = {method: '${httpMethod}'}`) + .find(j.Property) + .get().value, + ); + } + } + }); }); [ diff --git a/packages/codemods/try.js b/packages/codemods/try.js index 0bb45caa..8e8ef89e 100644 --- a/packages/codemods/try.js +++ b/packages/codemods/try.js @@ -5,7 +5,7 @@ console.log( codemod( ` import fetchMock from 'fetch-mock'; -fetchMock.lastUrl(1, 2) +fetchMock.getAny(200, {name: 'who'}) `, jscodeshift, ),