diff --git a/lib/expect/matchers/toHaveColor.function.spec.ts b/lib/expect/matchers/toHaveColor.function.spec.ts new file mode 100644 index 0000000..81797b9 --- /dev/null +++ b/lib/expect/matchers/toHaveColor.function.spec.ts @@ -0,0 +1,66 @@ +import { RGBA, screen } from "../../../index"; +import { Point } from "../../point.class"; +import { mockPartial } from "sneer"; +import { toHaveColor } from "./toHaveColor.function"; + +// jest.mock("jimp", () => {}); + +const targetPoint = new Point(400, 400); + +describe(".toHaveColor", () => { + it("should succeed when screen pixel has the correct RGBA value", async () => { + // GIVEN + screen.colorAt = mockPartial(() => { + return new RGBA(0, 0, 0, 0); + }); + + // WHEN + const result = await toHaveColor(targetPoint, new RGBA(0, 0, 0, 0)); + + // THEN + expect(result.pass).toBeTruthy(); + }); + + it("should fail when the screen pixel has the incorrect RGBA value", async () => { + // GIVEN + screen.colorAt = mockPartial(() => { + return new RGBA(255, 0, 5, 0); + }); + + // WHEN + const result = await toHaveColor(targetPoint, new RGBA(0, 0, 0, 0)); + + // THEN + expect(result.pass).toBeFalsy(); + }); + + it("should succeed when the screen pixel has the correct RGBA value", async () => { + // GIVEN + screen.colorAt = mockPartial(() => { + return new RGBA(0, 0, 0, 0); + }); + expect.extend({ + toHaveColor, + }); + + // WHEN + + // THEN + await expect(targetPoint).toHaveColor(new RGBA(0, 0, 0, 0)); + }); + + it("should succeed when the screen pixel has the incorrect RGBA value", async () => { + // GIVEN + screen.colorAt = mockPartial(() => { + return new RGBA(255, 0, 5, 0); + }); + expect.extend({ + toHaveColor, + }); + + // WHEN + + // THEN + await expect(targetPoint).not.toHaveColor(new RGBA(0, 0, 0, 0)); + }); +}); diff --git a/lib/expect/matchers/toHaveColor.function.ts b/lib/expect/matchers/toHaveColor.function.ts new file mode 100644 index 0000000..80808cc --- /dev/null +++ b/lib/expect/matchers/toHaveColor.function.ts @@ -0,0 +1,19 @@ +import { Point, RGBA, screen } from "../../../index"; + +export const toHaveColor = async (received: Point, needle: RGBA) => { + const color = await screen.colorAt(received); + const match = color.toHex() === needle.toHex(); + if (match) { + return { + message: () => + `Expected pixel ${received.toString()} not to to have color ${needle.toHex()}`, + pass: true, + }; + } else { + return { + message: () => + `Expected pixel ${received.toString()} to have color ${needle.toHex()} but is ${color.toHex()}`, + pass: false, + }; + } +};