Skip to content

Commit 5130d25

Browse files
committed
add spec
1 parent c818255 commit 5130d25

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

src/components/Text.spec.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Paragraph, H2, H3 } from './Text';
1+
import { Paragraph, H2, H3, Heading } from './Text';
22
import renderer from 'react-test-renderer';
3+
import { render } from '@testing-library/react';
4+
import React from 'react';
35

46
describe('Text', () => {
57
it('matches body snapshot', () => {
@@ -24,3 +26,41 @@ describe('Text', () => {
2426
});
2527

2628
});
29+
30+
describe('Heading', () => {
31+
it('defaults variant to match the semantic level', () => {
32+
const tree = renderer.create(
33+
<Heading level={2}>This is a heading</Heading>
34+
).toJSON();
35+
expect(tree).toMatchObject({ type: 'h2', props: { className: 'text-h2' } });
36+
});
37+
38+
it('renders the element for the given level', () => {
39+
const tree = renderer.create(
40+
<Heading level={4}>This is a heading</Heading>
41+
).toJSON();
42+
expect((tree as unknown as { type: string }).type).toBe('h4');
43+
});
44+
45+
it('decouples visual style from semantic level', () => {
46+
// an <h2> element styled like an h3
47+
const tree = renderer.create(
48+
<Heading level={2} variant="h3">This is a heading</Heading>
49+
).toJSON();
50+
expect(tree).toMatchObject({ type: 'h2', props: { className: 'text-h3' } });
51+
});
52+
53+
it('merges a consumer className with the variant class', () => {
54+
const tree = renderer.create(
55+
<Heading level={2} className="custom">This is a heading</Heading>
56+
).toJSON();
57+
expect((tree as unknown as { props: { className: string } }).props.className).toBe('text-h2 custom');
58+
});
59+
60+
it('forwards a ref to the underlying element', () => {
61+
const ref = React.createRef<HTMLHeadingElement>();
62+
render(<Heading level={2} ref={ref}>This is a heading</Heading>);
63+
expect(ref.current).not.toBeNull();
64+
expect(ref.current?.tagName).toBe('H2');
65+
});
66+
});

0 commit comments

Comments
 (0)