Skip to content

Commit 7d9215a

Browse files
committed
test: add FileStore tests
1 parent 1348299 commit 7d9215a

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/test/store.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as assert from 'assert';
2+
import * as tmp from 'tmp';
3+
import * as fs from 'fs';
4+
import { FileStore } from '../store';
5+
6+
describe('FileStore', () => {
7+
it('works', async () => {
8+
const name = tmp.tmpNameSync();
9+
const store = await FileStore.open(name);
10+
11+
assert.deepStrictEqual(store.get('joe'), undefined);
12+
assert.ok(!fs.existsSync(name));
13+
14+
await store.add({ name: 'joe', pat: 'abc' });
15+
assert.deepStrictEqual(store.get('joe'), { name: 'joe', pat: 'abc' });
16+
assert.deepStrictEqual(JSON.parse(fs.readFileSync(name, 'utf8')), {
17+
publishers: [{ name: 'joe', pat: 'abc' }],
18+
});
19+
20+
await store.add({ name: 'joe', pat: 'what' });
21+
assert.deepStrictEqual(store.get('joe'), { name: 'joe', pat: 'what' });
22+
assert.deepStrictEqual(JSON.parse(fs.readFileSync(name, 'utf8')), {
23+
publishers: [{ name: 'joe', pat: 'what' }],
24+
});
25+
26+
await store.add({ name: 'jane', pat: 'oh' });
27+
assert.deepStrictEqual(store.get('joe'), { name: 'joe', pat: 'what' });
28+
assert.deepStrictEqual(store.get('jane'), { name: 'jane', pat: 'oh' });
29+
assert.deepStrictEqual(JSON.parse(fs.readFileSync(name, 'utf8')), {
30+
publishers: [
31+
{ name: 'joe', pat: 'what' },
32+
{ name: 'jane', pat: 'oh' },
33+
],
34+
});
35+
36+
await store.delete('joe');
37+
assert.deepStrictEqual(store.get('joe'), undefined);
38+
assert.deepStrictEqual(store.get('jane'), { name: 'jane', pat: 'oh' });
39+
assert.deepStrictEqual(JSON.parse(fs.readFileSync(name, 'utf8')), {
40+
publishers: [{ name: 'jane', pat: 'oh' }],
41+
});
42+
43+
await store.delete('joe');
44+
assert.deepStrictEqual(store.get('joe'), undefined);
45+
assert.deepStrictEqual(store.get('jane'), { name: 'jane', pat: 'oh' });
46+
assert.deepStrictEqual(JSON.parse(fs.readFileSync(name, 'utf8')), {
47+
publishers: [{ name: 'jane', pat: 'oh' }],
48+
});
49+
50+
await store.delete('jane');
51+
assert.deepStrictEqual(store.get('joe'), undefined);
52+
assert.deepStrictEqual(store.get('jane'), undefined);
53+
assert.deepStrictEqual(JSON.parse(fs.readFileSync(name, 'utf8')), {
54+
publishers: [],
55+
});
56+
57+
fs.unlinkSync(name);
58+
});
59+
});

0 commit comments

Comments
 (0)