Skip to content

Commit

Permalink
Add test in window events
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Apr 23, 2024
1 parent 4173531 commit 656a2d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<p data-wp-text="state.counter" data-testid="counter">0</p>
</div>
</div>
<div data-wp-on-window--resize="actions.resizeHandler" data-wp-on-window--resize--second="actions.resizeSecondHandler">
<p data-wp-text="state.resizeHandler" data-testid="resizeHandler">no</p>
<p data-wp-text="state.resizeSecondHandler" data-testid="resizeSecondHandler">no</p>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const { state } = store( 'directive-on-window', {
counter: 0,
isVisible: true,
isEventAttached: 'no',
resizeHandler: 'no',
resizeSecondHandler: 'no',
},
callbacks: {
resizeHandler() {
Expand All @@ -39,5 +41,11 @@ const { state } = store( 'directive-on-window', {
state.isEventAttached = 'no';
state.isVisible = ! state.isVisible;
},
resizeHandler: () => {
state.resizeHandler = 'yes';
},
resizeSecondHandler: () => {
state.resizeSecondHandler = 'yes';
}
}
} );
21 changes: 21 additions & 0 deletions test/e2e/specs/interactivity/directive-on-window.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,25 @@ test.describe( 'data-wp-on-window', () => {
await page.setViewportSize( { width: 200, height: 600 } );
await expect( counter ).toHaveText( '2' );
} );
test( 'should work with multiple event handlers on the same event type', async ( {
page,
} ) => {
const resizeHandler = page.getByTestId( 'resizeHandler' );
const resizeSecondHandler = page.getByTestId( 'resizeSecondHandler' );

// Initial value.
await expect( resizeHandler ).toHaveText( 'no' );
await expect( resizeSecondHandler ).toHaveText( 'no' );

// Make sure the event listener is attached.
await page
.getByTestId( 'isEventAttached' )
.filter( { hasText: 'yes' } )
.waitFor();

// This keyboard press should increase the counter.
await page.setViewportSize( { width: 600, height: 600 } );
await expect( resizeHandler ).toHaveText( 'yes' );
await expect( resizeSecondHandler ).toHaveText( 'yes' );
} );
} );

0 comments on commit 656a2d6

Please sign in to comment.