Skip to content

Commit

Permalink
test: add tests for events emitted from defineModel (#629)
Browse files Browse the repository at this point in the history
Co-authored-by: Vasily Kuzin <exer7um@gmail.com>
  • Loading branch information
danielroe and ExEr7um committed Dec 5, 2023
1 parent 1206db7 commit 33651af
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 10 additions & 1 deletion examples/app-vitest-full/components/WrapperTests.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ function testExpose () {
return 'expose was successful'
}
const modelValue = defineModel({ default: false })
defineExpose({
testExpose
})
</script>

<template>
<div>
<button @click="emit('customEvent', 'foo'); $emit('otherEvent')">
<button
id="emitCustomEvent"
@click="emit('customEvent', 'foo'); $emit('otherEvent')"
>
Click me!
</button>
<button
id="changeModelValue"
@click="modelValue = true"
/>
</div>
</template>
7 changes: 7 additions & 0 deletions examples/app-vitest-full/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
export default defineNuxtConfig({
devtools: { enabled: true },
modules: ['@nuxt/test-utils/module', '~/modules/custom'],
vite: {
vue: {
script: {
defineModel: true,
},
},
},
vitest: {
startOnBoot: true,
logToConsole: true,
Expand Down
18 changes: 17 additions & 1 deletion examples/app-vitest-full/tests/nuxt/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import FetchComponent from '~/components/FetchComponent.vue'
import OptionsComponent from '~/components/OptionsComponent.vue'
import WrapperTests from '~/components/WrapperTests.vue'

import { mount } from '@vue/test-utils'

describe('client-side nuxt features', () => {
it('can use core nuxt composables within test file', () => {
expect(useAppConfig().hey).toMatchInlineSnapshot('false')
Expand Down Expand Up @@ -100,7 +102,7 @@ describe('test utils', () => {

it('can receive emitted events from components mounted within nuxt suspense', async () => {
const component = await mountSuspended(WrapperTests)
component.find('button').trigger('click')
component.find('button#emitCustomEvent').trigger('click')
expect(component.emitted()).toMatchInlineSnapshot(`
{
"customEvent": [
Expand Down Expand Up @@ -128,6 +130,20 @@ describe('test utils', () => {
await server.close()
})

// This test works (you can delete it later)
it('can receive emitted events from components using defineModel', () => {
const component = mount(WrapperTests)
component.find('button#changeModelValue').trigger('click')
expect(component.emitted()).toHaveProperty('update:modelValue')
})

// TODO: fix this failing test
it.todo('can receive emitted events from components mounted within nuxt suspense using defineModel', async () => {
const component = await mountSuspended(WrapperTests)
component.find('button#changeModelValue').trigger('click')
expect(component.emitted()).toHaveProperty('update:modelValue')
})

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

0 comments on commit 33651af

Please sign in to comment.