diff --git a/spec/autocomplete-spec.coffee b/spec/autocomplete-spec.coffee index 7d09c0f9..8e0e1c18 100644 --- a/spec/autocomplete-spec.coffee +++ b/spec/autocomplete-spec.coffee @@ -65,220 +65,238 @@ describe "Autocomplete", -> describe "AutocompleteView", -> [autocomplete, editorView, editor] = [] - beforeEach -> - atom.workspaceView = new WorkspaceView - editorView = new EditorView editor: atom.project.openSync("sample.js") - {editor} = editorView - autocomplete = new AutocompleteView editorView - - describe "on changed events", -> - it "should attach when finding suggestions", -> - editorView.attachToDom() - expect(editorView.find(".autocomplete-plus")).not.toExist() - - # Trigger an autocompletion - triggerAutocompletion editor - advanceClock completionDelay - expect(editorView.find(".autocomplete-plus")).toExist() - - # Check suggestions - suggestions = ["function", "if", "left", "shift"] - editorView.find(".autocomplete li span").each (index, item) -> - $item = $(item) - expect($item.text()).toEqual suggestions[index] - - it "should not attach when not finding suggestions", -> - editorView.attachToDom() - expect(editorView.find(".autocomplete-plus")).not.toExist() - - # Trigger an autocompletion - editor.moveCursorToBottom() - editor.insertText("x") - advanceClock completionDelay - expect(editorView.find(".autocomplete-plus")).not.toExist() + describe "when auto-activation is enabled", -> + beforeEach -> + atom.config.set "autocomplete-plus.enableAutoActivation", true + atom.workspaceView = new WorkspaceView + editorView = new EditorView editor: atom.project.openSync("sample.js") + {editor} = editorView + autocomplete = new AutocompleteView editorView - describe "accepting suggestions", -> - describe "when pressing enter while suggestions are visible", -> - it "inserts the word and moves the cursor to the end of the word", -> + describe "on changed events", -> + it "should attach when finding suggestions", -> editorView.attachToDom() expect(editorView.find(".autocomplete-plus")).not.toExist() # Trigger an autocompletion triggerAutocompletion editor advanceClock completionDelay + expect(editorView.find(".autocomplete-plus")).toExist() - # Accept suggestion - autocomplete.trigger "autocomplete-plus:confirm" - - # Check for result - expect(editor.getBuffer().getLastLine()).toEqual "function" - - # Check for cursor position - bufferPosition = editor.getCursorBufferPosition() - expect(bufferPosition.row).toEqual 13 - expect(bufferPosition.column).toEqual 8 + # Check suggestions + suggestions = ["function", "if", "left", "shift"] + editorView.find(".autocomplete li span").each (index, item) -> + $item = $(item) + expect($item.text()).toEqual suggestions[index] - it "hides the suggestions", -> + it "should not attach when not finding suggestions", -> editorView.attachToDom() expect(editorView.find(".autocomplete-plus")).not.toExist() # Trigger an autocompletion editor.moveCursorToBottom() - editor.moveCursorToBeginningOfLine() - editor.insertText("f") + editor.insertText("x") advanceClock completionDelay - expect(editorView.find(".autocomplete-plus")).toExist() + expect(editorView.find(".autocomplete-plus")).not.toExist() - # Accept suggestion - autocomplete.trigger "autocomplete-plus:confirm" + describe "accepting suggestions", -> + describe "when pressing enter while suggestions are visible", -> + it "inserts the word and moves the cursor to the end of the word", -> + editorView.attachToDom() + expect(editorView.find(".autocomplete-plus")).not.toExist() - expect(editorView.find(".autocomplete-plus")).not.toExist() + # Trigger an autocompletion + triggerAutocompletion editor + advanceClock completionDelay - describe "move-up event", -> - it "selects the previous item in the list", -> - editorView.attachToDom() + # Accept suggestion + autocomplete.trigger "autocomplete-plus:confirm" - # Trigger an autocompletion - triggerAutocompletion editor - advanceClock completionDelay + # Check for result + expect(editor.getBuffer().getLastLine()).toEqual "function" - expect(editorView.find(".autocomplete-plus li:eq(0)")).toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(1)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(3)")).not.toHaveClass("selected") + # Check for cursor position + bufferPosition = editor.getCursorBufferPosition() + expect(bufferPosition.row).toEqual 13 + expect(bufferPosition.column).toEqual 8 - # Accept suggestion - autocomplete.trigger "core:move-up" + it "hides the suggestions", -> + editorView.attachToDom() + expect(editorView.find(".autocomplete-plus")).not.toExist() - expect(editorView.find(".autocomplete-plus li:eq(0)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(1)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(3)")).toHaveClass("selected") + # Trigger an autocompletion + editor.moveCursorToBottom() + editor.moveCursorToBeginningOfLine() + editor.insertText("f") + advanceClock completionDelay + expect(editorView.find(".autocomplete-plus")).toExist() - describe "move-down event", -> - it "selects the next item in the list", -> - editorView.attachToDom() + # Accept suggestion + autocomplete.trigger "autocomplete-plus:confirm" - # Trigger an autocompletion - triggerAutocompletion editor - advanceClock completionDelay + expect(editorView.find(".autocomplete-plus")).not.toExist() - expect(editorView.find(".autocomplete-plus li:eq(0)")).toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(1)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(3)")).not.toHaveClass("selected") + describe "move-up event", -> + it "selects the previous item in the list", -> + editorView.attachToDom() - # Accept suggestion - autocomplete.trigger "core:move-down" + # Trigger an autocompletion + triggerAutocompletion editor + advanceClock completionDelay - expect(editorView.find(".autocomplete-plus li:eq(0)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(1)")).toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") - expect(editorView.find(".autocomplete-plus li:eq(3)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(0)")).toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(1)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(3)")).not.toHaveClass("selected") - describe "when a suggestion is clicked", -> - it "should select the item and confirm the selection", -> - editorView.attachToDom() + # Accept suggestion + autocomplete.trigger "core:move-up" - # Trigger an autocompletion - triggerAutocompletion editor - advanceClock completionDelay + expect(editorView.find(".autocomplete-plus li:eq(0)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(1)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(3)")).toHaveClass("selected") - # Get the second item - item = editorView.find(".autocomplete-plus li:eq(1)") + describe "move-down event", -> + it "selects the next item in the list", -> + editorView.attachToDom() - # Click the item, expect list to be hidden and - # text to be added - item.mousedown() - expect(item).toHaveClass "selected" - item.mouseup() + # Trigger an autocompletion + triggerAutocompletion editor + advanceClock completionDelay - expect(editorView.find(".autocomplete-plus")).not.toExist() - expect(editor.getBuffer().getLastLine()).toEqual item.text() + expect(editorView.find(".autocomplete-plus li:eq(0)")).toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(1)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(3)")).not.toHaveClass("selected") - describe "Positioning", -> - beforeEach -> - editorView.attachToDom() - setEditorHeightInLines editorView, 13 - editorView.resetDisplay() # Ensures the editor only has 13 lines visible + # Accept suggestion + autocomplete.trigger "core:move-down" - describe "when the autocomplete view fits below the cursor", -> - it "adds the autocomplete view to the editor below the cursor", -> - editor.setCursorBufferPosition [1, 2] - editor.insertText "f" - advanceClock completionDelay - expect(editorView.find(".autocomplete-plus")).toExist() + expect(editorView.find(".autocomplete-plus li:eq(0)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(1)")).toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(2)")).not.toHaveClass("selected") + expect(editorView.find(".autocomplete-plus li:eq(3)")).not.toHaveClass("selected") - # Check position - cursorPixelPosition = editorView.pixelPositionForScreenPosition editor.getCursorScreenPosition() - expect(autocomplete.position().top).toBe cursorPixelPosition.top + editorView.lineHeight - expect(autocomplete.position().left).toBe cursorPixelPosition.left + describe "when a suggestion is clicked", -> + it "should select the item and confirm the selection", -> + editorView.attachToDom() - describe "when the autocomplete view does not fit below the cursor", -> - it "adds the autocomplete view to the editor above the cursor", -> - # Trigger autocompletion - editor.setCursorScreenPosition [11, 0] - editor.insertText "t" + # Trigger an autocompletion + triggerAutocompletion editor advanceClock completionDelay - expect(editorView.find(".autocomplete-plus")).toExist() - # Check position - cursorPixelPosition = editorView.pixelPositionForScreenPosition editor.getCursorScreenPosition() - autocompleteBottom = autocomplete.position().top + autocomplete.outerHeight() - expect(autocompleteBottom).toBe cursorPixelPosition.top - expect(autocomplete.position().left).toBe cursorPixelPosition.left + # Get the second item + item = editorView.find(".autocomplete-plus li:eq(1)") - describe ".cancel()", -> - it "unbinds autocomplete event handlers for move-up and move-down", -> - triggerAutocompletion editor, false - autocomplete.cancel() + # Click the item, expect list to be hidden and + # text to be added + item.mousedown() + expect(item).toHaveClass "selected" + item.mouseup() - editorView.trigger "core:move-down" - expect(editor.getCursorBufferPosition().row).toBe 1 + expect(editorView.find(".autocomplete-plus")).not.toExist() + expect(editor.getBuffer().getLastLine()).toEqual item.text() - editorView.trigger "core:move-up" - expect(editor.getCursorBufferPosition().row).toBe 0 + describe "Positioning", -> + beforeEach -> + editorView.attachToDom() + setEditorHeightInLines editorView, 13 + editorView.resetDisplay() # Ensures the editor only has 13 lines visible + + describe "when the autocomplete view fits below the cursor", -> + it "adds the autocomplete view to the editor below the cursor", -> + editor.setCursorBufferPosition [1, 2] + editor.insertText "f" + advanceClock completionDelay + expect(editorView.find(".autocomplete-plus")).toExist() + + # Check position + cursorPixelPosition = editorView.pixelPositionForScreenPosition editor.getCursorScreenPosition() + expect(autocomplete.position().top).toBe cursorPixelPosition.top + editorView.lineHeight + expect(autocomplete.position().left).toBe cursorPixelPosition.left + + describe "when the autocomplete view does not fit below the cursor", -> + it "adds the autocomplete view to the editor above the cursor", -> + # Trigger autocompletion + editor.setCursorScreenPosition [11, 0] + editor.insertText "t" + advanceClock completionDelay + expect(editorView.find(".autocomplete-plus")).toExist() + + # Check position + cursorPixelPosition = editorView.pixelPositionForScreenPosition editor.getCursorScreenPosition() + autocompleteBottom = autocomplete.position().top + autocomplete.outerHeight() + expect(autocompleteBottom).toBe cursorPixelPosition.top + expect(autocomplete.position().left).toBe cursorPixelPosition.left + + describe ".cancel()", -> + it "unbinds autocomplete event handlers for move-up and move-down", -> + triggerAutocompletion editor, false + autocomplete.cancel() + + editorView.trigger "core:move-down" + expect(editor.getCursorBufferPosition().row).toBe 1 + + editorView.trigger "core:move-up" + expect(editor.getCursorBufferPosition().row).toBe 0 + + it "adds words to the wordlist after they have been written", -> + editorView.attachToDom() + editor.insertText "somethingNew" + editor.insertText " " - it "adds words to the wordlist after they have been written", -> - editorView.attachToDom() - editor.insertText "somethingNew" - editor.insertText " " + expect(autocomplete.wordList.indexOf("somethingNew")).not.toEqual(-1) - expect(autocomplete.wordList.indexOf("somethingNew")).not.toEqual(-1) + it "sets the width of the view to be wide enough to contain the longest completion without scrolling (+ 15 pixels)", -> + editorView.attachToDom() + editor.insertText('thisIsAReallyReallyReallyLongCompletion ') + editor.moveCursorToBottom() + editor.insertNewline() + editor.insertText "t" + advanceClock completionDelay + expect(autocomplete.list.prop('scrollWidth') + 15).toBe autocomplete.list.width() - it "sets the width of the view to be wide enough to contain the longest completion without scrolling (+ 15 pixels)", -> - editorView.attachToDom() - editor.insertText('thisIsAReallyReallyReallyLongCompletion ') - editor.moveCursorToBottom() - editor.insertNewline() - editor.insertText "t" - advanceClock completionDelay - expect(autocomplete.list.prop('scrollWidth') + 15).toBe autocomplete.list.width() + it "includes completions for the scope's completion preferences", -> + waitsForPromise -> + atom.packages.activatePackage('language-css') - it "includes completions for the scope's completion preferences", -> - waitsForPromise -> - atom.packages.activatePackage('language-css') + runs -> + cssEditorView = new EditorView(editor: atom.project.openSync('css.css')) + cssEditor = cssEditorView.editor + autocomplete = new AutocompleteView(cssEditorView) - runs -> - cssEditorView = new EditorView(editor: atom.project.openSync('css.css')) - cssEditor = cssEditorView.editor - autocomplete = new AutocompleteView(cssEditorView) + cssEditorView.attachToDom() + cssEditor.moveCursorToEndOfLine() + cssEditor.insertText "o" + cssEditor.insertText "u" + cssEditor.insertText "t" - cssEditorView.attachToDom() - cssEditor.moveCursorToEndOfLine() - cssEditor.insertText "o" - cssEditor.insertText "u" - cssEditor.insertText "t" + advanceClock completionDelay - advanceClock completionDelay + expect(cssEditorView.find(".autocomplete-plus")).toExist() - expect(cssEditorView.find(".autocomplete-plus")).toExist() + expect(autocomplete.list.find("li").length).toBe 10 + expect(autocomplete.list.find("li:eq(0)")).toHaveText "outline" + expect(autocomplete.list.find("li:eq(1)")).toHaveText "outline-color" + expect(autocomplete.list.find("li:eq(2)")).toHaveText "outline-width" + expect(autocomplete.list.find("li:eq(3)")).toHaveText "outline-style" - expect(autocomplete.list.find("li").length).toBe 10 - expect(autocomplete.list.find("li:eq(0)")).toHaveText "outline" - expect(autocomplete.list.find("li:eq(1)")).toHaveText "outline-color" - expect(autocomplete.list.find("li:eq(2)")).toHaveText "outline-width" - expect(autocomplete.list.find("li:eq(3)")).toHaveText "outline-style" + describe "Hotkey Activation", -> + beforeEach -> + atom.config.set "autocomplete-plus.enableAutoActivation", false + atom.workspaceView = new WorkspaceView + editorView = new EditorView editor: atom.project.openSync("sample.js") + {editor} = editorView + autocomplete = new AutocompleteView editorView + + it "does not show suggestions after a delay", -> + triggerAutocompletion editor + advanceClock completionDelay + expect(editorView.find(".autocomplete-plus")).not.toExist() + + editorView.trigger "autocomplete-plus:activate" + expect(editorView.find(".autocomplete-plus")).toExist() describe "File blacklist", -> beforeEach ->