|
| 1 | +import { ReactTestInstance } from 'react-test-renderer'; |
| 2 | +import { |
| 3 | + RECEIVED_COLOR, |
| 4 | + matcherHint, |
| 5 | + printWithType, |
| 6 | + printReceived, |
| 7 | +} from 'jest-matcher-utils'; |
| 8 | +import prettyFormat, { plugins } from 'pretty-format'; |
| 9 | +import redent from 'redent'; |
| 10 | +import { isHostElement } from '../helpers/component-tree'; |
| 11 | + |
| 12 | +class HostElementTypeError extends Error { |
| 13 | + constructor( |
| 14 | + received: unknown, |
| 15 | + matcherFn: jest.CustomMatcher, |
| 16 | + context: jest.MatcherContext |
| 17 | + ) { |
| 18 | + super(); |
| 19 | + |
| 20 | + /* istanbul ignore next */ |
| 21 | + if (Error.captureStackTrace) { |
| 22 | + Error.captureStackTrace(this, matcherFn); |
| 23 | + } |
| 24 | + |
| 25 | + let withType = ''; |
| 26 | + try { |
| 27 | + withType = printWithType('Received', received, printReceived); |
| 28 | + /* istanbul ignore next */ |
| 29 | + } catch (e) { |
| 30 | + // Deliberately empty. |
| 31 | + } |
| 32 | + |
| 33 | + this.message = [ |
| 34 | + matcherHint( |
| 35 | + `${context.isNot ? '.not' : ''}.${matcherFn.name}`, |
| 36 | + 'received', |
| 37 | + '' |
| 38 | + ), |
| 39 | + '', |
| 40 | + `${RECEIVED_COLOR('received')} value must be a host element.`, |
| 41 | + withType, |
| 42 | + ].join('\n'); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Throws HostElementTypeError if passed element is not a host element. |
| 48 | + * |
| 49 | + * @param element ReactTestInstance to check. |
| 50 | + * @param matcherFn Matcher function calling the check used for formatting error. |
| 51 | + * @param context Jest matcher context used for formatting error. |
| 52 | + */ |
| 53 | +export function checkHostElement( |
| 54 | + element: ReactTestInstance | null | undefined, |
| 55 | + matcherFn: jest.CustomMatcher, |
| 56 | + context: jest.MatcherContext |
| 57 | +): asserts element is ReactTestInstance { |
| 58 | + if (!isHostElement(element)) { |
| 59 | + throw new HostElementTypeError(element, matcherFn, context); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/*** |
| 64 | + * Format given element as a pretty-printed string. |
| 65 | + * |
| 66 | + * @param element Element to format. |
| 67 | + */ |
| 68 | +export function formatElement(element: ReactTestInstance | null) { |
| 69 | + if (element == null) { |
| 70 | + return 'null'; |
| 71 | + } |
| 72 | + |
| 73 | + return redent( |
| 74 | + prettyFormat( |
| 75 | + { |
| 76 | + // This prop is needed persuade the prettyFormat that the element is |
| 77 | + // a ReactTestRendererJSON instance, so it is formatted as JSX. |
| 78 | + $$typeof: Symbol.for('react.test.json'), |
| 79 | + type: element.type, |
| 80 | + props: element.props, |
| 81 | + }, |
| 82 | + { |
| 83 | + plugins: [plugins.ReactTestComponent, plugins.ReactElement], |
| 84 | + printFunctionName: false, |
| 85 | + printBasicPrototype: false, |
| 86 | + highlight: true, |
| 87 | + } |
| 88 | + ), |
| 89 | + 2 |
| 90 | + ); |
| 91 | +} |
0 commit comments