-
-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: generate mocks for all responses #1284
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure if I should commit this sample with multiple responses defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I think adding samples/tests is always a good idea. Just my two cents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a sample project is unnecessary. As I commented elsewhere, I want to control this response as an option, and by default I think it is sufficient to output only the minimum success status, so it is sufficient to check it in the test rather than in the sample project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed the sample project and added a config test.
I'll check it in order 👍 |
@huwshimi |
Making it optional sounds like a good idea. I've modified this to add that config option. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thaks! I made a comment, please check it.
packages/mock/src/msw/index.ts
Outdated
|
||
if ( | ||
generatorOptions.mock && | ||
'generateEachHttpStatus' in generatorOptions.mock && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this existence check unnecessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.mock
can be an object or a function, this is probably more readable if it uses isObject(generatorOptions.mock)
so I'll change it to that.
const handlerName = `get${pascal(operationId)}MockHandler`; | ||
const getResponseMockFunctionName = `get${pascal(operationId)}ResponseMock`; | ||
|
||
const baseDefinition = generateDefinition( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a refactoring consultation.
Is it possible to increase the generated http status with options and then loop the generation targets as shown below?
const isGenerateEachHttpStatus = generatorOptions.mock && 'generateEachHttpStatus' in generatorOptions.mock && generatorOptions.mock.generateEachHttpStatus;
generateStatuses = isGenerateEachHttpStatus ? [...response.types.success, ...response.types.errors] : [response.types.success]
generateStatuses.forEach(
(statusResponse) => {}
)
With the current implementation, duplicate handlers and mock definition for success will be created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a difference between the "base" and other success responses as I didn't want to introduce a breaking change. The "base" response behaves like the response generated before my change, which picks a random success response each time e.g.
export const getListPetsResponseMock = (
overrideResponse: any = {},
): Okay | Accepted | Success =>
faker.helpers.arrayElement([ // pick a random "success" response
Array.from( // the "Okay" type (an array of pets)
{ length: faker.number.int({ min: 1, max: 10 }) },
(_, i) => i + 1,
).map(() =>
faker.helpers.arrayElement([
{
breed: faker.helpers.arrayElement(['Labradoodle'] as const),
...
},
...
]),
),
{ // The "Accepted" response
code: faker.number.int({ min: undefined, max: undefined }),
message: faker.word.sample(),
...overrideResponse,
},
{ // The "Success" response
code: faker.number.int({ min: undefined, max: undefined }),
message: faker.word.sample(),
...overrideResponse,
},
]);
Whereas the other mocks that are generated only return the specific response for a status:
export const getListPetsResponseMock200 = (overrideResponse: any = {}): Okay =>
Array.from(
{ length: faker.number.int({ min: 1, max: 10 }) },
(_, i) => i + 1,
).map(() =>
faker.helpers.arrayElement([
{
breed: faker.helpers.arrayElement(['Labradoodle'] as const),
...
},
...
]),
);
export const getListPetsResponseMock202 = (
overrideResponse: any = {},
): Accepted => ({
code: faker.number.int({ min: undefined, max: undefined }),
message: faker.word.sample(),
...overrideResponse,
});
export const getListPetsResponseMock2xx = (
overrideResponse: any = {},
): Success => ({
code: faker.number.int({ min: undefined, max: undefined }),
message: faker.word.sample(),
...overrideResponse,
});
I could change it to return a single success response for the "base" response if you would be happy with a breaking change, though it could be difficult to know which response to return (I guess it could be the lowest 2xx number defined in the responses).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's sounds good. I had missed this.
In other words, does this mean that when multiple success responses are defined, the mocked types are different, so it is not a duplicate, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's sounds good. I had missed this. In other words, does this mean that when multiple success responses are defined, the mocked types are different, so it is not a duplicate, right?
That's correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huwshimi
Thank you for great job !
Status
READY
Description
Generate mocks for all responses defined in an openapi spec.
Fixes: #1280.
Todos
Steps to Test or Reproduce
samples/react-query/status-codes
yarn generate-api
.samples/react-query/status-codes/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
getListPetsMockHandler()
is included in the array of handlers (this is so that when passing the handlers to the worker there is only one http response for each path).