Skip to content
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(msw): possible to overwrite value when creating an object with msw mock #1165

Conversation

soartec-lab
Copy link
Member

@soartec-lab soartec-lab commented Jan 20, 2024

Status

READY

Description

I responded to part of #822

If there is only one response case and the type is object, the returned value can be overwritten externally by specifying overrideResponse as an argument.
In order to handle the case where it is an array of object such as Pets, after calculating the return value, we determine whether overwriting is possible based on whether overrideResponse is included in it.

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

  1. Specify basic pet schema as input
  2. execute orval
orval
  1. generated mock function bellow:
export const getListPetsMock = (overrideResponse?: any) => (Array.from({
  length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => ({
    id: faker.number.int({min: undefined, max: undefined}),
    name: faker.word.sample(),
    tag: faker.helpers.arrayElement([faker.word.sample(), undefined]),
    ...overrideResponse
  }))
)

export const getShowPetByIdMock = (overrideResponse?: any) => ({
  id: faker.number.int({min: undefined, max: undefined}),
  name: faker.word.sample(),
  tag: faker.helpers.arrayElement([faker.word.sample(), undefined]), ...overrideResponse
})
  1. usage
import { getListPetsMock, getShowPetByIdMock } from 'gen/pets.msw'

const pets = getListPetsMock({name: 'orverride'})
console.log(pets)
// => [{ id: 645061935104000, name: "orverride", tag: undefined }, { id: 83169910980608, name: "orverride", tag: undefined }]
​​
const pet = getShowPetByIdMock({name: 'orverride'})
console.log(pet)
// => { id: 7272122785202176, ​name: "orverride", tag: undefined }

@melloware melloware added the enhancement New feature or request label Jan 20, 2024
@melloware
Copy link
Collaborator

@Will-Mann-16 can you review since you recently did a lot of work in Mock?

@melloware melloware added this to the 6.24.0 milestone Jan 20, 2024
@soartec-lab soartec-lab changed the title feat: possible to overwrite value when creating an object with msw mock feat(msw): possible to overwrite value when creating an object with msw mock Jan 27, 2024
@soartec-lab
Copy link
Member Author

@Will-Mann-16
Hi, could you review this as well?

@melloware
Copy link
Collaborator

It looks good to me if @Will-Mann-16 has an issue we can re-open.

@melloware melloware merged commit c7ccb65 into orval-labs:master Jan 27, 2024
2 checks passed
Copy link
Contributor

@Will-Mann-16 Will-Mann-16 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only thing I saw - if it's tested and works I'm happy. Otherwise might need a separate issue but would be an easy fix.

@@ -80,7 +82,7 @@ export const generateMSW = (
implementation: {
function:
value && value !== 'undefined'
? `export const ${functionName} = () => (${value})\n\n`
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse?: any` : ''}) => (${value})\n\n`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some ESLint config setups would throw errors if you try to spread an undefined value, is it worth setting this to a default param of {}, empty object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Will-Mann-16

Thank you for your comment. That's good. I didn't have that perspective. I'll fix it in another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants