-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathperiod-database.test.ts
More file actions
111 lines (98 loc) · 3.52 KB
/
period-database.test.ts
File metadata and controls
111 lines (98 loc) · 3.52 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
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
import db from "@db/period-database"
import { keyOf, MILL_PER_PERIOD } from "@util/period"
import { formatTimeYMD } from "@util/time"
import { mockStorage } from "../__mock__/storage"
function resultOf(date: Date, orderNum: number, milliseconds: number): timer.period.Result {
return { ...keyOf(date, orderNum), milliseconds }
}
describe('period-database', () => {
beforeAll(mockStorage)
beforeEach(async () => chrome.storage.local.clear())
test('1', async () => {
const date = new Date(2021, 5, 7)
const dateStr = formatTimeYMD(date)
const yesterday = new Date(2021, 5, 6)
expect((await db.get(dateStr))).toEqual({})
const toAdd: timer.period.Result[] = [
resultOf(date, 0, 56999),
resultOf(date, 1, 2),
resultOf(yesterday, 95, 2)
]
await db.accumulate(toAdd)
await db.accumulate([
resultOf(date, 1, 20)
])
const data = await db.get(dateStr)
expect(data).toEqual({ 0: 56999, 1: 22 })
const yesterdayStr = formatTimeYMD(yesterday)
const yesterdayData = await db.get(yesterdayStr)
expect(yesterdayData).toEqual({ 95: 2 })
})
test('getBatch', async () => {
const date = new Date(2021, 5, 7)
const yesterday = new Date(2021, 5, 6)
const toAdd: timer.period.Result[] = [
resultOf(date, 0, 56999),
resultOf(date, 1, 2),
resultOf(yesterday, 95, 2)
]
await db.accumulate(toAdd)
let list = await db.getBatch(['20210607', '20210606'])
expect(list.length).toEqual(3)
let all = await db.getAll()
expect(all).toEqual(toAdd)
})
test("importData", async () => {
const date = new Date(2021, 5, 7)
const yesterday = new Date(2021, 5, 6)
const toAdd: timer.period.Result[] = [
resultOf(date, 0, 56999),
resultOf(date, 1, 2),
resultOf(yesterday, 95, 2)
]
await db.accumulate(toAdd)
const data2Import = await db.storage.get()
chrome.storage.local.clear()
expect(await db.getAll()).toEqual([])
data2Import.foo = "bar"
db.importData(data2Import)
const imported = await db.getAll()
expect(imported.length).toEqual(3)
})
// Invalid data
test("importData2", async () => {
await db.importData(undefined)
expect(await db.getAll()).toEqual([])
await db.importData({ foo: "bar" })
expect(await db.getAll()).toEqual([])
await db.importData([])
expect(await db.getAll()).toEqual([])
await db.importData(1)
expect(await db.getAll()).toEqual([])
await db.importData({
__timer__PERIOD20210607: {
"-1": 100,
foo: "bar",
96: 1000,
85: "???",
3: undefined,
4: "",
}
})
expect(await db.getAll()).toEqual([])
})
test("importData3", async () => {
await db.importData({
__timer__PERIOD20210607: {
0: MILL_PER_PERIOD + 1,
1: 100,
2: "100",
}
})
const imported: timer.period.Result[] = await db.getAll()
expect(imported.length).toEqual(3)
const orderMillMap: Record<string, number> = {}
imported.forEach(({ milliseconds, order }) => orderMillMap[order] = milliseconds)
expect(orderMillMap).toEqual({ 0: MILL_PER_PERIOD, 1: 100, 2: 100 })
})
})