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

Contact Form Block: Fix Missing Button Color Attributes #14898

Merged
merged 3 commits into from
Mar 5, 2020
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
12 changes: 7 additions & 5 deletions extensions/blocks/amazon/attributes.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const hexRegex = /^#?[A-Fa-f0-9]{6}$/;
const colourValidator = value => hexRegex.test( value );
/**
* Internal dependencies
*/
import colorValidator from '../../shared/colorValidator';

export default {
backgroundColor: {
type: 'string',
validator: colourValidator,
validator: colorValidator,
},
textColor: {
type: 'string',
validator: colourValidator,
validator: colorValidator,
},
buttonAndLinkColor: {
type: 'string',
validator: colourValidator,
validator: colorValidator,
},
style: {
type: 'string',
Expand Down
17 changes: 9 additions & 8 deletions extensions/blocks/calendly/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
*/
import { __ } from '@wordpress/i18n';

const hexRegex = /^#?[A-Fa-f0-9]{6}$/;

const colourValidator = value => hexRegex.test( value );
/**
* Internal dependencies
*/
import colorValidator from '../../shared/colorValidator';

const urlValidator = url => ! url || url.startsWith( 'https://calendly.com/' );

export default {
backgroundColor: {
type: 'string',
default: 'ffffff',
validator: colourValidator,
validator: colorValidator,
},
submitButtonText: {
type: 'string',
Expand All @@ -33,12 +34,12 @@ export default {
primaryColor: {
type: 'string',
default: '00A2FF',
validator: colourValidator,
validator: colorValidator,
},
textColor: {
type: 'string',
default: '4D5055',
validator: colourValidator,
validator: colorValidator,
},
style: {
type: 'string',
Expand All @@ -57,10 +58,10 @@ export default {
},
customBackgroundButtonColor: {
type: 'string',
validator: colourValidator,
validator: colorValidator,
},
customTextButtonColor: {
type: 'string',
validator: colourValidator,
validator: colorValidator,
},
};
17 changes: 15 additions & 2 deletions extensions/blocks/contact-form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import JetpackFieldTextarea from './components/jetpack-field-textarea';
import JetpackFieldCheckbox from './components/jetpack-field-checkbox';
import JetpackFieldMultiple from './components/jetpack-field-multiple';
import renderMaterialIcon from '../../shared/render-material-icon';
import colorValidator from '../../shared/colorValidator';

export const name = 'contact-form';

Expand Down Expand Up @@ -48,8 +49,20 @@ export const settings = {
type: 'string',
default: __( 'Submit', 'jetpack' ),
},
customBackgroundButtonColor: { type: 'string' },
customTextButtonColor: { type: 'string' },
backgroundButtonColor: {
type: 'string',
},
textButtonColor: {
type: 'string',
},
customBackgroundButtonColor: {
type: 'string',
validator: colorValidator,
},
customTextButtonColor: {
type: 'string',
validator: colorValidator,
},
submitButtonClasses: { type: 'string' },
hasFormSettingsSet: {
type: 'string',
Expand Down
5 changes: 5 additions & 0 deletions extensions/shared/colorValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const hexRegex = /^#?[A-Fa-f0-9]{6}$/;

export default function colorValidator( value ) {
return hexRegex.test( value );
}
143 changes: 52 additions & 91 deletions extensions/shared/test/get-validated-attributes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/**
* Internal dependencies
*/
import colorValidator from '../colorValidator';
import { getValidatedAttributes } from '../get-validated-attributes';

const hexRegex = /^#?[A-Fa-f0-9]{6}$/;

const colourValidator = value => hexRegex.test( value );

const urlValidator = url => ! url || url.startsWith( 'http' );

const defaultAttributes = {
Expand All @@ -17,7 +14,7 @@ const defaultAttributes = {
color: {
default: 'ffffff',
type: 'string',
validator: colourValidator,
validator: colorValidator,
},
language: {
default: 'en',
Expand All @@ -36,116 +33,80 @@ const defaultAttributes = {

describe( 'getValidatedAttributes', () => {
test( 'boolean attributes', () => {
expect(
getValidatedAttributes( defaultAttributes, { boolean: true } )
).toStrictEqual(
{ boolean: true }
);

expect(
getValidatedAttributes( defaultAttributes, { boolean: 'true' } )
).toStrictEqual(
{ boolean: true }
);
expect( getValidatedAttributes( defaultAttributes, { boolean: true } ) ).toStrictEqual( {
boolean: true,
} );

expect(
getValidatedAttributes( defaultAttributes, { boolean: false } )
).toStrictEqual(
{ boolean: false }
);
expect( getValidatedAttributes( defaultAttributes, { boolean: 'true' } ) ).toStrictEqual( {
boolean: true,
} );

expect(
getValidatedAttributes( defaultAttributes, { boolean: 'false' } )
).toStrictEqual(
{ boolean: false }
);
expect( getValidatedAttributes( defaultAttributes, { boolean: false } ) ).toStrictEqual( {
boolean: false,
} );

expect( getValidatedAttributes( defaultAttributes, { boolean: 'false' } ) ).toStrictEqual( {
boolean: false,
} );
} );

test( 'attributes with a validator function', () => {
expect(
getValidatedAttributes( defaultAttributes, { color: 'ffffff' } )
).toStrictEqual(
{ color: 'ffffff' }
);
expect( getValidatedAttributes( defaultAttributes, { color: 'ffffff' } ) ).toStrictEqual( {
color: 'ffffff',
} );

expect(
getValidatedAttributes( defaultAttributes, { color: 'white' } )
).toStrictEqual(
{ color: 'ffffff' }
);
expect( getValidatedAttributes( defaultAttributes, { color: 'white' } ) ).toStrictEqual( {
color: 'ffffff',
} );

expect(
getValidatedAttributes( defaultAttributes, { color: '000000' } )
).toStrictEqual(
{ color: '000000' }
);
expect( getValidatedAttributes( defaultAttributes, { color: '000000' } ) ).toStrictEqual( {
color: '000000',
} );

expect(
getValidatedAttributes( defaultAttributes, { color: 'black' } )
).toStrictEqual(
{ color: 'ffffff' }
);
} );
expect( getValidatedAttributes( defaultAttributes, { color: 'black' } ) ).toStrictEqual( {
color: 'ffffff',
} );
} );

test( 'attributes with valid values', () => {
expect(
getValidatedAttributes( defaultAttributes, { language: 'en' } )
).toStrictEqual(
{ language: 'en' }
);
expect( getValidatedAttributes( defaultAttributes, { language: 'en' } ) ).toStrictEqual( {
language: 'en',
} );

expect(
getValidatedAttributes( defaultAttributes, { language: 'fr' } )
).toStrictEqual(
{ language: 'fr' }
);
expect( getValidatedAttributes( defaultAttributes, { language: 'fr' } ) ).toStrictEqual( {
language: 'fr',
} );

expect(
getValidatedAttributes( defaultAttributes, { language: 'pt' } )
).toStrictEqual(
{ language: 'en' }
);
} );
expect( getValidatedAttributes( defaultAttributes, { language: 'pt' } ) ).toStrictEqual( {
language: 'en',
} );
} );

test( 'attributes without a default values', () => {
expect(
getValidatedAttributes( defaultAttributes, { url: 'http://jetpack.com' } )
).toStrictEqual(
{ url: 'http://jetpack.com' }
);
).toStrictEqual( { url: 'http://jetpack.com' } );

expect(
getValidatedAttributes( defaultAttributes, { url: 'https://jetpack.com' } )
).toStrictEqual(
{ url: 'https://jetpack.com' }
);
).toStrictEqual( { url: 'https://jetpack.com' } );


expect(
getValidatedAttributes( defaultAttributes, { url: 'jetpack.com' } )
).toStrictEqual(
{ url: undefined }
);
expect( getValidatedAttributes( defaultAttributes, { url: 'jetpack.com' } ) ).toStrictEqual( {
url: undefined,
} );
} );

test( 'attributes without validation', () => {
expect(
getValidatedAttributes( defaultAttributes, { freeform: 'one' } )
).toStrictEqual(
{ freeform: 'one' }
);
expect( getValidatedAttributes( defaultAttributes, { freeform: 'one' } ) ).toStrictEqual( {
freeform: 'one',
} );

expect(
getValidatedAttributes( defaultAttributes, { freeform: 'two' } )
).toStrictEqual(
{ freeform: 'two' }
);

expect(
getValidatedAttributes( defaultAttributes, { freeform: undefined } )
).toStrictEqual(
{ freeform: undefined }
);
expect( getValidatedAttributes( defaultAttributes, { freeform: 'two' } ) ).toStrictEqual( {
freeform: 'two',
} );

expect( getValidatedAttributes( defaultAttributes, { freeform: undefined } ) ).toStrictEqual( {
freeform: undefined,
} );
} );
} );