From 47649c1f47bc4effa8912ff6422a4607ce424f10 Mon Sep 17 00:00:00 2001 From: Tyson Cadenhead Date: Wed, 7 Aug 2024 09:34:47 -0500 Subject: [PATCH] Add tasks --- README.md | 34 +++++++++++++++++++++++ package.json | 2 +- src/__tests__/task.spec.ts | 13 +++++++++ src/index.ts | 2 ++ src/storage.ts | 4 +++ src/task.ts | 57 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/task.spec.ts create mode 100644 src/storage.ts create mode 100644 src/task.ts diff --git a/README.md b/README.md index 0f54be4..4287f3b 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,40 @@ describe("My tests", () => { }); ``` +### Events and Tasks + +We also include mocks to override Ampt events and tasks. They can be used with a testing library in a similar fashion: + +```js +import { task, events } from "ampt-data-mock"; + +jest.mock("@ampt/sdk", () => ({ + task, + events, +})); +``` + +Events are triggered immediately upon publishing so that you can syncronously validate your end to end tests. + +Tasks can be tested by exporting them and calling `.run()` inside your tests suite. + +```js +// Your code +import { task } from "@ampt/sdk"; + +const myTask = task("my task", (event) => { + return true; +}); + +// Your test +describe("My Task", () => { + it("Should run my task", async () => { + const result = await myTask.run(); + expect(result).toEqual(true); + }); +}); +``` + ## Resetting Since this package persists data in memory, it is best practice to reset the data before each unit test using the `reset()` function diff --git a/package.json b/package.json index ffdee76..b4eaaf4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ampt-data-mock", - "version": "1.1.2", + "version": "1.1.3", "description": "", "main": "lib/index.js", "scripts": { diff --git a/src/__tests__/task.spec.ts b/src/__tests__/task.spec.ts new file mode 100644 index 0000000..0844766 --- /dev/null +++ b/src/__tests__/task.spec.ts @@ -0,0 +1,13 @@ +import { task } from "../task"; + +describe("Task", () => { + it("Should create and run a task", async () => { + const myTask = task("myTask", async (event, context) => { + return event.body; + }); + + const result = await myTask.run({ message: "Hello, World!" }); + + expect(result).toEqual({ message: "Hello, World!" }); + }); +}); diff --git a/src/index.ts b/src/index.ts index 66dafcc..add5aa1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ export * from "./data"; export * from "./events"; export * from "./helpers"; +export * from "./task"; +export * from "./storage"; diff --git a/src/storage.ts b/src/storage.ts new file mode 100644 index 0000000..d900cb5 --- /dev/null +++ b/src/storage.ts @@ -0,0 +1,4 @@ +export const storage = () => ({ + read: async () => {}, + write: async () => {}, +}); diff --git a/src/task.ts b/src/task.ts new file mode 100644 index 0000000..e45e632 --- /dev/null +++ b/src/task.ts @@ -0,0 +1,57 @@ +interface EventHandlerEvent { + target: string; + id: string; + name: string; + body: BodyType; + time: number; + delay: number; + attempt?: number; +} + +declare interface TaskContext { + setTimeout(ms: number): void; + progress(message: string, percent: number): void; +} + +declare interface TaskEvent extends EventHandlerEvent {} + +declare interface TaskHandler { + (event: TaskEvent, context: TaskContext): Promise; +} + +interface ITaskOptions { + timeout?: number; + attempts?: number; +} + +export const task = ( + taskName: string, + handlerOrOptions: ITaskOptions | TaskHandler, + handler?: TaskHandler +) => { + return { + run: async (expressionOrInput: string | any, input?: any) => { + const fn = handler || (handlerOrOptions as TaskHandler); + return await fn( + { + delay: 0, + id: new Date().getTime().toString(), + name: taskName, + target: taskName, + time: new Date().getTime(), + body: input || expressionOrInput, + }, + { + setTimeout: (ms: number) => {}, + progress: (message: string, percent: number) => {}, + } + ); + }, + every: (interval: string, input?: any) => {}, + cron: (expression: string, input?: any) => {}, + }; +}; + +task.status = (executionId: string) => { + return {}; +};