Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 35ded32

Browse files
committed
chore: adapt http client test suite for React Native (iOS and Android)
1 parent 04ea765 commit 35ded32

File tree

7 files changed

+72
-9
lines changed

7 files changed

+72
-9
lines changed

packages/ipfs-http-client/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"test:electron-renderer": "aegir test -t electron-renderer",
4545
"test:chrome": "aegir test -t browser -t webworker -- --browsers ChromeHeadless",
4646
"test:firefox": "aegir test -t browser -t webworker -- --browsers FirefoxHeadless",
47+
"test:react-native:android": "aegir test -t react-native-android",
48+
"test:react-native:ios": "aegir test -t react-native-ios",
4749
"lint": "aegir lint",
4850
"prepare": "aegir build --no-bundle",
4951
"coverage": "npx nyc -r html npm run test:node -- --bail",
@@ -76,6 +78,7 @@
7678
"nanoid": "^3.1.12",
7779
"native-abort-controller": "^1.0.3",
7880
"parse-duration": "^0.4.4",
81+
"react-native-fetch-api": "^1.0.2",
7982
"stream-to-it": "^0.2.2",
8083
"uint8arrays": "^2.0.5"
8184
},
@@ -89,7 +92,11 @@
8992
"it-concat": "^1.0.1",
9093
"it-first": "^1.0.4",
9194
"nock": "^13.0.2",
92-
"rimraf": "^3.0.2"
95+
"react-native-polyfill-globals": "^3.0.0",
96+
"react-native-test-runner": "^3.0.2",
97+
"rimraf": "^3.0.2",
98+
"text-encoding": "^0.7.0",
99+
"typescript": "4.0.x"
93100
},
94101
"engines": {
95102
"node": ">=10.3.0",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
5+
module.exports = {
6+
require: path.join(__dirname, 'rn-test.require.js'),
7+
runner: 'mocha',
8+
nativeModules: [
9+
'react-native-get-random-values',
10+
'react-native-url-polyfill',
11+
'web-streams-polyfill'
12+
],
13+
patches: [{
14+
path: require.resolve('react-native-polyfill-globals/patches/react-native+0.63.3.patch')
15+
}, {
16+
path: require.resolve('react-native-polyfill-globals/patches/react-native-url-polyfill+1.2.0.patch'),
17+
cwd: path.join(__dirname, '../..')
18+
}]
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
require('react-native-get-random-values')
4+
const { polyfill: polyfillReadableStream } = require('react-native-polyfill-globals/src/readable-stream')
5+
const { polyfill: polyfillEncoding } = require('react-native-polyfill-globals/src/encoding')
6+
const { polyfill: polyfillURL } = require('react-native-polyfill-globals/src/url')
7+
8+
polyfillReadableStream()
9+
polyfillEncoding()
10+
polyfillURL()

packages/ipfs-http-client/test/constructor.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ const f = require('./utils/factory')()
77
const ipfsClient = require('../src/index.js')
88
const { isBrowser } = require('ipfs-utils/src/env')
99

10+
const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
11+
1012
describe('ipfs-http-client constructor tests', () => {
1113
describe('parameter permuations', () => {
1214
it('none', () => {
1315
const ipfs = ipfsClient()
14-
if (typeof self !== 'undefined') {
16+
if (typeof self !== 'undefined' && !isReactNative) {
1517
const { hostname, port } = self.location
1618
expectConfig(ipfs, { host: hostname, port })
1719
} else {

packages/ipfs-http-client/test/dag.spec.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const CID = require('cids')
1111
const f = require('./utils/factory')()
1212
const ipfsHttpClient = require('../src')
1313

14+
const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
15+
1416
let ipfs
1517

1618
describe('.dag', function () {
@@ -21,7 +23,12 @@ describe('.dag', function () {
2123

2224
after(() => f.clean())
2325

24-
it('should be able to put and get a DAG node with format dag-pb', async () => {
26+
it('should be able to put and get a DAG node with format dag-pb', async function () {
27+
if (isReactNative) {
28+
// React Native does not support constructing Blobs out of arrays
29+
return this.skip()
30+
}
31+
2532
const data = uint8ArrayFromString('some data')
2633
const node = new DAGNode(data)
2734

@@ -36,7 +43,12 @@ describe('.dag', function () {
3643
expect(result.value.Data).to.deep.equal(data)
3744
})
3845

39-
it('should be able to put and get a DAG node with format dag-cbor', async () => {
46+
it('should be able to put and get a DAG node with format dag-cbor', async function () {
47+
if (isReactNative) {
48+
// React Native does not support constructing Blobs out of arrays
49+
return this.skip()
50+
}
51+
4052
const cbor = { foo: 'dag-cbor-bar' }
4153
let cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
4254

@@ -50,7 +62,9 @@ describe('.dag', function () {
5062
})
5163

5264
it('should be able to put and get a DAG node with format raw', async () => {
53-
const node = uint8ArrayFromString('some data')
65+
const textData = 'some data'
66+
const rawData = uint8ArrayFromString(textData)
67+
const node = isReactNative ? textData : rawData
5468
let cid = await ipfs.dag.put(node, { format: 'raw', hashAlg: 'sha2-256' })
5569

5670
expect(cid.codec).to.equal('raw')
@@ -59,7 +73,7 @@ describe('.dag', function () {
5973

6074
const result = await ipfs.dag.get(cid)
6175

62-
expect(result.value).to.deep.equal(node)
76+
expect(result.value).to.deep.equal(rawData)
6377
})
6478

6579
it('should error when missing DAG resolver for multicodec from requested CID', async () => {
@@ -100,7 +114,12 @@ describe('.dag', function () {
100114
expect(askedToLoadFormat).to.be.true()
101115
})
102116

103-
it('should allow formats to be specified without overwriting others', async () => {
117+
it('should allow formats to be specified without overwriting others', async function () {
118+
if (isReactNative) {
119+
// React Native does not support constructing Blobs out of arrays
120+
return this.skip()
121+
}
122+
104123
const ipfs2 = ipfsHttpClient({
105124
url: `http://${ipfs.apiHost}:${ipfs.apiPort}`,
106125
ipld: {

packages/ipfs-http-client/test/files.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
66
const { expect } = require('aegir/utils/chai')
77
const f = require('./utils/factory')()
88

9+
const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
10+
911
describe('.add', function () {
1012
this.timeout(20 * 1000)
1113

@@ -18,7 +20,8 @@ describe('.add', function () {
1820
after(() => f.clean())
1921

2022
it('should ignore metadata until https://github.com/ipfs/go-ipfs/issues/6920 is implemented', async () => {
21-
const data = uint8ArrayFromString('some data')
23+
const textData = 'some data'
24+
const data = isReactNative ? textData : uint8ArrayFromString(textData)
2225
const result = await ipfs.add(data, {
2326
mode: 0o600,
2427
mtime: {

packages/ipfs-http-client/test/log.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
77
const f = require('./utils/factory')()
88
const first = require('it-first')
99

10+
const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
11+
1012
describe('.log', function () {
1113
this.timeout(100 * 1000)
1214

@@ -21,7 +23,8 @@ describe('.log', function () {
2123
it('.log.tail', async () => {
2224
const i = setInterval(async () => {
2325
try {
24-
await ipfs.add(uint8ArrayFromString('just adding some data to generate logs'))
26+
const textData = 'just adding some data to generate logs'
27+
await ipfs.add(isReactNative ? textData : uint8ArrayFromString(textData))
2528
} catch (_) {
2629
// this can error if the test has finished and we're shutting down the node
2730
}

0 commit comments

Comments
 (0)