Fix bug that could cause auto formatting to fail for the last block.

Add tests for language auto detection and formatting.
This commit is contained in:
Jonatan Heyman 2023-12-25 23:27:05 +01:00
parent 6fa956f6ec
commit bc77fa725a
4 changed files with 90 additions and 3 deletions

View File

@ -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,
@ -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,

46
tests/formatting.spec.js Normal file
View File

@ -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)
})

View File

@ -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)")
})

View File

@ -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)
}