| 1 | function hasAbsoluteFileName(hook) { |
| 2 | const fileName = hook.hookSource ? hook.hookSource.fileName : null; |
| 3 | if (fileName == null) { |
| 4 | return false; |
| 5 | } else { |
| 6 | return fileName.indexOf('/react-devtools-shared/') > 0; |
| 7 | } |
| 8 | } |
| 9 | |
| 10 | function serializeHook(hook) { |
| 11 | if (!hook.hookSource) return hook; |
| 12 | |
| 13 | // Remove user-specific portions of this file path. |
| 14 | let fileName = hook.hookSource.fileName; |
| 15 | const index = fileName.lastIndexOf('/react-devtools-shared/'); |
| 16 | fileName = fileName.slice(index + 1); |
| 17 | |
| 18 | let subHooks = hook.subHooks; |
| 19 | if (subHooks) { |
| 20 | subHooks = subHooks.map(serializeHook); |
| 21 | } |
| 22 | |
| 23 | return { |
| 24 | ...hook, |
| 25 | hookSource: { |
| 26 | ...hook.hookSource, |
| 27 | fileName, |
| 28 | |
| 29 | // Otherwise changes in any test case or formatting might invalidate other tests. |
| 30 | columnNumber: 'removed by Jest serializer', |
| 31 | lineNumber: 'removed by Jest serializer', |
| 32 | }, |
| 33 | subHooks, |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | // `test` is part of Jest's serializer API |
| 38 | export function test(maybeHook) { |
| 39 | if (maybeHook === null || typeof maybeHook !== 'object') { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | const hasOwnProperty = Object.prototype.hasOwnProperty.bind(maybeHook); |
| 44 | |
| 45 | return ( |
| 46 | hasOwnProperty('id') && |
| 47 | hasOwnProperty('isStateEditable') && |
| 48 | hasOwnProperty('name') && |
| 49 | hasOwnProperty('subHooks') && |
| 50 | hasOwnProperty('value') && |
| 51 | // Don't re-process an already printed hook. |
| 52 | hasAbsoluteFileName(maybeHook) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | // print() is part of Jest's serializer API |
| 57 | export function print(hook, serialize, indent) { |
| 58 | // Don't stringify this object; that would break nested serializers. |
| 59 | return serialize(serializeHook(hook)); |
| 60 | } |