|
1 |
| -import { Image } from "./image.class"; |
| 1 | +import {Image} from "./image.class"; |
| 2 | +import {imageToJimp} from "./provider/io/imageToJimp.function"; |
| 3 | +import {ColorMode} from "./colormode.enum"; |
| 4 | + |
| 5 | +jest.mock("./provider/io/imageToJimp.function", () => { |
| 6 | + return { |
| 7 | + imageToJimp: jest.fn() |
| 8 | + } |
| 9 | +}); |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + jest.resetAllMocks(); |
| 13 | +}); |
2 | 14 |
|
3 | 15 | describe("Image class", () => {
|
4 |
| - it("should return alphachannel = true for > 3 channels", () => { |
5 |
| - const SUT = new Image(200, 200, 123, 4, "id"); |
6 |
| - expect(SUT.hasAlphaChannel).toBeTruthy(); |
7 |
| - }); |
8 |
| - |
9 |
| - it("should return alphachannel = false for <= 3 channels", () => { |
10 |
| - const SUT = new Image(200, 200, 123, 3, "id"); |
11 |
| - expect(SUT.hasAlphaChannel).toBeFalsy(); |
12 |
| - }); |
13 |
| - it("should return alphachannel = false for <= 3 channels", () => { |
14 |
| - const SUT = new Image(200, 200, 123, 2, "id"); |
15 |
| - expect(SUT.hasAlphaChannel).toBeFalsy(); |
16 |
| - }); |
17 |
| - it("should return alphachannel = false for <= 3 channels", () => { |
18 |
| - const SUT = new Image(200, 200, 123, 1, "id"); |
19 |
| - expect(SUT.hasAlphaChannel).toBeFalsy(); |
20 |
| - }); |
21 |
| - |
22 |
| - it("should throw for <= 0 channels", () => { |
23 |
| - expect(() => new Image(200, 200, 123, 0, "id")).toThrowError("Channel <= 0"); |
24 |
| - }); |
25 |
| - |
26 |
| - it("should have a default pixel density of 1.0", () => { |
27 |
| - const SUT = new Image(200, 200, 123, 1, "id"); |
28 |
| - expect(SUT.pixelDensity).toEqual({ scaleX: 1.0, scaleY: 1.0 }); |
29 |
| - }); |
| 16 | + it("should return alphachannel = true for > 3 channels", () => { |
| 17 | + const SUT = new Image(200, 200, 123, 4, "id"); |
| 18 | + expect(SUT.hasAlphaChannel).toBeTruthy(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should return alphachannel = false for <= 3 channels", () => { |
| 22 | + const SUT = new Image(200, 200, 123, 3, "id"); |
| 23 | + expect(SUT.hasAlphaChannel).toBeFalsy(); |
| 24 | + }); |
| 25 | + it("should return alphachannel = false for <= 3 channels", () => { |
| 26 | + const SUT = new Image(200, 200, 123, 2, "id"); |
| 27 | + expect(SUT.hasAlphaChannel).toBeFalsy(); |
| 28 | + }); |
| 29 | + it("should return alphachannel = false for <= 3 channels", () => { |
| 30 | + const SUT = new Image(200, 200, 123, 1, "id"); |
| 31 | + expect(SUT.hasAlphaChannel).toBeFalsy(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should throw for <= 0 channels", () => { |
| 35 | + expect(() => new Image(200, 200, 123, 0, "id")).toThrowError("Channel <= 0"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should have a default pixel density of 1.0", () => { |
| 39 | + const SUT = new Image(200, 200, 123, 1, "id"); |
| 40 | + expect(SUT.pixelDensity).toEqual({scaleX: 1.0, scaleY: 1.0}); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("Colormode", () => { |
| 44 | + it("should not try to convert an image to BGR if it already has the correct color mode", async () => { |
| 45 | + // GIVEN |
| 46 | + const bgrImage = new Image(100, 100, Buffer.from([]), 3, "testImage"); |
| 47 | + |
| 48 | + // WHEN |
| 49 | + const convertedImage = await bgrImage.toBGR(); |
| 50 | + |
| 51 | + // THEN |
| 52 | + expect(convertedImage).toBe(bgrImage); |
| 53 | + expect(imageToJimp).not.toBeCalledTimes(1) |
| 54 | + }); |
| 55 | + |
| 56 | + it("should not try to convert an image to RGB if it already has the correct color mode", async () => { |
| 57 | + // GIVEN |
| 58 | + const rgbImage = new Image(100, 100, Buffer.from([]), 3, "testImage", ColorMode.RGB); |
| 59 | + |
| 60 | + // WHEN |
| 61 | + const convertedImage = await rgbImage.toRGB(); |
| 62 | + |
| 63 | + // THEN |
| 64 | + expect(convertedImage).toBe(rgbImage); |
| 65 | + expect(imageToJimp).not.toBeCalledTimes(1) |
| 66 | + }); |
| 67 | + }); |
30 | 68 | });
|
0 commit comments