* @see README.md * @param component The component for which to retrieve metadata.
(
component: T,
_refs?: Map<T, number>,
)
| 1000 | * @param component The component for which to retrieve metadata. |
| 1001 | */ |
| 1002 | getMetadata<T = unknown>( |
| 1003 | component: T, |
| 1004 | _refs?: Map<T, number>, |
| 1005 | ): MockMetadata<T> | null { |
| 1006 | const refs = _refs || new Map<T, number>(); |
| 1007 | const ref = refs.get(component); |
| 1008 | if (ref != null) { |
| 1009 | return {ref}; |
| 1010 | } |
| 1011 | |
| 1012 | const type = getType(component); |
| 1013 | if (!type) { |
| 1014 | return null; |
| 1015 | } |
| 1016 | |
| 1017 | const metadata: MockMetadata<T> = {type}; |
| 1018 | if ( |
| 1019 | type === 'constant' || |
| 1020 | type === 'collection' || |
| 1021 | type === 'undefined' || |
| 1022 | type === 'null' |
| 1023 | ) { |
| 1024 | metadata.value = component; |
| 1025 | return metadata; |
| 1026 | } else if (type === 'function') { |
| 1027 | // @ts-expect-error component is a function so it has a name, but not |
| 1028 | // necessarily a string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#function_names_in_classes |
| 1029 | const componentName = component.name; |
| 1030 | if (typeof componentName === 'string') { |
| 1031 | metadata.name = componentName; |
| 1032 | } |
| 1033 | if (this.isMockFunction(component)) { |
| 1034 | metadata.mockImpl = component.getMockImplementation() as T; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | metadata.refID = refs.size; |
| 1039 | refs.set(component, metadata.refID); |
| 1040 | |
| 1041 | let members: Record<string, MockMetadata<T>> | null = null; |
| 1042 | // Leave arrays alone |
| 1043 | if (type !== 'array') { |
| 1044 | // @ts-expect-error component is object |
| 1045 | for (const slot of this._getSlots(component)) { |
| 1046 | if ( |
| 1047 | type === 'function' && |
| 1048 | this.isMockFunction(component) && |
| 1049 | slot.startsWith('mock') |
| 1050 | ) { |
| 1051 | continue; |
| 1052 | } |
| 1053 | // @ts-expect-error no index signature |
| 1054 | const slotMetadata = this.getMetadata<T>(component[slot], refs); |
| 1055 | if (slotMetadata) { |
| 1056 | if (!members) { |
| 1057 | members = {}; |
| 1058 | } |
| 1059 | members[slot] = slotMetadata; |