Skip to content

Commit

Permalink
adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonmorixe committed Mar 8, 2022
1 parent 2b34247 commit 1bd8f0a
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 2 deletions.
58 changes: 58 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/writeToStore.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,64 @@ Object {
}
`;
exports[`writing to the store root type policy merge is called before cache deep merge 1`] = `
Array [
Array [
Object {
"existing": Object {
"__ref": "Person:123",
},
"incoming": Object {
"__typename": "Person",
"age": 28,
"id": 123,
"name": "Gaston",
"status": "ACTIVE",
"updatedAt": 100000,
},
"times": 1,
},
],
Array [
Object {
"existing": undefined,
"incoming": Object {
"__ref": "Person:123",
},
"times": 2,
},
],
Array [
Object {
"existing": Object {
"__ref": "Person:123",
},
"incoming": Object {
"__typename": "Person",
"id": 123,
"status": "DISABLED",
"updatedAt": 50,
},
"times": 3,
},
],
Array [
Object {
"existing": Object {
"__ref": "Person:123",
},
"incoming": Object {
"__typename": "Person",
"id": 123,
"status": "PENDING",
"updatedAt": 100001,
},
"times": 4,
},
],
]
`;
exports[`writing to the store should not keep reference when type of mixed inlined field changes to non-inlined field 1`] = `
[MockFunction] {
"calls": Array [
Expand Down
151 changes: 151 additions & 0 deletions src/cache/inmemory/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,157 @@ describe('writing to the store', () => {
});
});

it("root type policy merge is called before cache deep merge", () => {
const personMergeMock = jest.fn()

let times = 0
const cache = new InMemoryCache({
typePolicies: {
Person: {
merge(existing, incoming, tools) {
times++

personMergeMock({
times,
existing,
incoming,
})

if (tools.isReference(existing) && !tools.isReference(incoming)) {
const cachedData = tools.cache.data.lookup(existing.__ref)
const existingUpdatedAt = cachedData?.["updatedAt"]
const incomingUpdatedAt = incoming?.["updatedAt"]
if (
typeof existingUpdatedAt === "number" &&
typeof incomingUpdatedAt === "number" &&
existingUpdatedAt > incomingUpdatedAt
) {
return existing
}
}

return tools.mergeObjects(existing, incoming)
},
},
},
})

expect(times).toEqual(0)

const query = gql`
query Person($offset: Int, $limit: Int) {
person {
id
name
age
status
updatedAt
}
}
`

expect(times).toEqual(0)

cache.writeQuery({
query,
data: {
person: {
__typename: "Person",
id: 123,
name: "Gaston",
age: 28,
status: "ACTIVE",
updatedAt: 100000,
},
},
variables: {},
})

expect(times).toEqual(2) // TODO: ideally this should only be called once here

expect(cache.extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
person: {
__ref: "Person:123",
},
},
"Person:123": {
__typename: "Person",
id: 123,
name: "Gaston",
age: 28,
status: "ACTIVE",
updatedAt: 100000,
},
})

cache.writeQuery({
query,
data: {
person: {
__typename: "Person",
id: 123,
status: "DISABLED",
updatedAt: 50,
},
},
variables: {},
})

expect(times).toEqual(3)

expect(cache.extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
person: {
__ref: "Person:123",
},
},
"Person:123": {
__typename: "Person",
id: 123,
name: "Gaston",
age: 28,
status: "ACTIVE",
updatedAt: 100000,
},
})

cache.writeQuery({
query,
data: {
person: {
__typename: "Person",
id: 123,
status: "PENDING",
updatedAt: 100001,
},
},
variables: {},
})

expect(personMergeMock.mock.calls).toMatchSnapshot()
expect(times).toEqual(4)

expect(cache.extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
person: {
__ref: "Person:123",
},
},
"Person:123": {
__typename: "Person",
id: 123,
name: "Gaston",
age: 28,
status: "PENDING",
updatedAt: 100001,
},
})
})

describe("StoreWriter", () => {
const writer = new StoreWriter(new InMemoryCache());

Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/entityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export abstract class EntityStore implements NormalizedCache {
}
}

protected lookup(dataId: string, dependOnExistence?: boolean): StoreObject | undefined {
public lookup(dataId: string, dependOnExistence?: boolean): StoreObject | undefined {
// The has method (above) calls lookup with dependOnExistence = true, so
// that it can later be invalidated when we add or remove a StoreObject for
// this dataId. Any consumer who cares about the contents of the StoreObject
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type BroadcastOptions = Pick<
>

export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
private data: EntityStore;
public data: EntityStore;
private optimisticData: EntityStore;

protected config: InMemoryCacheConfig;
Expand Down

0 comments on commit 1bd8f0a

Please sign in to comment.