generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for resizing feature
- Loading branch information
Showing
2 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { expect, describe, it } from "vitest"; | ||
import { isWrapped } from "../src/imgur/resizing/MarkdownImagePieces"; | ||
import findImgurMarkdownImage from "../src/imgur/resizing/md-image-parsing"; | ||
|
||
describe("findImgurMarkdownImage", () => { | ||
const simplestImage = "![](https://i.imgur.com/m3RpPCV.png)"; | ||
|
||
it.each([ | ||
{ line: simplestImage, cursorAt: 0 }, | ||
{ line: simplestImage, cursorAt: 35 }, | ||
{ line: "![](https://i.imgur.com/m3RpPCVm.png)", cursorAt: 0 }, | ||
])("GIVEN line '$line' and cursor pos: $cursorAt", ({ line, cursorAt }) => { | ||
const match = findImgurMarkdownImage(line, cursorAt); | ||
expect(match.exists).toBeTruthy(); | ||
}); | ||
|
||
it("matches an image when cursor position is set to last character of image", () => { | ||
const match = findImgurMarkdownImage(simplestImage, 35); | ||
expect(match.exists).toBeTruthy(); | ||
}); | ||
|
||
it("does not match an image when cursor position is after an image", () => { | ||
const match = findImgurMarkdownImage(simplestImage, 36); | ||
expect(match.exists).toBeFalsy(); | ||
}); | ||
|
||
it("matches 2nd image when cursor is between the 1st and second one", () => { | ||
const match = findImgurMarkdownImage( | ||
"![](https://i.imgur.com/m3RpPCV.png)![](https://i.imgur.com/pLIMYhw.png)", | ||
36 | ||
); | ||
expect(match.exists).toBeTruthy(); | ||
expect(match.mdImagePieces.imageId).toBe("pLIMYhw"); | ||
}); | ||
|
||
it("throws error for images with unexpected length of image id", () => { | ||
it.each([ | ||
{ line: "![](https://i.imgur.com/m3RpPCVsm.png)" }, | ||
{ line: "![](https://i.imgur.com/m3RpPC.png)" }, | ||
])( | ||
"GIVEN line '$line' an error reporting incorrect image id size will be thrown", | ||
({ line }) => { | ||
const match = findImgurMarkdownImage(line, 0); | ||
expect(match.mdImagePieces).toThrowError(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe("isWrapped type predicate", () => { | ||
it("treats simple image as not-wrapped", () => { | ||
const matchedPieces = findImgurMarkdownImage( | ||
"![](https://i.imgur.com/m3RpPCV.png)", | ||
0 | ||
).mdImagePieces; | ||
expect(isWrapped(matchedPieces)).toBeFalsy(); | ||
}); | ||
|
||
it("correctly detects wrapped image", () => { | ||
const matchedPieces = findImgurMarkdownImage( | ||
"[![](https://i.imgur.com/m3RpPCVs.png)](https://i.imgur.com/m3RpPCV.png)", | ||
0 | ||
).mdImagePieces; | ||
expect(isWrapped(matchedPieces)).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { expect, describe, it } from "vitest"; | ||
import resizeTo from "../src/imgur/resizing/resizing"; | ||
import ImgurSize from "../src/imgur/resizing/ImgurSize"; | ||
import imgurMarkdownImageRegexMatch from "../src/imgur/resizing/md-image-parsing"; | ||
|
||
describe("resizeTo", () => { | ||
it.each([ | ||
{ | ||
input: "![](https://i.imgur.com/m3RpPCVm.png)", | ||
size: ImgurSize.SMALL_SQUARE, | ||
output: | ||
"[![](https://i.imgur.com/m3RpPCVs.png)](https://i.imgur.com/m3RpPCV.png)", | ||
}, | ||
{ | ||
input: "![](https://i.imgur.com/m3RpPCVm.png)", | ||
size: ImgurSize.BIG_SQUARE, | ||
output: | ||
"[![](https://i.imgur.com/m3RpPCVb.png)](https://i.imgur.com/m3RpPCV.png)", | ||
}, | ||
{ | ||
input: "![](https://i.imgur.com/m3RpPCVm.png)", | ||
size: ImgurSize.SMALL_THUMBNAIL, | ||
output: | ||
"[![](https://i.imgur.com/m3RpPCVt.png)](https://i.imgur.com/m3RpPCV.png)", | ||
}, | ||
{ | ||
input: "![](https://i.imgur.com/m3RpPCVm.png)", | ||
size: ImgurSize.MEDIUM_THUMBNAIL, | ||
output: | ||
"[![](https://i.imgur.com/m3RpPCVm.png)](https://i.imgur.com/m3RpPCV.png)", | ||
}, | ||
{ | ||
input: "![](https://i.imgur.com/m3RpPCVm.png)", | ||
size: ImgurSize.LARGE_THUMBNAIL, | ||
output: | ||
"[![](https://i.imgur.com/m3RpPCVl.png)](https://i.imgur.com/m3RpPCV.png)", | ||
}, | ||
{ | ||
input: "![](https://i.imgur.com/m3RpPCVm.png)", | ||
size: ImgurSize.HUGE_THUMBNAIL, | ||
output: | ||
"[![](https://i.imgur.com/m3RpPCVh.png)](https://i.imgur.com/m3RpPCV.png)", | ||
}, | ||
])("resizes an image to '$size' as expected", ({ input, size, output }) => { | ||
const match = imgurMarkdownImageRegexMatch(input, 0); | ||
const replacement = resizeTo(size)(match.mdImagePieces).content; | ||
|
||
expect(replacement).toBe(output); | ||
}); | ||
|
||
it("can resize already resized image", () => { | ||
const smallThumbnail = | ||
"[![](https://i.imgur.com/m3RpPCVt.png)](https://i.imgur.com/m3RpPCV.png)"; | ||
const match = imgurMarkdownImageRegexMatch(smallThumbnail, 0); | ||
const replacement = resizeTo(ImgurSize.LARGE_THUMBNAIL)( | ||
match.mdImagePieces | ||
).content; | ||
|
||
expect(replacement).toBe( | ||
"[![](https://i.imgur.com/m3RpPCVl.png)](https://i.imgur.com/m3RpPCV.png)" | ||
); | ||
}); | ||
|
||
it("provides correct range to be replaced", () => { | ||
const originalImage = " ![](https://i.imgur.com/m3RpPCV.png)"; | ||
const match = imgurMarkdownImageRegexMatch(originalImage, 3); | ||
const replacement = resizeTo(ImgurSize.LARGE_THUMBNAIL)( | ||
match.mdImagePieces | ||
); | ||
|
||
expect(replacement.from).toBe(3); | ||
expect(replacement.to).toBe(39); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
input: | ||
"[![](https://i.imgur.com/m3RpPCVs.png)](https://i.imgur.com/m3RpPCV.png)", | ||
inputDescription: "wrapped resized image", | ||
}, | ||
{ | ||
input: "![](https://i.imgur.com/m3RpPCVm.png)", | ||
inputDescription: "resized image", | ||
}, | ||
])("resizes '$inputDescription' to original size", ({ input }) => { | ||
const match = imgurMarkdownImageRegexMatch(input, 0); | ||
const replacement = resizeTo(ImgurSize.ORIGINAL)( | ||
match.mdImagePieces | ||
).content; | ||
|
||
expect(replacement).toBe("![](https://i.imgur.com/m3RpPCV.png)"); | ||
}); | ||
}); |