forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit-database.test.ts
More file actions
144 lines (133 loc) · 4.44 KB
/
limit-database.test.ts
File metadata and controls
144 lines (133 loc) · 4.44 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
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
import db from "@db/limit-database"
import { formatTimeYMD } from "@util/time"
import { mockStorage } from "../__mock__/storage"
import { mockLegacyData } from './migratable'
describe('limit-database', () => {
beforeAll(() => mockStorage())
beforeEach(async () => chrome.storage.local.clear())
test('test1', async () => {
const toAdd: timer.limit.Rule = {
id: 1,
name: "foobar",
cond: ['123'],
time: 20,
enabled: true,
allowDelay: false,
locked: false,
}
const id = await db.save(toAdd)
let all: timer.limit.Rule[] = await db.all()
expect(all.length).toEqual(1)
let saved = all[0]
expect(saved.cond).toEqual(toAdd.cond)
expect(saved.time).toEqual(toAdd.time)
expect(saved.name).toEqual(toAdd.name)
expect(saved.enabled).toEqual(toAdd.enabled)
expect(saved.allowDelay).toEqual(toAdd.allowDelay)
const toRewrite = {
id,
name: 'hahah',
cond: ['123'],
time: 21,
enabled: true,
allowDelay: false,
locked: false,
}
// Not rewrite
await db.save(toRewrite)
all = await db.all()
saved = all[0]
expect(saved.cond).toEqual(toAdd.cond)
expect(saved.time).toEqual(toAdd.time)
expect(saved.name).toEqual(toAdd.name)
expect(saved.enabled).toEqual(toAdd.enabled)
expect(saved.allowDelay).toEqual(toAdd.allowDelay)
await db.remove(id)
expect((await db.all()).length).toEqual(0)
})
test("update waste", async () => {
const date = formatTimeYMD(new Date())
const id1 = await db.save({
name: "foobar",
cond: ["a.*.com"],
time: 21,
enabled: true,
allowDelay: false,
locked: false,
})
await db.save({
name: "foobar",
cond: ["*.b.com"],
time: 20,
enabled: true,
allowDelay: false,
locked: false,
})
await db.updateWaste(date, {
[id1]: 10,
// Not exist, no error throws
[-1]: 20,
})
const all = await db.all()
const used = all.find(a => a.cond?.includes("a.*.com"))
expect(used?.records?.[date]).toBeTruthy()
expect(used?.records?.[date].mill).toEqual(10)
})
test("import data", async () => {
const cond1: MakeOptional<timer.limit.Rule, 'id'> = {
name: 'foobar1',
cond: ["cond1"],
time: 20,
allowDelay: false,
enabled: true,
locked: false,
}
const cond2: MakeOptional<timer.limit.Rule, 'id'> = {
name: 'foobar2',
cond: ["cond2"],
time: 20,
allowDelay: false,
enabled: false,
locked: false,
}
await db.save(cond1)
await db.save(cond2)
const data2Import = await db.storage.get()
// clear
chrome.storage.local.clear()
expect(await db.all()).toEqual([])
await db.importData(mockLegacyData(data2Import))
const imported = await db.all()
const cond2After = imported.find(a => a.cond?.includes("cond2"))
expect(Object.values(cond2After?.records || {})).toBeTruthy()
expect(cond2After?.allowDelay).toEqual(cond2.allowDelay)
expect(cond2After?.enabled).toEqual(cond2.enabled)
})
test("import data2", async () => {
const importData: Record<string, any> = {}
// Invalid data, no error throws
await db.importData(mockLegacyData(importData))
// Valid data
importData["__timer__LIMIT"] = {}
await db.importData(mockLegacyData(importData))
expect(await db.all()).toEqual([])
})
test("update delay", async () => {
const data: MakeOptional<timer.limit.Rule, 'id'> = {
name: 'foobar',
cond: ["cond1"],
time: 20,
allowDelay: false,
enabled: true,
locked: false,
}
const id = await db.save(data)
await db.updateDelay(id, true)
await db.updateDelay(Number.MAX_VALUE, true)
const all = await db.all()
expect(all.length).toEqual(1)
const item = all[0]
expect(item.allowDelay).toBeTruthy()
expect(item.cond).toEqual(["cond1"])
})
})