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): separated mock handler and made it reusable #1182

Conversation

soartec-lab
Copy link
Member

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

Status

READY

Description

Fix #822

I separate mock handler functions and aggregate functions. That way you can just use separate handlers. This will result in more reusable mocks.

Before

export const getPetsMock = () => [
  http.get('*/pets', async () => {
    await delay(1000);
    return new HttpResponse(JSON.stringify(getListPetsMock()),
      { 
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        }
      }
    )
  }),
  http.post('*/pets', async () => {
    await delay(1000);
    return new HttpResponse(null,
      { 
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        }
      }
    )
  }),
  http.get('*/pets/:petId', async () => {
    await delay(1000);
    return new HttpResponse(JSON.stringify(getShowPetByIdMock()),
      { 
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        }
      }
    )
  })
]

After

// Individual handler
export const getListPetsMockHandler = http.get('*/pets', async () => {
  await delay(1000);
  return new HttpResponse(JSON.stringify(getListPetsMock()),
    { 
      status: 200,
      headers: {
        'Content-Type': 'application/json',
      }
    }
  )
})

export const getCreatePetsMockHandler = http.post('*/pets', async () => {
  await delay(1000);
  return new HttpResponse(null,
    { 
      status: 200,
      headers: {
        'Content-Type': 'application/json',
      }
    }
  )
})

export const getShowPetByIdMockHandler = http.get('*/pets/:petId', async () => {
  await delay(1000);
  return new HttpResponse(JSON.stringify(getShowPetByIdMock()),
    { 
      status: 200,
      headers: {
        'Content-Type': 'application/json',
      }
    }
  )
})

// handlers aggregation
export const getPetsMock = () => [
  getListPetsMockHandler,
  getCreatePetsMockHandler,
  getShowPetByIdMockHandler
]

Related PRs

#822

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

  1. use basic pet store difinition
  2. execute orval
  3. check generate msw mocks

Sorry, something went wrong.

@soartec-lab soartec-lab changed the title feat(msw): separate mock handler and aggregate function definition feat(msw): separated mock handler and made it reusable Jan 27, 2024
@melloware
Copy link
Collaborator

cc @Will-Mann-16

@melloware melloware added the enhancement New feature or request label Jan 27, 2024
@melloware melloware merged commit 02161de into orval-labs:master Jan 27, 2024
@soartec-lab soartec-lab added this to the 6.24.0 milestone Jan 28, 2024
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.

MSW: Overwriting mock object returned by the generated mock factory function.
3 participants