Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/devtools-kit/__tests__/component/replacer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { stringifyReplacer } from '../../src/core/component/state/replacer'

// `stringifyReplacer` reads the value from `this[key]`, mirroring how it is
// called during `JSON.stringify`.
function replace(value: unknown) {
return stringifyReplacer.call({ value }, 'value') as { _custom?: { type?: string, displayText?: string } }
}

describe('stringifyReplacer: component definition detection', () => {
// #1100: Pinia store state can hold plain class instances that happen to
// expose a `render` method. Those must not be reported as Vue components.
it('does not treat a plain class instance with a render method as a component', () => {
class Chart {
data = [1, 2, 3]
render() {
return 'draw the chart'
}
}

const result = replace(new Chart())

expect(result?._custom?.type).not.toBe('component-definition')
})

it('still detects a real Vue component definition', () => {
const Button = defineComponent({
name: 'MyButton',
render() {
return h('button', 'Click me')
},
})

const result = replace(Button)

expect(result?._custom?.type).toBe('component-definition')
expect(result?._custom?.displayText).toBe('MyButton')
})
})
2 changes: 1 addition & 1 deletion packages/devtools-kit/src/core/component/state/replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function stringifyReplacer(key: string | number, _value: any, depth?: num
seenInstance?.set(val, depth!)
return componentVal
}
else if (ensurePropertyExists(val, 'render', true) && typeof val.render === 'function') {
else if (Object.prototype.hasOwnProperty.call(val, 'render') && typeof (val as Record<string, unknown>).render === 'function') {
return getComponentDefinitionDetails(val)
}
else if (val.constructor && val.constructor.name === 'VNode') {
Expand Down