-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply init/destroy EventListeners feature to modal component (#602)
This PR adds the eventListeners option to the modal component along with the `initEventListeners()` and `destroyEventListeners()` methods to the API. This PR fixes #601
- Loading branch information
Showing
7 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import Modal from '../index.js'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { transitionStart, transitionEnd } from './helpers/transition'; | ||
|
||
let modal; | ||
const ev = new Event('transitionend'); | ||
|
||
const markup = ` | ||
<button data-modal-open="modal-default">Modal Default</button> | ||
<div data-modal="modal-default" class="modal"> | ||
<div data-modal-dialog class="modal__dialog"> | ||
<button data-modal-close>Close</button> | ||
</div> | ||
</div> | ||
`; | ||
|
||
beforeEach(() => { | ||
document.body.innerHTML = null; | ||
}); | ||
|
||
afterEach(() => { | ||
modal.destroy(); | ||
modal.destroyEventListeners(); | ||
modal = null; | ||
}); | ||
|
||
test('should not set event listeners when option is set to false', async () => { | ||
document.body.innerHTML = markup; | ||
modal = new Modal({ eventListeners: false }); | ||
const el = document.querySelector('[data-modal]'); | ||
const btn = document.querySelector('[data-modal-open]'); | ||
|
||
modal.init(); | ||
expect(el).toHaveClass('modal is-closed'); | ||
|
||
btn.click(); | ||
await transitionStart(el); | ||
expect(el).not.toHaveClass('is-opening'); | ||
|
||
await transitionEnd(el); | ||
expect(el).not.toHaveClass('is-opened'); | ||
}); | ||
|
||
test('should add event listeners using api call', async () => { | ||
document.body.innerHTML = markup; | ||
modal = new Modal({ eventListeners: false }); | ||
const el = document.querySelector('[data-modal]'); | ||
const btn = document.querySelector('[data-modal-open]'); | ||
|
||
modal.init(); | ||
expect(el).toHaveClass('modal is-closed'); | ||
|
||
btn.click(); | ||
await transitionStart(el); | ||
expect(el).not.toHaveClass('is-opening'); | ||
|
||
await transitionEnd(el); | ||
expect(el).not.toHaveClass('is-opened'); | ||
|
||
modal.initEventListeners(); | ||
|
||
btn.click(); | ||
await transitionStart(el); | ||
expect(el).toHaveClass('is-opening'); | ||
|
||
await transitionEnd(el); | ||
expect(el).toHaveClass('is-opened'); | ||
}); | ||
|
||
test('should remove event listeners using api call', async () => { | ||
document.body.innerHTML = markup; | ||
modal = new Modal(); | ||
const el = document.querySelector('[data-modal]'); | ||
const btn = document.querySelector('[data-modal-open]'); | ||
|
||
modal.init(); | ||
expect(el).toHaveClass('modal is-closed'); | ||
|
||
modal.destroyEventListeners(); | ||
|
||
btn.click(); | ||
await transitionStart(el); | ||
expect(el).not.toHaveClass('is-opening'); | ||
|
||
el.dispatchEvent(ev); | ||
await transitionEnd(el); | ||
expect(el).not.toHaveClass('is-opened'); | ||
}); |