apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.34k stars 2.66k forks source link

When updating the list of mocks and calling 'rerender' on the component under test, the updated list of mocks is ignored and the original is still used. #11982

Closed jfstephe closed 2 months ago

jfstephe commented 2 months ago

Issue Description

On my project we are migrating use MockedProvider and in our tests we build up the mocks, step by step, starting with an initial set of mocks and then adding to it as the test scenario builds up. We don't want to setup/define all the mocks up-font.

When trying this out we are seeing that the updated list of mocks isn't used in a render and the original mocked list is still being used.

A codesandbox, based off the one in the documentation, is attached demonstrating this - see the 'should render another newly added Dog' test.

Link to Reproduction

https://codesandbox.io/p/sandbox/condescending-jang-cstl3k?file=%2Fsrc%2Fdog.test.js%3A36%2C40

Reproduction Steps

See codesandbox.

@apollo/client version

3.11.2

phryneas commented 2 months ago

MockedProvider will use MockLink internally. MockLink copies the mocks array on initialization and then keeps working on that. It doesn't watch for modifications of the original array (and even if it did, relying on something like that would be relying on an implementation detail).

That said, MockLink has a addMockedResponse method to dynamically add new mocks - maybe that would work for you?

jfstephe commented 2 months ago

Awesome, thanks for the quick response. I used the MockLink and the tests are passing. 👍 (Although, I'm not sure I agree with implementation detail, I'd rather fall back on the principle of least surprise).

github-actions[bot] commented 2 months ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

github-actions[bot] commented 1 month ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.