-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
34 lines (29 loc) · 839 Bytes
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import test from 'ava';
import {HttpError, throwIfHttpError} from './index.js';
globalThis.fetch = async url => {
if (url === '/ok') {
return {
ok: true,
status: 200,
statusText: 'OK',
url,
};
}
return {
ok: false,
status: 404,
statusText: 'Not Found',
url,
};
};
test('throwIfHttpError - should not throw for ok responses', async t => {
const response = await fetch('/ok');
await t.notThrowsAsync(throwIfHttpError(response));
});
test('throwIfHttpError - should throw HttpError for non-ok responses', async t => {
const response = await fetch('/not-found');
await t.throwsAsync(throwIfHttpError(response), {instanceOf: HttpError});
});
test('throwIfHttpError - should work with promise responses', async t => {
await t.throwsAsync(throwIfHttpError(fetch('/not-found')), {instanceOf: HttpError});
});