Skip to content

Commit

Permalink
[3][corvid-types][Deprecate Codemodel] Allow storing additional compo…
Browse files Browse the repository at this point in the history
…nent event handlers in runtime (#64)
  • Loading branch information
Dolev Hadar authored Aug 31, 2021
1 parent 1fa4dee commit fe904e0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/dynamicTypes/eventHandlersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@ export interface EventHandlersService {
getComponentEventHandlers: (
componentName: string
) => EventHandler[] | undefined;
registerComponentEventHandlers: (
componentName: string,
handlers: EventHandler[]
) => void;
}

const eventHandlers = eventHandlersJSON as ComponentsEventHandlers;
const $wEventHandlers = eventHandlersJSON as ComponentsEventHandlers;
const eventHandlers = { ...$wEventHandlers };

const eventHandlersService: EventHandlersService = {
getComponentEventHandlers: (
componentName: string
): EventHandler[] | undefined => {
return eventHandlers[componentName];
},
registerComponentEventHandlers(
componentName: string,
handlers: EventHandler[]
): void {
eventHandlers[componentName] = handlers;
}
};

Expand Down
42 changes: 42 additions & 0 deletions test/it/event-handlers-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import eventHandlersService from "../../src/dynamicTypes/eventHandlersService";

describe("eventHandlersService", () => {
it("should allow getting a $w component event handlers", () => {
const buttonHandlers = eventHandlersService.getComponentEventHandlers(
"$w.Button"
);
expect(buttonHandlers).toHaveLength(6);

const onClickHandler = buttonHandlers?.find(
handler => handler.name === "onClick"
);
expect(onClickHandler).not.toBe(undefined);
});

it("should allow adding component event handlers in runtime", () => {
eventHandlersService.registerComponentEventHandlers("MyComponent", [
{
name: "CustomEvent",
origin: "MyComponent",
description: "My custom event",
kind: "function",
type: "customEvent",
handlerArgs: [
{
name: "arg",
type: "string"
}
]
}
]);
const handlers = eventHandlersService.getComponentEventHandlers(
"MyComponent"
);
expect(handlers).toHaveLength(1);

const customHandler = handlers?.find(
handler => handler.name === "CustomEvent"
);
expect(customHandler).not.toBe(undefined);
});
});

0 comments on commit fe904e0

Please sign in to comment.