Skip to content

Commit

Permalink
Merge pull request #2446 from dusk-network/feature-2443
Browse files Browse the repository at this point in the history
web-wallet: Move cache test helpers in a common folder
  • Loading branch information
ascartabelli authored Sep 20, 2024
2 parents 204431f + 7544a96 commit 97bc50f
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 80 deletions.
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 web-wallet/src/lib/test-helpers/__tests__/getCacheDatabase.spec.js
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();
});
});
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 web-wallet/src/lib/test-helpers/__tests__/sortCacheNotes.spec.js
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);
});
});
26 changes: 26 additions & 0 deletions web-wallet/src/lib/test-helpers/fillCacheDatabase.js
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;
16 changes: 16 additions & 0 deletions web-wallet/src/lib/test-helpers/getCacheDatabase.js
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;
13 changes: 13 additions & 0 deletions web-wallet/src/lib/test-helpers/getCacheTableCount.js
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;
4 changes: 4 additions & 0 deletions web-wallet/src/lib/test-helpers/index.js
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";
15 changes: 15 additions & 0 deletions web-wallet/src/lib/test-helpers/sortCacheNotes.js
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;
Loading

0 comments on commit 97bc50f

Please sign in to comment.