From 3b7daf71663662265c63169ba036b13e0856b053 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sun, 1 Sep 2024 15:49:30 +0100 Subject: [PATCH] feat: use get("*") as a better replacement for getAny() --- .../src/__test__/method-codemods.test.js | 22 +++----- packages/codemods/src/codemods/methods.js | 54 ++++++++++--------- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/packages/codemods/src/__test__/method-codemods.test.js b/packages/codemods/src/__test__/method-codemods.test.js index c44d0014..3814dd5e 100644 --- a/packages/codemods/src/__test__/method-codemods.test.js +++ b/packages/codemods/src/__test__/method-codemods.test.js @@ -136,49 +136,43 @@ describe('codemods operating on methods', () => { }); ['get', 'post', 'put', 'delete', 'head', 'patch'].forEach((method) => { - describe(`${method}Any() -> any()`, () => { + describe(`${method}Any() -> ${method}("*")`, () => { it('when only has response', () => { expectCodemodResult( `fetchMock.${method}Any(200)`, - `fetchMock.any(200, {method: '${method}'})`, + `fetchMock.${method}("*", 200)`, ); }); it('when has additional options', () => { expectCodemodResult( `fetchMock.${method}Any(200, {name: "my-route"})`, - `fetchMock.any(200, { - name: "my-route", - method: '${method}' -})`, + `fetchMock.${method}("*", 200, {name: "my-route"})`, ); }); it('when has name', () => { expectCodemodResult( `fetchMock.${method}Any(200, "my-route")`, - `fetchMock.any(200, {name: "my-route", method: '${method}'})`, + `fetchMock.${method}("*", 200, "my-route")`, ); }); }); - describe(`${method}AnyOnce() -> anyOnce()`, () => { + describe(`${method}AnyOnce() -> ${method}Once("*")`, () => { it('when only has response', () => { expectCodemodResult( `fetchMock.${method}AnyOnce(200)`, - `fetchMock.anyOnce(200, {method: '${method}'})`, + `fetchMock.${method}Once("*", 200)`, ); }); it('when has additional options', () => { expectCodemodResult( `fetchMock.${method}AnyOnce(200, {name: "my-route"})`, - `fetchMock.anyOnce(200, { - name: "my-route", - method: '${method}' -})`, + `fetchMock.${method}Once("*", 200, {name: "my-route"})`, ); }); it('when has name', () => { expectCodemodResult( `fetchMock.${method}AnyOnce(200, "my-route")`, - `fetchMock.anyOnce(200, {name: "my-route", method: '${method}'})`, + `fetchMock.${method}Once("*", 200, "my-route")`, ); }); }); diff --git a/packages/codemods/src/codemods/methods.js b/packages/codemods/src/codemods/methods.js index 4e1517e7..21edd1dc 100644 --- a/packages/codemods/src/codemods/methods.js +++ b/packages/codemods/src/codemods/methods.js @@ -25,35 +25,37 @@ export function simpleMethods(fetchMockVariableName, root, j) { path.value.callee.property.name = 'route'; } ['get', 'post', 'put', 'delete', 'head', 'patch'].some((httpMethod) => { - let applyMethod = false; + let prependStar = false; if (method === `${httpMethod}Any`) { - applyMethod = true; - path.value.callee.property.name = 'any'; + prependStar = true; + path.value.callee.property.name = httpMethod; } else if (method === `${httpMethod}AnyOnce`) { - applyMethod = true; - path.value.callee.property.name = 'anyOnce'; + prependStar = true; + path.value.callee.property.name = `${httpMethod}Once`; } - 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, - ); - } + if (prependStar) { + // const options = + path.value.arguments.unshift(j.stringLiteral('*')); + // [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, + // ); + // } } }); });