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
|
import { IndexedDBPersistentStorage } from '~/lib/apollo/indexed_db_persistent_storage';
import { db } from '~/lib/apollo/local_db';
import CACHE_WITH_PERSIST_DIRECTIVE_AND_FIELDS from './mock_data/cache_with_persist_directive_and_field.json';
describe('IndexedDBPersistentStorage', () => {
let subject;
const seedData = async (cacheKey, data = CACHE_WITH_PERSIST_DIRECTIVE_AND_FIELDS) => {
const { ROOT_QUERY, ...rest } = data;
await db.table('queries').put(ROOT_QUERY, cacheKey);
const asyncPuts = Object.entries(rest).map(async ([key, value]) => {
const {
groups: { type, gid },
} = /^(?<type>.+?):(?<gid>.+)$/.exec(key);
const tableName = type.toLowerCase();
if (tableName !== 'projectmember' && tableName !== 'groupmember') {
await db.table(tableName).put(value, gid);
}
});
await Promise.all(asyncPuts);
};
beforeEach(async () => {
subject = await IndexedDBPersistentStorage.create();
});
afterEach(() => {
db.close();
});
it('returns empty response if there is nothing stored in the DB', async () => {
const result = await subject.getItem('some-query');
expect(result).toEqual({});
});
it('returns stored cache if cache was persisted in IndexedDB', async () => {
await seedData('issues_list', CACHE_WITH_PERSIST_DIRECTIVE_AND_FIELDS);
const result = await subject.getItem('issues_list');
expect(result).toEqual(CACHE_WITH_PERSIST_DIRECTIVE_AND_FIELDS);
});
it('puts the results in database on `setItem` call', async () => {
await subject.setItem(
'issues_list',
JSON.stringify({
ROOT_QUERY: 'ROOT_QUERY_KEY',
'Project:gid://gitlab/Project/6': {
__typename: 'Project',
id: 'gid://gitlab/Project/6',
},
}),
);
await expect(db.table('queries').get('issues_list')).resolves.toEqual('ROOT_QUERY_KEY');
await expect(db.table('project').get('gid://gitlab/Project/6')).resolves.toEqual({
__typename: 'Project',
id: 'gid://gitlab/Project/6',
});
});
it('does not put results into non-existent table', async () => {
const queryId = 'issues_list';
await subject.setItem(
queryId,
JSON.stringify({
ROOT_QUERY: 'ROOT_QUERY_KEY',
'DNE:gid://gitlab/DNE/1': {},
}),
);
expect(db.tables.map((x) => x.name)).not.toContain('dne');
});
it('when removeItem is called, clears all data', async () => {
await seedData('issues_list', CACHE_WITH_PERSIST_DIRECTIVE_AND_FIELDS);
await subject.removeItem();
const actual = await Promise.all(db.tables.map((x) => x.toArray()));
expect(actual).toEqual(db.tables.map(() => []));
});
});
|