Use async/await in FindView spec
ADD
spec/async-spec-helpers.js
+/** @babel */
+
+export function beforeEach (fn) {
+ global.beforeEach(function () {
+ const result = fn()
+ if (result instanceof Promise) {
+ waitsForPromise(() => result)
+ }
+ })
+}
+
+export function afterEach (fn) {
+ global.afterEach(function () {
+ const result = fn()
+ if (result instanceof Promise) {
+ waitsForPromise(() => result)
+ }
+ })
+}
+
+['it', 'fit', 'ffit', 'fffit'].forEach(function (name) {
+ module.exports[name] = function (description, fn) {
+ global[name](description, function () {
+ const result = fn()
+ if (result instanceof Promise) {
+ waitsForPromise(() => result)
+ }
+ })
+ }
+})
+
+export async function conditionPromise (condition) {
+ const startTime = Date.now()
+
+ while (true) {
+ await timeoutPromise(100)
+
+ if (await condition()) {
+ return
+ }
+
+ if (Date.now() - startTime > 5000) {
+ throw new Error("Timed out waiting on condition")
+ }
+ }
+}
+
+export function timeoutPromise (timeout) {
+ return new Promise(function (resolve) {
+ global.setTimeout(resolve, timeout)
+ })
+}
+
+function waitsForPromise (fn) {
+ const promise = fn()
+ global.waitsFor('spec promise to resolve', function (done) {
+ promise.then(done, function (error) {
+ jasmine.getEnv().currentSpec.fail(error)
+ done()
+ })
+ })
+}

