Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix: add test for multiple queries
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCoady committed May 24, 2019
1 parent 1e0793a commit 09f05a9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/sync/src/cache/createMutationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const createMutationOptions = (options: MutationHelperOptions): MutationO

const update: MutationUpdaterFn = (cache, { data }) => {
if (isArray(updateQuery)) {
updateQuery.forEach(query => {
for (const query of updateQuery) {
getUpdateFunction(operationName, idField, query, operationType)(cache, { data });
});
}
} else {
getUpdateFunction(operationName, idField, updateQuery, operationType)(cache, { data });
}
Expand Down
71 changes: 68 additions & 3 deletions packages/sync/test/CacheHelpersTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { expect, should } from "chai";
import { MutationHelperOptions, CacheOperation, createMutationOptions, createSubscriptionOptions } from "../src/cache";
import { CacheOperation, createMutationOptions, createSubscriptionOptions } from "../src/cache";
import {
CREATE_ITEM,
GET_ITEMS,
GET_LISTS,
DELETE_ITEM,
ITEM_CREATED_SUB,
ITEM_DELETED_SUB,
ITEM_UPDATED_SUB
ITEM_UPDATED_SUB,
CREATE_LIST,
DOESNT_EXIST,
GET_NON_EXISTENT
} from "./mock/mutations";
import { NormalizedCacheObject } from "apollo-cache-inmemory";
import { OfflineClient } from "../src";
Expand Down Expand Up @@ -39,6 +43,26 @@ describe("CacheHelpers", () => {
operationType: CacheOperation.DELETE,
idField: "id"
});
const builtOptionsWithArray = createMutationOptions({
mutation: CREATE_LIST,
variables: {
"title": "new list"
},
updateQuery: [{ query: GET_ITEMS, variables: {} }, { query: GET_LISTS, variables: {} }],
typeName: "List",
operationType: CacheOperation.ADD,
idField: "id"
});
const builtNonExistent = createMutationOptions({
mutation: DOESNT_EXIST,
variables: {
"title": "new list"
},
updateQuery: { query: GET_NON_EXISTENT, variables: {} },
typeName: "Something",
operationType: CacheOperation.ADD,
idField: "id"
});
const builtSubCreateOptions = createSubscriptionOptions({
subscriptionQuery: ITEM_CREATED_SUB,
cacheUpdateQuery: GET_ITEMS,
Expand Down Expand Up @@ -70,6 +94,34 @@ describe("CacheHelpers", () => {
}
};

const createList = (id: string) => {
if (builtOptionsWithArray && builtOptionsWithArray.update) {
builtOptionsWithArray.update(client.cache, {
data: {
"createList": {
"id": id,
"title": "new list" + id,
"__typename": "Item"
}
}
});
}
};

const createNonExistent = (id: string) => {
if (builtNonExistent && builtNonExistent.update) {
builtNonExistent.update(client.cache, {
data: {
"somethingFake": {
"id": id,
"title": "new list" + id,
"__typename": "Item"
}
}
});
}
};

const remove = (id: string) => {
if (builtDeleteOptions && builtDeleteOptions.update) {
builtDeleteOptions.update(client.cache, {
Expand All @@ -96,6 +148,12 @@ describe("CacheHelpers", () => {
"allItems": []
}
});
client.writeQuery({
query: GET_LISTS,
data: {
"allLists": []
}
});
});

it("ensures built mutation options include a function", () => {
Expand All @@ -109,6 +167,13 @@ describe("CacheHelpers", () => {
expect(client.readQuery({ query: GET_ITEMS }).allItems).to.have.length(1);
});

it("add item to cache with multiple", () => {
expect(client.readQuery({ query: GET_LISTS }).allLists).to.have.length(0);
createList("1");
expect(client.readQuery({ query: GET_LISTS }).allLists).to.have.length(1);
expect(client.readQuery({ query: GET_ITEMS }).allItems).to.have.length(1);
});

it("delete item from cache", async () => {
expect(client.readQuery({ query: GET_ITEMS }).allItems).to.have.length(0);
create("1");
Expand Down Expand Up @@ -232,7 +297,7 @@ describe("CacheHelpers", () => {
{
subscriptionData: {
data: {
itemUpdated: { }
itemUpdated: {}
}
}
});
Expand Down
34 changes: 34 additions & 0 deletions packages/sync/test/mock/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ mutation createItem($title: String!){
}
`;

export const CREATE_LIST = gql`
mutation createList($title: String!){
createList(title: $title){
title
}
}
`;

export const DELETE_ITEM = gql`
mutation deleteItem($id: ID!){
deleteItem(id: $id){
Expand All @@ -16,6 +24,14 @@ mutation deleteItem($id: ID!){
}
`;

export const DOESNT_EXIST = gql`
mutation somethingFake($id: ID!){
somethingFake(id: $id){
title
}
}
`;

export const GET_ITEMS = gql`
query allItems($first: Int) {
allItems(first: $first) {
Expand All @@ -25,6 +41,24 @@ export const GET_ITEMS = gql`
}
`;

export const GET_LISTS = gql`
query allLists($first: Int) {
allLists(first: $first) {
id
title
}
}
`;

export const GET_NON_EXISTENT = gql`
query somethingFake($first: Int) {
somethingFake(first: $first) {
id
title
}
}
`;

export const ITEM_CREATED_SUB = gql`
subscription itemCreated {
itemCreated {
Expand Down

0 comments on commit 09f05a9

Please sign in to comment.