chai = {expect} = require 'chai'
{stub, spy} = require 'sinon'
chai.use require 'sinon-chai'
describe 'mixco.script', ->
{isinstance} = require 'heterarchy'
mixco = require 'mixco'
{Script, register} = mixco.script
control = mixco.control
class TestScript extends Script
__registeredName: 'testscript'
describe 'Script', ->
script = null
beforeEach ->
script = new TestScript
it 'configures controller id to be de script name', ->
expect(script.config())
.to.match /<controller id=\"testscript\">[^$]*<\/controller>/
it 'can generate configuration with partial metadata', ->
delete script.info.wiki
expect(script.config())
.not.to.contain "undefined"
describe 'register', ->
testModule = null
beforeEach ->
testModule =
exports: {}
filename: 'testscript.mixco.js'
it 'registers a class in the given NodeJs module', ->
register testModule, TestScript
expect(isinstance testModule.exports.testscript, TestScript)
.to.be.true
it 'can generate a script type from a definition', ->
spier = stub
constructor: spy()
preinit: ->
init: ->
shutdown: ->
postshutdown: ->
register testModule,
constructor: -> spier.constructor()
preinit: ->
spier.preinit()
expect(@_isInit).not.to.exist
init: -> spier.init()
postshutdown: ->
spier.postshutdown()
expect(@_isInit).not.to.exist
shutdown: -> spier.shutdown()
info: author: 'Jimmy Jazz'
script = testModule.exports.testscript
expect(script.name).to.be.eq 'testscript'
expect(script.info.author).to.be.eq 'Jimmy Jazz'
expect(spier.constructor).to.have.been.called
script.init()
expect(spier.preinit).to.have.been.called
expect(spier.init).to.have.been.called
script.shutdown()
expect(spier.shutdown).to.have.been.called
expect(spier.postshutdown).to.have.been.called
it 'controls created during construction are registered autoamtically', ->
expectedControls = []
register testModule,
constructor: ->
expectedControls.push control.input()
expectedControls.push control.control()
expect(expectedControls.length)
.to.be.eq 2
expect(testModule.exports.testscript.controls)
.to.eql expectedControls