diff --git a/packages/codemods/src/__test__/method-codemods.test.js b/packages/codemods/src/__test__/method-codemods.test.js index b16ad0f0..0155076e 100644 --- a/packages/codemods/src/__test__/method-codemods.test.js +++ b/packages/codemods/src/__test__/method-codemods.test.js @@ -89,6 +89,52 @@ describe('codemods operating on methods', () => { }); }); + describe('converting lastOptions()', () => { + it('single .lastOptions()', () => { + expectCodemodResult( + 'fetchMock.lastOptions()', + 'fetchMock.callHistory.lastCall()?.options', + ); + }); + it('lastOptions() with arguments', () => { + expectCodemodResult( + `fetchMock.lastOptions('name', {method: 'get'})`, + `fetchMock.callHistory.lastCall('name', {method: 'get'})?.options`, + ); + }); + + it('works with other names for fetch-mock', () => { + expectCodemodResult( + `fm.lastOptions('name', {method: 'get'})`, + `fm.callHistory.lastCall('name', {method: 'get'})?.options`, + 'fm', + ); + }); + }); + + describe('converting lastResponse()', () => { + it('single .lastResponse()', () => { + expectCodemodResult( + 'fetchMock.lastResponse()', + 'fetchMock.callHistory.lastCall()?.response', + ); + }); + it('lastResponse() with arguments', () => { + expectCodemodResult( + `fetchMock.lastResponse('name', {method: 'get'})`, + `fetchMock.callHistory.lastCall('name', {method: 'get'})?.response`, + ); + }); + + it('works with other names for fetch-mock', () => { + expectCodemodResult( + `fm.lastResponse('name', {method: 'get'})`, + `fm.callHistory.lastCall('name', {method: 'get'})?.response`, + 'fm', + ); + }); + }); + // // .lastOptions() => .callHistory.lastCall()?.options // .lastResponse() => .callHistory.lastCall()?.response diff --git a/packages/codemods/src/codemods/methods.js b/packages/codemods/src/codemods/methods.js index 4075b793..affa512c 100644 --- a/packages/codemods/src/codemods/methods.js +++ b/packages/codemods/src/codemods/methods.js @@ -25,25 +25,33 @@ export function simpleMethods(fetchMockVariableName, root, j) { path.value.callee.property.name = 'route'; } }); - const lastUrl = root - .find(j.CallExpression, { - callee: { - object: { - type: 'Identifier', - name: fetchMockVariableName, - }, - property: { - name: 'lastUrl', - }, - }, - }) - .closest(j.ExpressionStatement); - lastUrl.replaceWith((path) => { - const oldCall = j(path).find(j.CallExpression).get(); - const builder = j(`${fetchMockVariableName}.callHistory.lastCall()?.url`); - const newCall = builder.find(j.CallExpression).get(); - newCall.value.arguments = oldCall.value.arguments; - return builder.find(j.ExpressionStatement).get().value; + [ + ['lastUrl', 'url'], + ['lastOptions', 'options'], + ['lastResponse', 'response'], + ].forEach(([oldMethod, newProperty]) => { + root + .find(j.CallExpression, { + callee: { + object: { + type: 'Identifier', + name: fetchMockVariableName, + }, + property: { + name: oldMethod, + }, + }, + }) + .closest(j.ExpressionStatement) + .replaceWith((path) => { + const oldCall = j(path).find(j.CallExpression).get(); + const builder = j( + `${fetchMockVariableName}.callHistory.lastCall()?.${newProperty}`, + ); + const newCall = builder.find(j.CallExpression).get(); + newCall.value.arguments = oldCall.value.arguments; + return builder.find(j.ExpressionStatement).get().value; + }); }); }