Skip to content

Commit

Permalink
test: add e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Jan 16, 2025
1 parent 9c31c88 commit 53d0195
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
10 changes: 3 additions & 7 deletions e2e/shim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { Editor } from '@milkdown/core'
import type { EditorView } from '@milkdown/prose/view'
import type { Telemetry } from '@milkdown/ctx'
import type { Crepe } from '@milkdown/crepe'

declare global {
var __milkdown__: Editor
Expand All @@ -14,13 +15,8 @@ declare global {

var __inspect__: () => Telemetry[]

namespace Cypress {
interface Chainable {
paste: (payload: Record<string, unknown>) => Chainable<void>
isMarkdown: (markdown: string) => Chainable<void>
markdownFixture: (path: string) => Chainable<void>
}
}
var __beforeCrepeCreate__: (crepe: Crepe) => void
var __afterCrepeCreated__: (crepe: Crepe) => void

var commands: {
toggleStrong?: () => void
Expand Down
6 changes: 6 additions & 0 deletions e2e/src/crepe/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ setup(async () => {
const crepe = new Crepe({
root: '#app',
})
if (globalThis.__beforeCrepeCreate__) {
globalThis.__beforeCrepeCreate__(crepe)
}
await crepe.create()
if (globalThis.__afterCrepeCreated__) {
globalThis.__afterCrepeCreated__(crepe)
}
return crepe.editor
})
52 changes: 52 additions & 0 deletions e2e/tests/crepe/listener.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect, test } from '@playwright/test'
import { focusEditor } from 'tests/misc';

test('listen change before editor is created', async ({ page }) => {
await page.addInitScript(() => {
window.__beforeCrepeCreate__ = (crepe) => {
crepe.on(listener => {
listener.markdownUpdated((_, markdown) => {
// oxlint-disable-next-line no-console
console.log('markdown', markdown)
})
})
}
});
await page.goto('/crepe/')

const messagePromise = page.waitForEvent('console', (msg) => {
return msg.text().includes('Hello');
});

await focusEditor(page)
await page.keyboard.type('Hello')

const message = await messagePromise;
const log: string = await message.args()[1]?.jsonValue()
expect(log.trim()).toBe('Hello')
});

test('listen change after editor is created', async ({ page }) => {
await page.addInitScript(() => {
window.__afterCrepeCreated__ = (crepe) => {
crepe.on(listener => {
listener.markdownUpdated((_, markdown) => {
// oxlint-disable-next-line no-console
console.log('markdown', markdown)
})
})
}
});
await page.goto('/crepe/')

const messagePromise = page.waitForEvent('console', (msg) => {
return msg.text().includes('World');
});

await focusEditor(page)
await page.keyboard.type('World')

const message = await messagePromise;
const log: string = await message.args()[1]?.jsonValue()
expect(log.trim()).toBe('World')
})

0 comments on commit 53d0195

Please sign in to comment.