11import utils from "../../core/utils" ;
2+ import pat_modal from "../modal/modal" ;
23import registry from "../../core/registry" ;
34
45import "./close-panel" ;
5- import "../modal/modal" ;
66import "../tooltip/tooltip" ;
77
88describe ( "pat close-panel" , function ( ) {
@@ -28,20 +28,21 @@ describe("pat close-panel", function () {
2828 document . querySelector ( ".pat-tooltip" ) . click ( ) ;
2929 await utils . timeout ( 1 ) ; // wait a tick async to settle.
3030 expect ( document . querySelectorAll ( ".tooltip-container" ) . length ) . toBe ( 1 ) ;
31- expect ( document . querySelectorAll ( ".tooltip-container.has-close-panel" ) . length ) . toBe ( 1 ) ; // prettier-ignore
3231 expect ( document . querySelectorAll ( ".tooltip-container .pat-tooltip--close-button.close-panel" ) . length ) . toBe ( 1 ) ; // prettier-ignore
3332
3433 document . querySelector ( ".pat-tooltip--close-button" ) . click ( ) ;
35- await utils . timeout ( 1 ) ; // wait a tick async to settle.
34+ await utils . timeout ( 1 ) ; // close-button is async - wait for it.
35+ await utils . timeout ( 1 ) ; // hide is async - wait for it.
3636 expect ( document . querySelectorAll ( ".tooltip-container" ) . length ) . toBe ( 0 ) ;
3737 expect ( document . querySelectorAll ( ".tooltip-container .pat-tooltip--close-button" ) . length ) . toBe ( 0 ) ; // prettier-ignore
3838
3939 document . querySelector ( "#close-modal" ) . click ( ) ;
40- await utils . timeout ( 1 ) ; // wait a tick async to settle.
40+ await utils . timeout ( 1 ) ; // close-button is async - wait for it.
41+ await utils . timeout ( 1 ) ; // destroy is async - wait for it.
4142 expect ( document . querySelectorAll ( ".pat-modal" ) . length ) . toBe ( 0 ) ;
4243 } ) ;
4344
44- it ( "Closes a dialog's panel." , function ( ) {
45+ it ( "Closes a dialog's panel." , async function ( ) {
4546 document . body . innerHTML = `
4647 <dialog open>
4748 <button class="close-panel">close</button>
@@ -55,7 +56,37 @@ describe("pat close-panel", function () {
5556 expect ( dialog . open ) . toBe ( true ) ;
5657
5758 document . querySelector ( ".close-panel" ) . click ( ) ;
59+ await utils . timeout ( 1 ) ; // close-button is async - wait for it.
5860
5961 expect ( dialog . open ) . toBe ( false ) ;
6062 } ) ;
63+
64+ it ( "Prevents closing a panel with an invalid form when submitting but allow to cancel and close." , async function ( ) {
65+ const spy_destroy_modal = jest . spyOn ( pat_modal . prototype , "destroy" ) ;
66+
67+ document . body . innerHTML = `
68+ <div class="pat-modal">
69+ <form action="." class="pat-validation">
70+ <input name="ok" required />
71+ <button class="close-panel submit">submit</button>
72+ <button class="close-panel cancel" type="button">cancel</button>
73+ </form>
74+ </div>
75+ ` ;
76+ const el = document . querySelector ( "form" ) ;
77+
78+ registry . scan ( document . body ) ;
79+ await utils . timeout ( 1 ) ; // wait a tick for async to settle.
80+
81+ el . querySelector ( "button.submit" ) . click ( ) ;
82+ await utils . timeout ( 1 ) ; // wait a tick for async to settle.
83+
84+ expect ( spy_destroy_modal ) . not . toHaveBeenCalled ( ) ;
85+
86+ // A non-submit close-panel button does not check for validity.
87+ el . querySelector ( "button.cancel" ) . click ( ) ;
88+ await utils . timeout ( 1 ) ; // wait a tick for async to settle.
89+
90+ expect ( spy_destroy_modal ) . toHaveBeenCalled ( ) ;
91+ } ) ;
6192} ) ;
0 commit comments