Skip to content

Commit 1e5fcec

Browse files
TechQuerymo
authored andcommitted
test: add Test cases for throwIfAborted, timeout & any methods
1 parent 78cced8 commit 1e5fcec

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Here is a basic example of [abortable fetch running in IE 8](https://github.com/
158158
* [Sai Srinivasan](https://github.com/SairamSrinivasan)
159159
* [Ambar Lee](https://github.com/ambar)
160160
* [Sebastian Silbermann](https://github.com/eps1lon)
161+
* [Yao Shi](https://github.com/TechQuery)
161162

162163
# License
163164

tests/basic.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,62 @@ const runBasicTests = (testSuiteTitle, TESTPAGE_URL) => {
347347
});
348348
expect(signalReason).toEqual('My reason');
349349
});
350+
351+
it('throws if the signal is aborted', async () => {
352+
await browser.url(TESTPAGE_URL);
353+
const result = await browser.executeAsync(async (done) => {
354+
const controller = new AbortController();
355+
controller.abort(new Error('My reason'));
356+
try {
357+
controller.signal.throwIfAborted();
358+
done('FAIL');
359+
} catch (error) {
360+
done([error.name, error.message] + '');
361+
}
362+
});
363+
expect(result).toBe('AbortError,My reason');
364+
});
365+
366+
it('makes a new signal with a timeout', async () => {
367+
await browser.url(TESTPAGE_URL);
368+
const result = await browser.executeAsync(async (done) => {
369+
const signal = new AbortSignal.timeout(100);
370+
setTimeout(() => {
371+
if (signal.aborted) done('PASS');
372+
else done('FAIL');
373+
}, 100);
374+
});
375+
expect(result).toBe('PASS');
376+
});
377+
378+
it('aborted the new signal immediately if one of source signals is aborted already', async () => {
379+
await browser.url(TESTPAGE_URL);
380+
const result = await browser.executeAsync(async (done) => {
381+
const controller1 = new AbortController(),
382+
controller2 = new AbortController();
383+
controller1.abort();
384+
385+
const signal = AbortSignal.any([controller1.signal, controller2.signal]);
386+
if (signal.aborted) done('PASS');
387+
else done('FAIL');
388+
});
389+
expect(result).toBe('PASS');
390+
});
391+
392+
it('aborted the new signal asynchronously if one of source signals is aborted later', async () => {
393+
await browser.url(TESTPAGE_URL);
394+
const result = await browser.executeAsync(async (done) => {
395+
const controller1 = new AbortController(),
396+
controller2 = new AbortController();
397+
const signal = AbortSignal.any([controller1.signal, controller2.signal]);
398+
setTimeout(() => controller2.abort('Controller 2 is aborted'), 100);
399+
setTimeout(() => {
400+
if (signal.aborted) done(signal.reason);
401+
else done('FAIL');
402+
}, 100);
403+
});
404+
expect(result).toBe('Controller 2 is aborted');
405+
});
350406
});
351407
};
352408

0 commit comments

Comments
 (0)