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

Commit

Permalink
feat: add progress bar tests (#155)
Browse files Browse the repository at this point in the history
* feat: add progress bar tests

* feat: fixing progress bar tests

* feat: documenting add options
  • Loading branch information
dryajov authored and daviddias committed Oct 18, 2017
1 parent 842a544 commit fad3fa2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion API/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ Where `data` may be

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

`options` is an optional object argument containing the [DAG importer options](https://github.com/ipfs/js-ipfs-unixfs-engine#importer-api).
`options` is an optional object argument that might include the following keys:

- 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)
- progress (function): a function that will be called with the byte length of chunks as a file is added to ipfs.
- hashAlg || hash (string): multihash hashing algorithm to use

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

Expand Down
72 changes: 72 additions & 0 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ module.exports = (common) => {
})
})

it('BIG buffer with progress', (done) => {
const expectedMultihash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'

let progCount = 0
let progress = 0
const handler = (p) => {
progCount += 1
progress += p
}

ipfs.files.add(bigFile, {progress: handler}, (err, res) => {
expect(err).to.not.exist()
expect(res).to.have.length(1)
const file = res[0]
expect(file.hash).to.equal(expectedMultihash)
expect(file.path).to.equal(file.hash)
expect(progCount).to.equal(58)
expect(progress).to.equal(bigFile.byteLength)
done()
})
})

it('add a nested dir as array', (done) => {
// Needs https://github.com/ipfs/js-ipfs-api/issues/339 to be fixed
// for js-ipfs-api + go-ipfs
Expand Down Expand Up @@ -160,6 +182,56 @@ module.exports = (common) => {
})
})

it('add a nested dir as array with progress', (done) => {
// Needs https://github.com/ipfs/js-ipfs-api/issues/339 to be fixed
// for js-ipfs-api + go-ipfs
if (!isNode) { return done() }

const content = (name) => ({
path: `test-folder/${name}`,
content: directoryContent[name]
})

const emptyDir = (name) => ({
path: `test-folder/${name}`
})

const expectedRootMultihash = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'

const dirs = [
content('pp.txt'),
content('holmes.txt'),
content('jungle.txt'),
content('alice.txt'),
emptyDir('empty-folder'),
content('files/hello.txt'),
content('files/ipfs.txt'),
emptyDir('files/empty')
]

const total = dirs.reduce((i, entry) => {
return i + (entry.content ? entry.content.length : 0)
}, 0)

let progCount = 0
let progress = 0
const handler = (p) => {
progCount += 1
progress += p
}

ipfs.files.add(dirs, {progress: handler}, (err, res) => {
expect(err).to.not.exist()
const root = res[res.length - 1]

expect(root.path).to.equal('test-folder')
expect(root.hash).to.equal(expectedRootMultihash)
expect(progCount).to.equal(8)
expect(progress).to.equal(total)
done()
})
})

describe('.createAddStream', () => {
it('stream of valid files and dirs', (done) => {
const content = (name) => ({
Expand Down

0 comments on commit fad3fa2

Please sign in to comment.