From c3587ad5361416f29d808eda89b502c7f65489e4 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 31 Jan 2025 21:32:52 +0100 Subject: [PATCH] test(server/utils): add tests for getNoteTitle --- src/services/utils.spec.ts | 61 +++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/services/utils.spec.ts b/src/services/utils.spec.ts index 16bdbccc5..9fbb6c545 100644 --- a/src/services/utils.spec.ts +++ b/src/services/utils.spec.ts @@ -222,7 +222,66 @@ describe("#removeTextFileExtension", () => { }); -describe.todo("#getNoteTitle", () => {}); +describe("#getNoteTitle", () => { + const testCases: TestCase[] = [ + [ + "when file has no spaces, and no special file extension, it should return the filename unaltered", + ["test.json", true, undefined], + "test.json" + ], + [ + "when replaceUnderscoresWithSpaces is false, it should keep the underscores in the title", + ["test_file.json", false, undefined], + "test_file.json" + ], + [ + "when replaceUnderscoresWithSpaces is true, it should replace the underscores in the title", + ["test_file.json", true, undefined], + "test file.json" + ], + [ + "when filePath ends with one of the extra handled endings (.md), it should strip the file extension from the title", + ["test_file.md", false, undefined], + "test_file" + ], + [ + "when filePath ends with one of the extra handled endings (.md) and replaceUnderscoresWithSpaces is true, it should strip the file extension from the title and replace underscores", + ["test_file.md", true, undefined], + "test file" + ], + [ + "when filepath contains a full path, it should only return the basename of the file", + ["Trilium Demo/Scripting examples/Statistics/Most cloned notes/template.zip", true, undefined], + "template.zip" + ], + [ + "when filepath contains a full path and has extra handled ending (.html), it should only return the basename of the file and strip the file extension", + ["Trilium Demo/Scripting examples/Statistics/Most cloned notes/template.html", true, undefined], + "template" + ], + [ + "when a noteMeta object is passed, it should use the title from the noteMeta, if present", + //@ts-expect-error - passing in incomplete noteMeta - but we only care about the title prop here + ["test_file.md", true, { title: "some other title"}], + "some other title" + ], + [ + "when a noteMeta object is passed, but the title prop is empty, it should try to handle the filename as if no noteMeta was passed", + //@ts-expect-error - passing in incomplete noteMeta - but we only care about the title prop here + ["test_file.md", true, { title: ""}], + "test file" + ] + ]; + + testCases.forEach(testCase => { + const [desc, fnParams, expected] = testCase; + it(desc, () => { + const result = utils.getNoteTitle(...fnParams); + expect(result).toStrictEqual(expected); + }); + }); + +}); describe("#timeLimit", () => {