more tests

This commit is contained in:
Ticruz
2025-02-04 13:21:23 +01:00
committed by Jon Staab
parent 8c638a7d8f
commit 23f7244039
14 changed files with 83 additions and 80 deletions
+14
View File
@@ -4,6 +4,7 @@ import {afterEach, beforeEach, describe, expect, it, vi} from "vitest"
import {follow, mute, pin, unfollow, unmute, unpin} from "../src/commands"
import * as thunkModule from "../src/thunk"
import {thunkWorker} from "../src/thunk"
import {repository} from "../src/core"
vi.mock(import("@welshman/lib"), async importOriginal => ({
...(await importOriginal()),
@@ -48,6 +49,8 @@ describe("commands", () => {
vi.resetModules()
// Clear any cached data
vi.clearAllMocks()
repository.load([])
})
afterEach(() => {
@@ -64,6 +67,7 @@ describe("commands", () => {
it("should create new follows list if none exists", async () => {
const publishThunkSpy = vi.spyOn(thunkModule, "publishThunk")
await follow(["p", pubkey1])
await vi.runAllTimersAsync()
expect(publishThunkSpy).toHaveBeenCalledWith(
expect.objectContaining({
@@ -85,6 +89,8 @@ describe("commands", () => {
await follow(["p", pubkey2])
await vi.runAllTimersAsync()
expect(publishThunkSpy).toHaveBeenCalledWith(
expect.objectContaining({
event: expect.objectContaining({
@@ -126,6 +132,8 @@ describe("commands", () => {
await mute(["p", pubkey1])
await vi.runAllTimersAsync()
expect(publishThunkSpy).toHaveBeenCalledWith({
event: expect.objectContaining({
kind: MUTES,
@@ -144,6 +152,8 @@ describe("commands", () => {
await mute(["p", pubkey2])
await vi.runAllTimersAsync()
expect(publishThunkSpy).toHaveBeenCalledWith(
expect.objectContaining({
event: expect.objectContaining({
@@ -184,6 +194,8 @@ describe("commands", () => {
const publishThunkSpy = vi.spyOn(thunkModule, "publishThunk")
await pin(["e", event1])
await vi.runAllTimersAsync()
expect(publishThunkSpy).toHaveBeenCalledWith(
expect.objectContaining({
event: expect.objectContaining({
@@ -204,6 +216,8 @@ describe("commands", () => {
await pin(["e", event2])
await vi.runAllTimersAsync()
expect(publishThunkSpy).toHaveBeenCalledWith(
expect.objectContaining({
event: expect.objectContaining({
+4 -2
View File
@@ -4,7 +4,6 @@ import {Repository} from "@welshman/util"
import {Tracker} from "@welshman/net"
import {
initStorage,
closeStorage,
clearStorage,
storageAdapters,
dead,
@@ -25,7 +24,7 @@ describe("storage", () => {
afterEach(async () => {
vi.useRealTimers()
await closeStorage()
await clearStorage()
// Clean up the test database
await new Promise((resolve, reject) => {
const req = indexedDB.deleteDatabase(DB_NAME)
@@ -99,9 +98,12 @@ describe("storage", () => {
store.update(items => items.filter(item => item.id !== "1"))
await vi.runAllTimersAsync()
const itemsPromise = getAll("items")
await vi.runAllTimersAsync()
const items = await itemsPromise
await vi.runAllTimersAsync()
expect(items).toHaveLength(1)
expect(items[0]).toEqual({id: "2", value: "test2"})
+3 -2
View File
@@ -18,7 +18,7 @@ import {
// Mock dependencies
vi.mock("@welshman/net", () => ({
publish: vi.fn(),
publish: vi.fn().mockReturnValue({emitter: {on: vi.fn()}}),
PublishStatus: {
Pending: "pending",
Success: "success",
@@ -269,8 +269,9 @@ describe("thunkWorker", async () => {
it("should handle publish failures", async () => {
const mockSigner = {
sign: vi.fn().mockRejectedValue(new Error("Signing failed")),
sign: vi.fn().mockRejectedValue("Signing failed"),
}
vi.mocked(sessionModule.getSigner).mockReturnValue(mockSigner)
const thunk = makeThunk(mockRequest)