|
| 1 | +import { join } from "path"; |
| 2 | +import { cwd } from "process"; |
| 3 | +import { FileType } from "./file-type.enum"; |
| 4 | +import { generateOutputPath } from "./generate-output-path.function"; |
| 5 | + |
| 6 | +describe("generate-output-path", () => { |
| 7 | + it("should default to a PNG file without pre- or postfix in the current directory", () => { |
| 8 | + // GIVEN |
| 9 | + const filename = "asdf"; |
| 10 | + const ext = FileType.PNG; |
| 11 | + const expectedPath = join(cwd(), `${filename}${ext}`); |
| 12 | + |
| 13 | + // WHEN |
| 14 | + const result = generateOutputPath(filename); |
| 15 | + |
| 16 | + // THEN |
| 17 | + expect(result).toEqual(expectedPath); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should should allow to add a prefix to the filename", () => { |
| 21 | + // GIVEN |
| 22 | + const filename = "asdf"; |
| 23 | + const pre = "foo_"; |
| 24 | + const ext = FileType.PNG; |
| 25 | + const expectedPath = join(cwd(), `${pre}${filename}${ext}`); |
| 26 | + |
| 27 | + // WHEN |
| 28 | + const result = generateOutputPath(filename, {prefix: pre}); |
| 29 | + |
| 30 | + // THEN |
| 31 | + expect(result).toEqual(expectedPath); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should should allow to add a postfix to the filename", () => { |
| 35 | + // GIVEN |
| 36 | + const filename = "asdf"; |
| 37 | + const post = "_bar"; |
| 38 | + const ext = FileType.PNG; |
| 39 | + const expectedPath = join(cwd(), `${filename}${post}${ext}`); |
| 40 | + |
| 41 | + // WHEN |
| 42 | + const result = generateOutputPath(filename, {postfix: post}); |
| 43 | + |
| 44 | + // THEN |
| 45 | + expect(result).toEqual(expectedPath); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should should allow to add both a prefix and a postfix to the filename", () => { |
| 49 | + // GIVEN |
| 50 | + const filename = "asdf"; |
| 51 | + const pre = "foo_"; |
| 52 | + const post = "_bar"; |
| 53 | + const ext = FileType.PNG; |
| 54 | + const expectedPath = join(cwd(), `${pre}${filename}${post}${ext}`); |
| 55 | + |
| 56 | + // WHEN |
| 57 | + const result = generateOutputPath(filename, { |
| 58 | + postfix: post, |
| 59 | + prefix: pre, |
| 60 | + }); |
| 61 | + |
| 62 | + // THEN |
| 63 | + expect(result).toEqual(expectedPath); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should should allow to configure the file path", () => { |
| 67 | + // GIVEN |
| 68 | + const filename = "asdf"; |
| 69 | + const filepath = "/foo/test/bar"; |
| 70 | + const ext = FileType.PNG; |
| 71 | + const expectedPath = join(filepath, `${filename}${ext}`); |
| 72 | + |
| 73 | + // WHEN |
| 74 | + const result = generateOutputPath(filename, { |
| 75 | + path: filepath, |
| 76 | + }); |
| 77 | + |
| 78 | + // THEN |
| 79 | + expect(result).toEqual(expectedPath); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should handle relative file path", () => { |
| 83 | + // GIVEN |
| 84 | + const filename = "asdf"; |
| 85 | + const filepath = "/foo/../bar"; |
| 86 | + const ext = FileType.PNG; |
| 87 | + const expectedPath = join(filepath, `${filename}${ext}`); |
| 88 | + |
| 89 | + // WHEN |
| 90 | + const result = generateOutputPath(filename, { |
| 91 | + path: filepath, |
| 92 | + }); |
| 93 | + |
| 94 | + // THEN |
| 95 | + expect(result).toEqual(expectedPath); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should handle different file types", () => { |
| 99 | + // GIVEN |
| 100 | + const filename = "asdf"; |
| 101 | + const ext = FileType.JPG; |
| 102 | + const expectedPath = join(cwd(), `${filename}${ext}`); |
| 103 | + |
| 104 | + // WHEN |
| 105 | + const result = generateOutputPath(filename, { |
| 106 | + type: FileType.JPG |
| 107 | + }); |
| 108 | + |
| 109 | + // THEN |
| 110 | + expect(result).toEqual(expectedPath); |
| 111 | + }); |
| 112 | +}); |
0 commit comments