Skip to content

Commit

Permalink
Requested change
Browse files Browse the repository at this point in the history
  • Loading branch information
wassgha committed Aug 17, 2017
1 parent c3f5ee3 commit c7dee6c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
9 changes: 5 additions & 4 deletions src/event-helper-listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,20 @@
*/
export function detectEvtListenerOptsSupport() {
// Only run the test once
if (typeof optsSupported != 'undefined') {
if (optsSupported !== undefined) {
return optsSupported;
}

optsSupported = false;
// Test whether browser supports EventListenerOptions or not
try {
// Test whether browser supports EventListenerOptions or not
const options = {
get capture() {
optsSupported = true;
},
};
self.addEventListener('test-opts', null, options);
self.addEventListener('test-options', null, options);
self.removeEventListener('test-options', null, options);
} catch (err) {
// EventListenerOptions are not supported
}
Expand All @@ -104,6 +105,6 @@
/**
* Resets the test for whether addEventListener supports options or not.
*/
export function resetEvtListenerOptsSupport() {
export function resetEvtListenerOptsSupportForTesting() {
optsSupported = undefined;
}
55 changes: 33 additions & 22 deletions test/functional/test-event-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../../src/event-helper';
import {
detectEvtListenerOptsSupport,
resetEvtListenerOptsSupport,
resetEvtListenerOptsSupportForTesting,
} from '../../src/event-helper-listen';
import {Observable} from '../../src/observable';
import * as sinon from 'sinon';
Expand All @@ -43,6 +43,7 @@ describe('EventHelper', () => {
let loadObservable;
let errorObservable;
let addEventListenerStub;
let removeEventListenerStub;

beforeEach(() => {
sandbox = sinon.sandbox.create();
Expand Down Expand Up @@ -251,44 +252,54 @@ describe('EventHelper', () => {
});

it('should detect when addEventListener options are supported', () => {
const eventListenerStubAcceptOpts = (type, listener, options) => {
const getCapture = options.capture;
if (getCapture) {
// Added to bypass linter (never used warning)
}
};
// Simulate an addEventListener that accepts options
addEventListenerStub = sandbox.stub(self, 'addEventListener',
(type, listener, options) => {
const getCapture = options.capture;
if (getCapture) {
// Added to bypass linter (never used warning)
}
}
);
resetEvtListenerOptsSupport();
addEventListenerStub =
sandbox.stub(self, 'addEventListener', eventListenerStubAcceptOpts);
// Simulate a removeEventListener that accepts options
removeEventListenerStub =
sandbox.stub(self, 'removeEventListener', eventListenerStubAcceptOpts);
resetEvtListenerOptsSupportForTesting();
expect(detectEvtListenerOptsSupport()).to.be.true;
expect(addEventListenerStub.called).to.be.true;
resetEvtListenerOptsSupport();
expect(removeEventListenerStub.called).to.be.true;
resetEvtListenerOptsSupportForTesting();
});

it('should cache the result of the test and only do it once', () => {
resetEvtListenerOptsSupport();
resetEvtListenerOptsSupportForTesting();
expect(detectEvtListenerOptsSupport()).to.be.true;
expect(addEventListenerStub.called).to.be.true;
expect(removeEventListenerStub.called).to.be.true;
expect(detectEvtListenerOptsSupport()).to.be.true;
expect(addEventListenerStub.calledOnce).to.be.true;
expect(removeEventListenerStub.calledOnce).to.be.true;
});

it('should detect when addEventListener options are not supported', () => {
const eventListenerStubRejectOpts = (type, listener, capture) => {
const getCapture = capture;
if (getCapture) {
// Added to bypass linter (never used warning)
}
};
// Simulate an addEventListener that does not accept options
addEventListenerStub = sandbox.stub(self, 'addEventListener',
(type, listener, capture) => {
const getCapture = capture;
if (getCapture) {
// Added to bypass linter (never used warning)
}
}
);
resetEvtListenerOptsSupport();
addEventListenerStub =
sandbox.stub(self, 'addEventListener', eventListenerStubRejectOpts);
// Simulate a removeEventListener that does not accept options
removeEventListenerStub =
sandbox.stub(self, 'removeEventListener', eventListenerStubRejectOpts);
resetEvtListenerOptsSupportForTesting();
expect(detectEvtListenerOptsSupport()).to.be.false;
expect(addEventListenerStub.called).to.be.true;
expect(removeEventListenerStub.called).to.be.true;
expect(detectEvtListenerOptsSupport()).to.be.false;
expect(addEventListenerStub.calledOnce).to.be.true;
expect(removeEventListenerStub.calledOnce).to.be.true;
});

});

0 comments on commit c7dee6c

Please sign in to comment.