heynote/tests/block-creation.spec.js

146 lines
4.5 KiB
JavaScript
Raw Normal View History

Add key bindings for inserting new blocks at the end/top of the buffer, as well as before the current block (#85) * Add functionality to insert new block after the last block - Update key bindings in `initial-content.ts` to include `Alt + Enter` for adding a new block after the last block. - Implement `getLastNoteBlock` function in `block.js` to retrieve the last block in the note. - Add `addNewBlockAfterLast` command in `commands.js` to handle the insertion of a new block after the last one. - Integrate `addNewBlockAfterLast` command into the keymap in `keymap.js`. * Add block insertion before/after current, before first and after last. Also, tests. - Added `getFirstNoteBlock` in `block.js` for accessing the first text block. - Implemented new functions in `commands.js` like `addNewBlockBeforeCurrent` and `addNewBlockBeforeFirst`. - Updated `keymap.js` with new key bindings to facilitate block creation. - Introduced `block-creation.spec.js` for testing the new block manipulation features. * Fix visual bug when inserting new block at the top * Update help text and Readme * Fix wrong cursor position after inserting new blocks at the top of the buffer, when the previous first block's delimiter is long (e.g. Markdown) * Make RegEx more generic * Fix import * Auto-generate the README.md and initial-content documentation - Add a documentation generator - Add an option to force the initial content to be erased with an env variable * Add more specific tests * Fix Mod key on Mac in test --------- Co-authored-by: Jonatan Heyman <jonatan@heyman.info>
2024-01-04 16:11:26 +01:00
import {expect, test} from "@playwright/test";
import {HeynotePage} from "./test-utils.js";
let heynotePage
test.beforeEach(async ({page}) => {
heynotePage = new HeynotePage(page)
await heynotePage.goto()
expect((await heynotePage.getBlocks()).length).toBe(1)
await heynotePage.setContent(`
Add key bindings for inserting new blocks at the end/top of the buffer, as well as before the current block (#85) * Add functionality to insert new block after the last block - Update key bindings in `initial-content.ts` to include `Alt + Enter` for adding a new block after the last block. - Implement `getLastNoteBlock` function in `block.js` to retrieve the last block in the note. - Add `addNewBlockAfterLast` command in `commands.js` to handle the insertion of a new block after the last one. - Integrate `addNewBlockAfterLast` command into the keymap in `keymap.js`. * Add block insertion before/after current, before first and after last. Also, tests. - Added `getFirstNoteBlock` in `block.js` for accessing the first text block. - Implemented new functions in `commands.js` like `addNewBlockBeforeCurrent` and `addNewBlockBeforeFirst`. - Updated `keymap.js` with new key bindings to facilitate block creation. - Introduced `block-creation.spec.js` for testing the new block manipulation features. * Fix visual bug when inserting new block at the top * Update help text and Readme * Fix wrong cursor position after inserting new blocks at the top of the buffer, when the previous first block's delimiter is long (e.g. Markdown) * Make RegEx more generic * Fix import * Auto-generate the README.md and initial-content documentation - Add a documentation generator - Add an option to force the initial content to be erased with an env variable * Add more specific tests * Fix Mod key on Mac in test --------- Co-authored-by: Jonatan Heyman <jonatan@heyman.info>
2024-01-04 16:11:26 +01:00
text
Block A
text
Block B
text
Block C`)
await page.waitForTimeout(100);
// check that blocks are created
expect((await heynotePage.getBlocks()).length).toBe(3)
// check that visual block layers are created
await expect(page.locator("css=.heynote-blocks-layer > div")).toHaveCount(3)
});
/* from A */
test("create block before current (A)", async ({page}) => {
// select the first block
await page.locator("body").press("ArrowUp")
await page.locator("body").press("ArrowUp")
await runTest(page, "Alt+Enter", ['D', 'A', 'B', 'C'])
})
test("create block after current (A)", async ({page}) => {
// select the first block
await page.locator("body").press("ArrowUp")
await page.locator("body").press("ArrowUp")
await runTest(page, "Mod+Enter", ['A', 'D', 'B', 'C'])
})
/* from B */
test("create block before current (B)", async ({page}) => {
// select the second block
await page.locator("body").press("ArrowUp")
await runTest(page, "Alt+Enter", ['A', 'D', 'B', 'C'])
})
test("create block after current (B)", async ({page}) => {
// select the second block
await page.locator("body").press("ArrowUp")
await runTest(page, "Mod+Enter", ['A', 'B', 'D', 'C'])
})
/* from C */
test("create block before current (C)", async ({page}) => {
await runTest(page, "Alt+Enter", ['A', 'B', 'D', 'C'])
})
test("create block after current (C)", async ({page}) => {
await runTest(page, "Mod+Enter", ['A', 'B', 'C', 'D'])
})
test("create block before first", async ({page}) => {
await runTest(page, "Alt+Shift+Enter", ['D', 'A', 'B', 'C'])
})
test("create block after last", async ({page}) => {
for (let i = 0; i < 3; i++) {
await page.locator("body").press("ArrowUp")
}
await runTest(page, "Mod+Shift+Enter", ['A', 'B', 'C', 'D'])
})
test("create block before Markdown block", async ({page}) => {
await heynotePage.setContent(`
markdown
# Markdown!
`)
await page.locator("body").press("Alt+Enter")
await page.waitForTimeout(100);
expect(await heynotePage.getCursorPosition()).toBe(11)
})
test("create block before first Markdown block", async ({page}) => {
await heynotePage.setContent(`
markdown
# Markdown!
text
`)
for (let i = 0; i < 5; i++) {
await page.locator("body").press("ArrowDown")
}
await page.locator("body").press("Alt+Shift+Enter")
await page.waitForTimeout(100);
expect(await heynotePage.getCursorPosition()).toBe(11)
})
const runTest = async (page, key, expectedBlocks) => {
// create a new block
await page.locator("body").press(key.replace("Mod", heynotePage.isMac ? "Meta" : "Control"))
await page.waitForTimeout(100);
await page.locator("body").pressSequentially("Block D")
// check that blocks are created
expect((await heynotePage.getBlocks()).length).toBe(4)
// check that the content of each block is correct
for (const expectedBlock of expectedBlocks) {
const index = expectedBlocks.indexOf(expectedBlock);
expect(await heynotePage.getBlockContent(index)).toBe(`Block ${expectedBlock}`)
}
// check that only one block delimiter widget has the class first
await expect(await page.locator("css=.heynote-block-start.first")).toHaveCount(1)
}
test("test custom default block language", async ({ page, browserName }) => {
heynotePage.setContent(`
text
Text block`)
await page.locator("css=.status-block.settings").click()
await page.locator("css=li.tab-editing").click()
await page.locator("css=select.block-language").selectOption("Rust")
await page.locator("body").press("Escape")
await page.locator("body").press((heynotePage.isMac ? "Meta" : "Control") + "+Enter")
expect(await heynotePage.getContent()).toBe(`
text
Text block
rust-a
`)
await page.locator("css=.status-block.settings").click()
await page.locator("css=li.tab-editing").click()
await page.locator("css=input.language-auto-detect").click()
await page.locator("body").press("Escape")
await page.locator("body").press((heynotePage.isMac ? "Meta" : "Control") + "+Enter")
expect(await heynotePage.getContent()).toBe(`
text
Text block
rust-a
rust
`)
})