Skip to content

Commit

Permalink
Add event listener return result test
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade committed Jan 8, 2024
1 parent 23df46a commit bda8d57
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/core/source/elements/RemoteElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ export abstract class RemoteElement<
);
}

addEventListener(
type: string,
listener:
| ((event: RemoteEvent) => void)
| {handleEvent: (event: RemoteEvent) => void}
| null,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
Expand Down
26 changes: 23 additions & 3 deletions packages/core/source/tests/elements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
// remoteProperties,
// remoteProperty,
RemoteRootElement,
type RemoteEvent,
type RemoteElementConstructor,
} from '../elements.ts';
import {RemoteReceiver, type RemoteReceiverElement} from '../receiver/basic.ts';
Expand Down Expand Up @@ -558,7 +559,7 @@ describe('RemoteElement', () => {
]);
});

it('calls event listeners with a CustomEvent containing a single function argument as the detail', () => {
it('calls event listeners with a RemoteEvent containing a single function argument as the detail', () => {
const ButtonElement = createRemoteElement<{onPress(detail: any): void}>(
{
properties: {onPress: {}},
Expand All @@ -580,7 +581,7 @@ describe('RemoteElement', () => {
);
});

it('calls event listeners with a CustomEvent containing multiple function argument as the detail', () => {
it('calls event listeners with a RemoteEvent containing multiple function argument as the detail', () => {
const ButtonElement = createRemoteElement<{
onPress(...detail: any[]): void;
}>({
Expand All @@ -602,6 +603,25 @@ describe('RemoteElement', () => {
);
});

it('returns the resolved value attached to a RemoteEvent', () => {
const ButtonElement = createRemoteElement<{
onPress(): void;
}>({
properties: {onPress: {}},
});

const {element} = createAndConnectRemoteElement(ButtonElement);

const response = 'Hello world';
element.addEventListener('press', (event: RemoteEvent) => {
event.respondWith(response);
});

const result = element.onPress();

expect(result).toBe(response);
});

it('removes an event listener property when the last event listener is removed', () => {
const ButtonElement = createRemoteElement<{onPress(): void}>({
properties: {onPress: {}},
Expand Down Expand Up @@ -695,7 +715,7 @@ describe('RemoteElement', () => {

describe('methods', () => {
it('calls a method on the remote receiver', () => {
class HelloElement extends RemoteElement<{}, {}, {greet(): void}> {
class HelloElement extends RemoteElement<{}, {greet(): void}> {
greet(name: string) {
return this.callRemoteMethod('greet', name);
}
Expand Down

0 comments on commit bda8d57

Please sign in to comment.