Skip to content

Commit

Permalink
feat: update codemods to use hardReset()
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Nov 15, 2024
1 parent c2c92a4 commit 757d480
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
22 changes: 4 additions & 18 deletions packages/codemods/src/__tests__/method-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,21 @@ describe('codemods operating on methods', () => {

describe('converting resetting methods', () => {
it('rewrites restore()', () => {
expectCodemodResult(
'fetchMock.restore()',
`fetchMock.clearHistory();
fetchMock.removeRoutes();
fetchMock.unmockGlobal();`,
);
expectCodemodResult('fetchMock.restore()', `fetchMock.hardReset();`);
});
it('rewrites restore() with {sticky: true}', () => {
expectCodemodResult(
'fetchMock.restore({sticky: true})',
`fetchMock.clearHistory();
fetchMock.removeRoutes({includeSticky: true});
fetchMock.unmockGlobal();`,
`fetchMock.hardReset({includeSticky: true});`,
);
});
it('rewrites reset()', () => {
expectCodemodResult(
'fetchMock.reset()',
`fetchMock.clearHistory();
fetchMock.removeRoutes();
fetchMock.unmockGlobal();`,
);
expectCodemodResult('fetchMock.reset()', `fetchMock.hardReset();`);
});
it('rewrites reset() with {sticky: true}', () => {
expectCodemodResult(
'fetchMock.reset({sticky: true})',
`fetchMock.clearHistory();
fetchMock.removeRoutes({includeSticky: true});
fetchMock.unmockGlobal();`,
`fetchMock.hardReset({includeSticky: true});`,
);
});

Expand Down
13 changes: 9 additions & 4 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ module.exports.simpleMethods = function (fetchMockVariableName, root) {
const sticky = path?.value?.arguments[0]?.properties?.find(
(prop) => prop.key.name === 'sticky',
)?.value?.value;
const newExpressions = j(`
${methodName !== 'resetBehavior' ? 'fetchMock.clearHistory()' : ''};
let expressionsString;
if (methodName === 'resetBehavior') {
expressionsString = `
fetchMock.removeRoutes(${sticky ? '{includeSticky: true}' : ''});
fetchMock.unmockGlobal();
`)
fetchMock.unmockGlobal();`;
} else {
expressionsString = `
fetchMock.hardReset(${sticky ? '{includeSticky: true}' : ''});`;
}
const newExpressions = j(expressionsString)
.find(j.ExpressionStatement)
.paths();
const insertLocation = j(path)
Expand Down
4 changes: 2 additions & 2 deletions packages/fetch-mock/src/FetchMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { MatcherDefinition, RouteMatcher } from './Matchers.js';
import CallHistory from './CallHistory.js';
import * as requestUtils from './RequestUtils.js';

export type HardResetOptions = {
export type HardResetOptions = {
includeSticky?: boolean;
}
};

export type FetchMockGlobalConfig = {
includeContentLength?: boolean;
Expand Down

0 comments on commit 757d480

Please sign in to comment.