@@ -11,6 +11,8 @@ import type { ReactiveController } from './types.js';
1111type CommandTestEvent = Event & { command : string ; source : HTMLElement } ;
1212
1313class CommandBehaviorControllerTestElement extends HTMLElement {
14+ static events : readonly string [ ] | undefined ;
15+
1416command ?: string ;
1517commandfor : string | null = null ;
1618commandForElement : HTMLElement | null = null ;
@@ -20,7 +22,12 @@ class CommandBehaviorControllerTestElement extends HTMLElement {
2022
2123constructor ( ) {
2224super ( ) ;
23- new TypeCommandController ( this ) ;
25+ const events = ( this . constructor as typeof CommandBehaviorControllerTestElement ) . events ;
26+ if ( events ) {
27+ new TypeCommandController ( this , { events } ) ;
28+ } else {
29+ new TypeCommandController ( this ) ;
30+ }
2431}
2532
2633addController ( controller : ReactiveController ) {
@@ -40,10 +47,21 @@ class CommandBehaviorControllerTestElement extends HTMLElement {
4047}
4148}
4249
50+ class MultiEventCommandBehaviorControllerTestElement extends CommandBehaviorControllerTestElement {
51+ static override events = [ 'change' , 'click' ] ;
52+ }
53+
4354if ( ! customElements . get ( 'command-behavior-controller-test-element' ) ) {
4455customElements . define ( 'command-behavior-controller-test-element' , CommandBehaviorControllerTestElement ) ;
4556}
4657
58+ if ( ! customElements . get ( 'multi-event-command-behavior-controller-test-element' ) ) {
59+ customElements . define (
60+ 'multi-event-command-behavior-controller-test-element' ,
61+ MultiEventCommandBehaviorControllerTestElement
62+ ) ;
63+ }
64+
4765describe ( 'CommandBehaviorController' , ( ) => {
4866let fixture : HTMLElement ;
4967
@@ -73,6 +91,30 @@ describe('CommandBehaviorController', () => {
7391expect ( event . source ) . toBe ( element ) ;
7492} ) ;
7593
94+ it ( 'should dispatch command events for every configured event' , async ( ) => {
95+ fixture = await createFixture (
96+ html `< multi-event-command-behavior-controller-test-element > </ multi-event-command-behavior-controller-test-element > < div
97+ id ="target "
98+ > </ div > `
99+ ) ;
100+ const element = fixture . querySelector < MultiEventCommandBehaviorControllerTestElement > (
101+ 'multi-event-command-behavior-controller-test-element'
102+ ) ! ;
103+ const target = fixture . querySelector < HTMLElement > ( '#target' ) ! ;
104+ const command = vi . fn ( ) ;
105+
106+ target . addEventListener ( 'command' , command ) ;
107+ element . command = '--test' ;
108+ element . commandForElement = target ;
109+ element . sync ( ) ;
110+
111+ element . dispatchEvent ( new Event ( 'change' ) ) ;
112+ await emulateClick ( element ) ;
113+ element . dispatchEvent ( new Event ( 'input' ) ) ;
114+
115+ expect ( command ) . toHaveBeenCalledTimes ( 2 ) ;
116+ } ) ;
117+
76118it ( 'should resolve command targets by commandfor id' , async ( ) => {
77119fixture = await createFixture (
78120html `< command-behavior-controller-test-element > </ command-behavior-controller-test-element > < div id ="target "> </ div > `
0 commit comments