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

Font Appearance Control: Refactor font appearance fallbacks #63215

Merged
merged 28 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
68dc225
Refactor font appearance fallbacks
mikachan Jul 6, 2024
3b434b1
Create new findNearestStyleAndWeight function
mikachan Jul 6, 2024
dacc850
Add tests for findNearestStyleAndWeight
mikachan Jul 6, 2024
23a8fa2
Refactor findNearestStyleAndWeight
mikachan Jul 6, 2024
2e0e9c5
Add a test for normal/100
mikachan Jul 6, 2024
5a22028
Add comments
mikachan Jul 6, 2024
9813347
Tidy up comments
mikachan Jul 6, 2024
23567d5
Fix test description
mikachan Jul 6, 2024
1f102c6
Update test descriptions
mikachan Jul 6, 2024
c74daeb
Update deps and wrap setFontAppearance and resetFontAppearance in use…
mikachan Jul 8, 2024
7e027b4
Add e2e test for appearance control dropdown menu
mikachan Jul 8, 2024
ef7350b
Add periods to end of comments
mikachan Jul 8, 2024
a796d13
Use better test data for font appearance e2e default test
mikachan Jul 8, 2024
72199cc
Add findNearestFontStyle function with tests
mikachan Jul 8, 2024
3796305
Limit the dependency array to just fontFamily
mikachan Jul 8, 2024
64b13cc
Add normal font style test
mikachan Jul 8, 2024
fc209a6
Add invalid font style test
mikachan Jul 8, 2024
1f69824
Try toHaveText instead of toContainText
mikachan Jul 8, 2024
a7a3b6a
Try using TT4 for e2e test rather than TT3
mikachan Jul 8, 2024
74edc0c
Merge branch 'trunk' into try/refactor-font-appearance-defaults
mikachan Jul 8, 2024
cec7d9d
Use combobox role rather than button in e2e test
mikachan Jul 8, 2024
08e73d7
Merge branch 'trunk' into try/refactor-font-appearance-defaults
mikachan Jul 8, 2024
2e23bc5
Merge branch 'trunk' into try/refactor-font-appearance-defaults
mikachan Jul 9, 2024
b06c4e4
Set nearestFontStyle to an empty string by default
mikachan Jul 9, 2024
b407be1
Refactor findNearestFontStyle
mikachan Jul 9, 2024
9d7dfc4
Do not activate a theme in font appearance e2e test
mikachan Jul 9, 2024
18e1bfe
Run findNearestStyleAndWeight only when fontFamily has changed
mikachan Jul 11, 2024
8288dd4
Only trigger appearance onChange if values are different
mikachan Jul 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
getFluidTypographyOptionsFromSettings,
getMergedFontFamiliesAndFontFamilyFaces,
findNearestFontWeight,
findNearestFontStyle,
findNearestStyleAndWeight,
} from '../typography-utils';

