This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
conflictBase.test.ts
189 lines (163 loc) · 5.29 KB
/
conflictBase.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import "fake-indexeddb/auto";
import "cross-fetch/polyfill";
import { MockNetworkStatus } from "./mock/MockNetworkStatus";
import { MockStore } from "./mock/MockStore";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";
import { ApolloOfflineClient } from "../src";
import gql from "graphql-tag";
const link = new HttpLink({ uri: "http://example.com/graphql" });
const fragmentMatcher = (type: any, _: any, context: any) => !!context.store.get(type.id);
it("base should be correctly calculated with regular id field", async function() {
const store = new MockStore();
const networkStatus = new MockNetworkStatus();
networkStatus.setOnline(false);
const cache = new InMemoryCache({
// https://github.com/apollographql/apollo-client/issues/3219#issuecomment-386186814
// workaround for heuristic fragment warning
fragmentMatcher: { match: fragmentMatcher }
});
const client = new ApolloOfflineClient({ link, cache, networkStatus, offlineStorage: store });
await client.init();
const newTask = {
id: "1",
description: "new",
title: "new",
version: 1,
author: "new",
__typename: "Task"
};
// @ts-ignore
const id = cache.config.dataIdFromObject(newTask) as any;
cache.writeData({ id , data: newTask });
const ADD_TASK_MUTATION = gql`mutation updateTask($id: ID, $description: String, $title: String, $version: String, $author: String) {
updateTask(id: $id, description: $description, title: $title, version: $version, author: $author) {
id
description
title
version
author
}
}`;
try {
await client.offlineMutate({
mutation: ADD_TASK_MUTATION,
variables: {
id: "1",
description: "updated",
title: "updated",
author: "updated",
version: 1
},
returnType: "Task"
});
} catch (e) {
const operationInQueue = client.queue.entries[0].operation.op;
expect(operationInQueue.context.conflictBase).toEqual(newTask);
}
});
it("base should be correctly calculated with custom id field", async function() {
const store = new MockStore();
const networkStatus = new MockNetworkStatus();
networkStatus.setOnline(false);
// Tell the cache to use a custom ID
const customDataIdFromObject = (data: any) => {
return `${data.__typename}:${data.uuid}`;
};
const cache = new InMemoryCache({
dataIdFromObject: customDataIdFromObject,
// https://github.com/apollographql/apollo-client/issues/3219#issuecomment-386186814
fragmentMatcher: { match: fragmentMatcher }
});
const client = new ApolloOfflineClient({ link, cache, networkStatus, offlineStorage: store });
await client.init();
const newTask = {
uuid: "1",
description: "new",
title: "new",
version: 1,
author: "new",
__typename: "Task"
};
// @ts-ignore
const id = cache.config.dataIdFromObject(newTask) as any;
cache.writeData({ id , data: newTask });
const ADD_TASK_MUTATION = gql`mutation updateTask($uuid: ID, $description: String, $title: String, $version: String, $author: String) {
updateTask(uuid: $id, description: $description, title: $title, version: $version, author: $author) {
uuid
description
title
version
author
}
}`;
try {
await client.offlineMutate({
mutation: ADD_TASK_MUTATION,
variables: {
uuid: "1",
description: "updated",
title: "updated",
author: "updated",
version: 1
},
returnType: "Task",
idField: "uuid"
});
} catch (e) {
const operationInQueue = client.queue.entries[0].operation.op;
expect(operationInQueue.context.conflictBase).toEqual(newTask);
}
});
it("base should be correctly calculated with if custom id is non stanard", async function() {
const store = new MockStore();
const networkStatus = new MockNetworkStatus();
networkStatus.setOnline(false);
// Tell the cache to use a custom ID
const customDataIdFromObject = (data: any) => {
return `foo-${data.__typename}-${data.uuid}`;
};
const cache = new InMemoryCache({
dataIdFromObject: customDataIdFromObject,
fragmentMatcher: { match: fragmentMatcher }
});
const client = new ApolloOfflineClient({ link, cache, networkStatus, offlineStorage: store });
await client.init();
const newTask = {
uuid: "1",
description: "new",
title: "new",
version: 1,
author: "new",
__typename: "Task"
};
// @ts-ignore
const id = cache.config.dataIdFromObject(newTask) as any;
cache.writeData({ id , data: newTask });
const ADD_TASK_MUTATION = gql`mutation updateTask($uuid: ID, $description: String, $title: String, $version: String, $author: String) {
updateTask(uuid: $id, description: $description, title: $title, version: $version, author: $author) {
uuid
description
title
version
author
}
}`;
try {
await client.offlineMutate({
mutation: ADD_TASK_MUTATION,
variables: {
uuid: "1",
description: "updated",
title: "updated",
author: "updated",
version: 1
},
returnType: "Task",
idField: "uuid"
});
} catch (e) {
const operationInQueue = client.queue.entries[0].operation.op;
expect(operationInQueue.context.conflictBase).toEqual(newTask);
}
});