 helpers = if typeof require is 'undefined' then window.viewHelpers else require './view_helper'
+getPs = (view) -> $('p', view.get('node')).map(-> @innerHTML).toArray()
 
 QUnit.module "Batman.View loop rendering"
 

  context = Batman({objects, currentSort: 'id'})

  helpers.render source, context, (node, view) ->
    names = ($('p', view.get('node')).map -> @innerHTML).toArray()
    deepEqual names, ['foo', 'bar']
    # multiple reordering all at once should not end up with duplicate DOM nodes
    context.set 'currentSort', 'name'
    delay =>
      ($('p', view.get('node')).map -> @innerHTML)
      context.set 'currentSort', 'id'
      delay =>
        ($('p', view.get('node')).map -> @innerHTML)
        context.set 'currentSort', 'name'
        delay =>
          names = ($('p', view.get('node')).map -> @innerHTML).toArray()
          deepEqual names, ['bar', 'foo']

asyncTest 'it should add items in order', ->
  source = '<p data-foreach-object="objects.sortedBy.id" class="present" data-bind="object.name"></p>'

  source = '<p data-foreach-object="objects" data-bind="object"></p><p data-foreach-object="objects" data-bind="object"></p>'
  objects = new Batman.Set()

  helpers.render source, false, {objects}, (node, view) ->
    equal $('p', node).length, 0
    objects.add 'foo', 'bar', 'baz'
    delay ->
      deepEqual getPs(view), ['foo', 'bar', 'baz', 'foo', 'bar', 'baz']
      objects.remove 'bar'
      delay ->
        deepEqual getPs(view), ['foo', 'baz', 'foo', 'baz']

asyncTest 'it should update the whole set of nodes if the collection changes', ->
  source = '<p data-foreach-object="objects" class="present" data-bind="object"></p>'
