file-manifest
Version:
Require all the files in a directory into a single object
359 lines (316 loc) • 11.9 kB
text/coffeescript
sinon = require 'sinon'
describe 'file-manifest', ->
Given -> =
banana: sinon.stub()
Given -> .banana.withArgs('absolute', 'cb').returns 'loaded'
Given -> =
banana: sinon.stub()
Given -> .banana.withArgs('manifest', 'file', 'next').returns 'reduced'
Given -> = require('proxyquire').noCallThru() '../../lib/file-manifest',
'./loaders':
'./reducers':
describe '.generate', ->
afterEach -> .run.restore()
afterEach -> .load.restore()
afterEach -> .name.restore()
afterEach -> .reduce.restore()
afterEach -> .standardizePath.restore()
Given -> sinon.stub , 'run'
Given -> sinon.stub(, 'load').returns 'load'
Given -> sinon.stub(, 'name').returns 'name'
Given -> sinon.stub(, 'reduce').returns 'reduce'
Given -> sinon.stub(, 'standardizePath').returnsArg 0
context 'no options', ->
When -> .generate 'dir'
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: {}
load: 'load'
name: 'name'
reduce: 'reduce'
context 'callback but no options', ->
Given -> = sinon.stub()
When -> .generate 'dir',
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback:
memo: {}
load: 'load'
name: 'name'
reduce: 'reduce'
context 'with match as array', ->
When -> .generate 'dir',
match: ['blah']
Then -> .run.should.be.calledWith
dir: 'dir'
match: ['blah']
callback: undefined
memo: {}
load: 'load'
name: 'name'
reduce: 'reduce'
context 'with match as string', ->
When -> .generate 'dir',
match: 'blah'
Then -> .run.should.be.calledWith
dir: 'dir'
match: ['blah']
callback: undefined
memo: {}
load: 'load'
name: 'name'
reduce: 'reduce'
context 'with memo', ->
When -> .generate 'dir',
memo: []
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: []
load: 'load'
name: 'name'
reduce: 'reduce'
context 'with load as a function', ->
Given -> = sinon.stub()
When -> .generate 'dir',
load:
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: {}
load:
name: 'name'
reduce: 'reduce'
context 'with load as a string', ->
Given -> .load.withArgs('custom').returns 'custom'
When -> .generate 'dir',
load: 'custom'
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: {}
load: 'custom'
name: 'name'
reduce: 'reduce'
context 'with name as a function', ->
Given -> = sinon.stub()
When -> .generate 'dir',
name:
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: {}
load: 'load'
name:
reduce: 'reduce'
context 'with name as a string', ->
Given -> .name.withArgs('custom').returns 'custom'
When -> .generate 'dir',
name: 'custom'
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: {}
load: 'load'
name: 'custom'
reduce: 'reduce'
context 'with reduce as a function', ->
Given -> = sinon.stub()
When -> .generate 'dir',
reduce:
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: {}
load: 'load'
name: 'name'
reduce:
context 'with reduce as a string', ->
Given -> .reduce.withArgs('custom').returns 'custom'
When -> .generate 'dir',
reduce: 'custom'
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: {}
load: 'load'
name: 'name'
reduce: 'custom'
context 'with reduce as "list"', ->
Given -> .reduce.withArgs('list').returns 'custom'
When -> .generate 'dir',
reduce: 'list'
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: []
load: 'load'
name: 'name'
reduce: 'custom'
context 'with reduce as "objectList"', ->
Given -> .reduce.withArgs('objectList').returns 'custom'
When -> .generate 'dir',
reduce: 'objectList'
Then -> .run.should.be.calledWith
dir: 'dir'
match: undefined
callback: undefined
memo: []
load: 'load'
name: 'name'
reduce: 'custom'
describe '.generateSync', ->
afterEach -> .generate.restore()
Given -> sinon.stub , 'generate'
When -> .generateSync 'dir', 'options'
Then -> .generate.should.be.calledWith 'dir', 'options'
describe '.generatePromise', ->
afterEach -> .generate.restore()
Given -> sinon.stub , 'generate'
context 'no error', ->
Given -> .generate.withArgs('dir', 'options', sinon.match.func).callsArgWithAsync 2, null, 'manifest'
When (done) -> .generatePromise('dir', 'options').then () => done()
Then -> .should.equal 'manifest'
context 'with error', ->
Given -> .generate.withArgs('dir', 'options', sinon.match.func).callsArgWithAsync 2, 'err'
When (done) -> .generatePromise('dir', 'options').then (() => done()), (() => done())
Then -> .should.equal 'err'
describe '.generateEvent', ->
afterEach -> .generate.restore()
Given -> sinon.stub , 'generate'
context 'no error', ->
Given -> .generate.withArgs('dir', 'options', sinon.match.func).callsArgWithAsync 2, null, 'manifest'
When (done) -> .generateEvent('dir', 'options').on 'manifest', () => done()
Then -> .should.equal 'manifest'
context 'with error', ->
Given -> .generate.withArgs('dir', 'options', sinon.match.func).callsArgWithAsync 2, 'err'
When (done) -> .generateEvent('dir', 'options').on 'error', () => done()
Then -> .should.equal 'err'
describe '.standardizePath', ->
Given -> = require('stack-trace')
afterEach -> .get.restore()
Given -> sinon.stub , 'get'
context 'absolute path', ->
When -> = .standardizePath '/foo/bar'
Then -> .should.equal '/foo/bar'
context 'relative path', ->
Given -> .get.returns [
getFileName: -> '/foo/banana.js'
]
When -> = .standardizePath 'blah'
Then -> .should.equal '/foo/blah'
describe '.run', ->
Given -> = require('defiled')
Given -> = require('pedestrian')
afterEach -> .walk.restore()
Given -> sinon.stub , 'walk'
Given -> = sinon.stub()
context 'without a callback', ->
Given -> .returnsArg 0
Given -> .walk.withArgs('dir', '').returns ['/foo/bar', '/foo/baz']
When -> .run
dir: 'dir'
memo: {}
reduce:
Then ->
.should.be.calledWith {}, sinon.match.has('_file', '/foo/bar')
.should.be.calledWith {}, sinon.match.has('_file', '/foo/baz')
context 'with a callback', ->
Given -> .callsArgWith 2, null, {}
Given -> .walk.withArgs('dir', '', sinon.match.func).callsArgWith 2, null, ['/foo/bar', '/foo/baz']
When (done) -> .run
dir: 'dir'
memo: {}
callback: done
reduce:
Then ->
.should.be.calledWith {}, sinon.match.has('_file', '/foo/bar'), sinon.match.func
.should.be.calledWith {}, sinon.match.has('_file', '/foo/baz'), sinon.match.func
context 'with an error', ->
Given -> .walk.withArgs('dir', '', sinon.match.func).callsArgWith 2, 'error', null
When (done) -> .run
dir: 'dir'
memo: {}
callback: () => done()
reduce:
Then ->
.should.equal 'error'
.called.should.be.false()
describe '.reduce', ->
afterEach -> ._getDefaultReducer.restore()
Given -> sinon.stub , '_getDefaultReducer'
context 'reducer does not exist', ->
Given -> ._getDefaultReducer.withArgs('memo').returns 'banana'
Given -> =
memo: 'memo'
When -> = .reduce 'apple'
Then -> .call(, 'manifest', 'file', 'next').should.equal 'reduced'
context 'reducer exists', ->
Given -> =
memo: 'memo'
When -> = .reduce 'banana'
Then -> .call(, 'manifest', 'file', 'next').should.equal 'reduced'
describe '.name', ->
context 'transformer exists', ->
Given -> = sinon.stub()
Given -> .withArgs(transform: 'blah').returns 'transformed blah'
When -> = .name 'blah'
And -> =
relative:
transformers:
blah: true
Then -> .should.equal 'transformed blah'
context 'transformer does not exist', ->
Given -> = sinon.stub()
Given -> .withArgs(transform: 'camel').returns 'transformed camel'
When -> = .name 'blah'
And -> =
relative:
transformers: {}
Then -> .should.equal 'transformed camel'
describe '.load', ->
afterEach -> ._getDefaultLoader.restore()
Given -> sinon.stub , '_getDefaultLoader'
Given -> = sinon.stub()
Given -> .returns 'absolute'
context 'loader exists', ->
When -> = .load 'banana'
And -> = abs: , 'cb'
Then -> .should.equal 'loaded'
context 'loader does not exist', ->
Given -> ._getDefaultLoader.withArgs({ abs: }).returns 'banana'
When -> = .load 'apple'
And -> = abs: , 'cb'
Then -> .should.equal 'loaded'
describe '._getDefaultReduce', ->
context 'memo is array', ->
Then -> ._getDefaultReducer([]).should.equal 'list'
context 'memo is not array', ->
Then -> ._getDefaultReducer({}).should.equal 'flat'
describe '._getDefaultLoader', ->
context 'file is js', ->
Given -> =
ext: sinon.stub().returns '.js'
When -> = ._getDefaultLoader
Then -> .should.equal 'require'
context 'file is json', ->
Given -> =
ext: sinon.stub().returns '.json'
When -> = ._getDefaultLoader
Then -> .should.equal 'require'
context 'file is neither js nor json', ->
Given -> =
ext: sinon.stub().returns '.txt'
When -> = ._getDefaultLoader
Then -> .should.equal 'readFile'