Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from AVA to Vitest #42

Merged
merged 12 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more tests
  • Loading branch information
fregante committed Apr 22, 2023
commit e2cba4ba7ee965ac886b39411c1134a268c98c81
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function delegate<
callback.call(baseElement, delegateEvent as DelegateEvent<GlobalEventHandlersEventMap[TEventType], TElement>);
if (once) {
baseElement.removeEventListener(type, listenerFn, nativeListenerOptions);
editLedger(false, baseElement, callback, setup);
}
}
};
Expand Down
55 changes: 36 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@ test.serial('should add an event listener', t => {
anchor.click();
});

test.serial('should add an event listener only once', t => {
t.plan(2);

// Only deduplicates the `capture` flag
// https://github.com/fregante/delegate-it/pull/11#discussion_r285481625

// Capture: false
delegate(container, 'a', 'click', t.pass);
delegate(container, 'a', 'click', t.pass, {passive: true});
delegate(container, 'a', 'click', t.pass, {capture: false});

// Capture: true
delegate(container, 'a', 'click', t.pass, true);
delegate(container, 'a', 'click', t.pass, {capture: true});

anchor.click();
});

test.serial('should handle events on text nodes', t => {
delegate(container, 'a', 'click', t.pass);
anchor.firstChild.dispatchEvent(new MouseEvent('click', {bubbles: true}));
Expand Down Expand Up @@ -164,9 +146,44 @@ test.serial('should not add an event listener when passed an already aborted sig
t.true(spy.notCalled);
});

test.serial('should called the listener once with the `once` option', t => {
test.serial('should call the listener once with the `once` option', t => {
const spy = sinon.spy();
delegate(container, 'a', 'click', spy, {once: true});

container.click();
t.true(spy.notCalled, 'It should not be called on the container');
anchor.click();
t.true(spy.calledOnce, 'It should be called on the delegate target');
anchor.click();
t.true(spy.calledOnce, 'It should not be called again on the delegate target');
});

test.serial('should add a specific event listener only once', t => {
t.plan(2);

// Only deduplicates the `capture` flag
// https://github.com/fregante/delegate-it/pull/11#discussion_r285481625

// Capture: false
delegate(container, 'a', 'click', t.pass);
delegate(container, 'a', 'click', t.pass, {passive: true});
delegate(container, 'a', 'click', t.pass, {capture: false});

// Capture: true
delegate(container, 'a', 'click', t.pass, true);
delegate(container, 'a', 'click', t.pass, {capture: true});

// Once
delegate(container, 'a', 'click', t.pass, {once: true});
delegate(container, 'a', 'click', t.pass, {once: false});

anchor.click();
});

test.serial('should deduplicate identical listeners added after `once:true`', t => {
const spy = sinon.spy();
delegate(container, 'a', 'click', spy, {once: true});
delegate(container, 'a', 'click', spy, {once: false});

container.click();
t.true(spy.notCalled, 'It should not be called on the container');
Expand Down