diff --git a/test/functional/lib/ipfs-import.test.js b/test/functional/lib/ipfs-import.test.js index 6dad1be3c..01e183dc4 100644 --- a/test/functional/lib/ipfs-import.test.js +++ b/test/functional/lib/ipfs-import.test.js @@ -1,7 +1,9 @@ 'use strict' import { expect } from 'chai' -import { describe, it } from 'mocha' -import { formatImportDirectory } from '../../../add-on/src/lib/ipfs-import.js' +import { describe, it, afterEach } from 'mocha' +import sinon from 'sinon' +import browser from 'sinon-chrome' +import { createIpfsImportHandler, formatImportDirectory } from '../../../add-on/src/lib/ipfs-import.js' describe('ipfs-import.js', function () { describe('formatImportDirectory', function () { @@ -30,4 +32,119 @@ describe('ipfs-import.js', function () { expect(formatImportDirectory(path)).to.equal('/ipfs-companion-imports/2017-11-05_120101/') }) }) + + describe('createIpfsImportHandler', function () { + const sandbox = sinon.createSandbox() + const getState = sandbox.stub() + const getIpfs = sandbox.stub() + const ipfsPathValidator = sandbox.stub() + const runtime = { + hasNativeProtocolHandler: false + } + const hasNativeProtocolHandlerStub = sandbox.stub(runtime, 'hasNativeProtocolHandler') + const copier = sandbox.stub() + + const ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime, copier) + + afterEach(function () { + sandbox.restore() + }) + + describe('openFilesAtWebUI', function () { + it('should open the webui with the correct path', async function () { + const state = { webuiRootUrl: 'http://localhost:5001' } + const mfsPath = '/my-directory' + const url = `${state.webuiRootUrl}#/files${mfsPath}` + getState.returns(state) + await ipfsImportHandler.openFilesAtWebUI(mfsPath) + sinon.assert.calledWith(browser.tabs.create, { url }) + }) + }) + + describe('openFilesAtGateway', function () { + it('should open the gateway with the correct path', async function () { + const ipfs = { + files: { + stat: () => Promise.resolve({ + cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa' + }) + } + } + const state = { pubGwURLString: 'https://ipfs.io' } + const mfsPath = '/my-directory' + const url = `${state.pubGwURLString}/ipfs/bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa` + getState.returns(state) + getIpfs.returns(ipfs) + await ipfsImportHandler.openFilesAtGateway(mfsPath) + sinon.assert.calledWith(browser.tabs.create, { url }) + }) + + it('should open the gateway with the correct path when native protocol handlers are set', async function () { + const ipfs = { + files: { + stat: () => Promise.resolve({ + cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa' + }) + } + } + getIpfs.returns(ipfs) + getState.returns({ pubGwURLString: 'https://ipfs.io' }) + hasNativeProtocolHandlerStub.value(true) + const mfsPath = '/my-directory' + const url = 'ipfs://bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa' + await ipfsImportHandler.openFilesAtGateway(mfsPath) + sinon.assert.calledWith(browser.tabs.create, { url }) + }) + }) + + describe('copyImportResultsToFiles', function () { + it('should copy files to the correct directory', async function () { + const cpSpy = sandbox.spy() + const mkdirSpy = sandbox.spy() + const ipfs = { + files: { + cp: cpSpy, + mkdir: mkdirSpy + } + } + const resultFiles = [ + { + cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa', + path: '/tmp/test-file1' + }, + { + cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqb', + path: '' + }, + { + cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqc', + path: '/tmp/test-file2' + } + ] + getIpfs.returns(ipfs) + await ipfsImportHandler.copyImportResultsToFiles(resultFiles, '/my-directory') + sinon.assert.calledWith(mkdirSpy, '/my-directory') + expect(cpSpy.getCalls()[0].args).to.deep.equal([ + '/ipfs/bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa', + '/my-directory/tmp/test-file1' + ]) + expect(cpSpy.getCalls()[1].args).to.deep.equal([ + '/ipfs/bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqc', + '/my-directory/tmp/test-file2' + ]) + }) + }) + + describe('copyShareLink', function () { + // TODO: implement + }) + + describe('preloadFilesAtPublicGateway', function () { + // TODO: implement + }) + + describe('filesCpImportCurrentTab', function () { + // TODO: implement + }) + }) })