mirror of
https://github.com/Mintplex-Labs/nut.js.git
synced 2026-07-19 22:15:20 -04:00
(#547) New matcher toHaveColor that verifies pixel colors on screen
This commit is contained in:
@@ -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));
|
||||
});
|
||||
});
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user