63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { defaultExportConfig } from "@md-to-pdf/core";
|
|
import {
|
|
EXPORT_CONFIG_STORAGE_KEY,
|
|
loadExportConfig,
|
|
parseStoredExportConfig,
|
|
saveExportConfig
|
|
} from "../src/export-settings";
|
|
|
|
function createMemoryStorage(initialValue: string | null = null) {
|
|
let value = initialValue;
|
|
return {
|
|
getItem(key: string) {
|
|
return key === EXPORT_CONFIG_STORAGE_KEY ? value : null;
|
|
},
|
|
setItem(key: string, nextValue: string) {
|
|
if (key === EXPORT_CONFIG_STORAGE_KEY) {
|
|
value = nextValue;
|
|
}
|
|
},
|
|
read() {
|
|
return value;
|
|
}
|
|
};
|
|
}
|
|
|
|
describe("导出设置缓存", () => {
|
|
it("在没有缓存时返回独立的默认配置", () => {
|
|
const first = parseStoredExportConfig(null);
|
|
const second = parseStoredExportConfig(null);
|
|
expect(first).toEqual(defaultExportConfig);
|
|
expect(first).not.toBe(second);
|
|
});
|
|
|
|
it("缓存损坏或版本过期时恢复默认配置", () => {
|
|
expect(parseStoredExportConfig("{")).toEqual(defaultExportConfig);
|
|
expect(
|
|
parseStoredExportConfig(
|
|
JSON.stringify({
|
|
...defaultExportConfig,
|
|
version: 1
|
|
})
|
|
)
|
|
).toEqual(defaultExportConfig);
|
|
});
|
|
|
|
it("保存并恢复经过校验的配置", () => {
|
|
const storage = createMemoryStorage();
|
|
const config = {
|
|
...defaultExportConfig,
|
|
paper: {
|
|
...defaultExportConfig.paper,
|
|
format: "A3" as const,
|
|
orientation: "landscape" as const
|
|
}
|
|
};
|
|
|
|
saveExportConfig(config, storage);
|
|
expect(storage.read()).toContain('"format":"A3"');
|
|
expect(loadExportConfig(storage)).toEqual(config);
|
|
});
|
|
});
|