|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import { dirname, join } from "node:path"; |
| 4 | +import { executeStep, runInteractions } from "../../src/interactions.js"; |
| 5 | + |
| 6 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 7 | +const FIXTURES = join(HERE, "../fixtures"); |
| 8 | + |
| 9 | +function mockPage() { |
| 10 | + return { |
| 11 | + evaluate: vi.fn().mockResolvedValue(undefined), |
| 12 | + click: vi.fn().mockResolvedValue(undefined), |
| 13 | + hover: vi.fn().mockResolvedValue(undefined), |
| 14 | + type: vi.fn().mockResolvedValue(undefined), |
| 15 | + waitForTimeout: vi.fn().mockResolvedValue(undefined), |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +describe("executeStep", () => { |
| 20 | + let page; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + page = mockPage(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("scroll — calls page.evaluate with the given y value", async () => { |
| 27 | + await executeStep(page, { action: "scroll", y: 500 }); |
| 28 | + expect(page.evaluate).toHaveBeenCalledOnce(); |
| 29 | + expect(page.evaluate).toHaveBeenCalledWith(expect.any(Function), 500); |
| 30 | + }); |
| 31 | + |
| 32 | + it("scroll — defaults y to 300 when omitted", async () => { |
| 33 | + await executeStep(page, { action: "scroll" }); |
| 34 | + expect(page.evaluate).toHaveBeenCalledWith(expect.any(Function), 300); |
| 35 | + }); |
| 36 | + |
| 37 | + it("click — calls page.click with the selector", async () => { |
| 38 | + await executeStep(page, { action: "click", selector: "#btn" }); |
| 39 | + expect(page.click).toHaveBeenCalledWith("#btn"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("hover — calls page.hover with the selector", async () => { |
| 43 | + await executeStep(page, { action: "hover", selector: ".menu" }); |
| 44 | + expect(page.hover).toHaveBeenCalledWith(".menu"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("type — calls page.type with selector and text", async () => { |
| 48 | + await executeStep(page, { action: "type", selector: "input", text: "hello" }); |
| 49 | + expect(page.type).toHaveBeenCalledWith("input", "hello"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("wait — calls page.waitForTimeout with the given ms", async () => { |
| 53 | + await executeStep(page, { action: "wait", ms: 500 }); |
| 54 | + expect(page.waitForTimeout).toHaveBeenCalledWith(500); |
| 55 | + }); |
| 56 | + |
| 57 | + it("unknown action — throws with a clear message", async () => { |
| 58 | + await expect(executeStep(page, { action: "fly" })).rejects.toThrow( |
| 59 | + 'Unknown interaction action: "fly"' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it("unknown action — no page methods are called before throwing", async () => { |
| 64 | + await expect(executeStep(page, { action: "teleport" })).rejects.toThrow(); |
| 65 | + expect(page.click).not.toHaveBeenCalled(); |
| 66 | + expect(page.evaluate).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("runInteractions", () => { |
| 71 | + it("reads the JSON file and executes all steps in order", async () => { |
| 72 | + const page = mockPage(); |
| 73 | + const calls = []; |
| 74 | + page.evaluate.mockImplementation(() => { calls.push("scroll"); }); |
| 75 | + page.waitForTimeout.mockImplementation(() => { calls.push("wait"); }); |
| 76 | + |
| 77 | + await runInteractions(page, join(FIXTURES, "interactions.json")); |
| 78 | + |
| 79 | + expect(calls).toEqual(["scroll", "wait"]); |
| 80 | + }); |
| 81 | + |
| 82 | + it("throws when the file does not exist", async () => { |
| 83 | + const page = mockPage(); |
| 84 | + await expect( |
| 85 | + runInteractions(page, join(FIXTURES, "nonexistent.json")) |
| 86 | + ).rejects.toThrow(); |
| 87 | + }); |
| 88 | +}); |
0 commit comments