var path = require("path")
  , expect = require("expect.js")
  , fs = require("fs")
  , Backbone = require("backbone")
  , Model = Backbone.Model
  , Collection = Backbone.Collection
  , ed = require("../lib/end-dash")

script(path.join(__dirname, "..", "lib", "end-dash.js"), { module: true })
script(path.join(__dirname, "..", "lib", "collection.js"), { module: true })
script(path.join(__dirname, "..", "lib", "parser.js"), { module: true })
script(path.join(__dirname, "..", "lib", "util.js"), { module: true })
 
describe("When I initialize a template with a view bound to it", function() {
  it("it should initialize the view correctly", function(done) {
    var model = new Model({ ohHi: "Hello There" })
      , TemplateGenerator = window.require("/lib/end-dash")
      , $ = window.$
      , Template
      , template


    function MockView(opts) {
      expect(this).to.be.a(MockView)
      expect(opts.model).to.be(model)
      expect(opts.el.is(".thing-")).to.be(true)
      expect(opts.parent).to.be(null)
      //next tick because we have to allow the template constructor to return
      //in order to check that it passed itself in
      process.nextTick(function() {
        expect(opts.template).to.be(template)
        done()
      })
    }

    TemplateGenerator.registerView("TestView", MockView)

    Template = new TemplateGenerator('<div class = "thing- testView-"></div>').generate()
    template = new Template(model)
  })

   it("should setup the view heirarchy correctly", function(done) {
    var model = new Model({ ohHi: "Hello There" })
      , TemplateGenerator = window.require("/lib/end-dash")
      , $ = window.$
      , Template
      , template

    var parentInstance
    function Parent() {
      parentInstance = this
    }

    function MockView(opts) {
      expect(this).to.be.a(MockView)
      expect(opts.model).to.be(model)
      expect(opts.el.is(".thing-")).to.be(true)
      expect(opts.parent).to.be.a(Parent)
      expect(opts.parent).to.be(parentInstance)
      //next tick because we have to allow the template constructor to return
      //in order to check that it passed itself in
      process.nextTick(function() {
        expect(opts.template).to.be(template)
        done()
      })
    }

    TemplateGenerator.registerView("EmbeddedTestView", MockView)
    TemplateGenerator.registerView("TestView", Parent)

    Template = new TemplateGenerator('<div class = "testView-"><div class = "thing- embeddedTestView-"></div></div>').generate()
    template = new Template(model)
  })
  it("should setup a view for collections", function(done) {
    var model = { things: new Collection([]) }
      , TemplateGenerator = window.require("/lib/end-dash")
      , $ = window.$
      , Template
      , template

    var parentInstance
    function Parent() {
      parentInstance = this
    }

    function MockView(opts) {
      expect(this).to.be.a(MockView)
      expect(opts.collection).to.be(model.things)
      expect(opts.el.is(".things-")).to.be(true)
      expect(opts.parent).to.be.a(Parent)
      expect(opts.parent).to.be(parentInstance)
      //next tick because we have to allow the template constructor to return
      //in order to check that it passed itself in
      process.nextTick(function() {
        expect(opts.template).to.be(template.collections["things"])
        done()
      })
    }

    TemplateGenerator.registerView("TestCollectionView", MockView)
    TemplateGenerator.registerView("TestView", Parent)

    Template = new TemplateGenerator('<div class = "testView-"><ul class = "things- testCollectionView-"><li class = "thing-"></li></ul></div>').generate()
    template = new Template(model)
  })
})

@@ -3681,11 +3681,1141 @@
 l)%0A  %7D)%0A
+  it(%22should bind a view to submodels%22, function(done) %7B%0A    var model = %7B thing: %7B value: %22derp%22 %7D %7D%0A      , TemplateGenerator = window.require(%22/lib/end-dash%22)%0A      , $ = window.$%0A      , Template%0A      , template%0A%0A    var parentInstance%0A    function Parent() %7B%0A      parentInstance = this%0A    %7D%0A%0A    function MockView(opts) %7B%0A      expect(this).to.be.a(MockView)%0A      expect(opts.model).to.be(model.thing)%0A      expect(opts.el.is(%22.thing-%22)).to.be(true)%0A      expect(opts.parent).to.be.a(Parent)%0A      expect(opts.parent).to.be(parentInstance)%0A      //next tick because we have to allow the template constructor to return%0A      //in order to check that it passed itself in%0A      process.nextTick(function() %7B%0A        expect(opts.template).to.be(template.models%5B%22thing%22%5D)%0A        done()%0A      %7D)%0A    %7D%0A%0A    TemplateGenerator.registerView(%22TestCollectionView%22, MockView)%0A    TemplateGenerator.registerView(%22TestView%22, Parent)%0A%0A    Template = new TemplateGenerator('%3Cdiv class = %22testView-%22%3E%3Cdiv class = %22thing- testCollectionView-%22%3E%3Cdiv class = %22value-%22%3E%3C/div%3E%3C/div%3E%3C/div%3E').generate()%0A    template = new Template(model)%0A  %7D) %0A
 %7D)%0A

test to ensure that views with models get correctly bound