Use async/await in ResultsView spec
MODIFY
spec/results-view-spec.js
spec/results-view-spec.js
+/** @babel */
+
const _ = require('underscore-plus');
const path = require('path');
const temp = require("temp");
const ResultsPaneView = require('../lib/project/results-pane');
const FileIcons = require('../lib/file-icons');
+const {beforeEach, it, fit, ffit, fffit} = require('./async-spec-helpers')
describe('ResultsView', () => {
let pack, projectFindView, resultsView, searchPromise, workspaceElement;
@@ -16,7 +19,7 @@ describe('ResultsView', () => {
return getExistingResultsPane().resultsView;
}
- beforeEach(() => {
+ beforeEach(async () => {
workspaceElement = atom.views.getView(atom.workspace);
workspaceElement.style.height = '1000px';
jasmine.attachToDOM(workspaceElement);
@@ -32,21 +35,21 @@ describe('ResultsView', () => {
atom.commands.dispatch(workspaceElement, 'project-find:show');
- waitsForPromise(() => activationPromise);
+ await activationPromise;
});
describe("result sorting", () => {
- beforeEach(() => {
+ beforeEach(async () => {
projectFindView.findEditor.setText('i');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
-
- waitsForPromise(() => searchPromise);
-
- runs(() => resultsView = getResultsView());
+ await searchPromise;
+ resultsView = getResultsView();
});
describe("when shouldRenderMoreResults is true", () => {
- beforeEach(() => spyOn(resultsView, 'shouldRenderMoreResults').andReturn(true));
+ beforeEach(() => {
+ spyOn(resultsView, 'shouldRenderMoreResults').andReturn(true)
+ });
it("displays the results in sorted order", () => {
let match = {
@@ -116,20 +119,16 @@ describe('ResultsView', () => {
});
describe("when the result is for a long line", () =>
- it("renders the context around the match", () => {
+ it("renders the context around the match", async () => {
projectFindView.findEditor.setText('ghijkl');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
-
expect(resultsView.find('.path-name').text()).toBe("one-long-line.coffee");
expect(resultsView.find('.preview').length).toBe(1);
expect(resultsView.find('.preview').text()).toBe('test test test test test test test test test test test a b c d e f g h i j k l abcdefghijklmnopqrstuvwxyz');
expect(resultsView.find('.match').text()).toBe('ghijkl');
- });
})
);
@@ -138,18 +137,15 @@ describe('ResultsView', () => {
atom.project.addPath(temp.mkdirSync("another-project-path"))
});
- it("includes the basename of the project path that contains the match", () => {
+ it("includes the basename of the project path that contains the match", async () => {
projectFindView.findEditor.setText('ghijkl');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
expect(resultsView.find('.path-name').text()).toBe(path.join("fixtures", "one-long-line.coffee"));
});
});
- });
describe("rendering replacement text", () => {
let modifiedDelay = null;
@@ -159,30 +155,23 @@ describe('ResultsView', () => {
modifiedDelay = projectFindView.replaceEditor.getModel().getBuffer().stoppedChangingDelay;
});
- it("renders the replacement when doing a search and there is a replacement pattern", () => {
+ it("renders the replacement when doing a search and there is a replacement pattern", async () => {
projectFindView.replaceEditor.setText('cats');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
-
expect(resultsView.find('.path-name').text()).toBe("one-long-line.coffee");
expect(resultsView.find('.preview').length).toBe(1);
expect(resultsView.find('.match').text()).toBe('ghijkl');
expect(resultsView.find('.replacement').text()).toBe('cats');
});
- });
- it("renders the replacement when changing the text in the replacement field", () => {
+ it("renders the replacement when changing the text in the replacement field", async () => {
atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
-
expect(resultsView.find('.match').text()).toBe('ghijkl');
expect(resultsView.find('.match')).toHaveClass('highlight-info');
expect(resultsView.find('.replacement').text()).toBe('');
@@ -204,18 +193,14 @@ describe('ResultsView', () => {
expect(resultsView.find('.replacement')).toBeHidden();
});
});
- });
describe("when list is scrollable", () => {
- it("adds more results to the DOM when scrolling", () => {
+ it("adds more results to the DOM when scrolling", async () => {
projectFindView.findEditor.setText(' ');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
-
expect(resultsView.prop('scrollHeight')).toBeGreaterThan(resultsView.height());
let previousScrollHeight = resultsView.prop('scrollHeight');
let previousOperationCount = resultsView.find("li").length;
@@ -225,15 +210,12 @@ describe('ResultsView', () => {
expect(resultsView.prop('scrollHeight')).toBeGreaterThan(previousScrollHeight);
expect(resultsView.find("li").length).toBeGreaterThan(previousOperationCount);
});
- });
- it("adds more results to the DOM when scrolled to bottom", () => {
+ it("adds more results to the DOM when scrolled to bottom", async () => {
projectFindView.findEditor.setText(' ');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
expect(resultsView.prop('scrollHeight')).toBeGreaterThan(resultsView.height());
@@ -245,17 +227,13 @@ describe('ResultsView', () => {
expect(resultsView.prop('scrollHeight')).toBeGreaterThan(previousScrollHeight);
expect(resultsView.find("li").length).toBeGreaterThan(previousOperationCount);
});
- });
- it("renders more results when a result is collapsed via core:move-left", () => {
+ it("renders more results when a result is collapsed via core:move-left", async () => {
projectFindView.findEditor.setText(' ');
projectFindView.confirm();
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
-
expect(resultsView.find(".path").length).toBe(1);
let pathNode = resultsView.find(".path")[0];
@@ -266,17 +244,13 @@ describe('ResultsView', () => {
pathNode.dispatchEvent(buildMouseEvent('mousedown', {target: pathNode, which: 1}));
expect(resultsView.find(".path").length).toBe(3);
});
- });
- it("renders more results when a result is collapsed via click", () => {
+ it("renders more results when a result is collapsed via click", async () => {
projectFindView.findEditor.setText(' ');
projectFindView.confirm();
+ await searchPromise;
- waitsForPromise(() => searchPromise);
-
- runs(() => {
resultsView = getResultsView();
-
expect(resultsView.find(".path-details").length).toBe(1);
atom.commands.dispatch(resultsView.element, 'core:move-down');
@@ -289,23 +263,20 @@ describe('ResultsView', () => {
expect(resultsView.find(".path-details").length).toBe(3);
});
- });
describe("core:page-up and core:page-down", () => {
- beforeEach(() => {
+ beforeEach(async () => {
workspaceElement.style.height = '300px';
workspaceElement.style.width = '1024px';
projectFindView.findEditor.setText(' ');
projectFindView.confirm();
- waitsForPromise(() => searchPromise);
+ await searchPromise;
- runs(() => {
resultsView = getResultsView();
expect(resultsView.prop('scrollTop')).toBe(0);
expect(resultsView.prop('scrollHeight')).toBeGreaterThan(resultsView.height());
});
- });
it("selects the first result on the next page when core:page-down is triggered", () => {
let itemHeight = resultsView.find('.selected').outerHeight();
@@ -364,14 +335,12 @@ describe('ResultsView', () => {
});
describe("core:move-to-top and core:move-to-bottom", () => {
- beforeEach(() => {
+ beforeEach(async () => {
workspaceElement.style.height = '200px';
projectFindView.findEditor.setText('so');
projectFindView.confirm();
-
- waitsForPromise(() => searchPromise);
-
- runs(() => resultsView = getResultsView());
+ await searchPromise;
+ resultsView = getResultsView();
});
it("renders all results and selects the last item when core:move-to-bottom is triggered; selects the first item when core:move-to-top is triggered", () => {
@@ -407,63 +376,65 @@ describe('ResultsView', () => {
});
describe("opening results", () => {
- let openHandler;
-
- beforeEach(() => {
- waitsForPromise(() => atom.workspace.open('sample.js'));
+ beforeEach(async () => {
+ await atom.workspace.open('sample.js');
- runs(() => {
projectFindView.findEditor.setText('items');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
- openHandler = jasmine.createSpy("open handler");
- return atom.workspace.onDidOpen(openHandler);
- });
-
- waitsForPromise(() => searchPromise);
+ await searchPromise;
- runs(() => {
resultsView = getResultsView();
resultsView.selectFirstResult();
- return openHandler.reset();
- });
});
- it("opens the correct file containing the result when 'core:confirm' is called", () => {
+ function paneItemOpening() {
+ return new Promise(resolve => {
+ const subscription = atom.workspace.onDidOpen(() => {
+ resolve()
+ subscription.dispose()
+ })
+ })
+ }
+
+ it("opens the file containing the result when 'core:confirm' is called", async () => {
// open something in sample.coffee
+ resultsView.focus();
_.times(3, () => atom.commands.dispatch(resultsView.element, 'core:move-down'));
- openHandler.reset();
atom.commands.dispatch(resultsView.element, 'core:confirm');
-
- waitsFor(() => openHandler.callCount === 1);
-
- runs(() => {
+ await paneItemOpening()
expect(atom.workspace.getActivePaneItem().getPath()).toContain('sample.');
// open something in sample.js
resultsView.focus();
_.times(6, () => atom.commands.dispatch(resultsView.element, 'core:move-down'));
- openHandler.reset();
- return atom.commands.dispatch(resultsView.element, 'core:confirm');
- });
-
- waitsFor(() => openHandler.callCount === 1);
-
- runs(() => expect(atom.workspace.getActivePaneItem().getPath()).toContain('sample.'));
+ atom.commands.dispatch(resultsView.element, 'core:confirm');
+ await paneItemOpening()
+ expect(atom.workspace.getActivePaneItem().getPath()).toContain('sample.');
});
- it("opens the file containing the result in non-pending state when the search result is double-clicked", () => {
- let pathNode = resultsView.find(".search-result")[0];
+ it("opens the file containing the result in a non-pending state when the search result is double-clicked", async () => {
+ const pathNode = resultsView.find(".search-result")[0];
pathNode.dispatchEvent(buildMouseEvent('mousedown', {target: pathNode, detail: 1}));
pathNode.dispatchEvent(buildMouseEvent('mousedown', {target: pathNode, detail: 2}));
- let editor = null;
- waitsFor(() => editor = atom.workspace.getActiveTextEditor());
- waitsFor(() => atom.workspace.getActivePane().getPendingItem() === null);
-
- runs(() => expect(atom.views.getView(editor)).toHaveFocus());
+ await paneItemOpening()
+ const editor = atom.workspace.getActiveTextEditor();
+ expect(atom.workspace.getActivePane().getPendingItem()).toBe(null);
+ expect(atom.views.getView(editor)).toHaveFocus();
});
+ it("opens the file containing the result in a pending state when the search result is single-clicked", async () => {
+ const pathNode = resultsView.find(".search-result")[0];
+ pathNode.dispatchEvent(buildMouseEvent('mousedown', {target: pathNode, which: 1}));
+ await paneItemOpening()
+ const editor = atom.workspace.getActiveTextEditor();
+ expect(atom.workspace.getActivePane().getPendingItem()).toBe(editor);
+ expect(atom.views.getView(editor)).toHaveFocus();
+ })
+
describe("when `projectSearchResultsPaneSplitDirection` option is none", () => {
- beforeEach(() => atom.config.set('find-and-replace.projectSearchResultsPaneSplitDirection', 'none'));
+ beforeEach(() => {
+ atom.config.set('find-and-replace.projectSearchResultsPaneSplitDirection', 'none');
+ });
it("does not specify a pane to split", () => {
spyOn(atom.workspace, 'open').andCallThrough();
@@ -474,7 +445,9 @@ describe('ResultsView', () => {
});
describe("when `projectSearchResultsPaneSplitDirection` option is right", () => {
- beforeEach(() => atom.config.set('find-and-replace.projectSearchResultsPaneSplitDirection', 'right'));
+ beforeEach(() => {
+ atom.config.set('find-and-replace.projectSearchResultsPaneSplitDirection', 'right');
+ });
it("always opens the file in the left pane", () => {
spyOn(atom.workspace, 'open').andCallThrough();
@@ -482,18 +455,6 @@ describe('ResultsView', () => {
atom.commands.dispatch(resultsView.element, 'core:confirm');
expect(atom.workspace.open.mostRecentCall.args[1].split).toBe('left');
});
-
- describe("when a search result is single-clicked", () => {
- it("opens the file containing the result in pending state", () => {
- let pathNode = resultsView.find(".search-result")[0];
- pathNode.dispatchEvent(buildMouseEvent('mousedown', {target: pathNode, which: 1}));
- let editor = null;
- waitsFor(() => editor = atom.workspace.getActiveTextEditor());
- waitsFor(() => atom.workspace.getActivePane().getPendingItem() === editor);
-
- runs(() => expect(atom.views.getView(editor)).toHaveFocus());
- })
- });
});
describe("when `projectSearchResultsPaneSplitDirection` option is down", () => {
@@ -505,49 +466,31 @@ describe('ResultsView', () => {
atom.commands.dispatch(resultsView.element, 'core:confirm');
expect(atom.workspace.open.mostRecentCall.args[1].split).toBe('up');
});
-
- describe("when a search result is single-clicked", () => {
- it("opens the file containing the result in pending state", () => {
- let pathNode = resultsView.find(".search-result")[0];
- pathNode.dispatchEvent(buildMouseEvent('mousedown', {target: pathNode, which: 1}));
- let editor = null;
- waitsFor(() => editor = atom.workspace.getActiveTextEditor());
- waitsFor(() => atom.workspace.getActivePane().getPendingItem() === editor);
-
- runs(() => expect(atom.views.getView(editor)).toHaveFocus());
- })
- });
});
});
describe("arrowing through the list", () => {
- it("arrows through the entire list without selecting paths and overshooting the boundaries", () => {
- waitsForPromise(() => atom.workspace.open('sample.js'));
+ it("arrows through the entire list without selecting paths and overshooting the boundaries", async () => {
+ await atom.workspace.open('sample.js');
- runs(() => {
projectFindView.findEditor.setText('items');
- return atom.commands.dispatch(projectFindView.element, 'core:confirm');
- });
-
- waitsForPromise(() => searchPromise);
+ atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- runs(() => {
resultsView = getResultsView();
- let lastSelectedItem = null;
-
- let { length } = resultsView.find("li > ul > li");
- expect(length).toBe(13);
+ let {length: resultCount} = resultsView.find("li > ul > li");
+ expect(resultCount).toBe(13);
resultsView.selectFirstResult();
// moves down for 13 results + 2 files
- _.times(length + 1, () => atom.commands.dispatch(resultsView.element, 'core:move-down'));
+ _.times(resultCount + 1, () => atom.commands.dispatch(resultsView.element, 'core:move-down'));
let selectedItem = resultsView.find('.selected');
expect(selectedItem).toHaveClass('search-result');
expect(selectedItem[0]).not.toBe(lastSelectedItem);
- lastSelectedItem = selectedItem[0];
+ let lastSelectedItem = selectedItem[0];
// stays at the bottom
_.times(2, () => atom.commands.dispatch(resultsView.element, 'core:move-down'));
@@ -557,7 +500,7 @@ describe('ResultsView', () => {
lastSelectedItem = selectedItem[0];
// moves up to the top
- _.times(length + 1, () => atom.commands.dispatch(resultsView.element, 'core:move-up'));
+ _.times(resultCount + 1, () => atom.commands.dispatch(resultsView.element, 'core:move-up'));
selectedItem = resultsView.find('.selected');
expect(selectedItem).toHaveClass('path');
expect(selectedItem[0]).not.toBe(lastSelectedItem);
@@ -569,14 +512,13 @@ describe('ResultsView', () => {
selectedItem = resultsView.find('.selected');
expect(selectedItem[0]).toBe(lastSelectedItem);
});
- });
describe("when there are hidden .path elements", () => {
- beforeEach(() => {
+ beforeEach(async () => {
projectFindView.findEditor.setText('i');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
- waitsForPromise(() => searchPromise);
- runs(() => resultsView = getResultsView());
+ await searchPromise;
+ resultsView = getResultsView();
});
it("ignores the invisible path elements", () => {
@@ -632,16 +574,15 @@ describe('ResultsView', () => {
});
describe("when there are a list of items", () => {
- beforeEach(() => {
+ beforeEach(async () => {
projectFindView.findEditor.setText('items');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
- waitsForPromise(() => searchPromise);
- runs(() => resultsView = getResultsView());
+ await searchPromise;
+ resultsView = getResultsView();
});
it("shows the preview-controls", () => {
- let { previewControls } = resultsView.parentView;
- expect(previewControls.isVisible()).toBe(true);
+ expect(resultsView.parentView.previewControls.isVisible()).toBe(true);
});
it("collapses the selected results view", () => {
@@ -725,52 +666,33 @@ describe('ResultsView', () => {
});
describe("when the results view is empty", () => {
- it("ignores core:confirm events", () => {
+ it("ignores core:confirm events", async () => {
projectFindView.findEditor.setText('thiswillnotmatchanythingintheproject');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
-
- waitsForPromise(() => searchPromise);
-
- runs(() => {
+ await searchPromise;
resultsView = getResultsView();
expect(() => atom.commands.dispatch(resultsView.element, 'core:confirm')).not.toThrow();
});
- });
- it("won't show the preview-controls", () => {
+ it("won't show the preview-controls", async () => {
projectFindView.findEditor.setText('thiswillnotmatchanythingintheproject');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
-
- waitsForPromise(() => searchPromise);
-
- runs(() => {
- let { previewControls } = getResultsView().parentView;
- expect(previewControls.isVisible()).toBe(false);
- });
+ await searchPromise;
+ expect(getResultsView().parentView.previewControls.isVisible()).toBe(false);
});
});
describe("copying items with core:copy", () => {
- let openHandler;
- [resultsView, openHandler] = [];
-
- beforeEach(() => {
- waitsForPromise(() => atom.workspace.open('sample.js'));
+ it("copies the selected line onto the clipboard", async () => {
+ await atom.workspace.open('sample.js');
- runs(() => {
projectFindView.findEditor.setText('items');
- return atom.commands.dispatch(projectFindView.element, 'core:confirm');
- });
-
- waitsForPromise(() => searchPromise);
+ atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
- runs(() => {
- resultsView = getResultsView();
+ const resultsView = getResultsView();
resultsView.selectFirstResult();
- });
- });
- it("copies the selected line onto the clipboard", () => {
_.times(2, () => atom.commands.dispatch(resultsView.element, 'core:move-down'));
atom.commands.dispatch(resultsView.element, 'core:copy');
expect(atom.clipboard.read()).toBe(' return items if items.length <= 1');
@@ -778,25 +700,16 @@ describe('ResultsView', () => {
});
describe("copying path with find-and-replace:copy-path", () => {
- let openHandler;
- [resultsView, openHandler] = [];
-
- beforeEach(() => {
- runs(() => {
+ it("copies the selected file path to clipboard", async () => {
projectFindView.findEditor.setText('items');
- return atom.commands.dispatch(projectFindView.element, 'core:confirm');
- });
+ atom.commands.dispatch(projectFindView.element, 'core:confirm');
- waitsForPromise(() => searchPromise);
+ await searchPromise;
- runs(() => {
- resultsView = getResultsView();
+ const resultsView = getResultsView();
resultsView.selectFirstResult();
- return resultsView.collapseResult();
- });
- });
+ resultsView.collapseResult();
- it("copies the selected file path to clipboard", () => {
atom.commands.dispatch(resultsView.element, 'find-and-replace:copy-path');
expect(atom.clipboard.read()).toBe('sample.coffee');
atom.commands.dispatch(resultsView.element, 'core:move-down');
@@ -806,7 +719,7 @@ describe('ResultsView', () => {
});
describe("icon-service lifecycle", () => {
- it('renders file icon classes based on the provided file-icons service', () => {
+ it('renders file icon classes based on the provided file-icons service', async () => {
const fileIconsDisposable = atom.packages.serviceHub.provide('atom.file-icons', '1.0.0', {
iconClassForPath(path, context) {
expect(context).toBe("find-and-replace");
@@ -821,29 +734,23 @@ describe('ResultsView', () => {
projectFindView.findEditor.setText('i');
atom.commands.dispatch(projectFindView.element, 'core:confirm');
- waitsForPromise(() => searchPromise);
- runs(() => {
- resultsView = getResultsView();
+ await searchPromise;
+ let resultsView = getResultsView();
let fileIconClasses = Array.from(resultsView.find('.path-details .icon')).map(el => el.className);
expect(fileIconClasses).toContain('first-icon-class second-icon-class icon');
expect(fileIconClasses).toContain('third-icon-class fourth-icon-class icon');
expect(fileIconClasses).not.toContain('icon-file-text icon');
- });
- runs(() => {
fileIconsDisposable.dispose();
projectFindView.findEditor.setText('e');
- return atom.commands.dispatch(projectFindView.element, 'core:confirm');
- });
+ atom.commands.dispatch(projectFindView.element, 'core:confirm');
- waitsForPromise(() => searchPromise);
- runs(() => {
+ await searchPromise;
resultsView = getResultsView();
- let fileIconClasses = Array.from(resultsView.find('.path-details .icon')).map(el => el.className);
+ fileIconClasses = Array.from(resultsView.find('.path-details .icon')).map(el => el.className);
expect(fileIconClasses).not.toContain('first-icon-class second-icon-class icon');
expect(fileIconClasses).not.toContain('third-icon-class fourth-icon-class icon');
expect(fileIconClasses).toContain('icon-file-text icon');
- });
})
});
});

Use async/await in ResultsView spec