storage/src/testing.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

import { KeyValueStorage, MemoryStorage } from "./basic.ts";
import { startRestServer } from "./server.ts";
2021-09-16 21:22:24 +00:00
import { describe, expect, it } from "../deps.testing.ts";
2021-08-16 16:15:01 +00:00
export { describe, expect, it };
2021-06-21 22:05:08 +00:00
2021-09-16 21:22:24 +00:00
const PORT = 5002;
2021-06-21 22:05:08 +00:00
/**
* Basic high-level test suite for any kind storage
*/
export async function basicCheck(storage: KeyValueStorage): Promise<void> {
expect(await storage.get("test")).toBe(null);
await storage.set("test", "val");
expect(await storage.get("test")).toBe("val");
expect(await storage.get("notest")).toBe(null);
await storage.set("test", null);
expect(await storage.get("test")).toBe(null);
}
/**
* Temporarily disable localStorage (constructor will throw error),
* for the duration of the callback
*/
export async function disableLocalStorage(
body: () => Promise<void>,
): Promise<void> {
const ns = window as any;
const removed = ns["localStorage"];
delete ns["localStorage"];
try {
2021-06-22 20:07:26 +00:00
await body();
2021-06-21 22:05:08 +00:00
} finally {
ns["localStorage"] = removed;
}
}
/**
* Run a temporary test server, with memory storage
*/
export async function runTestServer(
body: (url: string) => Promise<void>,
): Promise<void> {
2021-09-16 21:22:24 +00:00
const app = await startRestServer(PORT, new MemoryStorage());
2021-06-21 22:05:08 +00:00
try {
2021-09-16 21:22:24 +00:00
await body(`http://localhost:${PORT}`);
2021-06-21 22:05:08 +00:00
} finally {
await app.close();
}
}