Skip to content

Commit

Permalink
feat: finished implementing reset method codemods
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 15, 2024
1 parent 21e54af commit e9d59df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 49 deletions.
59 changes: 16 additions & 43 deletions packages/codemods/src/__test__/method-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,75 +69,48 @@ describe('codemods operating on methods', () => {
it('rewrites restore()', () => {
expectCodemodResult(
'fetchMock.restore()',
`
fetchMock.clearHistory();
fetchMock.removeRoutes({
includeFallback: true,
});
fetchMock.unmockGlobal();
`,
`fetchMock.clearHistory();
fetchMock.removeRoutes();
fetchMock.unmockGlobal();`,
);
});
it('rewrites restore() with {sticky: true}', () => {
expectCodemodResult(
'fetchMock.restore({sticky: true})',
`
fetchMock.clearHistory();
fetchMock.removeRoutes({
includeSticky: true,
includeFallback: true,
});
fetchMock.unmockGlobal();
`,
`fetchMock.clearHistory();
fetchMock.removeRoutes({includeSticky: true});
fetchMock.unmockGlobal();`,
);
});
it('rewrites reset()', () => {
expectCodemodResult(
'fetchMock.reset()',
`
fetchMock.clearHistory();
fetchMock.removeRoutes({
includeFallback: true,
});
fetchMock.unmockGlobal();
`,
`fetchMock.clearHistory();
fetchMock.removeRoutes();
fetchMock.unmockGlobal();`,
);
});
it('rewrites reset() with {sticky: true}', () => {
expectCodemodResult(
'fetchMock.reset({sticky: true})',
`
fetchMock.clearHistory();
fetchMock.removeRoutes({
includeSticky: true,
includeFallback: true,
});
fetchMock.unmockGlobal();
`,
`fetchMock.clearHistory();
fetchMock.removeRoutes({includeSticky: true});
fetchMock.unmockGlobal();`,
);
});

it('rewrites resetBehavior()', () => {
expectCodemodResult(
'fetchMock.resetBehavior()',
`
fetchMock.removeRoutes({
includeFallback: true,
});
fetchMock.unmockGlobal();
`,
`fetchMock.removeRoutes();
fetchMock.unmockGlobal();`,
);
});
it('rewrites resetBehavior() with {sticky: true}', () => {
expectCodemodResult(
'fetchMock.resetBehavior({sticky: true})',
`
fetchMock.removeRoutes({
includeSticky: true,
includeFallback: true,
});
fetchMock.unmockGlobal();
`,
`fetchMock.removeRoutes({includeSticky: true});
fetchMock.unmockGlobal();`,
);
});
it('rewrites resetHistory()', () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function simpleMethods(fetchMockVariableName, root, j) {
}
});
});
['reset', 'restore'].forEach((methodName) => {
['reset', 'restore', 'resetBehavior'].forEach((methodName) => {
root
.find(j.CallExpression, {
callee: {
Expand All @@ -68,11 +68,12 @@ export function simpleMethods(fetchMockVariableName, root, j) {
},
})
.forEach((path) => {
const sticky = path?.value?.arguments[0]?.properties?.find(
(prop) => prop.key.name === 'sticky',
)?.value?.value;
const newExpressions = j(`
fetchMock.clearHistory();
fetchMock.removeRoutes({
includeFallback: true,
});
${methodName !== 'resetBehavior' ? 'fetchMock.clearHistory()' : ''};
fetchMock.removeRoutes(${sticky ? '{includeSticky: true}' : ''});
fetchMock.unmockGlobal();
`)
.find(j.ExpressionStatement)
Expand Down
2 changes: 1 addition & 1 deletion packages/codemods/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ console.log(
codemod(
`
import fetchMock from 'fetch-mock';
fetchMock.restore();
fetchMock.resetBehavior();
`,
jscodeshift,
),
Expand Down

0 comments on commit e9d59df

Please sign in to comment.