import * as React from 'react';
import kebabCase from 'lodash/kebabCase';
import { render, screen } from '@testing-library/react';
import { Heading } from '../Heading';
import { ComponentClassNames } from '../../shared';
import { ComponentPropsToStylePropsMap } from '../../types';
describe('Heading:', () => {
it('renders an h6 tag by default', async () => {
render();
const heading = await screen.findByTestId('headingId');
expect(heading.nodeName).toBe('H6');
});
it('should render the heading classes', async () => {
render(
H1
H2
H3
H4
H5
H6
);
const h1 = await screen.findByTestId('h1');
const h2 = await screen.findByTestId('h2');
const h3 = await screen.findByTestId('h3');
const h4 = await screen.findByTestId('h4');
const h5 = await screen.findByTestId('h5');
const h6 = await screen.findByTestId('h6');
expect(h1.classList).toContain(`${ComponentClassNames['Heading']}--1`);
expect(h2.classList).toContain(`${ComponentClassNames['Heading']}--2`);
expect(h3.classList).toContain(`${ComponentClassNames['Heading']}--3`);
expect(h4.classList).toContain(`${ComponentClassNames['Heading']}--4`);
expect(h5.classList).toContain(`${ComponentClassNames['Heading']}--5`);
expect(h6.classList).toContain(`${ComponentClassNames['Heading']}--6`);
});
it('renders h1-h6 tags by passing level prop', async () => {
render(
H1
H2
H3
H4
H5
H6
);
const h1 = await screen.findByTestId('h1');
const h2 = await screen.findByTestId('h2');
const h3 = await screen.findByTestId('h3');
const h4 = await screen.findByTestId('h4');
const h5 = await screen.findByTestId('h5');
const h6 = await screen.findByTestId('h6');
expect(h1.nodeName).toBe('H1');
expect(h2.nodeName).toBe('H2');
expect(h3.nodeName).toBe('H3');
expect(h4.nodeName).toBe('H4');
expect(h5.nodeName).toBe('H5');
expect(h6.nodeName).toBe('H6');
});
it('can apply styling via props', async () => {
render();
const heading = await screen.findByTestId('headingId');
expect(heading.nodeName).toBe('H3');
expect(
heading.style.getPropertyValue(
kebabCase(ComponentPropsToStylePropsMap.fontStyle)
)
).toBe('italic');
});
it('can apply a custom className', async () => {
render();
const heading = await screen.findByTestId('headingId');
expect(heading.classList.contains('custom-heading')).toBe(true);
expect(heading.classList.contains(ComponentClassNames.Heading)).toBe(true);
});
it('can forward ref to DOM element', async () => {
const ref = React.createRef();
const headingText = 'Title';
render(
{headingText}
);
await screen.findByRole('heading');
expect(ref.current?.nodeName).toBe('H2');
expect(ref.current?.innerHTML).toBe(headingText);
});
it('can render any arbitrary data-* attribute', async () => {
render();
const heading = await screen.findByTestId('dataTest');
expect(heading.dataset['demo']).toBe('true');
});
it('should render the truncated class on Heading', async () => {
render(
truncated
);
const truncated = await screen.findByTestId('truncated');
expect(truncated.classList).toContain(
`${ComponentClassNames['Heading']}--truncated`
);
});
});