Skip to content

Commit

Permalink
add a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ksercs committed May 1, 2024
1 parent 0249d56 commit 7e5a620
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
10 changes: 4 additions & 6 deletions core/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import logger from './logger';
const debug = logger('quill:events');
const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];
if (hasWindow()) {
EVENTS.forEach((eventName) => {
EVENTS.forEach((eventName) => {

Check failure on line 9 in core/emitter.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
document.addEventListener(eventName, (...args) => {
const event = args[0];

const target = event?.target;
const shadowRoot = target?.shadowRoot;

const shadowRoot = event?.target?.shadowRoot;
const root = shadowRoot ?? document;
const quillContainers = root.querySelectorAll('.ql-container');

Array.from(root.querySelectorAll('.ql-container')).forEach((node) => {
Array.from(quillContainers).forEach((node) => {
const quill = instances.get(node);
if (quill && quill.emitter) {
quill.emitter.handleDOM(...args);
Expand Down
1 change: 1 addition & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import './unit/blots/block-embed';
import './unit/blots/inline';

import './unit/core/editor';
import './unit/core/emitter';
import './unit/core/selection';
import './unit/core/quill';
import './unit/core/input';
Expand Down
32 changes: 32 additions & 0 deletions test/unit/core/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Quill from '../../../core/quill';

describe('Emitter', function () {
it('handleDOM is called on event hadling', function () {
this.quill = this.initialize(Quill, '');
spyOn(this.quill.emitter, 'handleDOM');

const event = new Event('click');
document.dispatchEvent(event);

expect(this.quill.emitter.handleDOM).toHaveBeenCalledWith(event);
});

it('handleDOM is called on event hadling if quill container is in shadowDOM', function () {
const shadowContainer = document.body.appendChild(document.createElement('div'));
try {
shadowContainer.attachShadow({ mode: 'open' });
shadowContainer.shadowRoot.innerHTML = '<div></div>';
const quillContainer = shadowContainer.shadowRoot.querySelector('div');

this.quill = this.initialize(Quill, '', quillContainer);
spyOn(this.quill.emitter, 'handleDOM');

const event = new Event('click', { bubbles: true });
shadowContainer.dispatchEvent(event);

expect(this.quill.emitter.handleDOM).toHaveBeenCalledWith(event);
} finally {
shadowContainer.remove();
}
});
});

0 comments on commit 7e5a620

Please sign in to comment.