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 value of the mock handler function overridable #1186

Conversation

soartec-lab
Copy link
Collaborator

Status

READY

Description

I made the value of the mock handler function overridable. This makes the handler even more reusable.

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 getListPetsMockHandler = (overrideResponse?: Pets) => {
  return http.get('*/pets', async () => {
    await delay(1000);
    return new HttpResponse(JSON.stringify(overrideResponse ? overrideResponse : getListPetsMock()),
      {
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        }
      }
    )
  })
}

export const getShowPetByIdMockHandler = (overrideResponse?: Pet) => {
  return http.get('*/pets/:petId', async () => {
    await delay(1000);
    return new HttpResponse(JSON.stringify(overrideResponse ? overrideResponse : getShowPetByIdMock()),
      {
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        }
      }
    )
  })
}
  1. usage
import { Pet } from './gen/model/pet';
import { getListPetsMockHandler, getShowPetByIdMockHandler } from './gen/endpoints/pets/pets.msw';

const pet: Pet = { id: 1, name: 'test', tag: 'test' };

const listPetsMockHandlerhandler = getListPetsMockHandler([pet]);
console.log(listPetsMockHandlerhandler);
// => Object { info: {…}, isUsed: false, resolver: async getListPetsMockHandler(), resolverGenerator: undefined, resolverGeneratorResult: undefined, options: {} }

const showPetByIdMockHandler = getShowPetByIdMockHandler(pet);
console.log(showPetByIdMockHandler);
// => Object { info: {…}, isUsed: false, resolver: async getShowPetByIdMockHandler(), resolverGenerator: undefined, resolverGeneratorResult: undefined, options: {} }

@melloware melloware added the enhancement New feature or request label Jan 31, 2024
@melloware melloware added this to the 6.24.0 milestone Jan 31, 2024
@melloware melloware merged commit 0d05129 into anymaniax:master Jan 31, 2024
2 checks passed
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.

None yet

2 participants