Skip to content

Build your personal knowledge base with Trilium Notes

test(formatDownloadTitle): add basic test

Panagiotis Papadopoulosd173daa14e87
diff --git a/spec-es6/utils/formatDownloadTitle.spec.ts b/spec-es6/utils/formatDownloadTitle.spec.ts
new file mode 100644
index 0000000000..9237869150
--- /dev/null
+++ b/spec-es6/utils/formatDownloadTitle.spec.ts
@@ -0,0 +1,129 @@
+import { formatDownloadTitle } from "../../src/services/utils.ts";
+import { describe, it, execute, expect } from "../mini_test.ts";
+
+const testCases: [fnValue: Parameters<typeof formatDownloadTitle>, expectedValue: ReturnType<typeof formatDownloadTitle>][] = [
+ // empty fileName tests
+ [
+ ["", "text", ""],
+ "untitled.html"
+ ],
+
+ [
+ ["", "canvas", ""],
+ "untitled.json"
+ ],
+
+ [
+ ["", null, ""],
+ "untitled"
+ ],
+
+ // json extension from type tests
+ [
+ ["test_file", "canvas", ""],
+ "test_file.json"
+ ],
+
+ [
+ ["test_file", "relationMap", ""],
+ "test_file.json"
+ ],
+
+ [
+ ["test_file", "search", ""],
+ "test_file.json"
+ ],
+
+ // extension based on mime type
+ [
+ ["test_file", null, "text/csv"],
+ "test_file.csv"
+ ],
+
+ [
+ ["test_file_wo_ext", "image", "image/svg+xml"],
+ "test_file_wo_ext.svg"
+ ],
+
+ [
+ ["test_file_wo_ext", "file", "application/json"],
+ "test_file_wo_ext.json"
+ ],
+
+ [
+ ["test_file_w_fake_ext.ext", "image", "image/svg+xml"],
+ "test_file_w_fake_ext.ext.svg"
+ ],
+
+ [
+ ["test_file_w_correct_ext.svg", "image", "image/svg+xml"],
+ "test_file_w_correct_ext.svg"
+ ],
+
+ [
+ ["test_file_w_correct_ext.svgz", "image", "image/svg+xml"],
+ "test_file_w_correct_ext.svgz"
+ ],
+
+ [
+ ["test_file.zip", "file", "application/zip"],
+ "test_file.zip"
+ ],
+
+ [
+ ["test_file", "file", "application/zip"],
+ "test_file.zip"
+ ],
+
+ // application/octet-stream tests
+ [
+ ["test_file", "file", "application/octet-stream"],
+ "test_file"
+ ],
+
+ [
+ ["test_file.zip", "file", "application/octet-stream"],
+ "test_file.zip"
+ ],
+
+ [
+ ["test_file.unknown", null, "application/octet-stream"],
+ "test_file.unknown"
+ ],
+
+ // sanitized filename tests
+ [
+ ["test/file", null, "application/octet-stream"],
+ "testfile"
+ ],
+
+ [
+ ["test:file.zip", "file", "application/zip"],
+ "testfile.zip"
+ ],
+
+ [
+ [":::", "file", "application/zip"],
+ ".zip"
+ ],
+
+ [
+ [":::a", "file", "application/zip"],
+ "a.zip"
+ ],
+]
+
+
+describe("utils/formatDownloadTitle unit tests", () => {
+
+ testCases.forEach(testCase => {
+ return it(`With args '${JSON.stringify(testCase[0])}' it should return '${testCase[1]}'`, () => {
+ const [value, expected] = testCase;
+ const actual = formatDownloadTitle(...value);
+ expect(actual).toEqual(expected);
+ })
+ })
+
+})
+
+execute()
\ No newline at end of file