Skip to content

Commit

Permalink
feat: support converting getAny() etc
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 1, 2024
1 parent 300e0b8 commit 1ef70ec
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
53 changes: 49 additions & 4 deletions packages/codemods/src/__test__/method-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,56 @@ describe('codemods operating on methods', () => {
});
});

//
// .lastOptions() => .callHistory.lastCall()?.options
// .lastResponse() => .callHistory.lastCall()?.response
['get', 'post', 'put', 'delete', 'head', 'patch'].forEach((method) => {
describe(`${method}Any() -> any()`, () => {
it('when only has response', () => {
expectCodemodResult(
`fetchMock.${method}Any(200)`,
`fetchMock.any(200, {method: '${method}'})`,
);
});
it('when has additional options', () => {
expectCodemodResult(
`fetchMock.${method}Any(200, {name: "my-route"})`,
`fetchMock.any(200, {
name: "my-route",
method: '${method}'
})`,
);
});
it('when has name', () => {
expectCodemodResult(
`fetchMock.${method}Any(200, "my-route")`,
`fetchMock.any(200, {name: "my-route", method: '${method}'})`,
);
});
});
describe(`${method}AnyOnce() -> anyOnce()`, () => {
it('when only has response', () => {
expectCodemodResult(
`fetchMock.${method}AnyOnce(200)`,
`fetchMock.anyOnce(200, {method: '${method}'})`,
);
});
it('when has additional options', () => {
expectCodemodResult(
`fetchMock.${method}AnyOnce(200, {name: "my-route"})`,
`fetchMock.anyOnce(200, {
name: "my-route",
method: '${method}'
})`,
);
});
it('when has name', () => {
expectCodemodResult(
`fetchMock.${method}AnyOnce(200, "my-route")`,
`fetchMock.anyOnce(200, {name: "my-route", method: '${method}'})`,
);
});
});
});

// .sandbox() => .fetchHandler(and maybe a comment about.createInstance())
// .getAny(), .postAny(), .putAny(), .deleteAny(), .headAny(), .patchAny(), .getAnyOnce(), .postAnyOnce(), .putAnyOnce(), .deleteAnyOnce(), .headAnyOnce(), .patchAnyOnce() => calls to the underlying method + options
// restore() / reset()... once I've decided how to implement these
// lastCall() => try to change uses of this to expect a callLog, but probably just insert a commemnt / error
});
32 changes: 32 additions & 0 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,38 @@ export function simpleMethods(fetchMockVariableName, root, j) {
if (method === 'mock') {
path.value.callee.property.name = 'route';
}
['get', 'post', 'put', 'delete', 'head', 'patch'].some((httpMethod) => {
let applyMethod = false;
if (method === `${httpMethod}Any`) {
applyMethod = true;
path.value.callee.property.name = 'any';
} else if (method === `${httpMethod}AnyOnce`) {
applyMethod = true;
path.value.callee.property.name = 'anyOnce';
}
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,
);
}
}
});
});

[
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.lastUrl(1, 2)
fetchMock.getAny(200, {name: 'who'})
`,
jscodeshift,
),
Expand Down

0 comments on commit 1ef70ec

Please sign in to comment.