Skip to content

Commit

Permalink
removeFn
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Nov 11, 2024
1 parent 06710bc commit 98b3aac
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/services/internal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const EVERYTHING_ELSE = 1;
const MONTHS = 12;

type inputFormats = Date | string | number | Dayjs;
export type RemoveCallback = { remove: () => void; (): void };

// TODO: probably should make this configurable
const formatter = new Intl.RelativeTimeFormat("en", {
Expand Down Expand Up @@ -240,6 +241,23 @@ export class InternalDefinition {
};
public utils = new InternalUtils();

/**
*
* ```typescript
* // Is it
* const remove = doThing();
* // or
* const { remove } = doThing();
* ```
*
* Now it's both! Inconsistency is the now supported
*/
public removeFn(remove: () => TBlackHole): RemoveCallback {
const out = remove as RemoveCallback;
out.remove = remove;
return out;
}

// #MARK: safeExec
public async safeExec<T>(options: (() => TBlackHole) | SafeExecOptions): Promise<T> {
const logger = this.boilerplate.logger.systemLogger;
Expand Down
54 changes: 49 additions & 5 deletions testing/internal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BootstrapOptions, CreateApplication, TestRunner } from "../src";
import {
BootstrapOptions,
CreateApplication,
InternalDefinition,
TBlackHole,
TestRunner,
} from "../src";

export const BASIC_BOOT = {
configuration: { boilerplate: { LOG_LEVEL: "silent" } },
Expand All @@ -11,10 +17,10 @@ export const BASIC_BOOT = {

describe("Fetch Extension", () => {
beforeAll(async () => {
jest.spyOn(global.console, "error").mockImplementation(() => {});
jest.spyOn(global.console, "warn").mockImplementation(() => {});
jest.spyOn(global.console, "debug").mockImplementation(() => {});
jest.spyOn(global.console, "log").mockImplementation(() => {});
jest.spyOn(globalThis.console, "error").mockImplementation(() => {});
jest.spyOn(globalThis.console, "warn").mockImplementation(() => {});
jest.spyOn(globalThis.console, "debug").mockImplementation(() => {});
jest.spyOn(globalThis.console, "log").mockImplementation(() => {});
const preload = CreateApplication({
// @ts-expect-error testing
name: "testing",
Expand All @@ -27,6 +33,44 @@ describe("Fetch Extension", () => {
jest.restoreAllMocks();
});

describe("removeFn", () => {
const internal = new InternalDefinition();
const mockRemove: () => TBlackHole = jest.fn(() => ({}) as TBlackHole);

it("should return a function with a remove property", () => {
const result = internal.removeFn(mockRemove);

expect(typeof result).toBe("function");
expect(result.remove).toBe(mockRemove);
});

it("should correctly call the remove function", () => {
const result = internal.removeFn(mockRemove);

result(); // Call the function
expect(mockRemove).toHaveBeenCalledTimes(1);
});

it("should allow calling remove via the returned function", () => {
const result = internal.removeFn(mockRemove);

result.remove!(); // Call remove
expect(mockRemove).toHaveBeenCalledTimes(1);
});

it("should support both destructured and non-destructured usage", () => {
// Destructured case
const { remove } = internal.removeFn(mockRemove);
remove!(); // Call remove
expect(mockRemove).toHaveBeenCalledTimes(1);

// Non-destructured case
const result = internal.removeFn(mockRemove);
result(); // Call the function
expect(mockRemove).toHaveBeenCalledTimes(2); // Called once more
});
});

describe("relativeDate", () => {
describe("relativeDate", () => {
it("should return the correct relative time for a valid past date", async () => {
Expand Down

0 comments on commit 98b3aac

Please sign in to comment.