Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 684b2a1

Browse files
author
Alan Shaw
committed
test: add tests for lib fns
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent d84716a commit 684b2a1

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

test/lib.configure.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
const Multiaddr = require('multiaddr')
9+
10+
const configure = require('../src/lib/configure')
11+
12+
describe('lib/configure', () => {
13+
it('should accept no config', () => {
14+
configure(config => {
15+
expect(config.apiAddr).to.eql('http://localhost:5001')
16+
})()
17+
})
18+
19+
it('should accept string multiaddr', () => {
20+
const input = '/ip4/127.0.0.1/tcp/5001'
21+
configure(config => {
22+
expect(config.apiAddr).to.eql('http://127.0.0.1:5001')
23+
})(input)
24+
})
25+
26+
it('should accept multiaddr instance', () => {
27+
const input = Multiaddr('/ip4/127.0.0.1')
28+
configure(config => {
29+
expect(config.apiAddr).to.eql('http://127.0.0.1')
30+
})(input)
31+
})
32+
33+
it('should accept object with protocol, host and port', () => {
34+
const input = { protocol: 'https', host: 'ipfs.io', port: 138 }
35+
configure(config => {
36+
expect(config.apiAddr).to.eql('https://ipfs.io:138')
37+
})(input)
38+
})
39+
40+
it('should accept object with protocol only', () => {
41+
const input = { protocol: 'https' }
42+
configure(config => {
43+
expect(config.apiAddr).to.eql('https://localhost')
44+
})(input)
45+
})
46+
47+
it('should accept object with host only', () => {
48+
const input = { host: 'ipfs.io' }
49+
configure(config => {
50+
expect(config.apiAddr).to.eql('http://ipfs.io')
51+
})(input)
52+
})
53+
54+
it('should accept object with port only', () => {
55+
const input = { port: 138 }
56+
configure(config => {
57+
expect(config.apiAddr).to.eql('http://localhost:138')
58+
})(input)
59+
})
60+
})

test/lib.fetch.spec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
const throwsAsync = require('./utils/throws-async')
9+
10+
const { ok, toIterable } = require('../src/lib/fetch')
11+
12+
describe('lib/fetch', () => {
13+
describe('ok', () => {
14+
it('should parse json error response', async () => {
15+
const res = {
16+
ok: false,
17+
text: () => Promise.resolve(JSON.stringify({
18+
Message: 'boom',
19+
Code: 0,
20+
Type: 'error'
21+
})),
22+
status: 500
23+
}
24+
25+
const err = await throwsAsync(ok(res))
26+
27+
expect(err.message).to.eql('boom')
28+
expect(err.status).to.eql(500)
29+
})
30+
31+
it('should gracefully fail on parse json', async () => {
32+
const res = {
33+
ok: false,
34+
text: () => 'boom', // not valid json!
35+
status: 500
36+
}
37+
38+
const err = await throwsAsync(ok(res))
39+
40+
expect(err.message).to.eql('boom')
41+
expect(err.status).to.eql(500)
42+
})
43+
44+
it('should gracefully fail on read text', async () => {
45+
const res = {
46+
ok: false,
47+
text: () => Promise.reject(new Error('boom')),
48+
status: 500
49+
}
50+
51+
const err = await throwsAsync(ok(res))
52+
53+
expect(err.message).to.eql('unexpected status 500')
54+
expect(err.status).to.eql(500)
55+
})
56+
})
57+
58+
describe('toIterable', () => {
59+
it('should return input if already async iterable', () => {
60+
const input = { [Symbol.asyncIterator] () { return this } }
61+
expect(toIterable(input)).to.equal(input)
62+
})
63+
64+
it('should convert reader to async iterable', async () => {
65+
const inputData = [2, 31, 3, 4]
66+
const input = {
67+
getReader () {
68+
let i = 0
69+
return {
70+
read: async () => {
71+
return i === inputData.length
72+
? { done: true }
73+
: { value: inputData[i++] }
74+
},
75+
releaseLock: () => {}
76+
}
77+
}
78+
}
79+
80+
const chunks = []
81+
for await (const chunk of toIterable(input)) {
82+
chunks.push(chunk)
83+
}
84+
85+
expect(chunks).to.eql(inputData)
86+
})
87+
88+
it('should throw on unknown stream', () => {
89+
expect(() => toIterable({})).to.throw('unknown stream')
90+
})
91+
})
92+
})

test/utils/throws-async.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = async fnOrPromise => {
2+
try {
3+
await (fnOrPromise.then ? fnOrPromise : fnOrPromise())
4+
} catch (err) {
5+
return err
6+
}
7+
throw new Error('did not throw')
8+
}

0 commit comments

Comments
 (0)