 
   describe "when a directory is double-clicked", ->
     it "toggles the directory expansion state and does not change the focus to the editor", ->
-      sampleJs.trigger clickEvent(originalEvent: { detail: 1 })
-      subdir = treeView.root.find('.directory:first').view()
-      subdir.trigger clickEvent(originalEvent: { detail: 1 })
-      expect(subdir).toHaveClass 'selected'
-      expect(subdir).toHaveClass 'expanded'
-      subdir.trigger clickEvent(originalEvent: { detail: 2 })
-      expect(subdir).toHaveClass 'selected'
-      expect(subdir).not.toHaveClass 'expanded'
-      expect(rootView.getActiveView().isFocused).toBeFalsy()
+      subdir = null
+      waitsForFileToOpen ->
+        sampleJs.trigger clickEvent(originalEvent: { detail: 1 })
+
+      runs ->
+        subdir = treeView.root.find('.directory:first').view()
+        subdir.trigger clickEvent(originalEvent: { detail: 1 })
+        expect(subdir).toHaveClass 'selected'
+        expect(subdir).toHaveClass 'expanded'
+        subdir.trigger clickEvent(originalEvent: { detail: 2 })
+        expect(subdir).toHaveClass 'selected'
+        expect(subdir).not.toHaveClass 'expanded'
+        expect(rootView.getActiveView().isFocused).toBeFalsy()
 
   describe "when the active item changes on the active pane", ->
     describe "when the item has a path", ->
       it "selects the entry with that path in the tree view if it is visible", ->
-        sampleJs.click()
-        rootView.open(project.resolve('tree-view.txt'))
-
-        expect(sampleTxt).toHaveClass 'selected'
-        expect(treeView.find('.selected').length).toBe 1
+        waitsForFileToOpen ->
+          sampleJs.click()
+
+        runs ->
+          rootView.openSync(project.resolve('tree-view.txt'))
+          expect(sampleTxt).toHaveClass 'selected'
+          expect(treeView.find('.selected').length).toBe 1
 
       it "selects the path's parent dir if its entry is not visible", ->
-        rootView.open('dir1/sub-dir1/sub-file1')
+        rootView.openSync('dir1/sub-dir1/sub-file1')
         dirView = treeView.root.find('.directory:contains(dir1)').view()
         expect(dirView).toHaveClass 'selected'
 
     describe "when the item has no path", ->
       it "deselects the previously selected entry", ->
-        sampleJs.click()
-        rootView.getActivePane().showItem($$ -> @div('hello'))
-        expect(rootView.find('.selected')).not.toExist()
+        waitsForFileToOpen ->
+          sampleJs.click()
+
+        runs ->
+          rootView.getActivePane().showItem($$ -> @div('hello'))
+          expect(rootView.find('.selected')).not.toExist()
 
   describe "when a different editor becomes active", ->
     it "selects the file in that is open in that editor", ->
-      sampleJs.click()
-      leftEditor = rootView.getActiveView()
-      rightEditor = leftEditor.splitRight()
-      sampleTxt.click()
-
-      expect(sampleTxt).toHaveClass('selected')
-      leftEditor.focus()
-      expect(sampleJs).toHaveClass('selected')
+      leftEditor = null
+      rightEditor = null
+
+      waitsForFileToOpen ->
+        sampleJs.click()
+
+      runs ->
+        leftEditor = rootView.getActiveView()
+        rightEditor = leftEditor.splitRight()
+
+      waitsForFileToOpen ->
+        sampleTxt.click()
+
+      runs ->
+        expect(sampleTxt).toHaveClass('selected')
+        leftEditor.focus()
+        expect(sampleJs).toHaveClass('selected')
 
   describe "keyboard navigation", ->
     afterEach ->

TreeView = require '../lib/tree-view'
path = require 'path'
temp = require 'temp'

describe "TreeView", ->
  [treeView, sampleJs, sampleTxt] = []

        it "selects the entry after its parent directory", ->
          subdir1 = treeView.root.find('.directory:eq(1)').view()
          subdir1.expand()
          waitsForFileToOpen ->
            subdir1.entries.find('.entry:last').click()

          runs ->
            treeView.trigger 'core:move-down'
            expect(treeView.root.find('.entries > .entry:eq(2)')).toHaveClass 'selected'

      describe "when the last directory of another last directory is selected", ->
        [nested, nested2] = []
