Skip to content

Commit

Permalink
feat: lastOptions() and lastResponse()
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 1, 2024
1 parent a115a27 commit 300e0b8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
46 changes: 46 additions & 0 deletions packages/codemods/src/__test__/method-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 27 additions & 19 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
}

0 comments on commit 300e0b8

Please sign in to comment.