Skip to content

Commit fa2bae1

Browse files
committed
Add use cases for whitelist-database.ts
1 parent 9dd8d71 commit fa2bae1

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@
4545
"vue": "^3.0.11",
4646
"vue-router": "^4.0.8"
4747
}
48-
}
48+
}

src/background/context-menus-manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import whitelistDatabase from '../database/whitelist-database'
1+
import WhitelistDatabase from '../database/whitelist-database'
22
import { t2Chrome } from '../util/i18n/chrome/t'
33
import { extractHostname, isBrowserUrl } from '../util/pattern'
44

5-
const db = whitelistDatabase
5+
const db = new WhitelistDatabase(chrome.storage.local)
66

77
const menuId = '_timer_menu_item_' + Date.now()
88

src/database/whitelist-database.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { WHITELIST_KEY } from "./constant"
22

3-
4-
const ruleId = '_timer_whitelist_db_change_rule_id'
5-
63
class WhitelistDatabase {
74

8-
private localStorage = chrome.storage.local
5+
private localStorage: chrome.storage.StorageArea
96

10-
constructor() { }
7+
constructor(storage: chrome.storage.StorageArea) {
8+
this.localStorage = storage
9+
}
1110

1211
private update(selectAll: string[]): Promise<void> {
1312
const obj = {}
@@ -62,4 +61,4 @@ class WhitelistDatabase {
6261
}
6362
}
6463

65-
export default new WhitelistDatabase()
64+
export default WhitelistDatabase

src/service/timer-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import TimerDatabase, { TimerCondition } from '../database/timer-database'
2-
import whitelistDatabase from '../database/whitelist-database'
2+
import WhitelistDatabase from '../database/whitelist-database'
33
import ArchivedDatabase from '../database/archived-database'
44
import SiteInfo from '../entity/dto/site-info'
55
import { log } from '../common/logger'
@@ -13,6 +13,7 @@ const timerDatabase = new TimerDatabase(chrome.storage.local)
1313
const archivedDatabase = new ArchivedDatabase(chrome.storage.local)
1414
const iconUrlDatabase = new IconUrlDatabase(chrome.storage.local)
1515
const mergeRuleDatabase = new MergeRuleDatabase(chrome.storage.local)
16+
const whitelistDatabase = new WhitelistDatabase(chrome.storage.local)
1617

1718
declare type PageParam = {
1819
pageNum?: number

src/service/whitelist-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { log } from "../common/logger"
2-
import whitelistDatabase from "../database/whitelist-database"
2+
import WhitelistDatabase from "../database/whitelist-database"
33

4+
const whitelistDatabase = new WhitelistDatabase(chrome.storage.local)
45
/**
56
* Service of whitelist
67
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import WhitelistDatabase from "../../src/database/whitelist-database"
2+
import storage from "../__mock__/storage"
3+
4+
const db = new WhitelistDatabase(storage.local)
5+
6+
describe('timer-database', () => {
7+
beforeEach(async () => storage.local.clear())
8+
9+
test('1', async () => {
10+
await db.add('www.baidu.com')
11+
await db.add('google.com')
12+
const list = await db.selectAll()
13+
expect(list.sort()).toEqual(['www.baidu.com', 'google.com'].sort())
14+
expect((await db.includes('www.baidu.com'))).toBeTruthy()
15+
await db.remove('www.baidu.com')
16+
expect((await db.selectAll())).toEqual(['google.com'])
17+
expect((await db.includes('www.baidu.com'))).toBeFalsy()
18+
await db.add('google.com')
19+
expect((await db.selectAll())).toEqual(['google.com'])
20+
})
21+
})

0 commit comments

Comments
 (0)