Skip to content

Commit 0909dce

Browse files
committed
test: add waitFor util, fix jasmine spies
1 parent 2086cd9 commit 0909dce

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"shx": "^0.3.3",
8383
"temp": "^0.9.4",
8484
"typescript": "^4.3.5",
85-
"waitsfor": "^0.0.6"
85+
"waitit": "^1.0.3"
8686
},
8787
"prettier": "prettier-config-atomic"
8888
}

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/CodeFormatManager-spec.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import CodeFormatManager, { SAVE_TIMEOUT } from "../src/CodeFormatManager"
44
import UniversalDisposable from "@atom-ide-community/nuclide-commons/UniversalDisposable"
55
import temp from "temp"
66
import * as config from "../src/config"
7-
import { waitsFor } from "waitsfor"
8-
9-
const sleep = (n) => new Promise((r) => setTimeout(r, n))
7+
import { waitFor, sleep } from "./utils"
108

119
jasmine.DEFAULT_TIMEOUT_INTERVAL = SAVE_TIMEOUT + 100
1210
describe("CodeFormatManager", () => {
@@ -39,9 +37,7 @@ describe("CodeFormatManager", () => {
3937
})
4038
textEditor.setText("abc")
4139
atom.commands.dispatch(atom.views.getView(textEditor), "code-format:format-code")
42-
await waitsFor(() => textEditor.getText() === "def", {
43-
timeout: SAVE_TIMEOUT,
44-
})
40+
await waitFor(() => textEditor.getText() === "def")
4541
})
4642
it("format an editor using formatEntireFile", async () => {
4743
manager.addFileProvider({
@@ -54,9 +50,7 @@ describe("CodeFormatManager", () => {
5450
})
5551
textEditor.setText("abc")
5652
atom.commands.dispatch(atom.views.getView(textEditor), "code-format:format-code")
57-
await waitsFor(() => textEditor.getText() === "ghi", {
58-
timeout: SAVE_TIMEOUT,
59-
})
53+
await waitFor(() => textEditor.getText() === "ghi")
6054
})
6155
it("formats an editor on type", async () => {
6256
spyOn(config, "getFormatOnType").and.returnValue(true)
@@ -79,11 +73,9 @@ describe("CodeFormatManager", () => {
7973
textEditor.setCursorBufferPosition([0, 1])
8074
textEditor.insertText("b")
8175
textEditor.insertText("c")
82-
await waitsFor(() => textEditor.getText() === "def", {
83-
timeout: SAVE_TIMEOUT,
84-
})
76+
await waitFor(() => textEditor.getText() === "def")
8577
// Debouncing should ensure only one format call.
86-
expect(spy.mock.calls.length).toBe(1)
78+
expect(spy.calls.count()).toBe(1)
8779
})
8880
it("formats an editor on save", async () => {
8981
spyOn(config, "getFormatOnSave").and.returnValue(true)
@@ -117,9 +109,7 @@ describe("CodeFormatManager", () => {
117109
textEditor.save()
118110
// Wait until the buffer has been saved and verify it has been saved exactly
119111
// once.
120-
await waitsFor(() => spy.mock.calls.length > 0, {
121-
timeout: SAVE_TIMEOUT,
122-
})
123-
expect(spy.mock.calls.length).toBe(1)
112+
await waitFor(() => spy.calls.count() > 0)
113+
expect(spy.calls.count()).toBe(1)
124114
})
125115
})

spec/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as waitit from "waitit"
2+
3+
/** Wait until a function returns true */
4+
export function waitFor(fn: (...args: any[]) => boolean, timeout = 2500) {
5+
const interval = 100 // check very 100 ms
6+
return waitit.start({
7+
interval,
8+
maxTicks: timeout / interval,
9+
check: fn,
10+
})
11+
}
12+
13+
/** Sleep for ms */
14+
export function sleep(ms) {
15+
return new Promise((resolve) => setTimeout(resolve, ms))
16+
}

0 commit comments

Comments
 (0)