 path = require 'path'
 fs = require 'fs-plus'
-{WorkspaceView} = require 'atom'
 temp = require 'temp'
 
 describe "Whitespace", ->
-  [editor, buffer] = []
+  [editor, buffer, workspaceElement] = []
 
   beforeEach ->
     directory = temp.mkdirSync()
     atom.project.setPath(directory)
-    atom.workspaceView = new WorkspaceView()
-    atom.workspace = atom.workspaceView.model
+    workspaceElement = atom.views.getView(atom.workspace)
     filePath = path.join(directory, 'atom-whitespace.txt')
     fs.writeFileSync(filePath, '')
     fs.writeFileSync(path.join(directory, 'sample.txt'), 'Some text.\n')

    it "removes all \t characters and replaces them with spaces using the configured tab length", ->
      editor.setTabLength(2)
      buffer.setText('\ta\n\t\nb\t\nc\t\td')
      atom.workspaceView.trigger 'whitespace:convert-tabs-to-spaces'
      expect(buffer.getText()).toBe "  a\n  \nb  \nc    d"

      editor.setTabLength(3)
      buffer.setText('\ta\n\t\nb\t\nc\t\td')
      atom.workspaceView.trigger 'whitespace:convert-tabs-to-spaces'
      expect(buffer.getText()).toBe "   a\n   \nb   \nc      d"

  describe "when the 'whitespace:convert-spaces-to-tabs' command is run", ->
    it "removes all space characters and replaces them with hard tabs", ->
      editor.setTabLength(2)
      buffer.setText("  a\n  \nb  \nc    d")
      atom.workspaceView.trigger 'whitespace:convert-spaces-to-tabs'
      expect(buffer.getText()).toBe '\ta\n\t\nb\t\nc\t\td'

      editor.setTabLength(3)
      buffer.setText("   a\n   \nb   \nc      d"
      atom.workspaceView.trigger 'whitespace:convert-spaces-to-tabs'
      expect(buffer.getText()).toBe '\ta\n\t\nb\t\nc\t\td')

      buffer.setText("foo   \nbar\t   \n\nbaz")

    it "removes the trailing whitespace in the active editor", ->
      atom.commands.dispatch(workspaceElement, 'whitespace:remove-trailing-whitespace')
      expect(buffer.getText()).toBe "foo\nbar\n\nbaz"

    it "does not attempt to remove whitespace when the package is deactivated", ->