describe( 'typography utils', () => {
Expand Down Expand Up @@ -951,6 +953,329 @@ describe( 'typography utils', () => {
);
} );

describe( 'findNearestFontStyle', () => {
[
{
message:
'should return empty string when newFontStyleValue is `undefined`',
availableFontStyles: undefined,
newFontStyleValue: undefined,
expected: '',
},
{
message:
'should return newFontStyleValue value when availableFontStyles is empty',
availableFontStyles: [],
newFontStyleValue: 'italic',
expected: 'italic',
},
{
message:
'should return empty string if there is no new font style available',
availableFontStyles: [ { name: 'Normal', value: 'normal' } ],
newFontStyleValue: 'italic',
expected: '',
},
{
message:
'should return empty string if the new font style is invalid',
availableFontStyles: [
{ name: 'Regular', value: 'normal' },
{ name: 'Italic', value: 'italic' },
],
newFontStyleValue: 'not-valid',
expected: '',
},
{
message: 'should return italic if oblique is not available',
availableFontStyles: [
{ name: 'Regular', value: 'normal' },
{ name: 'Italic', value: 'italic' },
],
newFontStyleValue: 'oblique',
expected: 'italic',
},
{
message: 'should return normal if normal is available',
availableFontStyles: [
{ name: 'Regular', value: 'normal' },
{ name: 'Italic', value: 'italic' },
],
newFontStyleValue: 'normal',
expected: 'normal',
},
].forEach(
( {
message,
availableFontStyles,
newFontStyleValue,
expected,
} ) => {
it( `${ message }`, () => {
expect(
findNearestFontStyle(
availableFontStyles,
newFontStyleValue
)
).toEqual( expected );
} );
}
);
} );

describe( 'findNearestStyleAndWeight', () => {
[
{
message: 'should return empty object when all values are empty',
fontFamilyFaces: [],
fontStyle: undefined,
fontWeight: undefined,
expected: {},
},
{
message:
'should return original fontStyle and fontWeight when fontFamilyFaces is empty',
fontFamilyFaces: [],
fontStyle: 'italic',
fontWeight: '700',
expected: {
nearestFontStyle: 'italic',
nearestFontWeight: '700',
},
},
{
message:
'should return undefined values if both fontStyle and fontWeight are not available',
fontFamilyFaces: [
{
fontFamily: 'ABeeZee',
fontStyle: 'italic',
fontWeight: '400',
src: [
'file:./assets/fonts/esDT31xSG-6AGleN2tCkkJUCGpG-GQ.woff2',
],
},
],
fontStyle: undefined,
fontWeight: undefined,
expected: {
nearestFontStyle: undefined,
nearestFontWeight: undefined,
},
},
{
message:
'should return nearest fontStyle and fontWeight for normal/400',
fontFamilyFaces: [
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'italic',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Italic.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '700',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2',
],
},
],
fontStyle: 'normal',
fontWeight: '400',
expected: {
nearestFontStyle: 'normal',
nearestFontWeight: '400',
},
},
{
message:
'should return nearest fontStyle and fontWeight for normal/100',
fontFamilyFaces: [
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'italic',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Italic.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '700',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2',
],
},
],
fontStyle: 'normal',
fontWeight: '100',
expected: {
nearestFontStyle: 'normal',
nearestFontWeight: '400',
},
},
{
message:
'should return nearest fontStyle and fontWeight for italic/900',
fontFamilyFaces: [
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'italic',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Italic.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '700',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2',
],
},
],
fontStyle: 'italic',
fontWeight: '900',
expected: {
nearestFontStyle: 'italic',
nearestFontWeight: '700',
},
},
{
message:
'should return nearest fontStyle and fontWeight for oblique/600',
fontFamilyFaces: [
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'italic',
fontWeight: '700',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2',
],
},
],
fontStyle: 'oblique',
fontWeight: '600',
expected: {
nearestFontStyle: 'italic',
nearestFontWeight: '700',
},
},
{
message:
'should return nearest fontStyle and fontWeight for 300 font weight and empty font style',
fontFamilyFaces: [
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'italic',
fontWeight: '700',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2',
],
},
],
fontStyle: undefined,
fontWeight: '300',
expected: {
nearestFontStyle: 'normal',
nearestFontWeight: '400',
},
},
{
message:
'should return nearest fontStyle and fontWeight for oblique font style and empty font weight',
fontFamilyFaces: [
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'normal',
fontWeight: '400',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2',
],
},
{
fontFamily: 'IBM Plex Mono',
fontStyle: 'italic',
fontWeight: '700',
src: [
'file:./assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2',
],
},
],
fontStyle: 'oblique',
fontWeight: undefined,
expected: {
nearestFontStyle: 'italic',
nearestFontWeight: '400',
},
},
].forEach(
( {
message,
fontFamilyFaces,
fontStyle,
fontWeight,
expected,
} ) => {
it( `${ message }`, () => {
expect(
findNearestStyleAndWeight(
fontFamilyFaces,
fontStyle,
fontWeight
)
).toEqual( expected );
} );
}
);
} );

describe( 'typography utils', () => {
[
{
Expand Down
Loading
Loading