|
| 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 | +}) |
0 commit comments