From e6f5321444933a86e14da55b346141ff48c1cc23 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Wed, 22 Jan 2025 08:23:44 +0100 Subject: [PATCH] test(server/utils): move formatDownloadTitle tests to spec file --- .../utils.formatDownloadTitle.spec.ts | 61 --------- src/services/utils.spec.ts | 116 +++++++++++++++++- 2 files changed, 115 insertions(+), 62 deletions(-) delete mode 100644 src/services/utils.formatDownloadTitle.spec.ts diff --git a/src/services/utils.formatDownloadTitle.spec.ts b/src/services/utils.formatDownloadTitle.spec.ts deleted file mode 100644 index 0cc259e16..000000000 --- a/src/services/utils.formatDownloadTitle.spec.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { expect, describe, it } from "vitest"; -import { formatDownloadTitle } from "./utils.js"; - -const testCases: [fnValue: Parameters, expectedValue: ReturnType][] = [ - // 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).toStrictEqual(expected); - }); - }); -}); \ No newline at end of file diff --git a/src/services/utils.spec.ts b/src/services/utils.spec.ts index c2712ccc9..9986f5dc2 100644 --- a/src/services/utils.spec.ts +++ b/src/services/utils.spec.ts @@ -72,4 +72,118 @@ describe.todo("#getResourceDir", () => {}); describe.todo("#isMac", () => {}); -describe.todo("#isWindows", () => {}); \ No newline at end of file +describe.todo("#isWindows", () => {}); + + +describe("#formatDownloadTitle", () => { + + //prettier-ignore + const testCases: [fnValue: Parameters, expectedValue: ReturnType][] = [ + + // 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" + ] + ]; + + testCases.forEach((testCase) => { + const [fnParams, expected] = testCase; + return it(`With args '${JSON.stringify(fnParams)}', it should return '${expected}'`, () => { + const actual = utils.formatDownloadTitle(...fnParams); + expect(actual).toStrictEqual(expected); + }); + }); +}); \ No newline at end of file