MODIFY
spec/find-view-spec.js
spec/find-view-spec.js
+/** @babel */
+
const path = require("path");
+const {beforeEach, it, fit, ffit, fffit} = require('./async-spec-helpers')
describe("FindView", () => {
let workspaceElement, editorView, editor, findView, activationPromise;
@@ -19,182 +22,150 @@ describe("FindView", () => {
return result;
}
- beforeEach(() => {
+ beforeEach(async () => {
spyOn(atom, "beep");
workspaceElement = atom.views.getView(atom.workspace);
atom.project.setPaths([path.join(__dirname, "fixtures")]);
- waitsForPromise(() => atom.workspace.open("sample.js"));
+ await atom.workspace.open("sample.js");
- runs(() => {
jasmine.attachToDOM(workspaceElement);
editor = atom.workspace.getActiveTextEditor();
editorView = editor.getElement();
activationPromise = atom.packages.activatePackage("find-and-replace").then(function({mainModule}) {
mainModule.createViews();
- return {findView} = mainModule;
- });
+ ({findView} = mainModule);
});
});
describe("when find-and-replace:show is triggered", () => {
- it("attaches FindView to the root view", () => {
+ it("attaches FindView to the root view", async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
expect(workspaceElement.querySelector(".find-and-replace")).toBeDefined();
});
- });
- it("populates the findEditor with selection when there is a selection", () => {
+ it("populates the findEditor with selection when there is a selection", async () => {
editor.setSelectedBufferRange([[2, 8], [2, 13]]);
- atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- runs(() => {
expect(getFindAtomPanel()).toBeVisible();
expect(findView.findEditor.getText()).toBe("items");
+
findView.findEditor.setText("");
editor.setSelectedBufferRange([[2, 14], [2, 20]]);
atom.commands.dispatch(editorView, "find-and-replace:show");
expect(getFindAtomPanel()).toBeVisible();
expect(findView.findEditor.getText()).toBe("length");
});
- });
- it("does not change the findEditor text when there is no selection", () => {
+ it("does not change the findEditor text when there is no selection", async () => {
editor.setSelectedBufferRange([[2, 8], [2, 8]]);
- atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- runs(() => {
findView.findEditor.setText("kitten");
atom.commands.dispatch(editorView, "find-and-replace:show");
expect(findView.findEditor.getText()).toBe("kitten");
});
- });
- it("does not change the findEditor text when there is a multiline selection", () => {
+ it("does not change the findEditor text when there is a multiline selection", async () => {
editor.setSelectedBufferRange([[2, 8], [3, 12]]);
- atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- runs(() => {
expect(getFindAtomPanel()).toBeVisible();
expect(findView.findEditor.getText()).toBe("");
});
- });
- it("honors config settings for find options", () => {
+ it("honors config settings for find options", async () => {
atom.config.set("find-and-replace.useRegex", true);
atom.config.set("find-and-replace.caseSensitive", true);
atom.config.set("find-and-replace.inCurrentSelection", true);
- atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- runs(() => {
expect(findView.caseOptionButton).toHaveClass("selected");
expect(findView.regexOptionButton).toHaveClass("selected");
expect(findView.selectionOptionButton).toHaveClass("selected");
});
- });
- it("places selected text into the find editor and escapes it when Regex is enabled", () => {
+ it("places selected text into the find editor and escapes it when Regex is enabled", async () => {
atom.config.set("find-and-replace.useRegex", true);
editor.setSelectedBufferRange([[6, 6], [6, 65]]);
- atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- runs(() => {
expect(findView.findEditor.getText()).toBe(
"current < pivot \\? left\\.push\\(current\\) : right\\.push\\(current\\);"
);
});
});
- });
describe("when find-and-replace:toggle is triggered", () => {
- it("toggles the visibility of the FindView", () => {
+ it("toggles the visibility of the FindView", async () => {
atom.commands.dispatch(workspaceElement, "find-and-replace:toggle");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
expect(getFindAtomPanel()).toBeVisible();
atom.commands.dispatch(workspaceElement, "find-and-replace:toggle");
expect(getFindAtomPanel()).not.toBeVisible();
});
});
- });
- describe(
- "when the find-view is focused and window:focus-next-pane is triggered",
- () => {
- beforeEach(() => {
+ describe("when the find-view is focused and window:focus-next-pane is triggered", () => {
+ it("attaches FindView to the root view", async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
- });
-
- it("attaches FindView to the root view", () => {
expect(workspaceElement.querySelector(".find-and-replace")).toHaveFocus();
atom.commands.dispatch(findView.findEditor.element, "window:focus-next-pane");
expect(workspaceElement.querySelector(".find-and-replace")).not.toHaveFocus();
});
- }
- );
+ });
describe("find-and-replace:show-replace", () => {
- it("focuses the replace editor", () => {
+ it("focuses the replace editor", async () => {
atom.commands.dispatch(editorView, "find-and-replace:show-replace");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
expect(findView.replaceEditor).toHaveFocus();
});
- });
- it("places the current selection in the replace editor", () => {
+ it("places the current selection in the replace editor", async () => {
editor.setSelectedBufferRange([[0, 16], [0, 27]]);
- atom.commands.dispatch(editorView, "find-and-replace:show-replace");
- waitsForPromise(() => activationPromise);
+ atom.commands.dispatch(editorView, "find-and-replace:show-replace");
+ await activationPromise;
- runs(() => {
expect(findView.replaceEditor.getText()).toBe("function ()");
});
- });
- it("does not escape the text when the regex option is enabled", () => {
+ it("does not escape the text when the regex option is enabled", async () => {
editor.setSelectedBufferRange([[0, 16], [0, 27]]);
+
atom.commands.dispatch(editorView, "find-and-replace:show");
atom.commands.dispatch(editorView, "find-and-replace:toggle-regex-option");
atom.commands.dispatch(editorView, "find-and-replace:show-replace");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
expect(findView.replaceEditor.getText()).toBe("function ()");
});
});
- });
describe("when find-and-replace:clear-history is triggered", () => {
- it("clears the find and replace histories", () => {
+ it("clears the find and replace histories", async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
findView.findEditor.setText("items");
findView.replaceEditor.setText("cat");
findView.replaceAll();
@@ -204,24 +175,21 @@ describe("FindView", () => {
atom.commands.dispatch(editorView, "find-and-replace:clear-history");
atom.commands.dispatch(findView.findEditor.element, "core:move-up");
expect(findView.findEditor.getText()).toBe("");
+
atom.commands.dispatch(findView.replaceEditor.element, "core:move-up");
expect(findView.replaceEditor.getText()).toBe("");
});
});
- });
describe("core:cancel", () => {
- beforeEach(() => {
+ beforeEach(async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
findView.findEditor.setText("items");
atom.commands.dispatch(findView.findEditor.element, "core:confirm");
findView.focus();
});
- });
describe("when core:cancel is triggered on the find view", () => {
it("detaches from the workspace view", () => {
@@ -246,15 +214,12 @@ describe("FindView", () => {
});
describe("when core:cancel is triggered on an editor", () => {
- it("detaches from the workspace view", () => {
- waitsForPromise(() => atom.workspace.open());
-
- runs(() => {
+ it("detaches from the workspace view", async () => {
+ atom.workspace.open();
atom.commands.dispatch(editorView, "core:cancel");
expect(getFindAtomPanel()).not.toBeVisible();
});
});
- });
describe("when core:cancel is triggered on a mini editor", () => {
it("leaves the find view attached", () => {
@@ -273,12 +238,10 @@ describe("FindView", () => {
});
describe("serialization", () => {
- it("serializes find and replace history", () => {
+ it("serializes find and replace history", async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
findView.findEditor.setText("items");
findView.replaceEditor.setText("cat");
findView.replaceAll();
@@ -288,19 +251,15 @@ describe("FindView", () => {
findView.findEditor.setText("shift");
findView.replaceEditor.setText("ok");
findView.findNext(false);
- atom.packages.deactivatePackage("find-and-replace");
+ atom.packages.deactivatePackage("find-and-replace");
activationPromise = atom.packages.activatePackage("find-and-replace").then(function({mainModule}) {
mainModule.createViews();
- return {findView} = mainModule;
+ ({findView} = mainModule);
});
-
atom.commands.dispatch(editorView, "find-and-replace:show");
- });
-
- waitsForPromise(() => activationPromise);
+ await activationPromise;
- runs(() => {
atom.commands.dispatch(findView.findEditor.element, "core:move-up");
expect(findView.findEditor.getText()).toBe("shift");
atom.commands.dispatch(findView.findEditor.element, "core:move-up");
@@ -312,18 +271,16 @@ describe("FindView", () => {
atom.commands.dispatch(findView.replaceEditor.element, "core:move-up");
expect(findView.replaceEditor.getText()).toBe("cat");
});
- });
- it("serializes find options ", () => {
+ it("serializes find options ", async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
expect(findView.caseOptionButton).not.toHaveClass("selected");
expect(findView.regexOptionButton).not.toHaveClass("selected");
expect(findView.selectionOptionButton).not.toHaveClass("selected");
expect(findView.wholeWordOptionButton).not.toHaveClass("selected");
+
findView.caseOptionButton.click();
findView.regexOptionButton.click();
findView.selectionOptionButton.click();
@@ -332,46 +289,40 @@ describe("FindView", () => {
expect(findView.regexOptionButton).toHaveClass("selected");
expect(findView.selectionOptionButton).toHaveClass("selected");
expect(findView.wholeWordOptionButton).toHaveClass("selected");
- atom.packages.deactivatePackage("find-and-replace");
+ atom.packages.deactivatePackage("find-and-replace");
activationPromise = atom.packages.activatePackage("find-and-replace").then(function({mainModule}) {
mainModule.createViews();
- return {findView} = mainModule;
+ ({findView} = mainModule);
});
-
atom.commands.dispatch(editorView, "find-and-replace:show");
- });
-
- waitsForPromise(() => activationPromise);
+ await activationPromise;
- runs(() => {
expect(findView.caseOptionButton).toHaveClass("selected");
expect(findView.regexOptionButton).toHaveClass("selected");
expect(findView.selectionOptionButton).toHaveClass("selected");
expect(findView.wholeWordOptionButton).toHaveClass("selected");
});
});
- });
describe("finding", () => {
- beforeEach(() => {
+ beforeEach(async () => {
atom.config.set("find-and-replace.focusEditorAfterSearch", false);
editor.setCursorBufferPosition([2, 0]);
- atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- runs(() => {
findView.findEditor.setText("items");
atom.commands.dispatch(findView.findEditor.element, "core:confirm");
});
- });
describe("when find-and-replace:confirm is triggered", () => {
it("runs a search", () => {
findView.findEditor.setText("notinthefile");
atom.commands.dispatch(findView.findEditor.element, "find-and-replace:confirm");
expect(getResultDecorations(editor, "find-result")).toHaveLength(0);
+
findView.findEditor.setText("items");
atom.commands.dispatch(findView.findEditor.element, "find-and-replace:confirm");
expect(getResultDecorations(editor, "find-result")).toHaveLength(5);
@@ -852,40 +803,33 @@ describe("FindView", () => {
editor.setSelectedBufferRange([[0, 0], [0, 0]]);
});
- describe("when a new edit session is activated", () => {
- it("reruns the search on the new edit session", () => {
- waitsForPromise(() => atom.workspace.open("sample.coffee"));
-
- runs(() => {
+ describe("when a new editor is activated", () => {
+ it("reruns the search on the new editor", async () => {
+ await atom.workspace.open("sample.coffee");
editor = atom.workspace.getActivePaneItem();
expect(findView.resultCounter.text()).toEqual("7 found");
expect(editor.getSelectedBufferRange()).toEqual([[0, 0], [0, 0]]);
});
- });
- it("initially highlights the found text in the new edit session", () => {
+ it("initially highlights the found text in the new editor", async () => {
expect(getResultDecorations(editor, "find-result")).toHaveLength(6);
- waitsForPromise(() => atom.workspace.open("sample.coffee"));
-
- runs(() => {
+ await atom.workspace.open("sample.coffee");
expect(getResultDecorations(editor, "find-result")).toHaveLength(0);
+
const newEditor = atom.workspace.getActiveTextEditor();
expect(getResultDecorations(newEditor, "find-result")).toHaveLength(7);
});
- });
- it("highlights the found text in the new edit session when find next is triggered", () => {
- waitsForPromise(() => atom.workspace.open("sample.coffee"));
+ it("highlights the found text in the new editor when find next is triggered", async () => {
+ await atom.workspace.open("sample.coffee");
- runs(() => {
atom.commands.dispatch(findView.findEditor.element, "find-and-replace:find-next");
const newEditor = atom.workspace.getActiveTextEditor();
expect(getResultDecorations(newEditor, "find-result")).toHaveLength(6);
expect(getResultDecorations(newEditor, "current-result")).toHaveLength(1);
});
});
- });
describe("when all active pane items are closed", () => {
it("updates the result count", () => {
@@ -894,7 +838,7 @@ describe("FindView", () => {
});
});
- describe("when the active pane item is not an edit session", () => {
+ describe("when the active pane item is not an editor", () => {
let openerDisposable;
beforeEach(() => {
@@ -907,26 +851,20 @@ describe("FindView", () => {
openerDisposable.dispose();
});
- it("updates the result view", () => {
- waitsForPromise(() => atom.workspace.open("another"));
-
- runs(() => {
+ it("updates the result view", async () => {
+ await atom.workspace.open("another");
expect(findView.resultCounter.text()).toEqual("no results");
});
});
- });
- describe("when a new edit session is activated on a different pane", () => {
+ describe("when a new editor is activated on a different pane", () => {
it("initially highlights all the sample.js results", () => {
expect(getResultDecorations(editor, "find-result")).toHaveLength(6);
});
- it("reruns the search on the new editor", () => {
- let newEditor;
-
- waitsForPromise(() => atom.workspace.open("sample.coffee", {activateItem: false}).then(o => (newEditor = o)));
+ it("reruns the search on the new editor", async () => {
+ let newEditor = await atom.workspace.open("sample.coffee", {activateItem: false});
- runs(() => {
newEditor = atom.workspace.paneForItem(editor).splitRight({
items: [newEditor]
}).getActiveItem();
@@ -939,42 +877,27 @@ describe("FindView", () => {
expect(findView.resultCounter.text()).toEqual("1 of 7");
expect(newEditor.getSelectedBufferRange()).toEqual([[1, 9], [1, 14]]);
});
- });
- it("highlights the found text in the new edit session (and removes the highlights from the other)", () => {
- let newEditor;
-
- waitsForPromise(() => atom.workspace.open("sample.coffee").then((o) => (newEditor = o)));
-
- runs(() => {
+ it("highlights the found text in the new editor (and removes the highlights from the other)", async () => {
+ const newEditor = await atom.workspace.open("sample.coffee")
expect(getResultDecorations(editor, "find-result")).toHaveLength(0);
expect(getResultDecorations(newEditor, "find-result")).toHaveLength(7);
});
- });
- it("will still highlight results after the split pane has been destroyed", () => {
- let newEditor, newEditorView;
-
- waitsForPromise(() => atom.workspace.open("sample.coffee").then((o) => (newEditor = o)));
-
- runs(() => {
+ it("will still highlight results after the split pane has been destroyed", async () => {
+ const newEditor = await atom.workspace.open("sample.coffee")
const originalPane = atom.workspace.paneForItem(editor);
const splitPane = atom.workspace.paneForItem(editor).splitRight();
originalPane.moveItemToPane(newEditor, splitPane, 0);
expect(getResultDecorations(newEditor, "find-result")).toHaveLength(7);
- newEditorView = editor.getElement();
- atom.commands.dispatch(newEditorView, "core:close");
+ atom.commands.dispatch(editor.getElement(), "core:close");
editorView.focus();
expect(atom.workspace.getActiveTextEditor()).toBe(editor);
- });
-
- runs(() => {
expect(getResultDecorations(editor, "find-result")).toHaveLength(6);
});
});
});
- });
describe("when the buffer contents change", () => {
it("re-runs the search", () => {
@@ -1221,11 +1144,8 @@ describe("FindView", () => {
expect(getResultDecorations(editor, "find-result")).toHaveLength(5);
findView.findEditor.setText("notinthefilebro");
atom.commands.dispatch(findView.findEditor.element, "core:confirm");
-
- runs(() => {
expect(getResultDecorations(editor, "find-result")).toHaveLength(0);
});
- });
it("adds a class to the current match indicating it is the current match", () => {
const firstResultMarker = getResultDecoration("current-result");
@@ -1389,17 +1309,15 @@ describe("FindView", () => {
});
describe("replacing", () => {
- beforeEach(() => {
+ beforeEach(async () => {
editor.setCursorBufferPosition([2, 0]);
atom.commands.dispatch(editorView, "find-and-replace:show-replace");
- waitsForPromise(() => activationPromise);
+ await activationPromise;
- runs(() => {
findView.findEditor.setText("items");
findView.replaceEditor.setText("cats");
});
- });
describe("when the find string is empty", () => {
it("beeps", () => {
@@ -1579,9 +1497,9 @@ describe("FindView", () => {
});
describe("history", () => {
- beforeEach(() => {
+ beforeEach(async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ await activationPromise;
});
describe("when there is no history", () => {
@@ -1694,9 +1612,9 @@ describe("FindView", () => {
});
describe("panel focus", () => {
- beforeEach(() => {
+ beforeEach(async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ await activationPromise;
});
it("focuses the find editor when the panel gets focus", () => {
@@ -1722,27 +1640,24 @@ describe("FindView", () => {
});
describe("when language-javascript is active", () => {
- beforeEach(() => {
- waitsForPromise(() => atom.packages.activatePackage("language-javascript"));
+ beforeEach(async () => {
+ await atom.packages.activatePackage("language-javascript");
});
- it("uses the regexp grammar when regex-mode is loaded from configuration", () => {
+ it("uses the regexp grammar when regex-mode is loaded from configuration", async () => {
atom.config.set("find-and-replace.useRegex", true);
atom.commands.dispatch(editorView, "find-and-replace:show");
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
expect(findView.model.getFindOptions().useRegex).toBe(true);
expect(findView.findEditor.getModel().getGrammar().scopeName).toBe("source.js.regexp");
expect(findView.replaceEditor.getModel().getGrammar().scopeName).toBe("source.js.regexp.replacement");
});
- });
describe("when panel is active", () => {
- beforeEach(() => {
+ beforeEach(async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
- waitsForPromise(() => activationPromise);
+ await activationPromise;
});
it("does not use regexp grammar when in non-regex mode", () => {
@@ -1766,17 +1681,14 @@ describe("FindView", () => {
});
describe("when no buffer is open", () => {
- it("toggles regex via an event and finds text matching the pattern", () => {
+ it("toggles regex via an event and finds text matching the pattern", async () => {
atom.commands.dispatch(editorView, "find-and-replace:show");
editor.destroy();
+ await activationPromise;
- waitsForPromise(() => activationPromise);
-
- runs(() => {
findView.findEditor.setText("items");
atom.commands.dispatch(findView.findEditor.element, "find-and-replace:toggle-regex-option");
expect(findView.descriptionLabel.text()).toContain("No results");
});
});
});
-});

Use async/await in FindView spec