Skip to content

Commit

Permalink
Fix bug that could cause auto formatting to fail for the last block.
Browse files Browse the repository at this point in the history
Add tests for language auto detection and formatting.
  • Loading branch information
heyman committed Dec 25, 2023
1 parent 6fa956f commit bc77fa7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/editor/block/format-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export const formatBlockContent = async ({ state, dispatch }) => {
plugins: language.prettier.plugins,
tabWidth: state.tabSize,
}),
cursorOffset: cursorPos == block.content.from ? 0 : content.length,
}
formattedContent.cursorOffset = cursorPos == block.content.from ? 0 : formattedContent.formatted.length
} else {
formattedContent = await prettier.formatWithCursor(content, {
cursorOffset: cursorPos - block.content.from,
Expand All @@ -62,7 +62,7 @@ export const formatBlockContent = async ({ state, dispatch }) => {
to: block.content.to,
insert: formattedContent.formatted,
},
selection: EditorSelection.cursor(block.content.from + formattedContent.cursorOffset),
selection: EditorSelection.cursor(block.content.from + Math.min(formattedContent.cursorOffset, formattedContent.formatted.length)),
}, {
userEvent: "input",
scrollIntoView: true,
Expand Down
46 changes: 46 additions & 0 deletions tests/formatting.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { test, expect } from "@playwright/test";
import { HeynotePage } from "./test-utils.js";

let heynotePage

test.beforeEach(async ({ page }) => {
console.log("beforeEach")
heynotePage = new HeynotePage(page)
await heynotePage.goto()
})


test("JSON formatting", async ({ page }) => {
heynotePage.setContent(`
∞∞∞json
{"test": 1, "key2": "hey!"}
`)
expect(await page.locator("css=.status .status-block.lang")).toHaveText("JSON")
await page.locator("css=.status-block.format").click()
await page.waitForTimeout(100)
expect(await heynotePage.getBlockContent(0)).toBe(`{
"test": 1,
"key2": "hey!"
}
`)
})

test("JSON formatting (cursor at start)", async ({ page }) => {
heynotePage.setContent(`
∞∞∞json
{"test": 1, "key2": "hey!"}
`)
expect(await page.locator("css=.status .status-block.lang")).toHaveText("JSON")
for (let i=0; i<5; i++) {
await page.locator("body").press("ArrowUp")
}
await page.locator("css=.status-block.format").click()
await page.waitForTimeout(100)
expect(await heynotePage.getBlockContent(0)).toBe(`{
"test": 1,
"key2": "hey!"
}
`)
const block = (await heynotePage.getBlocks())[0]
expect(await page.evaluate(() => window._heynote_editor.view.state.selection.main.from)).toBe(block.content.from)
})
41 changes: 41 additions & 0 deletions tests/language-detection.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { test, expect } from "@playwright/test";
import { HeynotePage } from "./test-utils.js";

let heynotePage

test.beforeEach(async ({ page }) => {
console.log("beforeEach")
heynotePage = new HeynotePage(page)
await heynotePage.goto()
})


test("test valid JSON detection", async ({ page }) => {
page.locator("body").pressSequentially(`
{"test": 1, "key2": "hey!"}
`)
await page.waitForTimeout(200);
expect(await page.locator("css=.status .status-block.lang")).toHaveText("JSON (auto)")
const block = (await heynotePage.getBlocks())[0]
expect(block.language.name).toBe("json")
expect(block.language.auto).toBeTruthy()
})


test("python detection", async ({ page }) => {
page.locator("body").pressSequentially(`
# import complex math module
import cmath
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
`)
await page.waitForTimeout(1000);
expect(await page.locator("css=.status .status-block.lang")).toHaveText("Python (auto)")
})
2 changes: 1 addition & 1 deletion tests/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class HeynotePage {
async getContent() {
return await this.page.evaluate(() => window._heynote_editor.getContent())
}

async setContent(content) {
await this.page.evaluate((content) => window._heynote_editor.setContent(content), content)
}
Expand Down

0 comments on commit bc77fa7

Please sign in to comment.