|
| 1 | +/** |
| 2 | + * Tests for PDF font registration. |
| 3 | + * |
| 4 | + * Site Kit by Google, Copyright 2026 Google LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Internal dependencies |
| 21 | + */ |
| 22 | +import { PDF_FONT_FAMILY_DISPLAY, PDF_FONT_FAMILY_TEXT } from './pdf-theme'; |
| 23 | + |
| 24 | +// `@react-pdf/renderer` is auto-mocked via `__mocks__/@react-pdf/renderer.js`. |
| 25 | +// The module under test holds a session-scoped registration latch, so each test |
| 26 | +// reloads it (and the mocked renderer) with `jest.resetModules()`. |
| 27 | +async function setup() { |
| 28 | + const { Font } = await import( '@react-pdf/renderer' ); |
| 29 | + const { registerPDFFonts } = await import( './pdf-fonts-react' ); |
| 30 | + return { Font, registerPDFFonts }; |
| 31 | +} |
| 32 | + |
| 33 | +// The bundled (multi-weight) shape we always register with; narrows the |
| 34 | +// `SingleLoad | BulkLoad` union the typings expose for `Font.register`. |
| 35 | +type FontConfig = { |
| 36 | + family: string; |
| 37 | + fonts: Array< { src: string; fontWeight: number } >; |
| 38 | +}; |
| 39 | + |
| 40 | +describe( 'registerPDFFonts', () => { |
| 41 | + beforeEach( () => { |
| 42 | + jest.resetModules(); |
| 43 | + jest.clearAllMocks(); |
| 44 | + } ); |
| 45 | + |
| 46 | + it( 'registers both families with the bundled weights and returns the display family', async () => { |
| 47 | + const { Font, registerPDFFonts } = await setup(); |
| 48 | + |
| 49 | + const family = registerPDFFonts(); |
| 50 | + |
| 51 | + expect( family ).toBe( PDF_FONT_FAMILY_DISPLAY ); |
| 52 | + expect( Font.register ).toHaveBeenCalledTimes( 2 ); |
| 53 | + |
| 54 | + const calls = jest.mocked( Font.register ).mock.calls as Array< |
| 55 | + [ FontConfig ] |
| 56 | + >; |
| 57 | + const displayConfig = calls.find( |
| 58 | + ( [ config ] ) => config.family === PDF_FONT_FAMILY_DISPLAY |
| 59 | + )?.[ 0 ]; |
| 60 | + const textConfig = calls.find( |
| 61 | + ( [ config ] ) => config.family === PDF_FONT_FAMILY_TEXT |
| 62 | + )?.[ 0 ]; |
| 63 | + |
| 64 | + expect( displayConfig?.fonts.map( ( f ) => f.fontWeight ) ).toEqual( [ |
| 65 | + 400, |
| 66 | + ] ); |
| 67 | + expect( textConfig?.fonts.map( ( f ) => f.fontWeight ) ).toEqual( [ |
| 68 | + 400, 500, |
| 69 | + ] ); |
| 70 | + } ); |
| 71 | + |
| 72 | + it( 'registers URL strings (not data URIs) as the font src', async () => { |
| 73 | + const { Font, registerPDFFonts } = await setup(); |
| 74 | + |
| 75 | + registerPDFFonts(); |
| 76 | + |
| 77 | + const calls = jest.mocked( Font.register ).mock.calls as Array< |
| 78 | + [ FontConfig ] |
| 79 | + >; |
| 80 | + const sources = calls |
| 81 | + .flatMap( ( [ config ] ) => config.fonts ) |
| 82 | + .map( ( { src } ) => src ); |
| 83 | + |
| 84 | + expect( sources ).toHaveLength( 3 ); |
| 85 | + sources.forEach( ( src ) => { |
| 86 | + expect( typeof src ).toBe( 'string' ); |
| 87 | + expect( src.startsWith( 'data:' ) ).toBe( false ); |
| 88 | + } ); |
| 89 | + } ); |
| 90 | + |
| 91 | + it( 'registers a hyphenation callback that returns the whole word', async () => { |
| 92 | + const { Font, registerPDFFonts } = await setup(); |
| 93 | + |
| 94 | + registerPDFFonts(); |
| 95 | + |
| 96 | + expect( Font.registerHyphenationCallback ).toHaveBeenCalledTimes( 1 ); |
| 97 | + const callback = jest.mocked( Font.registerHyphenationCallback ).mock |
| 98 | + .calls[ 0 ][ 0 ]; |
| 99 | + expect( callback( 'visitors' ) ).toEqual( [ 'visitors' ] ); |
| 100 | + } ); |
| 101 | + |
| 102 | + it( 'is idempotent within a session', async () => { |
| 103 | + const { Font, registerPDFFonts } = await setup(); |
| 104 | + |
| 105 | + registerPDFFonts(); |
| 106 | + jest.mocked( Font.register ).mockClear(); |
| 107 | + jest.mocked( Font.registerHyphenationCallback ).mockClear(); |
| 108 | + |
| 109 | + registerPDFFonts(); |
| 110 | + |
| 111 | + expect( Font.register ).not.toHaveBeenCalled(); |
| 112 | + expect( Font.registerHyphenationCallback ).not.toHaveBeenCalled(); |
| 113 | + } ); |
| 114 | + |
| 115 | + it( 'propagates registration errors instead of falling back', async () => { |
| 116 | + const { Font, registerPDFFonts } = await setup(); |
| 117 | + |
| 118 | + jest.mocked( Font.register ).mockImplementationOnce( () => { |
| 119 | + throw new Error( 'register failed' ); |
| 120 | + } ); |
| 121 | + |
| 122 | + expect( () => registerPDFFonts() ).toThrow( 'register failed' ); |
| 123 | + } ); |
| 124 | +} ); |
0 commit comments