spur-common
Version:
Common node library that is implemented through the use of spur-ioc and bluebird promises.
84 lines (69 loc) • 1.99 kB
text/coffeescript
describe "Utils", ->
beforeEach ->
injector().inject (, , )=>
it "prop()", ->
ob = {k:"v"}
fn = .prop("k")
expect(fn(ob)).to.equal "v"
it "extendWith", ->
obToExtend = {a:"a"}
obToExtendWith = {b:"b"}
fn = .extendWith(obToExtendWith)
fn(obToExtend)
expect(obToExtend).to.deep.equal {
a:"a", b:"b"
}
it "capitalize()", ->
expect(.capitalize("foo"))
.to.equal "Foo"
it "identity()", ->
fn = .identity(2)
expect(fn()).to.equal 2
it "mapObject", ->
ob = {
one:1
two:2
}
timesTwo = (n)-> n+n
newOb = .mapObject(ob, timesTwo)
expect(newOb).not.to.equal ob
expect(newOb).to.deep.equal {
one:2
two:4
}
it "deepClone()", ->
ob = {name:"hi"}
ob2 = .deepClone(ob)
expect(ob).not.to.equal ob2
expect(ob).to.deep.equal ob2
it "promiseQueue()", (done)->
str = ""
delay = (ms, val)=> ()=>
.delay(ms)
.then ()-> str += val
.promiseQueue([
delay(5, 1)
delay(4, 2)
delay(3, 3)
delay(2, 4)
delay(1, 5)
]).done ()->
expect(str).to.equal "12345"
done()
it 'readFile()', ->
filePath = .join(__dirname, "../../", "fixtures/data/readFileTest.json")
.readFile(filePath)
.done (data)->
expect(data).to.equal('{\n "what": "test",\n "year": 2015\n}\n')
it 'readJsonFile()', ->
filePath = .join(__dirname, "../../", "fixtures/data/readFileTest.json")
.readJsonFile(filePath)
.done (data)->
expect(data.what).to.equal("test")
expect(data.year).to.equal(2015)
it 'readJsonFile() with JSON parse error', (done)->
filePath = .join(__dirname, "../../", "fixtures/data/readFileWithError.json")
.readJsonFile(filePath)
.catch (error)->
expect(error.message).to.contain("Error Parsing JSON")
done()