deno.land / x / replicache@v10.0.0-beta.0 / persist / idb-databases-store.test.ts

idb-databases-store.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
import {expect} from '@esm-bundle/chai';import {TestMemStore} from '../kv/test-mem-store';import {IDBDatabasesStore, IndexedDBDatabase} from './idb-databases-store';import * as sinon from 'sinon';
teardown(() => { sinon.restore();});
test('getDatabases with no existing record in db', async () => { const store = new IDBDatabasesStore(_ => new TestMemStore()); expect(await store.getDatabases()).to.deep.equal({});});
test('putDatabase with no existing record in db', async () => { sinon.replace(Date, 'now', () => 1);
const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB = { name: 'testName', replicacheName: 'testReplicacheName', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion', }; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), });});
test('putDatabase updates lastOpenedTimestampMS', async () => { let now = 1; sinon.replace(Date, 'now', () => now);
const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB = { name: 'testName', replicacheName: 'testReplicacheName', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion', }; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), });
now = 2; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), });});
test('putDatabase ignores passed in lastOpenedTimestampMS', async () => { const now = 2; sinon.replace(Date, 'now', () => now);
const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB = { name: 'testName', replicacheName: 'testReplicacheName', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion', lastOpenedTimestampMS: 1, }; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), });});
function withLastOpenedTimestampMS( db: IndexedDBDatabase, lastOpenedTimestampMS: number,): IndexedDBDatabase { return { ...db, lastOpenedTimestampMS, };}
test('putDatabase sequence', async () => { let now = 1; sinon.replace(Date, 'now', () => now); const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB1 = { name: 'testName1', replicacheName: 'testReplicacheName1', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion1', };
expect(await store.putDatabase(testDB1)).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), }); expect(await store.getDatabases()).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), });
const testDB2 = { name: 'testName2', replicacheName: 'testReplicacheName2', replicacheFormatVersion: 2, schemaVersion: 'testSchemaVersion2', };
now = 2;
expect(await store.putDatabase(testDB2)).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), testName2: withLastOpenedTimestampMS(testDB2, 2), }); expect(await store.getDatabases()).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), testName2: withLastOpenedTimestampMS(testDB2, 2), });});
test('close closes kv store', async () => { const memstore = new TestMemStore(); const store = new IDBDatabasesStore(_ => memstore); expect(memstore.closed).to.be.false; await store.close(); expect(memstore.closed).to.be.true;});
test('clear', async () => { let now = 1; sinon.replace(Date, 'now', () => now); const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB1 = { name: 'testName1', replicacheName: 'testReplicacheName1', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion1', };
expect(await store.putDatabase(testDB1)).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, now), }); expect(await store.getDatabases()).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, now), });
await store.clearDatabases();
expect(await store.getDatabases()).to.deep.equal({});
const testDB2 = { name: 'testName2', replicacheName: 'testReplicacheName2', replicacheFormatVersion: 2, schemaVersion: 'testSchemaVersion2', };
now = 2;
expect(await store.putDatabase(testDB2)).to.deep.equal({ testName2: withLastOpenedTimestampMS(testDB2, now), }); expect(await store.getDatabases()).to.deep.equal({ testName2: withLastOpenedTimestampMS(testDB2, now), });});
test('getProfileID', async () => { const store = new IDBDatabasesStore(_ => new TestMemStore()); const profileID = await store.getProfileID(); expect(profileID).to.be.a('string'); expect(profileID).to.match(/^p[a-zA-Z0-9]+$/); const profileID2 = await store.getProfileID(); expect(profileID2).to.equal(profileID);});
replicache

Version Info

Tagged at
2 years ago