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

Background image block support: Add tests for size and repeat output #57474

Merged
merged 1 commit into from
Jan 2, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 87 additions & 0 deletions packages/style-engine/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ describe( 'getCSSRules', () => {
expect(
getCSSRules(
{
background: {
backgroundImage: {
source: 'file',
url: 'https://example.com/image.jpg',
},
backgroundRepeat: 'no-repeat',
backgroundSize: '300px',
},
color: {
text: '#dddddd',
background: '#555555',
Expand Down Expand Up @@ -371,6 +379,21 @@ describe( 'getCSSRules', () => {
key: 'boxShadow',
value: '10px 10px red',
},
{
selector: '.some-selector',
key: 'backgroundImage',
value: "url( 'https://example.com/image.jpg' )",
},
{
selector: '.some-selector',
key: 'backgroundRepeat',
value: 'no-repeat',
},
{
selector: '.some-selector',
key: 'backgroundSize',
value: '300px',
},
] );
} );

Expand Down Expand Up @@ -400,4 +423,68 @@ describe( 'getCSSRules', () => {
},
] );
} );

it( 'should output fallback cover background size when no size is provided', () => {
expect(
getCSSRules(
{
background: {
backgroundImage: {
source: 'file',
url: 'https://example.com/image.jpg',
},
},
},
{
selector: '.some-selector',
}
)
).toEqual( [
{
selector: '.some-selector',
key: 'backgroundImage',
value: "url( 'https://example.com/image.jpg' )",
},
{
selector: '.some-selector',
key: 'backgroundSize',
value: 'cover',
},
] );
} );

it( 'should output fallback center position for contain background size', () => {
expect(
getCSSRules(
{
background: {
backgroundImage: {
source: 'file',
url: 'https://example.com/image.jpg',
},
backgroundSize: 'contain',
},
},
{
selector: '.some-selector',
}
)
).toEqual( [
{
selector: '.some-selector',
key: 'backgroundImage',
value: "url( 'https://example.com/image.jpg' )",
},
{
selector: '.some-selector',
key: 'backgroundSize',
value: 'contain',
},
{
selector: '.some-selector',
key: 'backgroundPosition',
value: 'center',
},
] );
} );
} );
17 changes: 17 additions & 0 deletions phpunit/block-supports/background-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ public function data_background_block_support() {
'expected_wrapper' => '<div style="background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
'wrapper' => '<div>Content</div>',
),
'background image style with contain, position, and repeat is applied' => array(
'theme_name' => 'block-theme-child-with-fluid-typography',
'block_name' => 'test/background-rules-are-output',
'background_settings' => array(
'backgroundImage' => true,
),
'background_style' => array(
'backgroundImage' => array(
'url' => 'https://example.com/image.jpg',
'source' => 'file',
),
'backgroundRepeat' => 'no-repeat',
'backgroundSize' => 'contain',
),
'expected_wrapper' => '<div style="background-image:url(&#039;https://example.com/image.jpg&#039;);background-position:center;background-repeat:no-repeat;background-size:contain;">Content</div>',
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
'wrapper' => '<div>Content</div>',
),
'background image style is appended if a style attribute already exists' => array(
'theme_name' => 'block-theme-child-with-fluid-typography',
'block_name' => 'test/background-rules-are-output',
Expand Down
14 changes: 9 additions & 5 deletions phpunit/style-engine/style-engine-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,22 @@ public function data_wp_style_engine_get_styles() {
'inline_background_image_url_with_background_size' => array(
'block_styles' => array(
'background' => array(
'backgroundImage' => array(
'backgroundImage' => array(
'url' => 'https://example.com/image.jpg',
),
'backgroundSize' => 'cover',
'backgroundPosition' => 'center',
'backgroundRepeat' => 'no-repeat',
'backgroundSize' => 'cover',
),
),
'options' => array(),
'expected_output' => array(
'css' => "background-image:url('https://example.com/image.jpg');background-size:cover;",
'css' => "background-image:url('https://example.com/image.jpg');background-position:center;background-repeat:no-repeat;background-size:cover;",
'declarations' => array(
'background-image' => "url('https://example.com/image.jpg')",
'background-size' => 'cover',
'background-image' => "url('https://example.com/image.jpg')",
'background-position' => 'center',
'background-repeat' => 'no-repeat',
'background-size' => 'cover',
),
),
),
Expand Down