Skip to content

Commit

Permalink
fix(core): even better types
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Jul 15, 2024
1 parent 791065a commit c196499
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/pfe-core/decorators/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { LitElement } from 'lit';
*/
export function listen<P extends LitElement>(
type: keyof HTMLElementEventMap,
options?: EventListenerOptions,
options?: AddEventListenerOptions,
) {
return function(
proto: LitElement,
Expand Down
32 changes: 32 additions & 0 deletions core/pfe-core/test/decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { customElement } from 'lit/decorators/custom-element.js';
import { property } from 'lit/decorators/property.js';
import { observes } from '../decorators/observes.js';
import { observed } from '../decorators/observed.js';
import { listen } from '../decorators/listen.js';

import { spy } from 'sinon';

Expand Down Expand Up @@ -111,6 +112,7 @@ describe('@observed(\'_myCallback\')', function() {
describe('@observed(() => {...})', function() {
let element: XObservedFunctionHost;
const dump = spy();

@customElement('x-observed-function-host')
class XObservedFunctionHost extends LitElement {
@observed(dump)
Expand All @@ -134,3 +136,33 @@ describe('@observed(() => {...})', function() {
});
});
});

@customElement('x-listen-host')
class XListenHost extends LitElement {
public events: string[] = [];
@listen('click')
@listen('change', { once: true })
handler(event: Event) {
this.events.push(event.type);
}
}

describe('@listen', function() {
let element: XListenHost;
beforeEach(async function() {
element = await fixture(document.createElement('x-listen-host'));
});
it('listens for a given event on the host', function() {
element.dispatchEvent(new Event('change'));
expect(element.events).to.deep.equal(['change']);
});
it('composes to listen for a multiple events on the host', function() {
element.dispatchEvent(new Event('change'));
element.dispatchEvent(new Event('click'));
element.dispatchEvent(new Event('change'));
element.dispatchEvent(new Event('click'));
expect(element.events).to.deep.equal(['change', 'click']);
});
});


0 comments on commit c196499

Please sign in to comment.