Skip to content

Commit

Permalink
chore(BackgroundImage): update tests (#9584)
Browse files Browse the repository at this point in the history
* chore(BackgroundImage): update tests

* add spread prop test, update test

* use styles obj

* update test name to use styles
  • Loading branch information
kmcfaul authored Sep 21, 2023
1 parent faddb9c commit 5a8c6bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { BackgroundImage } from '../BackgroundImage';
import styles from '@patternfly/react-styles/css/components/BackgroundImage/background-image';

test('has default className and src URL applied to style', () => {
test(`renders with default className ${styles.backgroundImage}`, () => {
render(<BackgroundImage src="/image/url.png" data-testid="test-id" />);
expect(screen.getByTestId('test-id')).toHaveClass(styles.backgroundImage, { exact: true });
});

test('spreads additional props', () => {
render(<BackgroundImage src="/image/url.png" data-testid="test-id" lang="en-US" />);
expect(screen.getByTestId('test-id')).toHaveProperty('lang');
});

const backgroundImage = screen.getByTestId('test-id');
const backgroundImageStyle = backgroundImage.getAttribute('style');
test('has src URL applied to style', () => {
render(<BackgroundImage src="/image/url.png" data-testid="test-id" />);

expect(backgroundImage).toHaveClass('pf-v5-c-background-image');
expect(backgroundImageStyle).toContain('--pf-v5-c-background-image--BackgroundImage');
expect(backgroundImageStyle).toContain('/image/url.png');
expect(screen.getByTestId('test-id')).toHaveAttribute(
'style',
'--pf-v5-c-background-image--BackgroundImage: url(/image/url.png);'
);
});

test('has additional className when one is provided', () => {
test('renders with custom className when one is provided', () => {
render(<BackgroundImage src="/image/url.png" className="another-class" data-testid="test-id" />);

expect(screen.getByTestId('test-id')).toHaveClass('another-class');
});

test('Matches the snapshot', () => {
const { asFragment } = render(<BackgroundImage src="/image/url.png" data-testid="test-id" />);
expect(asFragment()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Matches the snapshot 1`] = `
<DocumentFragment>
<div
class="pf-v5-c-background-image"
data-testid="test-id"
style="--pf-v5-c-background-image--BackgroundImage: url(/image/url.png);"
/>
</DocumentFragment>
`;

0 comments on commit 5a8c6bc

Please sign in to comment.