-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2446 from dusk-network/feature-2443
web-wallet: Move cache test helpers in a common folder
- Loading branch information
Showing
12 changed files
with
226 additions
and
80 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
web-wallet/src/lib/test-helpers/__tests__/fillCacheDatabase.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { getKey, sortWith } from "lamb"; | ||
|
||
import { | ||
cacheHistory, | ||
cacheSpentNotes, | ||
cacheUnspentNotes, | ||
} from "$lib/mock-data"; | ||
|
||
import { fillCacheDatabase, getCacheDatabase, sortCacheNotes } from ".."; | ||
|
||
const sortHistory = sortWith([getKey("id")]); | ||
|
||
describe("fillCacheDatabase", () => { | ||
it("should fill the database tables with mock data", async () => { | ||
const expectedHistory = sortHistory(cacheHistory); | ||
const expectedSpentNotes = sortCacheNotes(cacheSpentNotes); | ||
const expectedUnspentNotes = sortCacheNotes(cacheUnspentNotes); | ||
|
||
await fillCacheDatabase(); | ||
|
||
const db = getCacheDatabase(); | ||
|
||
await db.open(); | ||
|
||
await expect( | ||
db.table("history").toArray().then(sortHistory) | ||
).resolves.toStrictEqual(expectedHistory); | ||
await expect( | ||
db.table("spentNotes").toArray().then(sortCacheNotes) | ||
).resolves.toStrictEqual(expectedSpentNotes); | ||
await expect( | ||
db.table("unspentNotes").toArray().then(sortCacheNotes) | ||
).resolves.toStrictEqual(expectedUnspentNotes); | ||
|
||
db.close(); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
web-wallet/src/lib/test-helpers/__tests__/getCacheDatabase.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { getKey } from "lamb"; | ||
|
||
import { getCacheDatabase } from ".."; | ||
|
||
describe("getCacheDatabase", () => { | ||
it("should return the Dexie database used for the cache", async () => { | ||
const db = getCacheDatabase(); | ||
|
||
await db.open(); | ||
|
||
expect(db.tables.map(getKey("name"))).toMatchInlineSnapshot(` | ||
[ | ||
"history", | ||
"spentNotes", | ||
"unspentNotes", | ||
] | ||
`); | ||
|
||
db.close(); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
web-wallet/src/lib/test-helpers/__tests__/getCacheTableCount.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { getKey } from "lamb"; | ||
|
||
import { fillCacheDatabase, getCacheDatabase, getCacheTableCount } from ".."; | ||
|
||
describe("getCacheTableCount", () => { | ||
it("should return the amount of items in a database table", async () => { | ||
const db = getCacheDatabase(); | ||
|
||
await db.open(); | ||
|
||
const tableNames = /** @type {WalletCacheTableName[]} */ ( | ||
db.tables.map(getKey("name")) | ||
); | ||
|
||
for (const tableName of tableNames) { | ||
await expect(getCacheTableCount(tableName)).resolves.toBe(0); | ||
} | ||
|
||
await fillCacheDatabase(); | ||
|
||
for (const tableName of tableNames) { | ||
await expect(getCacheTableCount(tableName)).resolves.toBe( | ||
await db.table(tableName).count() | ||
); | ||
} | ||
|
||
db.close(); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
web-wallet/src/lib/test-helpers/__tests__/sortCacheNotes.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { cacheSpentNotes } from "$lib/mock-data"; | ||
|
||
import { sortCacheNotes } from ".."; | ||
|
||
describe("sortCacheNotes", () => { | ||
it("should sort a list of notes by their nullifier", () => { | ||
const sortedNotes = cacheSpentNotes.toSorted((a, b) => | ||
a.nullifier.toString() > b.nullifier.toString() ? 1 : -1 | ||
); | ||
|
||
expect(sortCacheNotes(cacheSpentNotes)).toStrictEqual(sortedNotes); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getCacheDatabase } from "."; | ||
|
||
import { | ||
cacheHistory, | ||
cacheSpentNotes, | ||
cacheUnspentNotes, | ||
} from "$lib/mock-data"; | ||
|
||
/** @type {() => Promise<void>} */ | ||
async function fillCacheDatabase() { | ||
const db = getCacheDatabase(); | ||
|
||
await db.open(); | ||
|
||
return db | ||
.transaction("rw", ["history", "spentNotes", "unspentNotes"], async () => { | ||
await db.table("history").bulkPut(cacheHistory); | ||
await db.table("spentNotes").bulkPut(cacheSpentNotes); | ||
await db.table("unspentNotes").bulkPut(cacheUnspentNotes); | ||
}) | ||
.finally(() => { | ||
db.close(); | ||
}); | ||
} | ||
|
||
export default fillCacheDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Dexie } from "dexie"; | ||
|
||
/** @type {() => Dexie} */ | ||
function getCacheDatabase() { | ||
const db = new Dexie("@dusk-network/wallet-cache"); | ||
|
||
db.version(1).stores({ | ||
history: "psk", | ||
spentNotes: "nullifier,&pos,psk", | ||
unspentNotes: "nullifier,&pos,psk", | ||
}); | ||
|
||
return db; | ||
} | ||
|
||
export default getCacheDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { getCacheDatabase } from "."; | ||
|
||
/** @type {(tableName: WalletCacheTableName) => Promise<number>} */ | ||
async function getCacheTableCount(tableName) { | ||
const db = await getCacheDatabase().open(); | ||
|
||
return db | ||
.table(tableName) | ||
.count() | ||
.finally(() => db.close()); | ||
} | ||
|
||
export default getCacheTableCount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { default as fillCacheDatabase } from "./fillCacheDatabase"; | ||
export { default as getCacheDatabase } from "./getCacheDatabase"; | ||
export { default as getCacheTableCount } from "./getCacheTableCount"; | ||
export { default as sortCacheNotes } from "./sortCacheNotes"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { sortWith } from "lamb"; | ||
|
||
/** | ||
* We need to sort the notes in tests as the | ||
* database doesn't guarantee a sort order. | ||
* | ||
* @type {(notes: WalletCacheNote[]) => WalletCacheNote[]} | ||
*/ | ||
const sortCacheNotes = sortWith([ | ||
/** @type {(entry: WalletCacheNote) => string} */ ( | ||
(entry) => entry.nullifier.toString() | ||
), | ||
]); | ||
|
||
export default sortCacheNotes; |
Oops, something went wrong.