Skip to content

Commit

Permalink
feat(helper): support options for mountSuspeded
Browse files Browse the repository at this point in the history
* Add options to `mountSuspended` helper
* Create a new component to test passing props/slots to async component
* Test the default and passed props/slots to async components
  • Loading branch information
ghazialhouwari committed Mar 14, 2023
1 parent c6cd405 commit a0eeef8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/vitest-environment-nuxt/src/runtime/mount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount, VueWrapper } from '@vue/test-utils'
import { mount, MountingOptions, VueWrapper } from '@vue/test-utils'
import { h, DefineComponent, Suspense, nextTick } from 'vue'

import { RouterLink } from './components/RouterLink'
Expand All @@ -8,7 +8,7 @@ import NuxtRoot from '#build/root-component.mjs'

export async function mountSuspended<
T extends DefineComponent<any, any, any, any>
>(component: T) {
>(component: T, options?: MountingOptions<any>) {
return new Promise<VueWrapper<InstanceType<T>>>(resolve => {
const vm = mount(
{
Expand All @@ -17,12 +17,22 @@ export async function mountSuspended<
h(
Suspense,
{ onResolve: () => nextTick().then(() => resolve(vm as any)) },
{ default: () => h(component) }
{
default: () =>
h(
component,
{ ...options?.props, ...options?.attrs },
{ ...options?.slots }
),
}
),
},
{
...options,
global: {
...options?.global,
components: {
...options?.global?.components,
RouterLink,
},
},
Expand Down
19 changes: 19 additions & 0 deletions playground/components/AsyncComponentWithProps.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<h2>{{ title }}</h2>
<div>
<slot />
</div>
</template>

<script setup lang="ts">
const props = withDefaults(
defineProps<{
title?: string
}>(),
{
title: 'The original',
}
)
const title = ref(props.title)
await new Promise(resolve => setTimeout(resolve, 1))
</script>
30 changes: 30 additions & 0 deletions playground/tests/nuxt/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mountSuspended, registerEndpoint } from 'vitest-environment-nuxt/utils'

import App from '~/app.vue'
import FetchComponent from '~/components/FetchComponent.vue'
import AsyncComponentWithProps from '~~/components/AsyncComponentWithProps.vue'

describe('client-side nuxt features', () => {
it('can use core nuxt composables within test file', () => {
Expand Down Expand Up @@ -43,6 +44,35 @@ describe('test utils', () => {
`)
})

it('should render default props within nuxt suspense', async () => {
const component = await mountSuspended(AsyncComponentWithProps)
expect(component.find('h2').html()).toMatchInlineSnapshot(
'"<h2>The original</h2>"'
)
})

it('should render passed props within nuxt suspense', async () => {
const component = await mountSuspended(AsyncComponentWithProps, {
props: {
title: 'title from mount suspense props',
},
})
expect(component.find('h2').html()).toMatchInlineSnapshot(
'"<h2>title from mount suspense props</h2>"'
)
})

it('can pass slots to mounted components within nuxt suspense', async () => {
const component = await mountSuspended(AsyncComponentWithProps, {
slots: {
default: 'slot from mount suspense',
},
})
expect(component.find('div').html()).toMatchInlineSnapshot(
'"<div>slot from mount suspense</div>"'
)
})

it('can mock fetch requests', async () => {
registerEndpoint('https://jsonplaceholder.typicode.com/todos/1', () => ({
title: 'title from mocked api',
Expand Down

0 comments on commit a0eeef8

Please sign in to comment.