Skip to content

Commit

Permalink
Revise unit test approach per feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Sep 11, 2023
1 parent 60d617f commit 376cc58
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 29 deletions.
31 changes: 31 additions & 0 deletions packages/rich-text/src/test/__snapshots__/to-dom.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ exports[`recordToDom should filter format boundary attributes 1`] = `
</body>
`;

exports[`recordToDom should handle SVG 1`] = `
<body>
<svg
contentEditable="false"
>
<path
d="M0,0 v2 h4"
/>
</svg>
</body>
`;

exports[`recordToDom should handle br 1`] = `
<body>
Expand Down Expand Up @@ -215,6 +230,22 @@ exports[`recordToDom should handle selection before br 1`] = `
</body>
`;

exports[`recordToDom should handle xlink 1`] = `
<body>
<svg
contentEditable="false"
>
<use
href="#a"
xlink:href="#a"
/>
</svg>
</body>
`;

exports[`recordToDom should ignore manually added object replacement character 1`] = `
<body>
test
Expand Down
46 changes: 46 additions & 0 deletions packages/rich-text/src/test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const em = { type: 'em' };
const strong = { type: 'strong' };
const img = { type: 'img', attributes: { src: '' } };
const a = { type: 'a', attributes: { href: '#' } };
const svg = { type: 'svg' };
const path = { type: 'path', attributes: { d: 'M0,0 v2 h4' } };
const use = { type: 'use', attributes: { 'xlink:href': '#a', href: '#a' } };

export const spec = [
{
Expand Down Expand Up @@ -570,6 +573,49 @@ export const spec = [
text: '\ufffc',
},
},
{
description: 'should handle SVG',
html: '<svg><path d="M0,0 v2 h4"/></svg>',
NS_URI: 'http://www.w3.org/2000/svg',
selectTarget: ( body ) => body.querySelector( 'path' ),
createRange: ( element ) => ( {
startOffset: 0,
startContainer: element,
endOffset: 0,
endContainer: element,
} ),
startPath: [ 0, 0, 0 ],
endPath: [ 0, 0, 0 ],
record: {
start: 0,
end: 0,
formats: [ [ svg ] ],
replacements: [ path ],
text: '\ufffc',
},
},
{
description: 'should handle xlink',
html: '<svg><use xlink:href="#a" href="#a"/></svg>',
NS_URI: 'http://www.w3.org/1999/xlink',
selectTarget: ( body ) =>
body.querySelector( 'use' ).getAttributeNode( 'xlink:href' ),
createRange: ( element ) => ( {
startOffset: 0,
startContainer: element,
endOffset: 0,
endContainer: element,
} ),
startPath: [ 0, 0, 0 ],
endPath: [ 0, 0, 0 ],
record: {
start: 0,
end: 0,
formats: [ [ svg ] ],
replacements: [ use ],
text: '\ufffc',
},
},
];

export const specWithRegistration = [
Expand Down
38 changes: 9 additions & 29 deletions packages/rich-text/src/test/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ describe( 'recordToDom', () => {
expect( selection ).toEqual( { startPath, endPath } );
} );
} );

spec.filter( ( props ) => 'NS_URI' in props ).forEach(
( { description, NS_URI, record, selectTarget } ) => {
it( `${ description } with correct namespace`, () => {
const { body } = toDom( { value: record } );
expect( selectTarget( body ).namespaceURI ).toEqual( NS_URI );
} );
}
);
} );

describe( 'applyValue', () => {
Expand Down Expand Up @@ -98,32 +107,3 @@ describe( 'applyValue', () => {
} );
} );
} );

describe( 'toDom-SVG', () => {
let body;
beforeAll( () => {
const svg = { type: 'svg' };
const use = { type: 'use', attributes: { 'xlink:href': '#logo' } };
body = toDom( {
value: {
start: 0,
end: 1,
formats: [ [ svg ] ],
replacements: [ use ],
text: '\ufffc',
},
} ).body;
} );

it( 'should create nodes with svg namespace', () => {
const target = body.firstElementChild;
expect( target.namespaceURI ).toEqual( 'http://www.w3.org/2000/svg' );
} );

it( 'should create attribute xlink:href with xlink namespace', () => {
const target = body
.querySelector( 'use' )
.getAttributeNode( 'xlink:href' );
expect( target.namespaceURI ).toEqual( 'http://www.w3.org/1999/xlink' );
} );
} );

0 comments on commit 376cc58

Please sign in to comment.