Skip to content

Commit 47ca54b

Browse files
committed
test: convert the tests to typescript
1 parent d5e1a76 commit 47ca54b

File tree

6 files changed

+132
-143
lines changed

6 files changed

+132
-143
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
"lint": "eslint . --fix",
1212
"test.lint": "eslint .",
1313
"test": "npm run build.unit && npm run test.only",
14-
"test.only": "atom --test spec",
14+
"test.only": "atom --test ./dist/spec",
1515
"clean": "shx rm -rf dist",
16-
"build.unit": "tsc -p ./src/tsconfig.json || echo done",
16+
"build.unit": "tsc -p ./tsconfig.json || echo done",
1717
"dev": "npm run clean && cross-env NODE_ENV=development cross-env BABEL_KEEP_MODULES=true rollup -c -w",
1818
"build": "npm run clean && cross-env NODE_ENV=production cross-env BABEL_KEEP_MODULES=true rollup -c ",
1919
"build-commit": "build-commit -o dist",
2020
"bump": "ncu -u -x coffeescript",
2121
"prepare": "npm run build"
2222
},
23-
"atomTestRunner": "./spec/runner",
23+
"atomTestRunner": "./dist/spec/runner",
2424
"activationHooks": [
2525
"core:loaded-shell-environment"
2626
],

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createPlugins } from "rollup-plugin-atomic"
22

3-
const plugins = createPlugins([["ts", { tsconfig: "./src/tsconfig.json" }, true], "js", "json"])
3+
const plugins = createPlugins([["ts", { tsconfig: "./tsconfig.json" }, true], "js", "json"])
44

55
const RollupConfig = [
66
{

spec/CodeFormatManager-spec.js

Lines changed: 0 additions & 136 deletions
This file was deleted.

spec/CodeFormatManager-spec.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { Range } from "atom"
2+
import { observeTextEditors } from "@atom-ide-community/nuclide-commons-atom/FileEventHandlers"
3+
import CodeFormatManager, { SAVE_TIMEOUT } from "../src/CodeFormatManager"
4+
import UniversalDisposable from "@atom-ide-community/nuclide-commons/UniversalDisposable"
5+
import temp from "temp"
6+
import * as config from "../src/config"
7+
import { waitsFor } from "waitsfor"
8+
9+
const sleep = (n) => new Promise((r) => setTimeout(r, n))
10+
11+
jasmine.DEFAULT_TIMEOUT_INTERVAL = SAVE_TIMEOUT + 100
12+
describe("CodeFormatManager", () => {
13+
let textEditor
14+
let manager
15+
let disposables
16+
beforeEach(async () => {
17+
manager = new CodeFormatManager()
18+
disposables = new UniversalDisposable(observeTextEditors())
19+
temp.track()
20+
const file = temp.openSync()
21+
textEditor = await atom.workspace.open(file.path)
22+
})
23+
afterEach(async () => {
24+
manager.dispose()
25+
disposables.dispose()
26+
})
27+
it("formats an editor on request", async () => {
28+
manager.addRangeProvider({
29+
grammarScopes: ["text.plain.null-grammar"],
30+
priority: 1,
31+
formatCode: () =>
32+
Promise.resolve([
33+
{
34+
oldRange: new Range([0, 0], [0, 3]),
35+
oldText: "abc",
36+
newText: "def",
37+
},
38+
]),
39+
})
40+
textEditor.setText("abc")
41+
atom.commands.dispatch(atom.views.getView(textEditor), "code-format:format-code")
42+
await waitsFor(() => textEditor.getText() === "def", {
43+
timeout: SAVE_TIMEOUT,
44+
})
45+
})
46+
it("format an editor using formatEntireFile", async () => {
47+
manager.addFileProvider({
48+
grammarScopes: ["text.plain.null-grammar"],
49+
priority: 1,
50+
formatEntireFile: () =>
51+
Promise.resolve({
52+
formatted: "ghi",
53+
}),
54+
})
55+
textEditor.setText("abc")
56+
atom.commands.dispatch(atom.views.getView(textEditor), "code-format:format-code")
57+
await waitsFor(() => textEditor.getText() === "ghi", {
58+
timeout: SAVE_TIMEOUT,
59+
})
60+
})
61+
it("formats an editor on type", async () => {
62+
spyOn(config, "getFormatOnType").and.returnValue(true)
63+
const provider = {
64+
grammarScopes: ["text.plain.null-grammar"],
65+
priority: 1,
66+
formatAtPosition: () =>
67+
Promise.resolve([
68+
{
69+
oldRange: new Range([0, 0], [0, 3]),
70+
oldText: "abc",
71+
newText: "def",
72+
},
73+
]),
74+
keepCursorPosition: false,
75+
}
76+
const spy = spyOn(provider, "formatAtPosition")
77+
manager.addOnTypeProvider(provider)
78+
textEditor.setText("a")
79+
textEditor.setCursorBufferPosition([0, 1])
80+
textEditor.insertText("b")
81+
textEditor.insertText("c")
82+
await waitsFor(() => textEditor.getText() === "def", {
83+
timeout: SAVE_TIMEOUT,
84+
})
85+
// Debouncing should ensure only one format call.
86+
expect(spy.mock.calls.length).toBe(1)
87+
})
88+
it("formats an editor on save", async () => {
89+
spyOn(config, "getFormatOnSave").and.returnValue(true)
90+
manager.addOnSaveProvider({
91+
grammarScopes: ["text.plain.null-grammar"],
92+
priority: 1,
93+
formatOnSave: () =>
94+
Promise.resolve([
95+
{
96+
oldRange: new Range([0, 0], [0, 3]),
97+
oldText: "abc",
98+
newText: "def",
99+
},
100+
]),
101+
})
102+
textEditor.setText("abc")
103+
await textEditor.save()
104+
expect(textEditor.getText()).toBe("def")
105+
})
106+
it("should still save on timeout", async () => {
107+
spyOn(config, "getFormatOnSave").and.returnValue(true)
108+
manager.addRangeProvider({
109+
grammarScopes: ["text.plain.null-grammar"],
110+
priority: 1,
111+
formatCode: async () => {
112+
await sleep(SAVE_TIMEOUT + 1000)
113+
return []
114+
},
115+
})
116+
const spy = spyOn(textEditor.getBuffer(), "save")
117+
textEditor.save()
118+
// Wait until the buffer has been saved and verify it has been saved exactly
119+
// once.
120+
await waitsFor(() => spy.mock.calls.length > 0, {
121+
timeout: SAVE_TIMEOUT,
122+
})
123+
expect(spy.mock.calls.length).toBe(1)
124+
})
125+
})

spec/runner.js renamed to spec/runner.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
"use babel"
21
import { createRunner } from "atom-jasmine3-test-runner"
32

43
// https://github.com/UziTech/atom-jasmine3-test-runner#api

src/tsconfig.json renamed to tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"module": "commonjs",
2929
"moduleResolution": "node",
3030
"importHelpers": false,
31-
"outDir": "../dist"
31+
"outDir": "./dist"
3232
},
33-
"compileOnSave": true
33+
"compileOnSave": true,
34+
"include": ["src/**/*", "spec/**/*"]
3435
}

0 commit comments

Comments
 (0)