|
| 1 | +/** |
| 2 | + * `@react-pdf/renderer` jest mock. |
| 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 | +// Manual Jest mock for `@react-pdf/renderer`. The real package ships ESM |
| 20 | +// with native deps that fail in jsdom. CommonJS to match `__mocks__/tabbable.js`. |
| 21 | + |
| 22 | +/** |
| 23 | + * External dependencies |
| 24 | + */ |
| 25 | +const React = require( 'react' ); |
| 26 | + |
| 27 | +function passthrough( name ) { |
| 28 | + function Component( props ) { |
| 29 | + return React.createElement( |
| 30 | + name.toLowerCase(), |
| 31 | + { 'data-react-pdf': name, ...props }, |
| 32 | + props && props.children |
| 33 | + ); |
| 34 | + } |
| 35 | + Component.displayName = name; |
| 36 | + return Component; |
| 37 | +} |
| 38 | + |
| 39 | +const Document = passthrough( 'Document' ); |
| 40 | +const Page = passthrough( 'Page' ); |
| 41 | +const View = passthrough( 'View' ); |
| 42 | +const Text = passthrough( 'Text' ); |
| 43 | +const Image = passthrough( 'Image' ); |
| 44 | +const Link = passthrough( 'Link' ); |
| 45 | +const Note = passthrough( 'Note' ); |
| 46 | +const Canvas = passthrough( 'Canvas' ); |
| 47 | + |
| 48 | +const StyleSheet = { |
| 49 | + create: ( styles ) => styles, |
| 50 | + flatten: ( style ) => Object.assign( {}, ...[].concat( style || {} ) ), |
| 51 | +}; |
| 52 | + |
| 53 | +const Font = { |
| 54 | + register: jest.fn(), |
| 55 | + getRegisteredFonts: jest.fn( () => [] ), |
| 56 | + clear: jest.fn(), |
| 57 | +}; |
| 58 | + |
| 59 | +const pdf = jest.fn( () => ( { |
| 60 | + toBlob: jest.fn( () => |
| 61 | + Promise.resolve( |
| 62 | + new Blob( [ 'mock-pdf' ], { type: 'application/pdf' } ) |
| 63 | + ) |
| 64 | + ), |
| 65 | + toBuffer: jest.fn( () => Promise.resolve( Buffer.from( 'mock-pdf' ) ) ), |
| 66 | + toString: jest.fn( () => Promise.resolve( 'mock-pdf' ) ), |
| 67 | + updateContainer: jest.fn(), |
| 68 | + on: jest.fn(), |
| 69 | +} ) ); |
| 70 | + |
| 71 | +const PDFViewer = passthrough( 'PDFViewer' ); |
| 72 | +const PDFDownloadLink = passthrough( 'PDFDownloadLink' ); |
| 73 | +function BlobProvider( { children } ) { |
| 74 | + return typeof children === 'function' |
| 75 | + ? children( { blob: null, url: null, loading: false, error: null } ) |
| 76 | + : null; |
| 77 | +} |
| 78 | + |
| 79 | +module.exports = { |
| 80 | + Document, |
| 81 | + Page, |
| 82 | + View, |
| 83 | + Text, |
| 84 | + Image, |
| 85 | + Link, |
| 86 | + Note, |
| 87 | + Canvas, |
| 88 | + StyleSheet, |
| 89 | + Font, |
| 90 | + pdf, |
| 91 | + PDFViewer, |
| 92 | + PDFDownloadLink, |
| 93 | + BlobProvider, |
| 94 | + __esModule: true, |
| 95 | + default: { |
| 96 | + Document, |
| 97 | + Page, |
| 98 | + View, |
| 99 | + Text, |
| 100 | + Image, |
| 101 | + Link, |
| 102 | + Note, |
| 103 | + Canvas, |
| 104 | + StyleSheet, |
| 105 | + Font, |
| 106 | + pdf, |
| 107 | + PDFViewer, |
| 108 | + PDFDownloadLink, |
| 109 | + BlobProvider, |
| 110 | + }, |
| 111 | +}; |
0 commit comments