Skip to content

Commit 7708a37

Browse files
committed
test(persistor): add test for persistence mapping
1 parent 4504596 commit 7708a37

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/__tests__/__snapshots__/persistCache.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`persistCache advanced usage passing a persistence mapper properly maps the persisted cache 1`] = `"{\\"User:1\\":{\\"id\\":1,\\"__typename\\":\\"User\\",\\"first\\":\\"Jane\\",\\"last\\":\\"Doe\\"},\\"ROOT_QUERY\\":{\\"__typename\\":\\"Query\\",\\"user({\\\\\\"id\\\\\\":1})\\":{\\"__ref\\":\\"User:1\\"}}}"`;
4+
35
exports[`persistCache advanced usage passing in debounce configures the debounce interval 1`] = `"{\\"ROOT_QUERY\\":{\\"__typename\\":\\"Query\\",\\"hello\\":\\"world\\"}}"`;
46

57
exports[`persistCache advanced usage passing in key customizes the storage key 1`] = `"{\\"ROOT_QUERY\\":{\\"__typename\\":\\"Query\\",\\"hello\\":\\"world\\"}}"`;

src/__tests__/persistCache.ts

+53
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,58 @@ describe('persistCache', () => {
170170
expect(await storage.getItem('apollo-cache-persist')).toBe(undefined);
171171
});
172172
xit('setting the trigger to background persists in the background', () => {});
173+
it('passing a persistence mapper properly maps the persisted cache', async () => {
174+
const storage = new MockStorage();
175+
const cache = new InMemoryCache();
176+
const persistenceMapper = async (data: any) => {
177+
const parsed = JSON.parse(data);
178+
delete parsed['Post:1'];
179+
delete parsed['ROOT_QUERY']['posts'];
180+
return JSON.stringify(parsed);
181+
};
182+
183+
const mappableOperation = gql`
184+
{
185+
user(id: 1) {
186+
id
187+
first
188+
last
189+
}
190+
posts {
191+
id
192+
title
193+
}
194+
}
195+
`;
196+
197+
const mappableResult = { data: {
198+
user: {
199+
id: 1,
200+
first: 'Jane',
201+
last: 'Doe',
202+
__typename: 'User',
203+
},
204+
posts: [
205+
{
206+
id: 1,
207+
title: 'Apollo is awesome',
208+
__typename: 'Post',
209+
},
210+
],
211+
}};
212+
213+
await simulateWrite({
214+
result: mappableResult,
215+
operation: mappableOperation,
216+
persistOptions: { storage, persistenceMapper },
217+
});
218+
219+
jest.runTimersToTime(1001);
220+
// without this line, this test won't pass.
221+
// see https://github.com/facebook/jest/issues/2157
222+
await Promise.resolve();
223+
const received = await storage.getItem('apollo-cache-persist');
224+
expect(received).toMatchSnapshot();
225+
});
173226
});
174227
});

0 commit comments

Comments
 (0)