11import { describe , it , expect , vi , beforeEach } from "vitest" ;
22import { screen } from "@testing-library/dom" ;
33import { userEvent } from "@testing-library/user-event" ;
4- import { qs } from "../../src/ts/utils/dom" ;
4+ import { ElementWithUtils , qsr } from "../../src/ts/utils/dom" ;
55
66describe ( "dom" , ( ) => {
77 describe ( "ElementWithUtils" , ( ) => {
88 describe ( "onChild" , ( ) => {
99 const handler = vi . fn ( ) ;
1010
11- function registerOnChild ( event : string , selector : string ) : void {
12- const parent = qs ( "#parent" ) ;
11+ function registerOnChild (
12+ event : string ,
13+ selector : string ,
14+ options ?: {
15+ parent ?: ElementWithUtils ;
16+ } ,
17+ ) : void {
18+ const parent = options ?. parent ?? qsr ( "#parent" ) ;
1319 parent ?. onChild ( event , selector , ( e ) =>
1420 handler ( {
1521 target : e . target ,
@@ -33,7 +39,10 @@ describe("dom", () => {
3339 <div id="inner1" class="inner">test</div>
3440 <div id="inner2" data-testid="inner2" class="inner">
3541 test
36- <button id="button" data-testid="button">click</button>
42+ <button id="button" data-testid="button">
43+ click me
44+ <i id="icon" data-testid="icon">test</i>
45+ </button>
3746 </div>
3847 </div>
3948 <div id="mid2" class="middle">
@@ -57,6 +66,18 @@ describe("dom", () => {
5766 expect ( handler ) . not . toHaveBeenCalled ( ) ;
5867 } ) ;
5968
69+ it ( "should not fire when selector doesnt match" , async ( ) => {
70+ //GIVEN
71+ const buttonEl = qsr ( "#button" ) ;
72+ registerOnChild ( "click" , "div" , { parent : buttonEl } ) ;
73+
74+ //WHEN
75+ await userEvent . click ( screen . getByTestId ( "icon" ) ) ;
76+
77+ //THEN
78+ expect ( handler ) . not . toHaveBeenCalled ( ) ;
79+ } ) ;
80+
6081 it ( "should fire when selector is clicked" , async ( ) => {
6182 //GIVEN
6283 registerOnChild ( "click" , "div" ) ;
0 commit comments