22 * Tests for email opt-in modal initialization
33 */
44
5+ import { createFocusTrap } from 'focus-trap' ;
56import { initOptInModal } from '../../../src/emailOptIn/modal' ;
67
78jest . mock (
@@ -15,9 +16,18 @@ jest.mock(
1516describe ( 'email opt-in modal init' , ( ) => {
1617 beforeEach ( ( ) => {
1718 jest . restoreAllMocks ( ) ;
19+ jest . useRealTimers ( ) ;
20+ document . body . innerHTML = '' ;
1821 window . onload = null ;
1922 } ) ;
2023
24+ afterEach ( ( ) => {
25+ jest . clearAllTimers ( ) ;
26+ jest . useRealTimers ( ) ;
27+ delete window . tb_show ;
28+ delete window . jQuery ;
29+ } ) ;
30+
2131 test ( 'does not overwrite existing window.onload handler' , ( ) => {
2232 const existingOnload = jest . fn ( ) ;
2333 window . onload = existingOnload ;
@@ -43,4 +53,35 @@ describe( 'email opt-in modal init', () => {
4353 expect ( addEventListenerSpy ) . toHaveBeenCalledWith ( 'mousemove' , expect . any ( Function ) , { once : true } ) ;
4454 expect ( addEventListenerSpy ) . toHaveBeenCalledWith ( 'scroll' , expect . any ( Function ) , { once : true } ) ;
4555 } ) ;
56+
57+ test ( 'marks the email opt-in ThickBox before activating its focus trap' , ( ) => {
58+ jest . useFakeTimers ( ) ;
59+
60+ const modal = document . createElement ( 'div' ) ;
61+ modal . id = 'TB_window' ;
62+ modal . innerHTML = '<button class="tb-close-icon">Close</button>' ;
63+
64+ const focusTrap = {
65+ activate : jest . fn ( ) ,
66+ deactivate : jest . fn ( ) ,
67+ } ;
68+ createFocusTrap . mockReturnValue ( focusTrap ) ;
69+ window . tb_show = jest . fn ( ( ) => document . body . appendChild ( modal ) ) ;
70+ window . jQuery = jest . fn ( ( ) => ( { one : jest . fn ( ) } ) ) ;
71+
72+ const addEventListenerSpy = jest . spyOn ( window , 'addEventListener' ) ;
73+ initOptInModal ( ) ;
74+
75+ const loadCall = addEventListenerSpy . mock . calls . find ( ( call ) => call [ 0 ] === 'load' ) ;
76+ loadCall [ 1 ] ( ) ;
77+ const mousemoveCall = addEventListenerSpy . mock . calls . find ( ( call ) => call [ 0 ] === 'mousemove' ) ;
78+ mousemoveCall [ 1 ] ( ) ;
79+
80+ expect ( modal . classList . contains ( 'edac-email-opt-in-modal' ) ) . toBe ( true ) ;
81+
82+ jest . advanceTimersByTime ( 250 ) ;
83+
84+ expect ( createFocusTrap ) . toHaveBeenCalledWith ( modal ) ;
85+ expect ( focusTrap . activate ) . toHaveBeenCalled ( ) ;
86+ } ) ;
4687} ) ;
0 commit comments