Skip to content

Commit

Permalink
refactor: wrap <script setup> assertion into isScriptSetup util and u…
Browse files Browse the repository at this point in the history
…se it

- drop hasSetupState check, if a component is <script setup> i always has setup state
- do note that isScriptSetup and hasSetupState check for different things. I tried to replaced hasSetupScript with isScriptSetup but some specs around expose started to fail so we are required to keep it for now.
  • Loading branch information
renatodeleao committed Jan 4, 2023
1 parent aec9623 commit 4dbed2e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
getComponentsFromStubs,
getDirectivesFromStubs,
hasSetupState,
isScriptSetup,
isFunctionalComponent,
isObject,
isObjectComponent,
Expand Down Expand Up @@ -485,7 +486,7 @@ export function mount(
// Also ensures not to include option API components in this this block
// since they can also have setup state but need to be patched using
// the regular method.
if (hasSetupState(this) && this.$.setupState.__isScriptSetup) {
if (isScriptSetup(this)) {
// add the mocks to setupState
for (const [k, v] of Object.entries(
global.mocks as { [key: string]: any }
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export function getDirectivesFromStubs(
.map(([key, value]) => [key.substring(1), value])
) as Record<string, Directive>
}

export function hasSetupState(
vm: ComponentPublicInstance
): vm is ComponentPublicInstance & {
Expand All @@ -195,3 +196,13 @@ export function hasSetupState(
(vm.$ as unknown as { devtoolsRawSetupState: any }).devtoolsRawSetupState
)
}

export function isScriptSetup(
vm: ComponentPublicInstance
): vm is ComponentPublicInstance & {
$: { setupState: Record<string, unknown> }
} {
return (
vm && (vm.$ as unknown as { setupState: any }).setupState.__isScriptSetup
)
}

0 comments on commit 4dbed2e

Please sign in to comment.