Skip to content

Commit

Permalink
test(playwright-ct): test focusPath reporting from PT-input
Browse files Browse the repository at this point in the history
Test that spans are reported with .text and everything else not.
  • Loading branch information
skogsmaskin committed Feb 19, 2024
1 parent a9c8eb9 commit 675fd22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {expect, test} from '@playwright/experimental-ct-react'
import {type Page} from '@playwright/test'
import {type Path, type SanityDocument} from '@sanity/types'

import {testHelpers} from '../../../utils/testHelpers'
import FocusTrackingStory from './FocusTrackingStory'

export type UpdateFn = () => {focusPath: Path; document: SanityDocument}
Expand Down Expand Up @@ -136,6 +137,24 @@ test.describe('Portable Text Input', () => {
await expect(blockObjectInput).not.toBeVisible()
})
})
test.only(`reports focus on spans with with .text prop, and everything else without`, async ({
mount,
page,
}) => {
const paths: Path[] = []
const pushPath = (path: Path) => paths.push(path)
await mount(<FocusTrackingStory document={document} onPathFocus={pushPath} />)
const {getFocusedPortableTextEditor} = testHelpers({page})
const $pte = await getFocusedPortableTextEditor('field-body')
await expect($pte).toBeFocused()
expect(paths.slice(-1)[0]).toEqual(['body', {_key: 'a'}, 'children', {_key: 'b'}, 'text'])
const $inlineObject = page.getByTestId('inline-preview')
await $inlineObject.click()
expect(paths.slice(-1)[0]).toEqual(['body', {_key: 'g'}, 'children', {_key: 'i'}])
const $blockObject = page.getByTestId('pte-block-object')
await $blockObject.click()
expect(paths.slice(-1)[0]).toEqual(['body', {_key: 'k'}])
})
})

function waitForFocusedNodeText(page: Page, text: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ const SCHEMA_TYPES = [

export function FocusTrackingStory({
focusPath,
onPathFocus,
document,
}: {
focusPath?: Path
onPathFocus?: (path: Path) => void
document?: SanityDocument
}) {
return (
<TestWrapper schemaTypes={SCHEMA_TYPES}>
<TestForm document={document} focusPath={focusPath} />
<TestForm document={document} focusPath={focusPath} onPathFocus={onPathFocus} />
</TestWrapper>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ declare global {

export function TestForm({
focusPath: focusPathFromProps,
onPathFocus: onPathFocusFromProps,
document: documentFromProps,
id: idFromProps = 'root',
}: {
focusPath?: Path
onPathFocus?: (path: Path) => void
document?: SanityDocument
id?: string
}) {
Expand Down Expand Up @@ -115,8 +117,9 @@ export function TestForm({
const handleFocus = useCallback(
(nextFocusPath: Path) => {
setFocusPath(nextFocusPath)
onPathFocusFromProps?.(nextFocusPath)
},
[setFocusPath],
[onPathFocusFromProps],
)

const handleBlur = useCallback(() => {
Expand Down

0 comments on commit 675fd22

Please sign in to comment.