 asyncTest 'it should atomically reorder DOM nodes when the set is reordered', ->
   source = '<div><p data-foreach-object="objects.sortedBy[currentSort]" class="present" data-bind="object.name"></p></div>'
   objects = new Batman.Set({id: 1, name: 'foo'}, {id: 2, name: 'bar'})
-  context = Batman({objects, currentSort: 'id'})
+  context = {objects, currentSort: 'id'}
 
   helpers.render source, context, (node, view) ->
     deepEqual getPs(view), ['foo', 'bar']
     # multiple reordering all at once should not end up with duplicate DOM nodes
-    context.set 'currentSort', 'name'
-    delay =>
-      context.set 'currentSort', 'id'
+    view.set('currentSort', 'name')
+    delay =>
+      view.set('currentSort', 'id')
       delay =>
-        context.set 'currentSort', 'name'
+        view.set('currentSort', 'name')
         delay =>
           deepEqual getPs(view), ['bar', 'foo']
 

asyncTest 'previously observed collections shouldn\'t have any effect if they are replaced', ->
  source = '<p data-foreach-object="objects" class="present" data-bind="object"></p>'
  oldObjects = new Batman.Set('foo', 'bar', 'baz')
  context = new Batman.Object(objects: oldObjects)

  helpers.render source, false, context, (node, view) ->
    context.set('objects', new Batman.Set('qux', 'corge'))
    oldObjects.add('no effect')
    delay =>
      equal $('.present', node).length, 2


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>'
  context = {objects: new Batman.Set('foo', 'bar', 'baz')}

  Batman.developer.suppress()
  helpers.render source, false, context, (node, view) ->
    equal $('.present', node).length, 3
    view.set('objects', new Batman.Set('qux', 'corge'))
    delay =>
      equal $('.present', node).length, 2
      view.set('objects', null)
      delay =>
        equal $('.present', node).length, 0
        view.set('objects', new Batman.Set('mario'))
        delay 60, =>
          equal $('.present', node).length, 1
          Batman.developer.unsuppress()

asyncTest 'it should not fail if the collection is cleared', ->
  source = '<p data-foreach-object="objects" class="present" data-bind="object"></p>'
  context = {objects: new Batman.Set('foo', 'bar', 'baz')}

  helpers.render source, false, context, (node, view) ->
    equal $('.present', node).length, 3
    view.get('objects').clear()
    delay =>
      equal $('.present', node).length, 0

asyncTest 'it should not fail if the iterator is killed', 1, ->
  source = '<p data-foreach-object="objects" class="present" data-bind="object"></p>'
  context = {objects: new Batman.Set([0...100]...)}

  oldIterator = Batman.DOM.IteratorBinding
  instance = false
