Skip to content

Commit

Permalink
feat: use get("*") as a better replacement for getAny()
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 1, 2024
1 parent 1ef70ec commit 3b7daf7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
22 changes: 8 additions & 14 deletions packages/codemods/src/__test__/method-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,49 +136,43 @@ describe('codemods operating on methods', () => {
});

['get', 'post', 'put', 'delete', 'head', 'patch'].forEach((method) => {
describe(`${method}Any() -> any()`, () => {
describe(`${method}Any() -> ${method}("*")`, () => {
it('when only has response', () => {
expectCodemodResult(
`fetchMock.${method}Any(200)`,
`fetchMock.any(200, {method: '${method}'})`,
`fetchMock.${method}("*", 200)`,
);
});
it('when has additional options', () => {
expectCodemodResult(
`fetchMock.${method}Any(200, {name: "my-route"})`,
`fetchMock.any(200, {
name: "my-route",
method: '${method}'
})`,
`fetchMock.${method}("*", 200, {name: "my-route"})`,
);
});
it('when has name', () => {
expectCodemodResult(
`fetchMock.${method}Any(200, "my-route")`,
`fetchMock.any(200, {name: "my-route", method: '${method}'})`,
`fetchMock.${method}("*", 200, "my-route")`,
);
});
});
describe(`${method}AnyOnce() -> anyOnce()`, () => {
describe(`${method}AnyOnce() -> ${method}Once("*")`, () => {
it('when only has response', () => {
expectCodemodResult(
`fetchMock.${method}AnyOnce(200)`,
`fetchMock.anyOnce(200, {method: '${method}'})`,
`fetchMock.${method}Once("*", 200)`,
);
});
it('when has additional options', () => {
expectCodemodResult(
`fetchMock.${method}AnyOnce(200, {name: "my-route"})`,
`fetchMock.anyOnce(200, {
name: "my-route",
method: '${method}'
})`,
`fetchMock.${method}Once("*", 200, {name: "my-route"})`,
);
});
it('when has name', () => {
expectCodemodResult(
`fetchMock.${method}AnyOnce(200, "my-route")`,
`fetchMock.anyOnce(200, {name: "my-route", method: '${method}'})`,
`fetchMock.${method}Once("*", 200, "my-route")`,
);
});
});
Expand Down
54 changes: 28 additions & 26 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,37 @@ export function simpleMethods(fetchMockVariableName, root, j) {
path.value.callee.property.name = 'route';
}
['get', 'post', 'put', 'delete', 'head', 'patch'].some((httpMethod) => {
let applyMethod = false;
let prependStar = false;
if (method === `${httpMethod}Any`) {
applyMethod = true;
path.value.callee.property.name = 'any';
prependStar = true;
path.value.callee.property.name = httpMethod;
} else if (method === `${httpMethod}AnyOnce`) {
applyMethod = true;
path.value.callee.property.name = 'anyOnce';
prependStar = true;
path.value.callee.property.name = `${httpMethod}Once`;
}
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,
);
}
if (prependStar) {
// const options =
path.value.arguments.unshift(j.stringLiteral('*'));
// [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

0 comments on commit 3b7daf7

Please sign in to comment.