|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import type { ComponentPublicInstance } from 'vue'; |
| 3 | + |
| 4 | +import { buildComponentHierarchyFrames } from '../src/buildComponentHierarchyFrames'; |
| 5 | +import { MAX_HIERARCHY_DEPTH } from '../src/constants'; |
| 6 | + |
| 7 | +function createMockInstance( |
| 8 | + name: string, |
| 9 | + { |
| 10 | + parent = null, |
| 11 | + file = undefined, |
| 12 | + props = undefined, |
| 13 | + }: { |
| 14 | + parent?: ComponentPublicInstance | null; |
| 15 | + file?: string; |
| 16 | + props?: Record<string, unknown>; |
| 17 | + } = {} |
| 18 | +): ComponentPublicInstance { |
| 19 | + return { |
| 20 | + $options: { |
| 21 | + __name: name, |
| 22 | + ...(file !== undefined ? { __file: file } : {}), |
| 23 | + }, |
| 24 | + $parent: parent, |
| 25 | + $props: props ?? {}, |
| 26 | + } as unknown as ComponentPublicInstance; |
| 27 | +} |
| 28 | + |
| 29 | +describe('buildComponentHierarchyFrames', () => { |
| 30 | + test('returns an empty array for null instance', () => { |
| 31 | + expect(buildComponentHierarchyFrames(null)).toEqual([]); |
| 32 | + }); |
| 33 | + |
| 34 | + test('returns a single frame for a root component', () => { |
| 35 | + const instance = createMockInstance('App'); |
| 36 | + |
| 37 | + expect(buildComponentHierarchyFrames(instance)).toEqual([{ component: 'App', file: null, props: {} }]); |
| 38 | + }); |
| 39 | + |
| 40 | + test('builds frames through $parent chain', () => { |
| 41 | + const grandparent = createMockInstance('App'); |
| 42 | + const parent = createMockInstance('Layout', { parent: grandparent }); |
| 43 | + const child = createMockInstance('Button', { parent }); |
| 44 | + |
| 45 | + const frames = buildComponentHierarchyFrames(child); |
| 46 | + |
| 47 | + expect(frames).toEqual([ |
| 48 | + { component: 'Button', file: null, props: {} }, |
| 49 | + { component: 'Layout', file: null, props: {} }, |
| 50 | + { component: 'App', file: null, props: {} }, |
| 51 | + ]); |
| 52 | + }); |
| 53 | + |
| 54 | + test('includes __file when available', () => { |
| 55 | + const parent = createMockInstance('App', { file: 'src/App.vue' }); |
| 56 | + const child = createMockInstance('Button', { |
| 57 | + parent, |
| 58 | + file: 'src/components/Button.vue', |
| 59 | + }); |
| 60 | + |
| 61 | + const frames = buildComponentHierarchyFrames(child); |
| 62 | + |
| 63 | + expect(frames[0].file).toBe('src/components/Button.vue'); |
| 64 | + expect(frames[1].file).toBe('src/App.vue'); |
| 65 | + }); |
| 66 | + |
| 67 | + test('sets file to null when __file is not present', () => { |
| 68 | + const instance = createMockInstance('App'); |
| 69 | + |
| 70 | + const frames = buildComponentHierarchyFrames(instance); |
| 71 | + |
| 72 | + expect(frames[0].file).toBeNull(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('includes props from the component instance', () => { |
| 76 | + const parent = createMockInstance('App'); |
| 77 | + const child = createMockInstance('UserCard', { |
| 78 | + parent, |
| 79 | + props: { userId: 42, name: 'Alice' }, |
| 80 | + }); |
| 81 | + |
| 82 | + const frames = buildComponentHierarchyFrames(child); |
| 83 | + |
| 84 | + expect(frames[0].props).toEqual({ userId: 42, name: 'Alice' }); |
| 85 | + expect(frames[1].props).toEqual({}); |
| 86 | + }); |
| 87 | + |
| 88 | + test('creates a shallow copy of props', () => { |
| 89 | + const originalProps = { userId: 42 }; |
| 90 | + const instance = createMockInstance('UserCard', { props: originalProps }); |
| 91 | + |
| 92 | + const frames = buildComponentHierarchyFrames(instance); |
| 93 | + |
| 94 | + expect(frames[0].props).toEqual({ userId: 42 }); |
| 95 | + expect(frames[0].props).not.toBe(originalProps); |
| 96 | + }); |
| 97 | + |
| 98 | + test('sets props to null when $props is null', () => { |
| 99 | + const instance = { |
| 100 | + $options: { __name: 'App' }, |
| 101 | + $parent: null, |
| 102 | + $props: null, |
| 103 | + } as unknown as ComponentPublicInstance; |
| 104 | + |
| 105 | + const frames = buildComponentHierarchyFrames(instance); |
| 106 | + |
| 107 | + expect(frames[0].props).toBeNull(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('uses AnonymousComponent for unnamed components in the chain', () => { |
| 111 | + const parent = createMockInstance('App'); |
| 112 | + const child = { |
| 113 | + $options: {}, |
| 114 | + $parent: parent, |
| 115 | + $props: {}, |
| 116 | + } as unknown as ComponentPublicInstance; |
| 117 | + |
| 118 | + const frames = buildComponentHierarchyFrames(child); |
| 119 | + |
| 120 | + expect(frames[0].component).toBe('AnonymousComponent'); |
| 121 | + expect(frames[1].component).toBe('App'); |
| 122 | + }); |
| 123 | + |
| 124 | + test('respects MAX_HIERARCHY_DEPTH limit', () => { |
| 125 | + let current: ComponentPublicInstance | null = null; |
| 126 | + |
| 127 | + for (let i = 0; i < MAX_HIERARCHY_DEPTH + 50; i++) { |
| 128 | + current = createMockInstance(`Component${i}`, { parent: current }); |
| 129 | + } |
| 130 | + |
| 131 | + const frames = buildComponentHierarchyFrames(current); |
| 132 | + |
| 133 | + expect(frames).toHaveLength(MAX_HIERARCHY_DEPTH); |
| 134 | + expect(frames[0].component).toBe(`Component${MAX_HIERARCHY_DEPTH + 49}`); |
| 135 | + expect(frames[MAX_HIERARCHY_DEPTH - 1].component).toBe('Component50'); |
| 136 | + }); |
| 137 | + |
| 138 | + test('combines file, props, and hierarchy correctly', () => { |
| 139 | + const root = createMockInstance('App', { file: 'src/App.vue' }); |
| 140 | + const layout = createMockInstance('Layout', { |
| 141 | + parent: root, |
| 142 | + file: 'src/layouts/Layout.vue', |
| 143 | + props: { sidebar: true }, |
| 144 | + }); |
| 145 | + const page = createMockInstance('UserProfile', { |
| 146 | + parent: layout, |
| 147 | + file: 'src/pages/UserProfile.vue', |
| 148 | + props: { userId: 42, tab: 'settings' }, |
| 149 | + }); |
| 150 | + |
| 151 | + const frames = buildComponentHierarchyFrames(page); |
| 152 | + |
| 153 | + expect(frames).toEqual([ |
| 154 | + { |
| 155 | + component: 'UserProfile', |
| 156 | + file: 'src/pages/UserProfile.vue', |
| 157 | + props: { userId: 42, tab: 'settings' }, |
| 158 | + }, |
| 159 | + { |
| 160 | + component: 'Layout', |
| 161 | + file: 'src/layouts/Layout.vue', |
| 162 | + props: { sidebar: true }, |
| 163 | + }, |
| 164 | + { |
| 165 | + component: 'App', |
| 166 | + file: 'src/App.vue', |
| 167 | + props: {}, |
| 168 | + }, |
| 169 | + ]); |
| 170 | + }); |
| 171 | +}); |
0 commit comments