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

Commit fad3fa2

Browse files
dryajovdaviddias
authored andcommitted
feat: add progress bar tests (#155)
* feat: add progress bar tests * feat: fixing progress bar tests * feat: documenting add options
1 parent 842a544 commit fad3fa2

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

API/files/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ Where `data` may be
2323

2424
If no `content` is passed, then the path is treated as an empty directory
2525

26-
`options` is an optional object argument containing the [DAG importer options](https://github.com/ipfs/js-ipfs-unixfs-engine#importer-api).
26+
`options` is an optional object argument that might include the following keys:
27+
28+
- cid-version (integer, default 0): the CID version to use when storing the data (storage keys are based on the CID, including it's version)
29+
- progress (function): a function that will be called with the byte length of chunks as a file is added to ipfs.
30+
- hashAlg || hash (string): multihash hashing algorithm to use
2731

2832
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` will be an array of:
2933

src/files.js

+72
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@ module.exports = (common) => {
123123
})
124124
})
125125

126+
it('BIG buffer with progress', (done) => {
127+
const expectedMultihash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
128+
129+
let progCount = 0
130+
let progress = 0
131+
const handler = (p) => {
132+
progCount += 1
133+
progress += p
134+
}
135+
136+
ipfs.files.add(bigFile, {progress: handler}, (err, res) => {
137+
expect(err).to.not.exist()
138+
expect(res).to.have.length(1)
139+
const file = res[0]
140+
expect(file.hash).to.equal(expectedMultihash)
141+
expect(file.path).to.equal(file.hash)
142+
expect(progCount).to.equal(58)
143+
expect(progress).to.equal(bigFile.byteLength)
144+
done()
145+
})
146+
})
147+
126148
it('add a nested dir as array', (done) => {
127149
// Needs https://github.com/ipfs/js-ipfs-api/issues/339 to be fixed
128150
// for js-ipfs-api + go-ipfs
@@ -160,6 +182,56 @@ module.exports = (common) => {
160182
})
161183
})
162184

185+
it('add a nested dir as array with progress', (done) => {
186+
// Needs https://github.com/ipfs/js-ipfs-api/issues/339 to be fixed
187+
// for js-ipfs-api + go-ipfs
188+
if (!isNode) { return done() }
189+
190+
const content = (name) => ({
191+
path: `test-folder/${name}`,
192+
content: directoryContent[name]
193+
})
194+
195+
const emptyDir = (name) => ({
196+
path: `test-folder/${name}`
197+
})
198+
199+
const expectedRootMultihash = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'
200+
201+
const dirs = [
202+
content('pp.txt'),
203+
content('holmes.txt'),
204+
content('jungle.txt'),
205+
content('alice.txt'),
206+
emptyDir('empty-folder'),
207+
content('files/hello.txt'),
208+
content('files/ipfs.txt'),
209+
emptyDir('files/empty')
210+
]
211+
212+
const total = dirs.reduce((i, entry) => {
213+
return i + (entry.content ? entry.content.length : 0)
214+
}, 0)
215+
216+
let progCount = 0
217+
let progress = 0
218+
const handler = (p) => {
219+
progCount += 1
220+
progress += p
221+
}
222+
223+
ipfs.files.add(dirs, {progress: handler}, (err, res) => {
224+
expect(err).to.not.exist()
225+
const root = res[res.length - 1]
226+
227+
expect(root.path).to.equal('test-folder')
228+
expect(root.hash).to.equal(expectedRootMultihash)
229+
expect(progCount).to.equal(8)
230+
expect(progress).to.equal(total)
231+
done()
232+
})
233+
})
234+
163235
describe('.createAddStream', () => {
164236
it('stream of valid files and dirs', (done) => {
165237
const content = (name) => ({

0 commit comments

Comments
 (0)