Skip to content

Commit

Permalink
Merge pull request #1963 from donavon/bugfix/set-remove
Browse files Browse the repository at this point in the history
Allow wb.removeEventListener to work without an exception
  • Loading branch information
philipwalton authored Mar 15, 2019
2 parents d971e3e + 122c0d5 commit 493ef61
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/workbox-window/utils/EventTargetShim.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class EventTargetShim {
* @param {Function} listener
*/
removeEventListener(type, listener) {
this._getEventListenersByType(type).remove(listener);
this._getEventListenersByType(type).delete(listener);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/workbox-window/unit/test-Workbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -868,5 +868,30 @@ describe(`[workbox-window] Workbox`, function() {
expect(externalActivated2Spy.callCount).to.equal(0);
});
});

describe(`removeEventListener()`, function() {
it(`will register and then unregister event listeners of a given type`, function() {
const eventType = 'testEventType';
const event = {type: eventType};
const eventListener1 = sandbox.stub();
const eventListener2 = sandbox.stub();

const wb = new Workbox(uniq('sw-clients-claim.tmp.js'));
wb.addEventListener(eventType, eventListener1);
wb.addEventListener(eventType, eventListener2);

wb.dispatchEvent(event);
expect(eventListener1.calledOnceWith(event)).to.be.true;
expect(eventListener2.calledOnceWith(event)).to.be.true;

wb.removeEventListener(eventType, eventListener2);
wb.dispatchEvent(event);

// The remaining stub should be called again.
expect(eventListener1.calledTwice).to.be.true;
// Make sure the removed stub was called only once.
expect(eventListener2.calledOnce).to.be.true;
});
});
});
});

0 comments on commit 493ef61

Please sign in to comment.