Skip to content

Commit

Permalink
extend test to distinguish between cache & network
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Sep 14, 2023
1 parent d8c6072 commit 2ad8485
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/cache/inmemory/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ describe("InMemoryCache tests exercising ApolloClient", () => {
])(
"results should be read from cache even when incomplete (fetchPolicy %s)",
(fetchPolicy) => {
const dateString = new Date().toISOString();
const dateFromCache = "2023-09-14T13:03:22.616Z";
const dateFromNetwork = "2023-09-15T13:03:22.616Z";

const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
date: {
read(existing) {
return new Date(existing || dateString);
return new Date(existing || dateFromCache);
},
},
},
Expand All @@ -42,7 +43,7 @@ describe("InMemoryCache tests exercising ApolloClient", () => {
data: {
// This raw string should be converted to a Date by the Query.date
// read function passed to InMemoryCache below.
date: dateString,
date: dateFromNetwork,
// Make sure we don't accidentally return fields not mentioned in
// the query just because the result is incomplete.
ignored: "irrelevant to the subscribed query",
Expand Down Expand Up @@ -88,18 +89,22 @@ describe("InMemoryCache tests exercising ApolloClient", () => {
// the "no-cache" policy. In this test, that means the Query.date field
// will remain as a raw string rather than being converted to a Date by
// the read function.
const expectedDate =
fetchPolicy === "no-cache" ? dateString : new Date(dateString);
const expectedDateAfterResult =
fetchPolicy === "cache-only"
? new Date(dateFromCache)
: fetchPolicy === "no-cache"
? dateFromNetwork
: new Date(dateFromNetwork);

if (adjustedCount === 1) {
expect(result.loading).toBe(true);
expect(result.data).toEqual({
date: expectedDate,
date: new Date(dateFromCache),
});
} else if (adjustedCount === 2) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
date: expectedDate,
date: expectedDateAfterResult,
// The no-cache fetch policy does return extraneous fields from the
// raw network result that were not requested in the query, since
// the cache is not consulted.
Expand Down Expand Up @@ -129,7 +134,7 @@ describe("InMemoryCache tests exercising ApolloClient", () => {
} else if (adjustedCount === 3) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
date: expectedDate,
date: expectedDateAfterResult,
missing: "not missing anymore",
});

Expand All @@ -144,7 +149,7 @@ describe("InMemoryCache tests exercising ApolloClient", () => {
? null
: {
// Make sure this field is stored internally as a raw string.
date: dateString,
date: dateFromNetwork,
}),
// Written explicitly with cache.writeQuery above.
missing: "not missing anymore",
Expand Down

0 comments on commit 2ad8485

Please sign in to comment.