Skip to content

Commit

Permalink
fix: allow access to nested computed values
Browse files Browse the repository at this point in the history
  • Loading branch information
Evobaso-J committed Feb 29, 2024
1 parent 894ae63 commit 438f990
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/createInstance.ts
Expand Up @@ -4,7 +4,6 @@ import {
defineComponent,
reactive,
shallowReactive,
isRef,
ref,
AppConfig,
ComponentOptions,
Expand All @@ -17,6 +16,7 @@ import { MountingOptions, Slot } from './types'
import {
getComponentsFromStubs,
getDirectivesFromStubs,
isDeepRef,
isFunctionalComponent,
isObject,
isObjectComponent,
Expand Down Expand Up @@ -174,7 +174,7 @@ export function createInstance(
...options?.props,
ref: MOUNT_COMPONENT_REF
}).forEach(([k, v]) => {
if (isRef(v)) {
if (isDeepRef(v)) {
refs[k] = v
} else {
props[k] = v
Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Expand Up @@ -7,7 +7,8 @@ import {
VNode,
VNodeProps,
FunctionalComponent,
ComponentInternalInstance
ComponentInternalInstance,
Ref
} from 'vue'

export interface RefSelector {
Expand Down Expand Up @@ -167,3 +168,12 @@ export type VueNode<T extends Node = Node> = T & {
export type VueElement = VueNode<Element>

export type DefinedComponent = new (...args: any[]) => any

/**
* T is a DeepRef if:
* - It's a Ref itself
* - It's an array containing a ref at any level
* - It's an object containing a ref at any level
*/
export type DeepRef<T> =
T extends Ref<T> ? T : T extends object ? DeepRef<T> : Ref<T>
17 changes: 15 additions & 2 deletions src/utils.ts
@@ -1,11 +1,12 @@
import { GlobalMountOptions, RefSelector, Stub, Stubs } from './types'
import { DeepRef, GlobalMountOptions, RefSelector, Stub, Stubs } from './types'
import {
Component,
ComponentOptions,
ComponentPublicInstance,
ConcreteComponent,
Directive,
FunctionalComponent
FunctionalComponent,
isRef
} from 'vue'
import { config } from './config'

Expand Down Expand Up @@ -252,3 +253,15 @@ export const getGlobalThis = (): any => {
: {})
)
}

/**
* Checks if the given value is a DeepRef.
*
* For both arrays and objects, it will recursively check
* if any of their values is a Ref.
*
* @param {DeepRef<T> | unknown} r - The value to check.
* @returns {boolean} Returns true if the value is a DeepRef, false otherwise.
*/
export const isDeepRef = <T>(r: DeepRef<T> | unknown): r is DeepRef<T> =>
isRef(r) || (isObject(r) && Object.values(r).some(isDeepRef))
55 changes: 55 additions & 0 deletions tests/components/WithDeepRef.vue
@@ -0,0 +1,55 @@
<template>
<div id="countValue">{{ countValue }}</div>
<div id="oneLayerCountObjectValue">{{ oneLayerCountObjectValue }}</div>
<div id="twoLayersCountObjectValue">{{ twoLayersCountObjectValue }}</div>
<div id="countArrayValue">{{ countArrayValue }}</div>
<div id="countMatrixValue">{{ countMatrixValue }}</div>
<div id="oneLayerCountObjectArrayValue">
{{ oneLayerCountObjectArrayValue }}
</div>
<div id="oneLayerCountArrayObjectValue">
{{ oneLayerCountArrayObjectValue }}
</div>
<div id="oneLayerCountObjectMatrixValue">
{{ oneLayerCountObjectMatrixValue }}
</div>
</template>

<script setup lang="ts">
import { computed, Ref } from 'vue'
type Props = {
count: Ref<number>
oneLayerCountObject: { count: Ref<number> }
twoLayersCountObject: { oneLayerCountObject: { count: Ref<number> } }
countArray: Ref<number>[]
countMatrix: Ref<number>[][]
oneLayerCountObjectArray: { count: Ref<number> }[]
oneLayerCountArrayObject: { count: Ref<number>[] }
oneLayerCountObjectMatrix: { count: Ref<number> }[][]
}
const props = defineProps<Props>()
const countValue = computed(() => props.count.value)
const oneLayerCountObjectValue = computed(
() => props.oneLayerCountObject.count.value
)
const twoLayersCountObjectValue = computed(
() => props.twoLayersCountObject.oneLayerCountObject.count.value
)
const countArrayValue = computed(() => props.countArray[0].value)
const countMatrixValue = computed(() => props.countMatrix[0][0].value)
const oneLayerCountObjectArrayValue = computed(
() => props.oneLayerCountObjectArray[0].count.value
)
const oneLayerCountArrayObjectValue = computed(
() => props.oneLayerCountArrayObject.count[0].value
)
const oneLayerCountObjectMatrixValue = computed(
() => props.oneLayerCountObjectMatrix[0][0].count.value
)
</script>
36 changes: 35 additions & 1 deletion tests/mount.spec.ts
@@ -1,7 +1,8 @@
import { describe, expect, it, vi } from 'vitest'
import { defineComponent } from 'vue'
import { defineComponent, ref } from 'vue'
import { mount } from '../src'
import DefinePropsAndDefineEmits from './components/DefinePropsAndDefineEmits.vue'
import WithDeepRef from './components/WithDeepRef.vue'
import HelloFromVitestPlayground from './components/HelloFromVitestPlayground.vue'

describe('mount: general tests', () => {
Expand Down Expand Up @@ -44,4 +45,37 @@ describe('mount: general tests', () => {
expect(wrapper.get('div').text()).toContain('Hello')
expect(wrapper.get('div').classes()).toContain('end')
})
it('allows access to nested computed values', async () => {
const wrapper = mount(WithDeepRef, {
props: {
count: ref(1),
oneLayerCountObject: { count: ref(2) },
twoLayersCountObject: { oneLayerCountObject: { count: ref(3) } },

countArray: [ref(4)],
countMatrix: [[ref(5)]],

oneLayerCountObjectArray: [{ count: ref(6) }],
oneLayerCountArrayObject: { count: [ref(7)] },
oneLayerCountObjectMatrix: [[{ count: ref(8) }]]
}
})

expect(wrapper.get('#countValue').text()).toBe('1')
expect(wrapper.vm.countValue).toBe(1)

Check failure on line 65 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'countValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'.

Check failure on line 65 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'countValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'.
expect(wrapper.get('#oneLayerCountObjectValue').text()).toBe('2')
expect(wrapper.vm.oneLayerCountObjectValue).toBe(2)

Check failure on line 67 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'oneLayerCountObjectValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountObject'?

Check failure on line 67 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'oneLayerCountObjectValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountObject'?
expect(wrapper.get('#twoLayersCountObjectValue').text()).toBe('3')
expect(wrapper.vm.twoLayersCountObjectValue).toBe(3)

Check failure on line 69 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'twoLayersCountObjectValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'twoLayersCountObject'?

Check failure on line 69 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'twoLayersCountObjectValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'twoLayersCountObject'?
expect(wrapper.get('#countArrayValue').text()).toBe('4')
expect(wrapper.vm.countArrayValue).toBe(4)

Check failure on line 71 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'countArrayValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'countArray'?

Check failure on line 71 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'countArrayValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'countArray'?
expect(wrapper.get('#countMatrixValue').text()).toBe('5')
expect(wrapper.vm.countMatrixValue).toBe(5)

Check failure on line 73 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'countMatrixValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'countMatrix'?

Check failure on line 73 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'countMatrixValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'countMatrix'?
expect(wrapper.get('#oneLayerCountObjectArrayValue').text()).toBe('6')
expect(wrapper.vm.oneLayerCountObjectArrayValue).toBe(6)

Check failure on line 75 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'oneLayerCountObjectArrayValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountObjectArray'?

Check failure on line 75 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'oneLayerCountObjectArrayValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountObjectArray'?
expect(wrapper.get('#oneLayerCountArrayObjectValue').text()).toBe('7')
expect(wrapper.vm.oneLayerCountArrayObjectValue).toBe(7)

Check failure on line 77 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'oneLayerCountArrayObjectValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountArrayObject'?

Check failure on line 77 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'oneLayerCountArrayObjectValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountArrayObject'?
expect(wrapper.get('#oneLayerCountObjectMatrixValue').text()).toBe('8')
expect(wrapper.vm.oneLayerCountObjectMatrixValue).toBe(8)

Check failure on line 79 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Property 'oneLayerCountObjectMatrixValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountObjectMatrix'?

Check failure on line 79 in tests/mount.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'oneLayerCountObjectMatrixValue' does not exist on type 'ComponentPublicInstance<NonNullable<Partial<{}> & Omit<{ readonly count: Ref<number>; readonly oneLayerCountObject: { count: Ref<number>; }; readonly twoLayersCountObject: { oneLayerCountObject: { count: Ref<...>; }; }; ... 4 more ...; readonly oneLayerCountObjectMatrix: { ...; }[][]; } & VNodeProps & AllowedCompone...'. Did you mean 'oneLayerCountObjectMatrix'?
})
})

0 comments on commit 438f990

Please sign in to comment.