Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Brand): update tests #9543

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 105 additions & 30 deletions packages/react-core/src/components/Brand/__tests__/Brand.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,109 @@ import { render, screen } from '@testing-library/react';
import { Brand } from '../Brand';
import '@testing-library/jest-dom';

describe('Brand', () => {
test('simple brand', () => {
const { asFragment } = render(<Brand alt="brand" />);
expect(asFragment()).toMatchSnapshot();
});

test('passing children creates picture brand', () => {
render(
<Brand alt="brand">
<div>test</div>
</Brand>
);
expect(screen.getByText('test')).toBeInTheDocument();
});

test('styles get spread when there are children', () => {
render(
<Brand alt="brand" data-testid="brand" style={{color: "blue"}}>
<div>test width</div>
</Brand>
);
expect(screen.getByTestId('brand')).toHaveStyle(`color: blue`);
});

test('styles get spread when there are no children', () => {
render(
<Brand alt="brand no children" style={{width: "30px"}} />
);
expect(screen.getByAltText('brand no children')).toHaveStyle(`width: 30px`);
});
test('simple brand', () => {
render(<Brand alt="brand" />);
expect(screen.getByAltText('brand')).toBeInTheDocument();
});

test('Renders with custom class name when className prop is provided', () => {
render(<Brand alt="brand" className="custom-class" />);
expect(screen.getByAltText('brand')).toHaveClass('custom-class');
});
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above, just testing the className is passed correctly when Brand has children


test('Renders with custom class name when className prop and children are provided', () => {
render(
<Brand alt="brand" className="custom-class">
<div>test</div>
</Brand>
);
expect(screen.getByText('test')?.parentElement).toHaveClass('custom-class');
});

test('Renders passed children', () => {
render(
<Brand alt="brand">
<div>test</div>
</Brand>
);
expect(screen.getByText('test')).toBeInTheDocument();
});

test('Renders as picture when children are present', () => {
render(
<Brand alt="brand">
<div>test</div>
</Brand>
);
expect(screen.getByText('test')?.parentElement?.tagName).toBe('PICTURE');
});

test('Renders as img when no children are present', () => {
render(<Brand alt="brand" />);
expect(screen.getByAltText('brand')?.tagName).toBe('IMG');
});

test('Has correct src with passed src prop', () => {
render(<Brand alt="brand" src="test.png" />);
const image = screen.getByRole('img') as HTMLImageElement;
expect(image.src).toMatch('test.png');
});

test('Has correct alt text with passed alt prop', () => {
render(<Brand alt="brand" src="test.png" />);

expect(screen.getByAltText('brand')).toBeTruthy();
});

test('Has correct src with passed children', () => {
render(
<Brand alt="brand" src="test.png">
<div>test width</div>
</Brand>
);
const image = screen.getByRole('img') as HTMLImageElement;
expect(image.src).toMatch('test.png');
});

test('Has correct alt text with passed children', () => {
render(
<Brand alt="brand" src="test.png">
<div>test width</div>
</Brand>
);
expect(screen.getByAltText('brand')).toBeTruthy();
});

test('styles get spread when there are children', () => {
render(
<Brand alt="brand" data-testid="brand" style={{ color: 'blue' }}>
<div>test width</div>
</Brand>
);
expect(screen.getByTestId('brand')).toHaveStyle(`color: blue`);
});

test('styles get spread when there are no children', () => {
render(<Brand alt="brand no children" style={{ width: '30px' }} />);
expect(screen.getByAltText('brand no children')).toHaveStyle(`width: 30px`);
});

test('width styles are present when passed', () => {
render(
<Brand
alt="brand with widths"
widths={{
default: '100px',
sm: '25px',
md: '50px',
lg: '100px',
xl: '125px',
'2xl': '150px'
}}
/>
);
expect(screen.getByAltText('brand with widths')).toHaveAttribute(
'style',
'--pf-v5-c-brand--Width: 100px; --pf-v5-c-brand--Width-on-sm: 25px; --pf-v5-c-brand--Width-on-md: 50px; --pf-v5-c-brand--Width-on-lg: 100px; --pf-v5-c-brand--Width-on-xl: 125px; --pf-v5-c-brand--Width-on-2xl: 150px;'
);
});

This file was deleted.