Open muhraff opened 4 years ago
@muhraff hi, did you fix this?
@korgara Unfortunately No.
In my case, as I inspect cache
in the debugger, readQuery
and writeQuery
are not defined at all. I'm trying to mock them out in my tests, without success so far.
@dmt0 Thank you for the clue. I can solve my problem with this code
I'm using react-testing-library
const renderWithProvider = async (mocks, cache) =>
<MockedProvider {...{ mocks }} cache={cache} addTypename={false}>
<MyComponent />
</MockedProvider>
it("example test case.", async () => {
const cache = new InMemoryCache({ addTypename: false });
const utils = await renderWithProvider(cache);
// Doing call mutation with writeQuery
// Don't forget to check the mutation is done
const afterMutationResult = cache.readQuery({
query: {WRITED_QUERY_HERE}
});
// then, the 'afterMutationResult' has data after writeQuery
expect(afterMutationResult).toStrictEqual(whatYouExpecting);
});
I ended up solving this like so:
export const mockCache = () => {
const map = new Map();
return {
size: () => map.size,
readQuery: ({query, variables}) =>
map.get(query + JSON.stringify(variables)),
writeQuery: ({query, variables, data}) =>
map.set(query + JSON.stringify(variables), data),
};
};
const cache = new InMemoryCache();
Object.assign(cache, mockCache());
Intended outcome:
I have a mutation with an
update
option which callscache.writeQuery
.This is updating the cache and re-rendering queries as expected when we run in the browser.
In unit tests for that component, the cache is not updating when
cache.writeQuery
is called.Container.js
Container.test.js
Actual outcome:
How to reproduce the issue:
Log the entire cache to the console before and after
writeQuery
is called. Value never changes.Version