forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-test.ts
More file actions
76 lines (67 loc) · 2.08 KB
/
Copy pathworker-test.ts
File metadata and controls
76 lines (67 loc) · 2.08 KB
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
// This test file is loaded by worker.test.ts
import {ReplicacheTest} from './replicache.js';
import type {ReadTransaction, WriteTransaction} from './transactions.js';
import {asyncIterableToArray} from './async-iterable-to-array.js';
import {expect} from '@esm-bundle/chai';
import type {JSONValue} from './json.js';
import {closeAllReps, reps} from './test-util.js';
onmessage = async (e: MessageEvent) => {
const {name, useMemstore} = e.data;
try {
await testGethasScanOnEmptyDB(name, useMemstore);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore TypeScripts type defs are incorrect.
postMessage(undefined);
await closeAllReps();
} catch (ex) {
await closeAllReps();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore TypeScripts type defs are incorrect.
postMessage(ex);
}
};
async function testGethasScanOnEmptyDB(name: string, useMemstore = false) {
const rep = new ReplicacheTest({
pushDelay: 60_000, // Large to prevent interferin;,
name,
useMemstore,
mutators: {
testMut: async (
tx: WriteTransaction,
args: {key: string; value: JSONValue},
) => {
const key = args['key'];
const value = args['value'];
await tx.put(key, value);
expect(await tx.has(key)).to.equal(true);
const v = await tx.get(key);
expect(v).to.deep.equal(value);
expect(await tx.del(key)).to.equal(true);
expect(await tx.has(key)).to.be.false;
},
},
});
reps.add(rep);
const {testMut} = rep.mutate;
for (const [key, value] of Object.entries({
a: true,
b: false,
c: null,
d: 'string',
e: 12,
f: {},
g: [],
h: {h1: true},
i: [0, 1],
})) {
await testMut({key, value: value as JSONValue});
}
async function t(tx: ReadTransaction) {
expect(await tx.get('key')).to.equal(undefined);
expect(await tx.has('key')).to.be.false;
const scanItems = await asyncIterableToArray(tx.scan());
expect(scanItems).to.have.length(0);
}
await t(rep);
await rep.query(t);
}