smartfile/test/test.ts

239 lines
9.3 KiB
TypeScript

import 'typings-test'
import * as smartfile from '../dist/index'
import path = require('path')
import { expect } from 'smartchai'
import * as vinyl from 'vinyl'
describe('smartfile', function () {
describe('.fs', function () {
describe('.fileExistsSync', function () {
it('should return an accurate boolean', function () {
expect(smartfile.fs.fileExistsSync('./test/mytest.json')).to.be.true
expect(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false
})
})
describe('.fileExists', function () {
it('should return a working promise', function () {
expect(smartfile.fs.fileExists('./test/mytest.json')).to.be.a('promise')
expect(smartfile.fs.fileExists('./test/mytest.json')).to.be.fulfilled
expect(smartfile.fs.fileExists('./test/notthere.json')).to.not.be.fulfilled
})
})
describe('.listFoldersSync()', function () {
it('should get the file type from a string', function () {
expect(smartfile.fs.listFoldersSync('./test/')).to.deep.include('testfolder')
expect(smartfile.fs.listFoldersSync('./test/')).to.not.deep.include('notExistentFolder')
})
})
describe('.listFolders()', function () {
it('should get the file type from a string', function (done) {
smartfile.fs.listFolders('./test/')
.then(function (folderArrayArg) {
expect(folderArrayArg).to.deep.include('testfolder')
expect(folderArrayArg).to.not.deep.include('notExistentFolder')
done()
})
})
})
describe('.listFilesSync()', function () {
it('should get the file type from a string', function () {
expect(smartfile.fs.listFilesSync('./test/')).to.deep.include('mytest.json')
expect(smartfile.fs.listFilesSync('./test/')).to.not.deep.include('notExistentFile')
expect(smartfile.fs.listFilesSync('./test/', /mytest\.json/)).to.deep.include('mytest.json')
expect(smartfile.fs.listFilesSync('./test/', /mytests.json/)).to.not.deep.include('mytest.json')
})
})
describe('.listFiles()', function () {
it('should get the file type from a string', function (done) {
smartfile.fs.listFiles('./test/')
.then(function (folderArrayArg) {
expect(folderArrayArg).to.deep.include('mytest.json')
expect(folderArrayArg).to.not.deep.include('notExistentFile')
done()
})
})
})
describe('.listFileTree()', function () {
it('should get a file tree', function (done) {
smartfile.fs.listFileTree(path.resolve('./test/'), '**/*.txt')
.then(function (folderArrayArg) {
expect(folderArrayArg).to.deep.include('testfolder/testfile1.txt')
expect(folderArrayArg).to.not.deep.include('mytest.json')
done()
})
})
})
describe('toObjectFromFileTree', function () {
it('should read a file tree into an Object', function () {
smartfile.fs.fileTreeToObject(path.resolve('./test/'), '**/*.txt')
.then((fileArrayArg) => {
// expect(fileArrayArg[1]).to.be.instanceof(smartfile.Smartfile)
})
})
})
describe('.copy()', function () {
it('should copy a directory', function () {
smartfile.fs.copy('./test/testfolder/', './test/temp/')
})
it('should copy a file', function () {
smartfile.fs.copy('./test/mytest.yaml', './test/temp/')
})
it('should copy a file and rename it', function () {
smartfile.fs.copy('./test/mytest.yaml', './test/temp/mytestRenamed.yaml')
})
})
describe('.remove()', function () {
it('should remove an entire directory', function () {
})
it('smartfile.fs.remove -> should remove single files', function (done) {
smartfile.fs.remove('./test/temp/mytestRenamed.yaml')
.then(() => { done() })
})
it('smartfile.fs.removeSync -> should remove single files synchronouly', function () {
smartfile.fs.removeSync('./test/temp/testfile1.txt')
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
})
it('smartfile.fs.removeMany -> should remove and array of files', function (done) {
smartfile.fs.removeMany([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ]).then(() => {
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
done()
})
})
it('smartfile.fs.removeManySync -> should remove and array of single files synchronouly', function () {
smartfile.fs.removeManySync([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ])
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
})
})
})
describe('.interpreter', function () {
describe('.filetype()', function () {
it('should get the file type from a string', function () {
expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json')
})
})
})
describe('.fs', function () {
describe('.toObjectSync()', function () {
it('should read an ' + '.yaml' + ' file to an object', function () {
let testData = smartfile.fs.toObjectSync('./test/mytest.yaml')
expect(testData).have.property('key1', 'this works')
expect(testData).have.property('key2', 'this works too')
})
it('should state unknown file type for unknown file types', function () {
let testData = smartfile.fs.toObjectSync('./test/mytest.txt')
})
it('should read an ' + '.json' + ' file to an object', function () {
let testData = smartfile.fs.toObjectSync('./test/mytest.json')
expect(testData).have.property('key1', 'this works')
expect(testData).have.property('key2', 'this works too')
})
})
describe('.toStringSync()', function () {
it('should read a file to a string', function () {
expect(smartfile.fs.toStringSync('./test/mytest.txt'))
.to.equal('Some TestString &&%$')
})
})
describe('.toVinylSync', function () {
it('should read an ' + '.json OR .yaml' + ' file to an ' + 'vinyl file object', function () {
let testData = smartfile.fs.toVinylSync('./test/mytest.json')
expect(vinyl.isVinyl(testData)).to.be.true
})
})
})
describe('.memory', function () {
describe('.toGulpStream()', function () {
it('should produce a valid gulp stream', function () {
let localArray = [ 'test1', 'test2', 'test3' ]
smartfile.memory.toGulpStream(localArray)
})
})
describe('toVinylFileSync()', function () {
it('should produce a vinylFile', function () {
let localString = 'myString'
let localOptions = { filename: 'vinylfile2', base: '/someDir' }
expect(smartfile.memory.toVinylFileSync(localString, localOptions) instanceof vinyl).to.be.true
})
})
describe('toVinylArraySync()', function () {
it('should produce a an array of vinylfiles', function () {
let localStringArray = [ 'string1', 'string2', 'string3' ]
let localOptions = { filename: 'vinylfile2', base: '/someDir' }
let testResult = smartfile.memory.toVinylArraySync(localStringArray, localOptions)
expect(testResult).to.be.a('array')
expect(testResult.length === 3).to.be.true
for (let myKey in testResult) {
expect(testResult[ myKey ] instanceof vinyl).to.be.true
}
})
})
describe('vinylToStringSync()', function () {
it('should produce a String from vinyl file', function () {
let localString = smartfile.memory.vinylToStringSync(new vinyl({
base: '/',
path: '/test.txt',
contents: new Buffer('myString')
}))
expect(localString).equal('myString')
})
})
describe('toFs()', function () {
it('should write a file to disk and return a promise', function (done) {
let localString = 'myString'
smartfile.memory.toFs(
localString,
path.join(process.cwd(), './test/temp/testMemToFs.txt')
).then(done)
})
})
describe('toFsSync()', function () {
it('should write a file to disk and return true if successfull', function () {
let localString = 'myString'
smartfile.memory.toFsSync(
localString,
path.join(process.cwd(), './test/temp/testMemToFsSync.txt')
)
})
})
})
describe('.remote', function () {
describe('.toString()', function () {
it('should load a remote file to a variable', function (done) {
this.timeout(5000)
smartfile.remote.toString(
'https://raw.githubusercontent.com/pushrocks/smartfile/master/test/mytest.txt'
).then(function (responseString) {
expect(responseString).to.equal('Some TestString &&%$')
done()
})
})
it('should reject a Promise when the link is false', function (done) {
this.timeout(10000)
smartfile.remote.toString('https://push.rocks/doesnotexist.txt')
.then(
function () {
throw new Error('this test should not be resolved')
},
function () {
done()
}
)
})
})
})
})