Skip to content

Commit

Permalink
feat: testing support jobs options (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
danewalters authored May 16, 2024
1 parent d8a8c3a commit e53210b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
53 changes: 37 additions & 16 deletions src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ import {
EnqueueFunction,
ListenFunction,
} from "./use-queue.ts";
import { JetQueueOptions, QueueJob, QueueJobId } from "./types.ts";
import {
EnqueueOptions,
JetQueueOptions,
QueueJob,
QueueJobId,
} from "./types.ts";

let jobs: Array<[string, QueueJob<Record<string, unknown>>]> = [];
type maybeOptions =
| Partial<EnqueueOptions<string, Record<string, unknown>>>
| undefined;
let jobs: Array<[string, QueueJob<Record<string, unknown>>, maybeOptions]> = [];
let jobIdCounter: QueueJobId = BigInt(1);

export function makeTestingFunctions<
Expand All @@ -16,12 +24,11 @@ export function makeTestingFunctions<
) {
const enqueue: EnqueueFunction<T> = function (
args,
_options,
options,
): ReturnType<EnqueueFunction<T>> {
const jobId: QueueJobId = jobIdCounter;
jobIdCounter = jobIdCounter + BigInt(1);
jobs.push([queue, { id: jobId, args }]);

jobs.push([queue, { id: jobId, args }, options]);
return Promise.resolve({
id: jobId,
is_conflict: false,
Expand Down Expand Up @@ -63,18 +70,32 @@ export function getJobs() {
export function findEnqueuedJob(
expectedQueue: string,
expectedArgs: Record<string, unknown>,
): QueueJob<Record<string, unknown>> | undefined {
return jobs.find(([queue, job]) => {
expectOptions?: maybeOptions,
): [string, QueueJob<Record<string, unknown>>, maybeOptions] | undefined {
return jobs.find(([queue, job, options]) => {
if (queue !== expectedQueue) return false;

return Object.entries(expectedArgs).every(([key, value]) => {
const argValue = job.args[key];
const argsMatched = matchObject(job.args, expectedArgs);

if (argValue instanceof Date && value instanceof Date) {
return argValue.getTime() === value.getTime();
} else {
return argValue === value;
}
});
})?.[1];
const optionsMatched = expectOptions === undefined ||
matchObject(options, expectOptions);

return argsMatched && optionsMatched;
});
}

function compareValues(a: unknown, b: unknown): boolean {
if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime();
}
return a === b;
}

function matchObject(
source: Record<string, unknown> | undefined,
target: Record<string, unknown>,
): boolean {
return !Object.entries(target).some(([key, value]) =>
!compareValues(source !== undefined ? source[key] : undefined, value)
);
}
30 changes: 29 additions & 1 deletion src/testing_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,35 @@ describe("Queue Testing Functions", () => {

const foundJob = findEnqueuedJob("testQueue", jobArgs);
assert(foundJob, "Job should be found");
assertEquals(foundJob?.args, jobArgs, "Found job args should match");
assertEquals(foundJob?.[1].args, jobArgs, "Found job args should match");
});

it("should correctly handle the scheduleAt in options when enqueueing and finding a job", async () => {
const { enqueue } = makeTestingFunctions("testQueue", { instanceName: "" });
const jobArgs = { task: "testTask" };
const scheduledAt = new Date();

await enqueue(jobArgs, { scheduledAt });

const jobs = getJobs();

assertEquals(jobs.length, 1, "There should be one job enqueued");

assertEquals(
jobs[0][2]?.scheduledAt?.getTime(),
scheduledAt.getTime(),
"Enqueued job scheduleAt should match",
);

const foundJob = findEnqueuedJob("testQueue", jobArgs, { scheduledAt });

assert(foundJob, "Job should be found");

assertEquals(
foundJob?.[2]?.scheduledAt?.getTime(),
scheduledAt.getTime(),
"Found job scheduledAt should match",
);
});

it("should handle clearing jobs correctly", () => {
Expand Down

0 comments on commit e53210b

Please sign in to comment.