|
| 1 | +import { createMovementApi } from "./movement.function"; |
| 2 | +import { mockPartial } from "sneer"; |
| 3 | +import { ProviderRegistry } from "./provider/provider-registry.class"; |
| 4 | +import { MouseProviderInterface } from "./provider"; |
| 5 | +import { Point } from "./point.class"; |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + jest.clearAllMocks(); |
| 9 | +}); |
| 10 | + |
| 11 | +const lineHelperMock = { |
| 12 | + straightLine: jest.fn(), |
| 13 | +}; |
| 14 | + |
| 15 | +const currentPosition = new Point(500, 500); |
| 16 | + |
| 17 | +const providerRegistryMock = mockPartial<ProviderRegistry>({ |
| 18 | + getMouse(): MouseProviderInterface { |
| 19 | + return mockPartial<MouseProviderInterface>({ |
| 20 | + currentMousePosition: () => Promise.resolve(currentPosition), |
| 21 | + }); |
| 22 | + }, |
| 23 | +}); |
| 24 | + |
| 25 | +describe("MovementApi", () => { |
| 26 | + describe("Relative movement", () => { |
| 27 | + it("should move the cursor down starting from the current mouse position", async () => { |
| 28 | + // GIVEN |
| 29 | + const SUT = createMovementApi(providerRegistryMock, lineHelperMock); |
| 30 | + const step = 100; |
| 31 | + const targetPoint = new Point( |
| 32 | + currentPosition.x, |
| 33 | + currentPosition.y + step |
| 34 | + ); |
| 35 | + |
| 36 | + // WHEN |
| 37 | + await SUT.down(step); |
| 38 | + |
| 39 | + // THEN |
| 40 | + expect(lineHelperMock.straightLine).toBeCalledTimes(1); |
| 41 | + expect(lineHelperMock.straightLine).toBeCalledWith( |
| 42 | + currentPosition, |
| 43 | + targetPoint |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should move the cursor up starting from the current mouse position", async () => { |
| 48 | + // GIVEN |
| 49 | + const SUT = createMovementApi(providerRegistryMock, lineHelperMock); |
| 50 | + const step = 100; |
| 51 | + const targetPoint = new Point( |
| 52 | + currentPosition.x, |
| 53 | + currentPosition.y - step |
| 54 | + ); |
| 55 | + |
| 56 | + // WHEN |
| 57 | + await SUT.up(step); |
| 58 | + |
| 59 | + // THEN |
| 60 | + expect(lineHelperMock.straightLine).toBeCalledTimes(1); |
| 61 | + expect(lineHelperMock.straightLine).toBeCalledWith( |
| 62 | + currentPosition, |
| 63 | + targetPoint |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should move the cursor left starting from the current mouse position", async () => { |
| 68 | + // GIVEN |
| 69 | + const SUT = createMovementApi(providerRegistryMock, lineHelperMock); |
| 70 | + const step = 100; |
| 71 | + const targetPoint = new Point( |
| 72 | + currentPosition.x - step, |
| 73 | + currentPosition.y |
| 74 | + ); |
| 75 | + |
| 76 | + // WHEN |
| 77 | + await SUT.left(step); |
| 78 | + |
| 79 | + // THEN |
| 80 | + expect(lineHelperMock.straightLine).toBeCalledTimes(1); |
| 81 | + expect(lineHelperMock.straightLine).toBeCalledWith( |
| 82 | + currentPosition, |
| 83 | + targetPoint |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should move the cursor right starting from the current mouse position", async () => { |
| 88 | + // GIVEN |
| 89 | + const SUT = createMovementApi(providerRegistryMock, lineHelperMock); |
| 90 | + const step = 100; |
| 91 | + const targetPoint = new Point( |
| 92 | + currentPosition.x + step, |
| 93 | + currentPosition.y |
| 94 | + ); |
| 95 | + |
| 96 | + // WHEN |
| 97 | + await SUT.right(step); |
| 98 | + |
| 99 | + // THEN |
| 100 | + expect(lineHelperMock.straightLine).toBeCalledTimes(1); |
| 101 | + expect(lineHelperMock.straightLine).toBeCalledWith( |
| 102 | + currentPosition, |
| 103 | + targetPoint |
| 104 | + ); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("straightTo", () => { |
| 109 | + it("should throw on non-Point input", async () => { |
| 110 | + // GIVEN |
| 111 | + const api = createMovementApi(providerRegistryMock, lineHelperMock); |
| 112 | + |
| 113 | + // WHEN |
| 114 | + const SUT = () => api.straightTo({ foo: "bar" } as unknown as Point); |
| 115 | + |
| 116 | + // THEN |
| 117 | + await expect(SUT()).rejects.toThrowError(/straightTo requires a Point.*/); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should move the cursor straight to the target point starting from the current mouse position", async () => { |
| 121 | + // GIVEN |
| 122 | + const SUT = createMovementApi(providerRegistryMock, lineHelperMock); |
| 123 | + const step = 100; |
| 124 | + const targetPoint = new Point( |
| 125 | + currentPosition.x, |
| 126 | + currentPosition.y - step |
| 127 | + ); |
| 128 | + |
| 129 | + // WHEN |
| 130 | + await SUT.straightTo(targetPoint); |
| 131 | + |
| 132 | + // THEN |
| 133 | + expect(lineHelperMock.straightLine).toBeCalledTimes(1); |
| 134 | + expect(lineHelperMock.straightLine).toBeCalledWith( |
| 135 | + currentPosition, |
| 136 | + targetPoint |
| 137 | + ); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments