Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function renderLink(
}

describe( 'PDFLink', () => {
it( 'links to the href with no underline', () => {
it( 'should link to the href with no underline', () => {
const tree = renderLink( {
href: 'https://example.com/dashboard',
children: 'View dashboard',
Expand All @@ -62,7 +62,7 @@ describe( 'PDFLink', () => {
expect( tree.props.style ).not.toHaveProperty( 'flexGrow' );
} );

it( 'uses the link color when no style sets a color', () => {
it( 'should use the link color when no style sets a color', () => {
const linkJSON = JSON.stringify(
renderLink( {
href: 'https://example.com/dashboard',
Expand All @@ -74,7 +74,7 @@ describe( 'PDFLink', () => {
expect( linkJSON ).toContain( PDF_COLORS.CONTENT_SECONDARY );
} );

it( 'renders the trailing icon after the link text', () => {
it( 'should render the trailing icon after the link text', () => {
const tree = renderLink( {
href: 'https://example.com/dashboard',
trailingIcon: (
Expand All @@ -90,7 +90,7 @@ describe( 'PDFLink', () => {
expect( JSON.stringify( tree ) ).toContain( 'M1 2 3 4' );
} );

it( 'renders only the link text when no icon is passed', () => {
it( 'should render only the link text when no icon is passed', () => {
const tree = renderLink( {
href: 'https://example.com/dashboard',
children: 'View dashboard',
Expand All @@ -99,7 +99,7 @@ describe( 'PDFLink', () => {
expect( tree.children ).toHaveLength( 1 );
} );

it( 'uses the color from the style prop', () => {
it( 'should use the color from the style prop', () => {
const linkJSON = JSON.stringify(
renderLink( {
href: 'https://example.com/dashboard',
Expand All @@ -111,7 +111,7 @@ describe( 'PDFLink', () => {
expect( linkJSON ).toContain( '#6c726e' );
} );

it( 'renders plain text in the default color when the href is an empty string', () => {
it( 'should render plain text in the default color when the href is an empty string', () => {
// An empty `href` is means a row with no link, so the text
// should render as plain text instead of as a link.
const tree = renderLink( { href: '', children: 'View dashboard' } );
Expand All @@ -123,7 +123,7 @@ describe( 'PDFLink', () => {
expect( treeJSON ).not.toContain( PDF_COLORS.CONTENT_SECONDARY );
} );

it( 'renders plain text in the default color when the href is undefined', () => {
it( 'should render plain text in the default color when the href is undefined', () => {
// An undefined/missing `href` should renders plain text,
// the same as an empty string above.
const tree = renderLink( {
Expand All @@ -138,7 +138,7 @@ describe( 'PDFLink', () => {
expect( treeJSON ).not.toContain( PDF_COLORS.CONTENT_SECONDARY );
} );

it( 'renders plain text in the default color when the href is null', () => {
it( 'should render plain text in the default color when the href is null', () => {
const tree = renderLink( { href: null, children: 'View dashboard' } );

expect( tree.type ).toBe( 'pdf-text' );
Expand All @@ -148,7 +148,7 @@ describe( 'PDFLink', () => {
expect( treeJSON ).not.toContain( PDF_COLORS.CONTENT_SECONDARY );
} );

it( 'truncates the linked text to one line when truncateContent is set', () => {
it( 'should truncate the linked text to one line when truncateContent is set', () => {
const tree = renderLink( {
href: 'https://example.com/dashboard',
truncateContent: true,
Expand All @@ -171,7 +171,7 @@ describe( 'PDFLink', () => {
);
} );

it( 'truncates the plain text to one line when truncateContent is set and href is empty', () => {
it( 'should truncate the plain text to one line when truncateContent is set and href is empty', () => {
const json = JSON.stringify(
renderLink( {
href: '',
Expand All @@ -184,7 +184,7 @@ describe( 'PDFLink', () => {
expect( json ).toContain( '"textOverflow":"ellipsis"' );
} );

it( 'omits the trailing icon when the href is empty', () => {
it( 'should omit the trailing icon when the href is empty', () => {
const tree = renderLink( {
href: '',
trailingIcon: (
Expand All @@ -197,4 +197,27 @@ describe( 'PDFLink', () => {

expect( JSON.stringify( tree ) ).not.toContain( 'M1 2 3 4' );
} );

it( 'should pass the hyphenation callback to the text of a link and of plain text', () => {
function hyphenationCallback( word: string ) {
return [ word ];
}

const linkTree = renderLink( {
href: 'https://example.com/dashboard',
hyphenationCallback,
children: 'View dashboard',
} );
const [ linkText ] =
linkTree.children as TestRenderer.ReactTestRendererJSON[];

expect( linkText.props.hyphenationCallback ).toBe(
hyphenationCallback
);

expect(
renderLink( { href: '', hyphenationCallback, children: 'Home' } )
.props.hyphenationCallback
).toBe( hyphenationCallback );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { FC, ReactNode } from 'react';
import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale';
import { PDF_COLORS } from '@/js/components/pdf-export/pdf-theme';
import PDFTypography, {
PDFTypographyProps,
PDFTypographySize,
PDFTypographyType,
} from './PDFTypography';
Expand Down Expand Up @@ -60,6 +61,8 @@ export interface PDFLinkProps {
style?: Style | Style[];
/** Ends the text in an ellipsis when it's too long, and limits it to one line unless the text's own `maxLines` sets another limit. */
truncateContent?: boolean;
/** Callback that splits each word into the parts a line can break between. */
hyphenationCallback?: PDFTypographyProps[ 'hyphenationCallback' ];
}

const PDFLink: FC< PDFLinkProps > = ( {
Expand All @@ -69,6 +72,7 @@ const PDFLink: FC< PDFLinkProps > = ( {
trailingIcon,
style,
truncateContent,
hyphenationCallback,
children,
} ) => {
if ( ! href ) {
Expand All @@ -78,6 +82,7 @@ const PDFLink: FC< PDFLinkProps > = ( {
size={ size }
style={ style }
truncateContent={ truncateContent }
hyphenationCallback={ hyphenationCallback }
>
{ children }
</PDFTypography>
Expand All @@ -96,6 +101,7 @@ const PDFLink: FC< PDFLinkProps > = ( {
size={ size }
style={ style ? [ linkColor ].concat( style ) : linkColor }
truncateContent={ truncateContent }
hyphenationCallback={ hyphenationCallback }
>
{ children }
</PDFTypography>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ describe( 'PDFTypography', () => {
} );
} );

it( 'should pass the hyphenation callback to the Text', () => {
function hyphenationCallback( word: string ) {
return [ word ];
}
const tree = TestRenderer.create(
<PDFTypography hyphenationCallback={ hyphenationCallback }>
Vacoas-Phoenix
</PDFTypography>
).toJSON();
if ( ! tree || Array.isArray( tree ) ) {
throw new Error( 'Unexpected render output.' );
}

expect( tree.props.hyphenationCallback ).toBe( hyphenationCallback );
} );

it( 'truncates the text to one line with an ellipsis when truncateContent is true', () => {
const json = renderJSON(
<PDFTypography truncateContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
import { Text, View } from '@react-pdf/renderer';
import type { Style } from '@react-pdf/stylesheet';
import { FC } from 'react';
import { ComponentProps, FC } from 'react';

/**
* Internal dependencies
Expand Down Expand Up @@ -57,6 +57,10 @@ export interface PDFTypographyProps {
maxLines?: number;
/** Ends the text in an ellipsis when it's too long, and limits it to one line unless `maxLines` sets another limit. */
truncateContent?: boolean;
/** Callback that splits each word into the parts a line can break between. */
hyphenationCallback?: ComponentProps<
typeof Text
>[ 'hyphenationCallback' ];
}

const PDFTypography: FC< PDFTypographyProps > = ( {
Expand All @@ -65,6 +69,7 @@ const PDFTypography: FC< PDFTypographyProps > = ( {
style,
maxLines,
truncateContent = false,
hyphenationCallback,
children,
} ) => {
const baseStyles: Style[] = [
Expand All @@ -85,7 +90,10 @@ const PDFTypography: FC< PDFTypographyProps > = ( {
}

const text = (
<Text style={ style ? baseStyles.concat( style ) : baseStyles }>
<Text
style={ style ? baseStyles.concat( style ) : baseStyles }
hyphenationCallback={ hyphenationCallback }
>
{ children }
</Text>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* PDFAudienceCardHeader tests.
*
* Site Kit by Google, Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import { ComponentProps } from 'react';
import TestRenderer from 'react-test-renderer';

/**
* Internal dependencies
*/
import { scalePDFValue } from '@/js/components/pdf-export/pdf-scale';
import { PDF_PARTIAL_DATA_BADGE_HEIGHT } from '@/js/components/pdf-export/shared-react-pdf-components/PDFPartialDataBadge';
import {
flattenPDFStyle,
renderJSON,
renderPDFStyle,
} from '@/js/components/pdf-export/test-utils';
import PDFAudienceCardHeader from './PDFAudienceCardHeader';

/**
* Renders the header and returns its title row.
*
* @since n.e.x.t
*
* @param {Object} props Props for the header.
* @return {Object} The row holding the audience name and the badge.
*/
function renderTitleRow(
props: ComponentProps< typeof PDFAudienceCardHeader >
) {
const tree = TestRenderer.create(
<PDFAudienceCardHeader { ...props } />
).toJSON() as TestRenderer.ReactTestRendererJSON;

const [ titleRow ] = tree.children as TestRenderer.ReactTestRendererJSON[];

return titleRow;
}

describe( 'PDFAudienceCardHeader', () => {
it( 'should render the audience name', () => {
expect(
renderJSON(
<PDFAudienceCardHeader
audienceName="New visitors"
isAudiencePartialData={ false }
/>
)
).toContain( 'New visitors' );
} );

it( 'should show the Partial data badge only when the audience is in a partial data state', () => {
expect(
renderJSON(
<PDFAudienceCardHeader
audienceName="New visitors"
isAudiencePartialData
/>
)
).toContain( 'Partial data' );

expect(
renderJSON(
<PDFAudienceCardHeader
audienceName="New visitors"
isAudiencePartialData={ false }
/>
)
).not.toContain( 'Partial data' );
} );

it( 'should fill its grid cell', () => {
expect(
renderPDFStyle(
<PDFAudienceCardHeader
audienceName="New visitors"
isAudiencePartialData={ false }
/>
)
).toMatchObject( { flexGrow: 1 } );
} );

it( 'should wrap a long name in the width the badge leaves, with a gap before the badge', () => {
const titleRow = renderTitleRow( {
audienceName:
'Visitors who completed a purchase in the last thirty days',
isAudiencePartialData: true,
} );
const [ name, badge ] =
titleRow.children as TestRenderer.ReactTestRendererJSON[];

expect( flattenPDFStyle( titleRow.props.style ) ).toMatchObject( {
columnGap: scalePDFValue( 10 ),
} );
expect( flattenPDFStyle( name.props.style ) ).toMatchObject( {
flex: 1,
} );
expect( JSON.stringify( badge ) ).toContain( 'Partial data' );
} );

it( 'should reserve the Partial data badge height on the title row with or without a badge', () => {
const minHeight = scalePDFValue( PDF_PARTIAL_DATA_BADGE_HEIGHT );

for ( const isAudiencePartialData of [ true, false ] ) {
const titleRow = renderTitleRow( {
audienceName: 'New visitors',
isAudiencePartialData,
} );

expect( flattenPDFStyle( titleRow.props.style ) ).toMatchObject( {
minHeight,
} );
}
} );
} );
Loading
Loading