       expect(buffer.subscriptionCount()).toBe 1
       expect(otherBuffer.subscriptionCount()).toBe 1
 
+  describe "when 'close' is triggered", ->
+    it "closes active edit session and loads next edit session", ->
+      editor.setBuffer(new Buffer())
+      spyOn(editor, "remove")
+      editor.trigger "close"
+      expect(editor.remove).not.toHaveBeenCalled()
+      expect(editor.buffer).toBe buffer
+
+    it "calls remove on the editor if there is one edit session and mini is false", ->
+      originalBuffer = editor.buffer
+      expect(editor.mini).toBeFalsy()
+      expect(editor.editSessions.length).toBe 1
+      spyOn(editor, 'remove')
+      editor.trigger 'close'
+      expect(editor.remove).toHaveBeenCalled()
+
+      editor.remove()
+      editor = new Editor(mini: true)
+      spyOn(editor, 'remove')
+      editor.trigger 'close'
+      expect(editor.remove).not.toHaveBeenCalled()
+
+    describe "when buffer is modified", ->
+      it "triggers alert and does not close session", ->
+        spyOn(editor, 'remove')
+        spyOn($native, 'alert')
+        editor.insertText("I AM CHANGED!")
+        editor.trigger "close"
+        expect(editor.remove).not.toHaveBeenCalled()
+        expect($native.alert).toHaveBeenCalled()
+
   describe ".setBuffer(buffer)", ->
     otherBuffer = null
 


        editor.scrollTop(0)
        expect(editor.lineElementForScreenRow(2)).toMatchSelector('.fold.selected')

  describe "editor-path-change event", ->
    it "emits event when buffer's path is changed", ->
      eventHandler = jasmine.createSpy('eventHandler')
      editor.on 'editor-path-change', eventHandler
      editor.buffer.setPath("moo.text")

    it "emits event when editor receives a new buffer", ->
      eventHandler = jasmine.createSpy('eventHandler')
      editor.on 'editor-path-change', eventHandler
      editor.setBuffer(new Buffer("something.txt"))
      expect(eventHandler).toHaveBeenCalled()

    it "stops listening to events on previously set buffers", ->
      eventHandler = jasmine.createSpy('eventHandler')
      oldBuffer = editor.buffer
      editor.on 'editor-path-change', eventHandler

      editor.setBuffer(new Buffer("something.txt"))
      expect(eventHandler).toHaveBeenCalled()

      eventHandler.reset()
      oldBuffer.setPath("bad.txt")
      expect(eventHandler).not.toHaveBeenCalled()

      eventHandler.reset()
      editor.buffer.setPath("new.txt")
      expect(eventHandler).toHaveBeenCalled()

  describe "split methods", ->
    describe "when inside a pane", ->
      fakePane = null
      beforeEach ->
        fakePane = { splitUp: jasmine.createSpy('splitUp').andReturn({}), remove: -> }
        spyOn(editor, 'pane').andReturn(fakePane)

      it "calls the corresponding split method on the containing pane with a copy of the editor", ->
        editor.splitUp()
        expect(fakePane.splitUp).toHaveBeenCalled()
        [editorCopy] = fakePane.splitUp.argsForCall[0]
        expect(editorCopy.serialize()).toEqual editor.serialize()
        expect(editorCopy).not.toBe editor
        editorCopy.remove()

    describe "when not inside a pane", ->
      it "does not split the editor, but doesn't throw an exception", ->
        editor.splitUp()
        editor.splitDown()
        editor.splitLeft()
        editor.splitRight()

  describe "when 'close' is triggered", ->
    it "closes active edit session and loads next edit session", ->
      editor.setBuffer(new Buffer())
      spyOn(editor, "remove")
      editor.trigger "close"
      expect(editor.remove).not.toHaveBeenCalled()
      expect(editor.buffer).toBe buffer

    it "calls remove on the editor if there is one edit session and mini is false", ->
      originalBuffer = editor.buffer
      expect(editor.mini).toBeFalsy()
      expect(editor.editSessions.length).toBe 1
      spyOn(editor, 'remove')
      editor.trigger 'close'
      expect(editor.remove).toHaveBeenCalled()

      editor.remove()
      editor = new Editor(mini: true)
      spyOn(editor, 'remove')
      editor.trigger 'close'
      expect(editor.remove).not.toHaveBeenCalled()

    describe "when buffer is modified", ->
      it "triggers alert and does not close session", ->
        spyOn(editor, 'remove')
        spyOn($native, 'alert')
        editor.insertText("I AM CHANGED!")
        editor.trigger "close"
        expect(editor.remove).not.toHaveBeenCalled()
        expect($native.alert).toHaveBeenCalled()

        editor.scrollTop(50)
        expect(editor.scrollTop()).toBe 50

  describe "split methods", ->
    describe "when inside a pane", ->
      fakePane = null
      beforeEach ->
        fakePane = { splitUp: jasmine.createSpy('splitUp').andReturn({}), remove: -> }
        spyOn(editor, 'pane').andReturn(fakePane)

      it "calls the corresponding split method on the containing pane with a copy of the editor", ->
        editor.splitUp()
        expect(fakePane.splitUp).toHaveBeenCalled()
        [editorCopy] = fakePane.splitUp.argsForCall[0]
        expect(editorCopy.serialize()).toEqual editor.serialize()
        expect(editorCopy).not.toBe editor
        editorCopy.remove()

    describe "when not inside a pane", ->
      it "does not split the editor, but doesn't throw an exception", ->
        editor.splitUp()
        editor.splitDown()
        editor.splitLeft()
        editor.splitRight()

  describe "editor-open event", ->
    it 'only triggers an editor-open event when it is first added to the DOM', ->
      openHandler = jasmine.createSpy('openHandler')
