mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Add a waitOnEventOrTimeout
helper function that allows waiting for an event or a timeout, whichever occurs first
This commit is contained in:
parent
28ce3b6185
commit
0c4985546a
2 changed files with 175 additions and 2 deletions
|
@ -14,7 +14,8 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
binarySearchFirstItem, EventBus, getPDFFileNameFromURL
|
||||
binarySearchFirstItem, EventBus, getPDFFileNameFromURL, waitOnEventOrTimeout,
|
||||
WaitOnType
|
||||
} from '../../web/ui_utils';
|
||||
import { createObjectURL, isNodeJS } from '../../src/shared/util';
|
||||
|
||||
|
@ -259,4 +260,118 @@ describe('ui_utils', function() {
|
|||
expect(count).toEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('waitOnEventOrTimeout', function() {
|
||||
let eventBus;
|
||||
|
||||
beforeAll(function(done) {
|
||||
eventBus = new EventBus();
|
||||
done();
|
||||
});
|
||||
|
||||
afterAll(function() {
|
||||
eventBus = null;
|
||||
});
|
||||
|
||||
it('should reject invalid parameters', function(done) {
|
||||
let invalidTarget = waitOnEventOrTimeout({
|
||||
target: 'window',
|
||||
name: 'DOMContentLoaded',
|
||||
}).then(function() {
|
||||
throw new Error('Should reject invalid parameters.');
|
||||
}, function(reason) {
|
||||
expect(reason instanceof Error).toEqual(true);
|
||||
});
|
||||
|
||||
let invalidName = waitOnEventOrTimeout({
|
||||
target: eventBus,
|
||||
name: '',
|
||||
}).then(function() {
|
||||
throw new Error('Should reject invalid parameters.');
|
||||
}, function(reason) {
|
||||
expect(reason instanceof Error).toEqual(true);
|
||||
});
|
||||
|
||||
let invalidDelay = waitOnEventOrTimeout({
|
||||
target: eventBus,
|
||||
name: 'pagerendered',
|
||||
delay: -1000,
|
||||
}).then(function() {
|
||||
throw new Error('Should reject invalid parameters.');
|
||||
}, function(reason) {
|
||||
expect(reason instanceof Error).toEqual(true);
|
||||
});
|
||||
|
||||
Promise.all([invalidTarget, invalidName, invalidDelay]).then(done,
|
||||
done.fail);
|
||||
});
|
||||
|
||||
it('should resolve on event, using the DOM', function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('Document in not supported in Node.js.');
|
||||
}
|
||||
let button = document.createElement('button');
|
||||
|
||||
let buttonClicked = waitOnEventOrTimeout({
|
||||
target: button,
|
||||
name: 'click',
|
||||
delay: 10000,
|
||||
});
|
||||
// Immediately dispatch the expected event.
|
||||
button.click();
|
||||
|
||||
buttonClicked.then(function(type) {
|
||||
expect(type).toEqual(WaitOnType.EVENT);
|
||||
done();
|
||||
}, done.fail);
|
||||
});
|
||||
|
||||
it('should resolve on timeout, using the DOM', function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('Document in not supported in Node.js.');
|
||||
}
|
||||
let button = document.createElement('button');
|
||||
|
||||
let buttonClicked = waitOnEventOrTimeout({
|
||||
target: button,
|
||||
name: 'click',
|
||||
delay: 10,
|
||||
});
|
||||
// Do *not* dispatch the event, and wait for the timeout.
|
||||
|
||||
buttonClicked.then(function(type) {
|
||||
expect(type).toEqual(WaitOnType.TIMEOUT);
|
||||
done();
|
||||
}, done.fail);
|
||||
});
|
||||
|
||||
it('should resolve on event, using the EventBus', function(done) {
|
||||
let pageRendered = waitOnEventOrTimeout({
|
||||
target: eventBus,
|
||||
name: 'pagerendered',
|
||||
delay: 10000,
|
||||
});
|
||||
// Immediately dispatch the expected event.
|
||||
eventBus.dispatch('pagerendered');
|
||||
|
||||
pageRendered.then(function(type) {
|
||||
expect(type).toEqual(WaitOnType.EVENT);
|
||||
done();
|
||||
}, done.fail);
|
||||
});
|
||||
|
||||
it('should resolve on timeout, using the EventBus', function(done) {
|
||||
let pageRendered = waitOnEventOrTimeout({
|
||||
target: eventBus,
|
||||
name: 'pagerendered',
|
||||
delay: 10,
|
||||
});
|
||||
// Do *not* dispatch the event, and wait for the timeout.
|
||||
|
||||
pageRendered.then(function(type) {
|
||||
expect(type).toEqual(WaitOnType.TIMEOUT);
|
||||
done();
|
||||
}, done.fail);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue