-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathnormalized-adapter.test.ts
More file actions
63 lines (48 loc) · 1.7 KB
/
normalized-adapter.test.ts
File metadata and controls
63 lines (48 loc) · 1.7 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
import assert from 'node:assert/strict'
import test from 'node:test'
import type { Adapter } from 'lowdb'
import { DEFAULT_SCHEMA_PATH, NormalizedAdapter } from './adapters/normalized-adapter.ts'
import type { RawData } from './adapters/normalized-adapter.ts'
import type { Data } from './service.ts'
class StubAdapter implements Adapter<RawData> {
#data: RawData | null
constructor(data: RawData | null) {
this.#data = data
}
async read(): Promise<RawData | null> {
return this.#data === null ? null : structuredClone(this.#data)
}
async write(data: RawData): Promise<void> {
this.#data = structuredClone(data)
}
get data(): RawData | null {
return this.#data
}
}
await test('read removes $schema and normalizes ids', async () => {
const adapter = new StubAdapter({
$schema: './custom/schema.json',
posts: [{ id: 1 }, { title: 'missing id' }],
profile: { name: 'x' },
})
const normalized = await new NormalizedAdapter(adapter).read()
assert.notEqual(normalized, null)
if (normalized === null) {
return
}
assert.equal(normalized['$schema'], undefined)
assert.deepEqual(normalized['profile'], { name: 'x' })
const posts = normalized['posts']
assert.ok(Array.isArray(posts))
assert.equal(posts[0]?.['id'], '1')
assert.equal(typeof posts[1]?.['id'], 'string')
assert.notEqual(posts[1]?.['id'], '')
})
await test('write always overwrites $schema', async () => {
const adapter = new StubAdapter(null)
const normalizedAdapter = new NormalizedAdapter(adapter)
await normalizedAdapter.write({ posts: [{ id: '1' }] } satisfies Data)
const data = adapter.data
assert.notEqual(data, null)
assert.equal(data?.['$schema'], DEFAULT_SCHEMA_PATH)
})