Skip to content

Commit

Permalink
Add tests for creating new Note buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Dec 4, 2024
1 parent 57aa3fc commit e51d094
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/common/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const SCRATCH_FILE_NAME = "scratch.txt"
export const AUTO_SAVE_INTERVAL = 2000
1 change: 1 addition & 0 deletions src/editor/block/commands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EditorSelection, Transaction } from "@codemirror/state"

import { heynoteEvent, LANGUAGE_CHANGE, CURRENCIES_LOADED, ADD_NEW_BLOCK, DELETE_BLOCK } from "../annotation.js";
import { blockState, getActiveNoteBlock, getFirstNoteBlock, getLastNoteBlock, getNoteBlockFromPos } from "./block"
import { moveLineDown, moveLineUp } from "./move-lines.js";
Expand Down
3 changes: 2 additions & 1 deletion src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { autoSaveContent } from "./save.js"
import { todoCheckboxPlugin} from "./todo-checkbox.ts"
import { links } from "./links.js"
import { NoteFormat } from "../common/note-format.js"
import { AUTO_SAVE_INTERVAL } from "../common/constants.js"
import { useNotesStore } from "../stores/notes-store.js";
import { useErrorStore } from "../stores/error-store.js";

Expand Down Expand Up @@ -105,7 +106,7 @@ export class HeynoteEditor {
return {class: view.state.facet(EditorView.darkTheme) ? "dark-theme" : "light-theme"}
}),

autoSaveContent(this, 2000),
autoSaveContent(this, AUTO_SAVE_INTERVAL),

todoCheckboxPlugin,
markdown(),
Expand Down
69 changes: 69 additions & 0 deletions tests/buffer-creation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {expect, test} from "@playwright/test";
import {HeynotePage} from "./test-utils.js";

import { AUTO_SAVE_INTERVAL } from "../src/common/constants.js"
import { NoteFormat } from "../src/common/note-format.js"
import exp from "constants";

let heynotePage

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

expect((await heynotePage.getBlocks()).length).toBe(1)
await heynotePage.setContent(`
∞∞∞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)
});


test("default buffer saved", async ({page}) => {
// make some change and make sure content is auto saved in default scratch buffer
await page.locator("body").pressSequentially("YAY")
await page.waitForTimeout(AUTO_SAVE_INTERVAL + 50);
const bufferList = await heynotePage.getStoredBufferList()
expect(Object.keys(bufferList).length).toBe(1)
expect(bufferList["scratch.txt"]).toBeTruthy()
})

test("create new buffer from block", async ({page}) => {
await page.locator("body").press(heynotePage.agnosticKey("Mod+S"))
await page.waitForTimeout(50)
await page.locator("body").pressSequentially("My New Buffer")
await page.locator("body").press("Enter")
await page.waitForTimeout(50)
await page.locator("body").press("Enter")
await page.locator("body").pressSequentially("New buffer content")
await page.waitForTimeout(AUTO_SAVE_INTERVAL + 50);

const buffers = Object.keys(await heynotePage.getStoredBufferList())
expect(buffers).toContain("scratch.txt")
expect(buffers).toContain("my-new-buffer.txt")

const defaultBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("scratch.txt"))
const newBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("my-new-buffer.txt"))


expect(defaultBuffer.content).toBe(`
∞∞∞text
Block A
∞∞∞text
Block B`)

expect(newBuffer.content).toBe(`
∞∞∞text
Block C
New buffer content`)

})
12 changes: 12 additions & 0 deletions tests/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ export class HeynotePage {
async getStoredSettings() {
return await this.page.evaluate(() => JSON.parse(window.localStorage.getItem("settings")))
}

async getStoredBufferList() {
return await this.page.evaluate(() => window.heynote.buffer.getList())
}

async getStoredBuffer(path) {
return await this.page.evaluate((path) => window.heynote.buffer.load(path), path)
}

agnosticKey(key) {
return key.replace("Mod", this.isMac ? "Meta" : "Control")
}
}
7 changes: 3 additions & 4 deletions webapp/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Exception } from "sass";
import { SETTINGS_CHANGE_EVENT, OPEN_SETTINGS_EVENT } from "../electron/constants";
import { NoteFormat } from "../src/common/note-format";

const NOTE_KEY_PREFIX = "heynote-library__"

const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)')
let themeCallback = null
mediaMatch.addEventListener("change", async (event) => {
Expand Down Expand Up @@ -75,9 +77,6 @@ if (settingsData !== null) {
initialSettings = Object.assign(initialSettings, JSON.parse(settingsData))
}


const NOTE_KEY_PREFIX = "heynote-library__"

function noteKey(path) {
return NOTE_KEY_PREFIX + path
}
Expand Down Expand Up @@ -161,7 +160,7 @@ const Heynote = {
return localStorage.getItem(noteKey(path)) !== null
},

async getList(path) {
async getList() {
//return {"scratch.txt": {name:"Scratch"}}
const notes = {}
for (let [key, content] of Object.entries(localStorage)) {
Expand Down

0 comments on commit e51d094

Please sign in to comment.