Skip to content

Commit

Permalink
test: add tests for custom block types that contain children
Browse files Browse the repository at this point in the history
  • Loading branch information
fritz-c authored and snorrees committed Apr 26, 2023
1 parent 75f1ec4 commit 120892b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/fixtures/062-custom-block-type-with-children.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type {ArbitraryTypedObject} from '@portabletext/types'

const input: ArbitraryTypedObject[] = [
{
_type: 'quote',
_key: '9a15ea2ed8a2',
background: 'blue',
children: [
{
_type: 'span',
_key: '9a15ea2ed8a2',
text: 'This is an inspirational quote',
},
],
},
]

export default {
input,
output: '<p style="background:blue">Customers say: This is an inspirational quote</p>',
}
2 changes: 2 additions & 0 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import inlineBlockWithText from './026-inline-block-with-text'
import styledListItems from './027-styled-list-items'
import customListItemType from './028-custom-list-item-type'
import customBlockType from './050-custom-block-type'
import customBlockTypeWithChildren from './062-custom-block-type-with-children'
import customMarks from './052-custom-marks'
import overrideDefaultMarks from './053-override-default-marks'
import listIssue from './060-list-issue'
Expand Down Expand Up @@ -56,6 +57,7 @@ export {
styledListItems,
customListItemType,
customBlockType,
customBlockTypeWithChildren,
customMarks,
overrideDefaultMarks,
listIssue,
Expand Down
35 changes: 35 additions & 0 deletions test/portable-text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,41 @@ tap.test('can specify custom component for custom block types', (t) => {
t.end()
})

tap.test('can specify custom component for custom block types with children', (t) => {
const {input, output} = fixtures.customBlockTypeWithChildren
const types: Partial<PortableTextReactComponents>['types'] = {
quote: ({renderNode, ...props}) => {
t.same(props, {
value: {
_type: 'quote',
_key: '9a15ea2ed8a2',
background: 'blue',
children: [
{
_type: 'span',
_key: '9a15ea2ed8a2',
text: 'This is an inspirational quote',
},
],
},
index: 0,
isInline: false,
})

return (
<p style={{background: props.value.background}}>
{props.value.children.map(({text}) => (
<React.Fragment key={text}>Customers say: {text}</React.Fragment>
))}
</p>
)
},
}
const result = render({value: input, components: {types}})
t.same(result, output)
t.end()
})

tap.test('can specify custom components for custom marks', (t) => {
const {input, output} = fixtures.customMarks
const highlight: PortableTextMarkComponent<{_type: 'highlight'; thickness: number}> = ({
Expand Down

0 comments on commit 120892b

Please sign in to comment.