Skip to content

Commit

Permalink
fix(test): refactor and fix all e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf authored and tiensonqin committed Apr 2, 2022
1 parent dbeca54 commit f1af534
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 259 deletions.
255 changes: 119 additions & 136 deletions e2e-tests/basic.spec.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions e2e-tests/code-editing.spec.ts
Expand Up @@ -19,7 +19,7 @@ test('switch code editing mode', async ({ page }) => {
// NOTE: multiple textarea elements are existed in the editor, be careful to select the right one

// code block with 0 line
await page.type(':nth-match(textarea, 1)', '```clojure\n')
await page.type('textarea >> nth=0', '```clojure\n')
// line number: 1
await page.waitForSelector('.CodeMirror pre', { state: 'visible' })
expect(await page.locator('.CodeMirror-gutter-wrapper .CodeMirror-linenumber').innerText()).toBe('1')
Expand All @@ -28,10 +28,10 @@ test('switch code editing mode', async ({ page }) => {

await page.press('.CodeMirror textarea', 'Escape')
await page.waitForSelector('.CodeMirror pre', { state: 'hidden' })
expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('```clojure\n```')
expect(await page.inputValue('textarea >> nth=0')).toBe('```clojure\n```')

await page.waitForTimeout(200)
await page.press(':nth-match(textarea, 1)', 'Escape')
await page.press('textarea >> nth=0', 'Escape')
await page.waitForSelector('.CodeMirror pre', { state: 'visible' })

// NOTE: must wait here, await loading of CodeMirror editor
Expand Down
50 changes: 28 additions & 22 deletions e2e-tests/dnd.spec.ts
@@ -1,6 +1,6 @@
import { expect } from '@playwright/test'
import { test } from './fixtures'
import { createRandomPage } from './utils'
import { createRandomPage, enterNextBlock } from './utils'

/**
* Drag and Drop tests.
Expand All @@ -11,66 +11,72 @@ import { createRandomPage } from './utils'
test('drop to left center', async ({ page }) => {
await createRandomPage(page)

await page.fill(':nth-match(textarea, 1)', 'block a')
await page.press(':nth-match(textarea, 1)', 'Enter')
await page.fill('textarea >> nth=0', 'block a')
await enterNextBlock(page)

await page.fill(':nth-match(textarea, 1)', 'block b')
await page.press(':nth-match(textarea, 1)', 'Escape')
await page.fill('textarea >> nth=0', 'block b')
await page.press('textarea >> nth=0', 'Escape')

const bullet = page.locator('span.bullet-container >> nth=-1')
const where = page.locator('div.ls-block >> nth=0')
const where = page.locator('.ls-block >> nth=0')
await bullet.dragTo(where, {
targetPosition: {
x: 30,
y: (await where.boundingBox()).height * 0.5
}
})

expect.soft(await page.locator('div.ls-block >> nth=0').innerText()).toBe("block b")
expect.soft(await page.locator('div.ls-block >> nth=1').innerText()).toBe("block a")
await page.keyboard.press('Escape')

const pageElem = page.locator('.page-blocks-inner')
await expect(pageElem).toHaveText('block b\nblock a', {useInnerText: true})
})


test('drop to upper left', async ({ page }) => {
await createRandomPage(page)

await page.fill(':nth-match(textarea, 1)', 'block a')
await page.press(':nth-match(textarea, 1)', 'Enter')
await page.fill('textarea >> nth=0', 'block a')
await enterNextBlock(page)

await page.fill(':nth-match(textarea, 1)', 'block b')
await page.press(':nth-match(textarea, 1)', 'Escape')
await page.fill('textarea >> nth=0', 'block b')
await page.press('textarea >> nth=0', 'Escape')

const bullet = page.locator('span.bullet-container >> nth=-1')
const where = page.locator('div.ls-block >> nth=0')
const where = page.locator('.ls-block >> nth=0')
await bullet.dragTo(where, {
targetPosition: {
x: 30,
y: 5
}
})

expect.soft(await page.locator('div.ls-block >> nth=0').innerText()).toBe("block b")
expect.soft(await page.locator('div.ls-block >> nth=1').innerText()).toBe("block a")
await page.keyboard.press('Escape')

const pageElem = page.locator('.page-blocks-inner')
await expect(pageElem).toHaveText('block b\nblock a', {useInnerText: true})
})

test('drop to bottom left', async ({ page }) => {
await createRandomPage(page)

await page.fill(':nth-match(textarea, 1)', 'block a')
await page.press(':nth-match(textarea, 1)', 'Enter')
await page.fill('textarea >> nth=0', 'block a')
await enterNextBlock(page)

await page.fill(':nth-match(textarea, 1)', 'block b')
await page.press(':nth-match(textarea, 1)', 'Escape')
await page.fill('textarea >> nth=0', 'block b')
await page.press('textarea >> nth=0', 'Escape')

const bullet = page.locator('span.bullet-container >> nth=-1')
const where = page.locator('div.ls-block >> nth=0')
const where = page.locator('.ls-block >> nth=0')
await bullet.dragTo(where, {
targetPosition: {
x: 30,
y: (await where.boundingBox()).height * 0.75
}
})

expect.soft(await page.locator('div.ls-block >> nth=0').innerText()).toBe("block a")
expect.soft(await page.locator('div.ls-block >> nth=1').innerText()).toBe("block b")
await page.keyboard.press('Escape')

const pageElem = page.locator('.page-blocks-inner')
await expect(pageElem).toHaveText('block a\nblock b', {useInnerText: true})
})
30 changes: 15 additions & 15 deletions e2e-tests/editor.spec.ts
@@ -1,6 +1,6 @@
import { expect } from '@playwright/test'
import { test } from './fixtures'
import { createRandomPage } from './utils'
import { createRandomPage, enterNextBlock, IsMac } from './utils'
import { dispatch_kb_events } from './util/keyboard-events'
import * as kb_events from './util/keyboard-events'

Expand Down Expand Up @@ -43,23 +43,24 @@ test(
test('hashtag and quare brackets in same line #4178', async ({ page }) => {
await createRandomPage(page)

await page.type(':nth-match(textarea, 1)', '#foo bar')
await page.press(':nth-match(textarea, 1)', 'Enter')
await page.type(':nth-match(textarea, 1)', 'bar [[blah]]')
await page.type('textarea >> nth=0', '#foo bar')
await enterNextBlock(page)
await page.type('textarea >> nth=0', 'bar [[blah]]', { delay: 100})

for (let i = 0; i < 12; i++) {
await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
await page.press('textarea >> nth=0', 'ArrowLeft')
}
await page.type(':nth-match(textarea, 1)', ' ')
await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
await page.type('textarea >> nth=0', ' ')
await page.press('textarea >> nth=0', 'ArrowLeft')

await page.type(':nth-match(textarea, 1)', '#')
await page.type('textarea >> nth=0', '#')
await page.waitForSelector('text="Search for a page"', { state: 'visible' })

await page.type(':nth-match(textarea, 1)', 'fo')
await page.type('textarea >> nth=0', 'fo')

await page.click('.absolute >> text=' + 'foo')

expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(
expect(await page.inputValue('textarea >> nth=0')).toBe(
'#foo bar [[blah]]'
)
})
Expand All @@ -68,7 +69,7 @@ test('hashtag and quare brackets in same line #4178', async ({ page }) => {
// test('copy & paste block ref and replace its content', async ({ page }) => {
// await createRandomPage(page)

// await page.type(':nth-match(textarea, 1)', 'Some random text')
// await page.type('textarea >> nth=0', 'Some random text')
// if (IsMac) {
// await page.keyboard.press('Meta+c')
// } else {
Expand All @@ -77,7 +78,7 @@ test('hashtag and quare brackets in same line #4178', async ({ page }) => {

// await page.pause()

// await page.press(':nth-match(textarea, 1)', 'Enter')
// await page.press('textarea >> nth=0', 'Enter')
// if (IsMac) {
// await page.keyboard.press('Meta+v')
// } else {
Expand All @@ -95,14 +96,13 @@ test('hashtag and quare brackets in same line #4178', async ({ page }) => {

// // Move cursor into the block ref
// for (let i = 0; i < 4; i++) {
// await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
// await page.press('textarea >> nth=0', 'ArrowLeft')
// }

// // Trigger replace-block-reference-with-content-at-point
// if (IsMac) {
// await page.keyboard.press('Meta+Shift+r')
// } else {
// await page.keyboard.press('Control+Shift+v')
// }
// }
// })

16 changes: 10 additions & 6 deletions e2e-tests/fixtures.ts
Expand Up @@ -38,6 +38,7 @@ base.beforeAll(async () => {
return
}

console.log(`Creating test graph directory: ${graphDir}`)
fs.mkdirSync(graphDir, {
recursive: true,
});
Expand Down Expand Up @@ -66,15 +67,15 @@ base.beforeAll(async () => {
// Direct Electron console to watcher
page.on('console', consoleLogWatcher)
page.on('crash', () => {
expect('page must not crash!').toBe('page crashed')
expect(false, "Page must not crash").toBeTruthy()
})
page.on('pageerror', (err) => {
console.error("[pageerror]", err)
expect('page must not have errors!').toBe('page has some error')
console.log(err)
expect(false, 'Page must not have errors!').toBeTruthy()
})

await page.waitForLoadState('domcontentloaded')
await page.waitForFunction('window.document.title != "Loading"')
// await page.waitForFunction(() => window.document.title != "Loading")
// NOTE: The following ensures first start.
// await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')

Expand Down Expand Up @@ -106,7 +107,7 @@ base.afterAll(async () => {
})

// hijack electron app into the test context
export const test = base.extend<{ page: Page, context: BrowserContext, app: ElectronApplication }>({
export const test = base.extend<{ page: Page, context: BrowserContext, app: ElectronApplication, graphDir: string }>({
page: async ({ }, use) => {
await use(page);
},
Expand All @@ -115,5 +116,8 @@ export const test = base.extend<{ page: Page, context: BrowserContext, app: Elec
},
app: async ({ }, use) => {
await use(electronApp);
}
},
graphDir: async ({ }, use) => {
await use(graphDir);
},
});
36 changes: 18 additions & 18 deletions e2e-tests/hotkey.spec.ts
Expand Up @@ -8,8 +8,7 @@ test('open search dialog', async ({ page }) => {
} else if (IsLinux) {
await page.keyboard.press('Control+k')
} else {
// TODO: test on Windows and other platforms
expect(false)
expect(false, "TODO: test on Windows and other platforms").toBeTruthy()
}

await page.waitForSelector('[placeholder="Search or create page"]')
Expand All @@ -30,26 +29,27 @@ test('insert link', async ({ page }) => {

// Case 1: empty link
await lastBlock(page)
await page.press(':nth-match(textarea, 1)', hotKey)
expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[]()')
await page.type(':nth-match(textarea, 1)', 'Logseq Website')
expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq Website]()')
await page.press('textarea >> nth=0', hotKey)
expect(await page.inputValue('textarea >> nth=0')).toBe('[]()')
await page.type('textarea >> nth=0', 'Logseq Website')
expect(await page.inputValue('textarea >> nth=0')).toBe('[Logseq Website]()')
await page.fill('textarea >> nth=0', '[Logseq Website](https://logseq.com)')

// Case 2: link with label
await newBlock(page)
await page.type(':nth-match(textarea, 1)', 'Logseq')
await page.press(':nth-match(textarea, 1)', selectAll)
await page.press(':nth-match(textarea, 1)', hotKey)
expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq]()')
await page.type(':nth-match(textarea, 1)', 'https://logseq.com/')
expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq](https://logseq.com/)')
await page.type('textarea >> nth=0', 'Logseq')
await page.press('textarea >> nth=0', selectAll)
await page.press('textarea >> nth=0', hotKey)
expect(await page.inputValue('textarea >> nth=0')).toBe('[Logseq]()')
await page.type('textarea >> nth=0', 'https://logseq.com/')
expect(await page.inputValue('textarea >> nth=0')).toBe('[Logseq](https://logseq.com/)')

// Case 3: link with URL
await newBlock(page)
await page.type(':nth-match(textarea, 1)', 'https://logseq.com/')
await page.press(':nth-match(textarea, 1)', selectAll)
await page.press(':nth-match(textarea, 1)', hotKey)
expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[](https://logseq.com/)')
await page.type(':nth-match(textarea, 1)', 'Logseq')
expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq](https://logseq.com/)')
await page.type('textarea >> nth=0', 'https://logseq.com/')
await page.press('textarea >> nth=0', selectAll)
await page.press('textarea >> nth=0', hotKey)
expect(await page.inputValue('textarea >> nth=0')).toBe('[](https://logseq.com/)')
await page.type('textarea >> nth=0', 'Logseq')
expect(await page.inputValue('textarea >> nth=0')).toBe('[Logseq](https://logseq.com/)')
})
8 changes: 5 additions & 3 deletions e2e-tests/page-rename.spec.ts
@@ -1,6 +1,6 @@
import { expect } from '@playwright/test'
import { test } from './fixtures'
import { IsMac, createPage, newBlock, newInnerBlock, randomString, lastInnerBlock } from './utils'
import { IsMac, createPage, newBlock, newInnerBlock, randomString, lastBlock } from './utils'

/***
* Test rename feature
Expand All @@ -18,19 +18,21 @@ async function page_rename_test(page, original_page_name: string, new_page_name:

await createPage(page, original_name)
await page.click('.page-title .title')
await page.waitForSelector('input[type="text"]')
await page.keyboard.press(selectAll)
await page.keyboard.press('Backspace')
await page.type('.title input', new_name)
await page.keyboard.press('Enter')
await page.click('.ui__confirm-modal button')

expect(await page.innerText('.page-title .title')).toBe(new_name)

// TODO: Test if page is renamed in re-entrance

// TODO: Test if page is hierarchy
}

test('page rename test', async ({ page }) => {
await page_rename_test(page, "abcd", "a.b.c.d")
await page_rename_test(page, "abcd", "a/b/c/d")
})
})

0 comments on commit f1af534

Please sign in to comment.