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

Commit e2cbcc0

Browse files
committed
chore: adapt http client test suite for React Native
1 parent fdc1608 commit e2cbcc0

File tree

7 files changed

+72
-9
lines changed

7 files changed

+72
-9
lines changed

packages/ipfs-http-client/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
"test:electron-renderer": "aegir test -t electron-renderer",
4444
"test:chrome": "aegir test -t browser -t webworker -- --browsers ChromeHeadless",
4545
"test:firefox": "aegir test -t browser -t webworker -- --browsers FirefoxHeadless",
46+
"test:react-native:android": "aegir test -t react-native-android",
47+
"test:react-native:ios": "aegir test -t react-native-ios",
4648
"lint": "aegir lint",
4749
"build": "npm run build:js && npm run build:types",
4850
"build:js": "aegir build",
@@ -76,6 +78,7 @@
7678
"nanoid": "^3.1.12",
7779
"native-abort-controller": "~0.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": "^1.1.0"
8184
},
@@ -88,8 +91,14 @@
8891
"it-all": "^1.0.4",
8992
"it-concat": "^1.0.1",
9093
"nock": "^13.0.2",
94+
"react-native-get-random-values": "^1.5.1",
95+
"react-native-polyfill-globals": "^3.0.0",
96+
"react-native-test-runner": "^2.3.0",
97+
"react-native-url-polyfill": "^1.2.0",
9198
"rimraf": "^3.0.2",
92-
"typescript": "4.0.x"
99+
"text-encoding": "^0.7.0",
100+
"typescript": "4.0.x",
101+
"web-streams-polyfill": "^3.0.1"
93102
},
94103
"engines": {
95104
"node": ">=10.3.0",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
],
11+
patches: [{
12+
path: require.resolve('react-native-polyfill-globals/patches/react-native+0.63.3.patch')
13+
}, {
14+
path: require.resolve('react-native-polyfill-globals/patches/react-native-url-polyfill+1.2.0.patch'),
15+
cwd: path.join(__dirname, '../..')
16+
}]
17+
}
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
@@ -8,11 +8,13 @@ const ipfsClient = require('../src/index.js')
88
const globalThis = require('ipfs-utils/src/globalthis')
99
const { isBrowser } = require('ipfs-utils/src/env')
1010

11+
const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
12+
1113
describe('ipfs-http-client constructor tests', () => {
1214
describe('parameter permuations', () => {
1315
it('none', () => {
1416
const ipfs = ipfsClient()
15-
if (typeof self !== 'undefined') {
17+
if (typeof self !== 'undefined' && !isReactNative) {
1618
const { hostname, port } = self.location
1719
expectConfig(ipfs, { host: hostname, port })
1820
} 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
@@ -6,6 +6,8 @@ const { expect } = require('aegir/utils/chai')
66
const uint8ArrayFromString = require('uint8arrays/from-string')
77
const f = require('./utils/factory')()
88

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

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

0 commit comments

Comments
 (0)