214 lines
8.0 KiB
TypeScript
214 lines
8.0 KiB
TypeScript
import type { Locator, Page } from '@playwright/test'
|
|
import { expect, test } from './support/test.js'
|
|
import { fieldGuide, forest, notebook } from './support/fixture.js'
|
|
|
|
function expectScrollHint(block: Locator, visible: boolean, divider = 'none') {
|
|
return expect
|
|
.poll(() =>
|
|
block.evaluate((node) => {
|
|
const { content, display, textAlign, borderBlockStartStyle } = getComputedStyle(
|
|
node,
|
|
'::after'
|
|
)
|
|
return { content, display, textAlign, borderBlockStartStyle }
|
|
})
|
|
)
|
|
.toMatchObject(
|
|
visible
|
|
? {
|
|
content: '"Scroll horizontally to see more" / ""',
|
|
display: 'block',
|
|
textAlign: 'center',
|
|
borderBlockStartStyle: divider
|
|
}
|
|
: { content: 'none' }
|
|
)
|
|
}
|
|
|
|
async function waitForHydration(page: Page) {
|
|
const search = page
|
|
.getByRole('navigation', { name: 'Global' })
|
|
.getByLabel('search', { exact: true })
|
|
await search.click()
|
|
await expect(page.getByRole('dialog', { name: 'Search', exact: true })).toBeVisible()
|
|
await page.keyboard.press('Escape')
|
|
}
|
|
|
|
async function clickInjectedNavigationLink(page: Page, href: string) {
|
|
// The fixture piece pages do not normally link to each other.
|
|
await page.evaluate((url) => {
|
|
const link = document.createElement('a')
|
|
link.href = url
|
|
link.textContent = 'Open next piece'
|
|
link.id = 'piece-math-test-link'
|
|
document.body.append(link)
|
|
}, href)
|
|
await page.locator('#piece-math-test-link').click()
|
|
await expect(page).toHaveURL(new URL(href, page.url()).href)
|
|
await page.locator('#piece-math-test-link').evaluate((link) => link.remove())
|
|
}
|
|
|
|
test('loads KaTeX and shows overflow hints after client navigation', async ({ page }) => {
|
|
await page.setViewportSize({ width: 320, height: 800 })
|
|
const katexRequests: string[] = []
|
|
page.on('request', (request) => {
|
|
if (/katex/i.test(new URL(request.url()).pathname)) katexRequests.push(request.url())
|
|
})
|
|
await page.goto('/pieces/notes')
|
|
await waitForHydration(page)
|
|
await page.evaluate(() => {
|
|
document.documentElement.dataset.mathNavigation = 'same-document'
|
|
})
|
|
const stylesheet = page.locator('head link[rel="stylesheet"][href*="katex"]')
|
|
await expect(stylesheet).toHaveCount(0)
|
|
expect(katexRequests).toHaveLength(0)
|
|
|
|
await page.getByRole('link', { name: forest.title, exact: true }).click()
|
|
await expect(page).toHaveURL(new RegExp(`${forest.publicUrl}$`))
|
|
await expect(page.locator('html')).toHaveAttribute('data-math-navigation', 'same-document')
|
|
await expect(stylesheet).toHaveCount(1)
|
|
const displays = page.locator('.katex-display')
|
|
await expect(displays).toHaveCount(2)
|
|
const [shortEquation, longEquation] = [displays.nth(0), displays.nth(1)]
|
|
await expectScrollHint(shortEquation, false)
|
|
await expectScrollHint(longEquation, true)
|
|
await expect(longEquation.locator('math annotation')).toContainText('x = 100')
|
|
|
|
await clickInjectedNavigationLink(page, notebook.publicUrl)
|
|
await expect(page.locator('html')).toHaveAttribute('data-math-navigation', 'same-document')
|
|
await expect(stylesheet).toHaveCount(1)
|
|
await expect(displays).toHaveCount(1)
|
|
await expectScrollHint(displays, true)
|
|
await expect(displays.locator('math annotation')).toContainText('y = 200')
|
|
|
|
await clickInjectedNavigationLink(page, fieldGuide.publicUrl)
|
|
await expect(page.locator('html')).toHaveAttribute('data-math-navigation', 'same-document')
|
|
await expect(stylesheet).toHaveCount(0)
|
|
await expect(displays).toHaveCount(0)
|
|
|
|
await page.goBack()
|
|
await expect(page).toHaveURL(new RegExp(`${notebook.publicUrl}$`))
|
|
await expect(stylesheet).toHaveCount(1)
|
|
await expectScrollHint(displays, true)
|
|
})
|
|
|
|
test('overflow hints and native scrolling work without JavaScript', async ({
|
|
browser,
|
|
baseURL
|
|
}) => {
|
|
const context = await browser.newContext({
|
|
baseURL,
|
|
javaScriptEnabled: false,
|
|
viewport: { width: 320, height: 800 }
|
|
})
|
|
try {
|
|
const page = await context.newPage()
|
|
await page.goto(forest.publicUrl)
|
|
await expect
|
|
.poll(() =>
|
|
page.evaluate(() =>
|
|
Array.from(document.fonts).some(
|
|
(face) => face.family === 'KaTeX_Main' && face.status === 'loaded'
|
|
)
|
|
)
|
|
)
|
|
.toBe(true)
|
|
const fraction = page.locator('.katex-display').filter({ has: page.locator('mfrac') })
|
|
expect(await fraction.evaluate((display) => display.scrollHeight - display.clientHeight)).toBe(
|
|
0
|
|
)
|
|
const equation = page.locator('.katex-display').filter({ has: page.locator('.tag') })
|
|
const code = page.locator('main .markdown pre')
|
|
const diagram = page.locator('main .markdown figure[data-luzzle-mermaid] > div')
|
|
const hiddenScrollbar = 'rgba(0, 0, 0, 0) rgba(0, 0, 0, 0)'
|
|
const visibleThumb = /^rgb\(\d+, \d+, \d+\) rgba\(0, 0, 0, 0\)$/
|
|
await expectScrollHint(fraction, false)
|
|
await expectScrollHint(equation, true)
|
|
await expectScrollHint(code, true, 'solid')
|
|
await expectScrollHint(diagram, true)
|
|
const diagramStart = await diagram.evaluate((region) => {
|
|
const svg = region.querySelector('svg')!
|
|
return {
|
|
scrollLeft: region.scrollLeft,
|
|
offset: svg.getBoundingClientRect().left - region.getBoundingClientRect().left
|
|
}
|
|
})
|
|
expect(diagramStart.scrollLeft).toBe(0)
|
|
expect(Math.abs(diagramStart.offset)).toBeLessThan(1)
|
|
for (const block of [equation, code, diagram]) {
|
|
await expect(block).toHaveCSS('scrollbar-color', hiddenScrollbar)
|
|
}
|
|
await diagram.focus()
|
|
await expect(diagram).toHaveCSS('scrollbar-color', visibleThumb)
|
|
await diagram.evaluate((block) => (block as HTMLElement).blur())
|
|
await expect(diagram).toHaveCSS('scrollbar-color', hiddenScrollbar)
|
|
await expect(equation).toHaveCSS('overflow-x', 'auto')
|
|
const restHeight = await equation.evaluate((display) => display.getBoundingClientRect().height)
|
|
await equation.hover()
|
|
await expect(equation).toHaveCSS('scrollbar-color', visibleThumb)
|
|
expect(await equation.evaluate((display) => display.getBoundingClientRect().height)).toBe(
|
|
restHeight
|
|
)
|
|
const scrollStart = await equation.evaluate((display) => display.scrollLeft)
|
|
await page.mouse.wheel(2000, 0)
|
|
await expect
|
|
.poll(() => equation.evaluate((display) => display.scrollLeft))
|
|
.toBeGreaterThan(scrollStart)
|
|
await expect
|
|
.poll(() =>
|
|
equation.evaluate((display) =>
|
|
Math.abs(display.scrollWidth - display.clientWidth - display.scrollLeft)
|
|
)
|
|
)
|
|
.toBeLessThan(2)
|
|
await expectScrollHint(equation, true)
|
|
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBe(320)
|
|
expect(
|
|
await equation.evaluate((display) => {
|
|
const tag = display.querySelector('.katex-html > .tag')!
|
|
const finalTerm = tag.previousElementSibling!
|
|
const tagBounds = tag.getBoundingClientRect()
|
|
return (
|
|
tagBounds.left >= finalTerm.getBoundingClientRect().right &&
|
|
tagBounds.right <= display.getBoundingClientRect().right + 1
|
|
)
|
|
})
|
|
).toBe(true)
|
|
|
|
await page.setViewportSize({ width: 1000, height: 800 })
|
|
for (const block of [equation, code, diagram]) await expectScrollHint(block, false)
|
|
const image = page.getByRole('img', { name: 'Standalone image' })
|
|
for (const media of [diagram.locator('svg'), image]) {
|
|
const { left, right } = await media.evaluate((element) => {
|
|
const content = element.getBoundingClientRect()
|
|
const container = element.parentElement!.getBoundingClientRect()
|
|
return {
|
|
left: content.left - container.left,
|
|
right: container.right - content.right
|
|
}
|
|
})
|
|
expect(left).toBeGreaterThan(0)
|
|
expect(Math.abs(left - right)).toBeLessThan(1)
|
|
}
|
|
|
|
await page.setViewportSize({ width: 320, height: 800 })
|
|
await diagram.evaluate((region) => {
|
|
// An authored template can put a diagram by itself in a shrink-to-fit column.
|
|
const column = document.createElement('div')
|
|
column.style.inlineSize = 'fit-content'
|
|
const figure = region.closest('figure')!
|
|
figure.before(column)
|
|
column.append(figure)
|
|
})
|
|
const { clientWidth, scrollWidth } = await diagram.evaluate((region) => ({
|
|
clientWidth: region.clientWidth,
|
|
scrollWidth: region.scrollWidth
|
|
}))
|
|
expect(clientWidth).toBeGreaterThan(0)
|
|
expect(scrollWidth).toBeGreaterThan(clientWidth)
|
|
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBe(320)
|
|
} finally {
|
|
await context.close()
|
|
}
|
|
})
|