Skip to content

Commit 893266f

Browse files
committed
fallback to color attribute if available / implement tests
1 parent 60f859f commit 893266f

5 files changed

Lines changed: 87 additions & 13 deletions

File tree

cypress/e2e/button/button-block.cy.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ describe( 'Button Block', () => {
4747
cy.postContentMatchesSnapshot();
4848
} );
4949

50-
it( 'Style should be visible in UI', () => {
50+
it.only( 'Style should be visible in UI', () => {
5151
cy.insertButtonBlock();
5252

5353
// Check default button style
5454
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
5555
'have.attr',
5656
'style',
57-
'background-color: rgb(0, 123, 255);'
57+
'background-color: rgb(0, 123, 255); color: rgb(255, 255, 255);'
5858
);
5959

6060
cy.selectButtonBlock();
@@ -66,7 +66,7 @@ describe( 'Button Block', () => {
6666
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
6767
'have.attr',
6868
'style',
69-
'background-color: rgb(108, 117, 125);'
69+
'background-color: rgb(108, 117, 125); color: rgb(255, 255, 255);'
7070
);
7171

7272
// Editor content should match snapshot

cypress/e2e/button/button-filters.cy.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,49 @@ describe( 'Button Block Filters', () => {
2727
// Style option should be applied
2828
cy.getSelectByLabel( 'Style' ).select( 'brand' );
2929

30+
// Style should be visible in UI
31+
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
32+
'have.attr',
33+
'style',
34+
'background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);'
35+
);
36+
37+
// Editor content should match snapshot
38+
cy.postContentMatchesSnapshot();
39+
} );
40+
41+
it( 'Deprecated color attribute should be visible in UI', () => {
42+
cy.insertButtonBlock();
43+
cy.selectButtonBlock();
44+
cy.ensureSidebarOpened();
45+
46+
cy.getSelectByLabel( 'Style' ).select( 'brand-deprecated-color' );
47+
48+
// Style should be visible in UI
49+
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
50+
'have.attr',
51+
'style',
52+
'background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);'
53+
);
54+
55+
// Editor content should match snapshot
56+
cy.postContentMatchesSnapshot();
57+
} );
58+
59+
it( 'Use default colors if textColor or bgColor attributes are missing in styleOption', () => {
60+
cy.insertButtonBlock();
61+
cy.selectButtonBlock();
62+
cy.ensureSidebarOpened();
63+
64+
cy.getSelectByLabel( 'Style' ).select( 'missing-colors' );
65+
66+
// Style should be visible in UI
67+
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
68+
'have.attr',
69+
'style',
70+
'background-color: rgb(0, 123, 255); color: rgb(255, 255, 255);'
71+
);
72+
3073
// Editor content should match snapshot
3174
cy.postContentMatchesSnapshot();
3275
} );

e2e-test-plugins/button-filters/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33
function buttonFiltersStyleOptions( styleOptions ) {
44
return [
55
...styleOptions,
6-
{ label: 'Brand', value: 'brand', color: '#FF0000' },
6+
{
7+
label: 'Brand',
8+
value: 'brand',
9+
bgColor: '#FF0000',
10+
textColor: '#FFFFFF',
11+
},
12+
{
13+
label: 'Brand (Deprecated Color)',
14+
value: 'brand-deprecated-color',
15+
color: '#FF0000',
16+
},
17+
{
18+
label: 'Missing colors',
19+
value: 'missing-colors',
20+
},
721
];
822
}
923
wp.hooks.addFilter(

snapshots.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ module.exports = {
8686
},
8787
"wp_bootstrap_blocks_button_default_attributes should override default attributes": {
8888
"1": "<!-- wp:wp-bootstrap-blocks/button /-->"
89+
},
90+
"Deprecated color attribute should be visible in UI": {
91+
"1": "<!-- wp:wp-bootstrap-blocks/button {\"style\":\"brand-deprecated-color\"} /-->"
92+
},
93+
"Use default colors if textColor or bgColor attributes are missing in styleOption": {
94+
"1": "<!-- wp:wp-bootstrap-blocks/button {\"style\":\"missing-colors\"} /-->"
8995
}
9096
},
9197
"Container Block": {

src/button/edit.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ let styleOptions = [
2323
label: __( 'Primary', 'wp-bootstrap-blocks' ),
2424
value: 'primary',
2525
bgColor: bgColors.primary,
26-
color: colors.white
26+
textColor: colors.white,
2727
},
2828
{
2929
label: __( 'Secondary', 'wp-bootstrap-blocks' ),
3030
value: 'secondary',
3131
bgColor: bgColors.secondary,
32-
color: colors.white
32+
textColor: colors.white,
3333
},
3434
];
3535
styleOptions = applyFilters(
@@ -38,7 +38,7 @@ styleOptions = applyFilters(
3838
);
3939

4040
const DEFAULT_BG_COLOR = bgColors.primary;
41-
const DEFAULT_COLOR = colors.white;
41+
const DEFAULT_TEXT_COLOR = colors.white;
4242
const NEW_TAB_REL_DEFAULT_VALUE = 'noreferrer noopener';
4343

4444
const BootstrapButtonEdit = ( {
@@ -66,22 +66,33 @@ const BootstrapButtonEdit = ( {
6666
} );
6767
};
6868

69+
// Fill empty color values with default values and check for usage of deprecated color attribute in styleOptions
70+
const styleOptionsWithDefault = styleOptions.map( ( styleOption ) => ( {
71+
...styleOption,
72+
textColor: styleOption.textColor || DEFAULT_TEXT_COLOR,
73+
bgColor: styleOption.bgColor || styleOption.color || DEFAULT_BG_COLOR, // Fallback to deprecated color attribute
74+
} ) );
75+
6976
// Prepare CSS rules for selected button style
7077
let inlineStyle = {
7178
backgroundColor:
72-
styleOptions.length > 0 ? styleOptions[ 0 ].bgColor : DEFAULT_BG_COLOR,
73-
color:
74-
styleOptions.length > 0 ? styleOptions[ 0 ].color : DEFAULT_COLOR,
79+
styleOptionsWithDefault.length > 0
80+
? styleOptionsWithDefault[ 0 ].bgColor
81+
: DEFAULT_BG_COLOR,
82+
color:
83+
styleOptionsWithDefault.length > 0
84+
? styleOptionsWithDefault[ 0 ].textColor
85+
: DEFAULT_TEXT_COLOR,
7586
};
7687

7788
if ( style ) {
78-
const selectedButtonColor = styleOptions.find(
89+
const selectedButtonColor = styleOptionsWithDefault.find(
7990
( styleOption ) => styleOption.value === style
8091
);
81-
if ( selectedButtonColor?.bgColor && selectedButtonColor?.color ) {
92+
if ( selectedButtonColor?.bgColor && selectedButtonColor?.textColor ) {
8293
inlineStyle = {
8394
backgroundColor: selectedButtonColor.bgColor,
84-
color: selectedButtonColor.color
95+
color: selectedButtonColor.textColor,
8596
};
8697
}
8798
}

0 commit comments

Comments
 (0)