Skip to content

Commit 42ea7fc

Browse files
committed
add tests
1 parent 97a6889 commit 42ea7fc

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

packages/storage/src/platform/node/connection.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ import { ErrorCode, Connection } from '../../implementation/connection';
1919
import { internalError } from '../../implementation/error';
2020
import nodeFetch, { FetchError } from 'node-fetch';
2121

22-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23-
const fetch: typeof window.fetch = nodeFetch as any;
24-
2522
/**
2623
* Network layer that works in Node.
2724
*
@@ -34,6 +31,8 @@ export class FetchConnection implements Connection {
3431
private body_: string | undefined;
3532
private headers_: Headers | undefined;
3633
private sent_: boolean = false;
34+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
35+
private fetch_: typeof window.fetch = nodeFetch as any;
3736

3837
constructor() {
3938
this.errorCode_ = ErrorCode.NO_ERROR;
@@ -50,7 +49,7 @@ export class FetchConnection implements Connection {
5049
}
5150
this.sent_ = true;
5251

53-
return fetch(url, {
52+
return this.fetch_(url, {
5453
method,
5554
headers: headers || {},
5655
body
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect } from "chai";
2+
import { SinonFakeXMLHttpRequest, useFakeXMLHttpRequest } from "sinon";
3+
import { ErrorCode } from "../../src/implementation/connection";
4+
import { XhrConnection } from "../../src/platform/browser/connection";
5+
6+
describe('Connections', () => {
7+
it('XhrConnection.send() should not reject on network errors', async () => {
8+
const fakeXHR = useFakeXMLHttpRequest();
9+
const connection = new XhrConnection();
10+
const sendPromise = connection.send('testurl', 'GET');
11+
// simulate an network error
12+
((connection as any).xhr_ as SinonFakeXMLHttpRequest).error();
13+
await sendPromise;
14+
expect(connection.getErrorCode()).to.equal(ErrorCode.NETWORK_ERROR);
15+
fakeXHR.restore();
16+
});
17+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { stub } from 'sinon';
2+
import { expect } from 'chai';
3+
import { ErrorCode } from '../../src/implementation/connection';
4+
import { FetchConnection } from '../../src/platform/node/connection';
5+
6+
describe('Connections', () => {
7+
it('FetchConnection.send() should not reject on network errors', async () => {
8+
const connection = new FetchConnection();
9+
10+
// need the casting here because fetch_ is a private member
11+
stub(connection as any, 'fetch_').rejects();
12+
await connection.send('testurl', 'GET');
13+
expect(connection.getErrorCode()).to.equal(ErrorCode.NETWORK_ERROR);
14+
});
15+
});

0 commit comments

Comments
 (0)