Reputation: 197
I'm pretty new to react. I'm writing components which are going to be used within <svg>
tag.
I would like to have tests for these components.
However, all my test are failing to recognize valid children of <svg>
e.g. <path>
, <rect>
and <circle>
, etc.
import React from 'react';
import ReactDOM from 'react-dom';
import { Layout } from './Layout';
import { LayoutAlignmentHorizontal, LayoutAlignmentVertical, LayoutFlow } from './Layout.enum';
it('renders without crashing', () => {
const svg = document.createElement('svg');
svg.setAttribute('xmlns','http://www.w3.org/2000/svg');
ReactDOM.render(<Layout flow={LayoutFlow.COLUMN}
horizontalAlignment={LayoutAlignmentHorizontal.LEFT}
verticalAlignment={LayoutAlignmentVertical.TOP}>
<rect width={50} height={50} fill={'red'}/>
<circle r={15} fill={'blue'} transform="translate(15 15)"/>
<path transform="scale(.15)"
d="M 10,30 A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z"
fill={'green'}/>
</Layout>, svg);
ReactDOM.unmountComponentAtNode(svg);
});
Actual:
$ react-scripts test
FAIL src/containers/layout/Layout.test.tsx
● Console
console.error node_modules/react-dom/cjs/react-dom.development.js:506
Warning: The tag <rect> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.
in rect (at Layout.test.tsx:11)
in g (at Layout.tsx:60)
in g (at Layout.tsx:71)
in Layout (at Layout.test.tsx:10)
console.error node_modules/react-dom/cjs/react-dom.development.js:506
Warning: The tag <g> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.
in g (at Layout.tsx:60)
in g (at Layout.tsx:71)
in Layout (at Layout.test.tsx:10)
console.error node_modules/react-dom/cjs/react-dom.development.js:506
Warning: The tag <circle> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.
in circle (at Layout.test.tsx:12)
in g (at Layout.tsx:60)
in g (at Layout.tsx:71)
in Layout (at Layout.test.tsx:10)
console.error node_modules/react-dom/cjs/react-dom.development.js:506
Warning: The tag <path> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.
in path (at Layout.test.tsx:13)
in g (at Layout.tsx:60)
in g (at Layout.tsx:71)
in Layout (at Layout.test.tsx:10)
Expected: svg tags should be rendered
Upvotes: 3
Views: 10247
Reputation: 4522
I had this issue using a SVG component in my Jest test suite.
FWIW, you can just wrap the component with <svg>
to make Jest tests pass:
test('something', () => {
mount(<svg><MySVGComponent /></svg>)
})
Upvotes: 6
Reputation: 197
Thanks to DoMiNeLa10's comment.
React should render the <svg>
element, then it works :)
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<svg xmlns="http://www.w3.org/2000/svg">
<Layout flow={LayoutFlow.COLUMN}
horizontalAlignment={LayoutAlignmentHorizontal.LEFT}
verticalAlignment={LayoutAlignmentVertical.TOP}>
<rect width={50} height={50} fill={'red'}/>
<circle r={15} fill={'blue'} transform="translate(15 15)"/>
<path transform="scale(.15)"
d="M 10,30 A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z"
fill={'green'}/>
</Layout>
</svg>, div);
ReactDOM.unmountComponentAtNode(div);
});
Upvotes: 0