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

Commit d75986a

Browse files
alanshawdaviddias
authored andcommitted
feat: adds pull stream tests for files.add
1 parent 940f049 commit d75986a

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

js/src/files.js

+66-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,26 @@ module.exports = (common) => {
156156
})
157157
})
158158

159-
it('a Readable Stream', (done) => {
159+
it('adds from readable stream', (done) => {
160+
const expectedCid = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
161+
162+
const rs = new Readable()
163+
rs.push(Buffer.from('some data'))
164+
rs.push(null)
165+
166+
ipfs.files.add(rs, (err, filesAdded) => {
167+
expect(err).to.not.exist()
168+
169+
expect(filesAdded).to.be.length(1)
170+
const file = filesAdded[0]
171+
expect(file.path).to.equal(expectedCid)
172+
expect(file.size).to.equal(17)
173+
expect(file.hash).to.equal(expectedCid)
174+
done()
175+
})
176+
})
177+
178+
it('adds from array of objects with readable stream content', (done) => {
160179
const expectedCid = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
161180

162181
const rs = new Readable()
@@ -177,6 +196,37 @@ module.exports = (common) => {
177196
})
178197
})
179198

199+
it('adds from pull stream (callback)', (done) => {
200+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
201+
202+
ipfs.files.add(pull.values([Buffer.from('test')]), (err, res) => {
203+
if (err) return done(err)
204+
expect(res).to.have.length(1)
205+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
206+
done()
207+
})
208+
})
209+
210+
it('adds from pull stream (promise)', () => {
211+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
212+
213+
return ipfs.files.add(pull.values([Buffer.from('test')]))
214+
.then((res) => {
215+
expect(res).to.have.length(1)
216+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
217+
})
218+
})
219+
220+
it('adds from array of objects with pull stream content', () => {
221+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
222+
223+
return ipfs.files.add([{ content: pull.values([Buffer.from('test')]) }])
224+
.then((res) => {
225+
expect(res).to.have.length(1)
226+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
227+
})
228+
})
229+
180230
it('add a nested directory as array of tupples', function (done) {
181231
// TODO: https://github.com/ipfs/js-ipfs-api/issues/339
182232
if (!isNode) { this.skip() }
@@ -367,6 +417,21 @@ module.exports = (common) => {
367417
})
368418
)
369419
})
420+
421+
it('adds with object chunks and pull stream content', (done) => {
422+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
423+
424+
pull(
425+
pull.values([{ content: pull.values([Buffer.from('test')]) }]),
426+
ipfs.files.addPullStream(),
427+
pull.collect((err, res) => {
428+
if (err) return done(err)
429+
expect(res).to.have.length(1)
430+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
431+
done()
432+
})
433+
)
434+
})
370435
})
371436

372437
describe('.cat', () => {

0 commit comments

Comments
 (0)