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

[Feature Request] Create dynamicVariables for mutation MockedProvider #89

Open
gabrieluizramos opened this issue Jan 29, 2019 · 3 comments
Labels
project-apollo-client (legacy) LEGACY TAG DO NOT USE 🧪 testing Feature requests related to testing

Comments

@gabrieluizramos
Copy link

Previosly created on react-apollo issue #2466

Intended outcome:
Use components at storybook, mocking a response for a mutation based on dynamic variables (e.g: using HoC that updates the variables and refetch the mutation)

Actual outcome:
Was throwing an error like no mocked responses for the query
screen shot 2018-10-03 at 11 10 24 am

How to reproduce the issue:
Creating a component with MockedProvider and using dynamic values as variables, such as:
https://codesandbox.io/s/w27mz5r44w

Version

  • apollo-boost@0.1.16
  • react-apollo@2.1.11

Maybe, creating some kind of dynamicVariables inside the mocks array for mockedProvider would be a nice way to help these cases, as PR 2467

@skratchdot
Copy link

it would be nice if request could be a function (similar to how response can be). if it returns "true" then we return the response, else we look for other mocks. I need to dig up how the code currently works, but I see:

https://www.apollographql.com/docs/react/api/react/testing/#example-mocks-array

const mocks = [
  {
    request: {
      query: GET_DOG,
      variables: { index: 4 }
    },
    result: {
      data: {
        dog: {
          name: "Douglas"
        }
      }
    }
  },
  {
    request: {
      query: GET_DOG,
      variables: { index: 8 }
    },
    error: new Error("Something went wrong")
  }
]

maybe we could make this also work:

const mocks = [
  {
    request: ({ query, variables }) => query === GET_DOG && variables.index === 4,
    result: {
      data: {
        dog: {
          name: "Douglas"
        }
      }
    }
  },
  {
    request: {
      query: GET_DOG,
      variables: { index: 8 }
    },
    error: new Error("Something went wrong")
  }
]

That way we could make either 4 or 5 return the response by editing the code above:

request: ({ query, variables }) => query === GET_DOG && [4, 5].includes(variables.index)

@rockettomatooo
Copy link

rockettomatooo commented Aug 13, 2021

Any updates on this? This would be a very pleasant feature to have!

My Usecase is storybook: I have a form in storybook and that form triggers a mutation. Right now, I have to exactly match the contents of the form to the mock to make this work. It would be very cool if I could just accept any variables and return them in the response - kind of like an echo server.

Thanks in advance! :)
(I don't quite know who to tag here unfortunately :/)

@c-sauerborn
Copy link

Hello, we would also be interested in such a feature. We are having the same challenge for our storybook mocks.

@jerelmiller jerelmiller added project-apollo-client (legacy) LEGACY TAG DO NOT USE 🧪 testing Feature requests related to testing labels Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project-apollo-client (legacy) LEGACY TAG DO NOT USE 🧪 testing Feature requests related to testing
Projects
None yet
Development

No branches or pull requests

5 participants