57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { KeyValueStorage, MemoryStorage } from "./basic.ts";
|
|
import { expect } from "https://code.thunderk.net/typescript/devtools/raw/1.2.2/testing.ts";
|
|
export {
|
|
describe,
|
|
expect,
|
|
it,
|
|
} from "https://code.thunderk.net/typescript/devtools/raw/1.2.2/testing.ts";
|
|
import { getAvailablePort } from "https://deno.land/x/port@1.0.0/mod.ts";
|
|
import { startRestServer } from "./server.ts";
|
|
|
|
/**
|
|
* 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 {
|
|
await body();
|
|
} finally {
|
|
ns["localStorage"] = removed;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Run a temporary test server, with memory storage
|
|
*/
|
|
export async function runTestServer(
|
|
body: (url: string) => Promise<void>,
|
|
): Promise<void> {
|
|
const port = await getAvailablePort();
|
|
if (!port) {
|
|
throw new Error("No port available for test server");
|
|
}
|
|
|
|
const app = await startRestServer(port, new MemoryStorage());
|
|
try {
|
|
await body(`http://localhost:${port}`);
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
}
|