-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e45c68
commit 65b788d
Showing
1 changed file
with
50 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,50 @@ | ||
import { getCanvasElement, loadImage } from "./../filtercanvas" | ||
|
||
describe("FilterCanvas", () => { | ||
beforeAll(() => (document.body.innerHTML = `<canvas id="canvas"></canvas>`)) | ||
|
||
it("should find a canvas element", () => { | ||
expect(getCanvasElement("#canvas")).toBeInstanceOf(HTMLCanvasElement) | ||
}) | ||
|
||
it("should throw on an invalid selector", () => { | ||
expect(() => getCanvasElement("#invalid")).toThrow(TypeError) | ||
}) | ||
}) | ||
|
||
describe("loadImage", () => { | ||
const originalImageFn = Object.getOwnPropertyDescriptor( | ||
Image.prototype, | ||
"src", | ||
) as PropertyDescriptor | ||
|
||
beforeAll(() => { | ||
const LOAD_SUCCESS = "image-success.png" | ||
|
||
Object.defineProperty(Image.prototype, "src", { | ||
set(src) { | ||
if (src === LOAD_SUCCESS) { | ||
setTimeout(() => this.dispatchEvent(new Event("load"))) | ||
} else { | ||
setTimeout(() => this.dispatchEvent(new Event("error"))) | ||
} | ||
}, | ||
}) | ||
}) | ||
|
||
afterAll(() => { | ||
Object.defineProperty(Image.prototype, "src", originalImageFn) | ||
}) | ||
|
||
it("should load if imgUrl is valid", async () => { | ||
await expect(loadImage("image-success.png")).resolves.toBeInstanceOf( | ||
HTMLImageElement, | ||
) | ||
}) | ||
|
||
it("should error if imgUrl is invalid", async () => { | ||
await expect(loadImage("image-error.png")).rejects.toThrow( | ||
"image could not be loaded", | ||
) | ||
}) | ||
}) |