Skip to content

Commit

Permalink
Add aborting test (not passing)
Browse files Browse the repository at this point in the history
see facebook/react-native#18115

what i tried:

- using fetch patch from abortcontroller polyfill
  - 'abortcontroller-polyfill/dist/polyfill-patch-fetch'
  - did not get it to work (weird error inside whatwg-fetch)
- patching react-native to use cross-fetch (by changing react-native/Libraries/Network/fetch.js)
  - did not get it to work (weird error inside cross-fetch)
- removing vendored whatwg-fetch from react-native source code (so the app loads real whatwg-fetch)
  - did not work
  • Loading branch information
Jyrno42 committed Dec 31, 2018
1 parent 5272fa0 commit 58f81ed
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
2 changes: 2 additions & 0 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import React, {Component} from 'react';
import {Button, ScrollView, StyleSheet, Text, View} from 'react-native';

import AbortTest from './tests/AbortTest';
import BasicFetchTest from './tests/BasicFetchTest';

// TODO: Replace with your ngrock url
Expand All @@ -18,6 +19,7 @@ type Props = {};

const allTests = {
basicFetch: BasicFetchTest,
abortTest: AbortTest,
};

const testKeys = Object.keys(allTests);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
},
"dependencies": {
"@tg-resources/fetch": "3.1.0-beta.0",
"@tg-resources/fetch-runtime": "^3.1.0-beta.0",
"abortcontroller-polyfill": "^1.2.1",
"cookie-parser": "^1.4.3",
"deep-equal": "^1.0.1",
"express": "^4.16.4",
Expand Down
69 changes: 69 additions & 0 deletions tests/AbortTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// @flow

// Compat issue 1: Needed due to https://github.com/facebook/react-native/issues/18115
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only';

import equal from 'deep-equal';

import { Component } from 'react';

import { AbortError } from 'tg-resources';
import { FetchResource as Resource } from '@tg-resources/fetch';


type Props = {
apiRoot: string,

onError: (e) => *,
onSuccess: () => *,
};

type State = {
controller: AbortController,
}

// TODO: Currently does not work see (https://github.com/facebook/react-native/issues/18115)
class AbortTest extends Component<Props, State> {
state = {
controller: new AbortController(),
};

async componentDidMount() {
const { apiRoot, onError, onSuccess } = this.props;
const { controller } = this.state;

setTimeout(() => {
controller.abort();
}, 100);

try {
await new Resource('/abort', {
apiRoot,
}).fetch(null, null, {
signal: controller.signal,
});

onError(new Error('Request should be aborted!'));
} catch (e) {
console.error(e);

if (!e || !e.isAbortError) {
onError(new Error('Raised error should be an instance of AbortError'));
return;
}

if (!e.isAbortError || e.type !== 'aborted' || e.name !== 'AbortError') {
onError(new Error(`AbortError signature is invalid: got=${JSON.stringify(e)}`));
return;
}

onSuccess();
}
}

render() {
return null;
}
}

export default AbortTest;
2 changes: 1 addition & 1 deletion tests/BasicFetchTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BasicFetchTest extends Component<Props> {
onSuccess();
} else {
onError(new Error(`Response does not match expected result: got=${JSON.stringify(result)}`));
}
}
} catch (e) {
onError(e);
}
Expand Down
41 changes: 34 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,15 @@
"@tg-resources/is" "^3.1.0-beta.0"
cookie ">=0.3.1"

"@tg-resources/fetch-runtime@^3.1.0-beta.0":
version "3.1.0-beta.0"
resolved "https://registry.yarnpkg.com/@tg-resources/fetch-runtime/-/fetch-runtime-3.1.0-beta.0.tgz#0e2d9d088e1890c21329a1f47e8b7af34f346d10"
integrity sha512-7M1lq3bb6Ujb73L5s/FOQus6p8dgaApJtOSvtCI2/rYE+XNmZlrqQdVkDK0BNSOotE7tswKYVahOno46SGKztQ==
dependencies:
cross-fetch "^3.0.0"
form-data "^2.3.3"
formdata-polyfill "^3.0.12"

"@tg-resources/fetch@3.1.0-beta.0":
version "3.1.0-beta.0"
resolved "https://registry.yarnpkg.com/@tg-resources/fetch/-/fetch-3.1.0-beta.0.tgz#562f3736aff3e3e5cbb16903e9e1aab6f04adb67"
Expand All @@ -678,6 +687,11 @@ abbrev@1:
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==

abortcontroller-polyfill@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/abortcontroller-polyfill/-/abortcontroller-polyfill-1.2.1.tgz#4e88847513a4ed691d5e4899d8b3a3af6f7d90ee"
integrity sha512-9jN7+BijYKWO8fxfcG7QZh7js6V+g3OjkxMRHfKWNjjs85048VY4cd27Uoe6yk55P66L/z7Dflu5+YEApgMzkA==

absolute-path@^0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/absolute-path/-/absolute-path-0.0.0.tgz#a78762fbdadfb5297be99b15d35a785b2f095bf7"
Expand Down Expand Up @@ -1628,6 +1642,14 @@ create-react-class@^15.6.3:
loose-envify "^1.3.1"
object-assign "^4.1.1"

cross-fetch@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.0.tgz#fb7fa6906f39c7233aa7ab8de1785e980532b899"
integrity sha512-P8HyKlMwT1ed9LqEWlJu+zfcxfn0KI4Nl4nxyvu1a8sg4vgtHdwhElZOgSNzoar44zQMdliZcve4QG/04AUi9Q==
dependencies:
node-fetch "2.3.0"
whatwg-fetch "3.0.0"

cross-spawn@^5.0.1, cross-spawn@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
Expand Down Expand Up @@ -2290,7 +2312,7 @@ forever-agent@~0.6.1:
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=

form-data@~2.3.2:
form-data@^2.3.3, form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
integrity sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==
Expand All @@ -2299,6 +2321,11 @@ form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"

formdata-polyfill@^3.0.12:
version "3.0.13"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-3.0.13.tgz#991321c83b7a760aaf3788f1b7bbbf77e85b09bb"
integrity sha512-aYrFSz9wxtBc9E1yCfD5IFhTK9FDZVRM53kUQn1W6Et5+R9c4tpa5Mg9hQIBIc2er3fLx0ElbKujipu+2551vQ==

forwarded@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
Expand Down Expand Up @@ -4149,6 +4176,11 @@ negotiator@0.6.1:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"
integrity sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=

node-fetch@2.3.0, node-fetch@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==

node-fetch@^1.0.1:
version "1.7.3"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
Expand All @@ -4157,11 +4189,6 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"

node-fetch@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -5946,7 +5973,7 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3:
dependencies:
iconv-lite "0.4.24"

whatwg-fetch@>=0.10.0:
whatwg-fetch@3.0.0, whatwg-fetch@>=0.10.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"
integrity sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==
Expand Down

0 comments on commit 58f81ed

Please sign in to comment.