Use async/await in ResultsModel spec
MODIFY
spec/results-model-spec.js
spec/results-model-spec.js
+/** @babel */
+
const path = require("path");
const ResultsModel = require("../lib/project/results-model");
const FindOptions = require("../lib/find-options");
+const {beforeEach, it, fit, ffit, fffit} = require('./async-spec-helpers')
describe("ResultsModel", () => {
- let editor, resultsModel, searchPromise;
+ let editor, resultsModel;
- beforeEach(() => {
+ beforeEach(async () => {
atom.config.set("core.excludeVcsIgnoredPaths", false);
atom.project.setPaths([path.join(__dirname, "fixtures")]);
- waitsForPromise(() => atom.workspace.open("sample.js"));
-
- runs(() => {
- editor = atom.workspace.getActiveTextEditor();
+ editor = await atom.workspace.open("sample.js");
resultsModel = new ResultsModel(new FindOptions());
});
- });
describe("searching for a pattern", () => {
- it("populates the model with all the results, and updates in response to changes in the buffer", () => {
+ it("populates the model with all the results, and updates in response to changes in the buffer", async () => {
const resultAddedSpy = jasmine.createSpy();
const resultRemovedSpy = jasmine.createSpy();
- runs(() => {
resultsModel.onDidAddResult(resultAddedSpy);
resultsModel.onDidRemoveResult(resultRemovedSpy);
- searchPromise = resultsModel.search("items", "*.js", "");
- });
-
- waitsForPromise(() => searchPromise);
+ await resultsModel.search("items", "*.js", "");
- runs(() => {
expect(resultAddedSpy).toHaveBeenCalled();
expect(resultAddedSpy.callCount).toBe(1);
- const result = resultsModel.getResult(editor.getPath());
+ let result = resultsModel.getResult(editor.getPath());
expect(result.matches.length).toBe(6);
expect(resultsModel.getPathCount()).toBe(1);
expect(resultsModel.getMatchCount()).toBe(6);
@@ -69,23 +63,11 @@ describe("ResultsModel", () => {
expect(resultsModel.getPathCount()).toBe(0);
expect(resultsModel.getMatchCount()).toBe(0);
});
- });
-
- it("ignores changes in untitled buffers", () => {
- const resultAddedSpy = jasmine.createSpy();
- const resultRemovedSpy = jasmine.createSpy();
-
- waitsForPromise(() => atom.workspace.open());
-
- runs(() => {
- resultsModel.onDidAddResult(resultAddedSpy);
- resultsModel.onDidRemoveResult(resultRemovedSpy);
- searchPromise = resultsModel.search("items", "*.js", "");
- });
- waitsForPromise(() => searchPromise);
+ it("ignores changes in untitled buffers", async () => {
+ await atom.workspace.open();
+ await resultsModel.search("items", "*.js", "");
- runs(() => {
editor = atom.workspace.getActiveTextEditor();
editor.setText("items\nitems");
spyOn(editor, "scan").andCallThrough();
@@ -93,7 +75,6 @@ describe("ResultsModel", () => {
expect(editor.scan).not.toHaveBeenCalled();
});
});
- });
describe("cancelling a search", () => {
let cancelledSpy;
@@ -103,29 +84,23 @@ describe("ResultsModel", () => {
resultsModel.onDidCancelSearching(cancelledSpy);
});
- it("populates the model with all the results, and updates in response to changes in the buffer", () => {
- searchPromise = resultsModel.search("items", "*.js", "");
+ it("populates the model with all the results, and updates in response to changes in the buffer", async () => {
+ const searchPromise = resultsModel.search("items", "*.js", "");
expect(resultsModel.inProgressSearchPromise).toBeTruthy();
resultsModel.clear();
expect(resultsModel.inProgressSearchPromise).toBeFalsy();
- waitsForPromise(() => searchPromise);
-
- runs(() => {
+ await searchPromise;
expect(cancelledSpy).toHaveBeenCalled();
});
- });
- it("populates the model with all the results, and updates in response to changes in the buffer", () => {
- searchPromise = resultsModel.search("items", "*.js", "");
- searchPromise = resultsModel.search("sort", "*.js", "");
- waitsForPromise(() => searchPromise);
+ it("populates the model with all the results, and updates in response to changes in the buffer", async () => {
+ resultsModel.search("items", "*.js", "");
+ await resultsModel.search("sort", "*.js", "");
- runs(() => {
expect(cancelledSpy).toHaveBeenCalled();
expect(resultsModel.getPathCount()).toBe(1);
expect(resultsModel.getMatchCount()).toBe(5);
});
});
});
-});

Use async/await in ResultsModel spec