1
1
import { describe , expect , it } from 'vitest'
2
2
3
- import { mountSuspended , registerEndpoint } from '@nuxt/test-utils/runtime-utils'
4
-
5
- import { listen } from 'listhen'
6
- import { createApp , eventHandler , toNodeListener } from 'h3'
3
+ import { mountSuspended } from '@nuxt/test-utils/runtime-utils'
7
4
8
5
import App from '~/app.vue'
9
- import FetchComponent from '~/components/FetchComponent.vue'
10
6
import OptionsComponent from '~/components/OptionsComponent.vue'
11
7
import WrapperTests from '~/components/WrapperTests.vue'
12
8
13
9
import { mount } from '@vue/test-utils'
14
10
15
- describe ( 'client-side nuxt features' , ( ) => {
16
- it ( 'can use core nuxt composables within test file' , ( ) => {
17
- expect ( useAppConfig ( ) . hey ) . toMatchInlineSnapshot ( 'false' )
18
- } )
11
+ import ExportDefaultComponent from '~/components/ExportDefaultComponent.vue'
12
+ import ExportDefineComponent from '~/components/ExportDefineComponent.vue'
13
+ import ExportDefaultWithRenderComponent from '~/components/ExportDefaultWithRenderComponent.vue'
14
+ import ExportDefaultReturnsRenderComponent from '~/components/ExportDefaultReturnsRenderComponent.vue'
19
15
20
- it ( 'can access auto-imported composables from within project' , ( ) => {
21
- const state = useSingleState ( )
22
- expect ( state . value ) . toMatchInlineSnapshot ( '{}' )
23
- state . value . field = 'new value'
24
- expect ( state . value . field ) . toMatchInlineSnapshot ( '"new value"' )
25
- expect ( useSingleState ( ) . value . field ) . toMatchInlineSnapshot ( '"new value"' )
26
- } )
27
-
28
- it ( 'can access injections from nuxt plugins' , ( ) => {
29
- const app = useNuxtApp ( )
30
- expect ( app . $auth . didInject ) . toMatchInlineSnapshot ( 'true' )
31
- expect ( app . $router ) . toBeDefined ( )
32
- } )
16
+ const formats = {
17
+ ExportDefaultComponent,
18
+ ExportDefineComponent,
19
+ ExportDefaultWithRenderComponent,
20
+ ExportDefaultReturnsRenderComponent,
21
+ }
33
22
34
- it ( 'defaults to index page' , async ( ) => {
35
- expect ( useRoute ( ) . matched [ 0 ] . meta ) . toMatchInlineSnapshot ( `
36
- {
37
- "value": "set in index",
38
- }
39
- ` )
40
- } )
41
-
42
- it ( 'allows pushing to other pages' , async ( ) => {
43
- await navigateTo ( '/something' )
44
- expect ( useNuxtApp ( ) . $router . currentRoute . value . path ) . toEqual ( '/something' )
45
- await nextTick ( )
46
- expect ( useRoute ( ) . path ) . toEqual ( '/something' )
47
- } )
48
- } )
49
-
50
- describe ( 'test utils' , ( ) => {
23
+ describe ( 'mountSuspended' , ( ) => {
51
24
it ( 'can mount components within nuxt suspense' , async ( ) => {
52
25
const component = await mountSuspended ( App )
53
26
expect ( component . html ( ) ) . toMatchInlineSnapshot ( `
@@ -114,19 +87,6 @@ describe('test utils', () => {
114
87
` )
115
88
} )
116
89
117
- it ( 'can use $fetch' , async ( ) => {
118
- const app = createApp ( ) . use (
119
- '/todos/1' ,
120
- eventHandler ( ( ) => ( { id : 1 } ) )
121
- )
122
- const server = await listen ( toNodeListener ( app ) )
123
- const [ { url } ] = await server . getURLs ( )
124
- expect ( await $fetch < unknown > ( '/todos/1' , { baseURL : url } ) ) . toMatchObject ( {
125
- id : 1 ,
126
- } )
127
- await server . close ( )
128
- } )
129
-
130
90
// This test works (you can delete it later)
131
91
it ( 'can receive emitted events from components using defineModel' , ( ) => {
132
92
const component = mount ( WrapperTests )
@@ -147,51 +107,19 @@ describe('test utils', () => {
147
107
expect ( component . vm . testExpose ?.( ) ) . toBe ( 'thing' )
148
108
expect ( component . vm . someRef ) . toBe ( 'thing' )
149
109
} )
110
+ } )
150
111
151
- it ( 'can mock fetch requests' , async ( ) => {
152
- registerEndpoint ( 'https://jsonplaceholder.typicode.com/todos/1' , ( ) => ( {
153
- title : 'title from mocked api' ,
154
- } ) )
155
- const component = await mountSuspended ( FetchComponent )
156
- expect ( component . html ( ) ) . toMatchInlineSnapshot (
157
- '"<div>title from mocked api</div>"'
158
- )
159
- } )
160
-
161
- it ( 'can mock fetch requests' , async ( ) => {
162
- registerEndpoint ( '/with-query' , ( ) => ( {
163
- title : 'mocked' ,
164
- } ) )
165
- expect (
166
- await $fetch < unknown > ( '/with-query' , { query : { test : true } } )
167
- ) . toMatchObject ( {
168
- title : 'mocked' ,
169
- } )
170
- } )
171
-
172
- it ( 'can mock fetch requests with explicit methods' , async ( ) => {
173
- registerEndpoint ( '/method' , {
174
- method : 'POST' ,
175
- handler : ( ) => ( { method : 'POST' } ) ,
176
- } )
177
- registerEndpoint ( '/method' , {
178
- method : 'GET' ,
179
- handler : ( ) => ( { method : 'GET' } ) ,
180
- } )
181
- expect ( await $fetch < unknown > ( '/method' , { method : 'POST' } ) ) . toMatchObject ( {
182
- method : 'POST' ,
112
+ describe . each ( Object . entries ( formats ) ) ( `%s` , ( name , component ) => {
113
+ it ( 'mounts with props' , async ( ) => {
114
+ const wrapper = await mountSuspended ( component , {
115
+ props : {
116
+ myProp : 'Hello nuxt-vitest' ,
117
+ } ,
183
118
} )
184
- expect ( await $fetch < unknown > ( '/method' ) ) . toMatchObject ( { method : 'GET' } )
185
- } )
186
-
187
- // TODO: reenable when merging Nuxt 3.7
188
- it . skip ( 'handles nuxt routing' , async ( ) => {
189
- const component = await mountSuspended ( App , { route : '/test' } )
190
- expect ( component . html ( ) ) . toMatchInlineSnapshot ( `
191
- "<div>This is an auto-imported component</div>
192
- <div> I am a global component </div>
193
- <div>/test</div>
194
- <a href=\\"/test\\"> Test link </a>"
195
- ` )
119
+ expect ( wrapper . html ( ) ) . toEqual ( `
120
+ <div>
121
+ <h1>${ name } </h1><pre>Hello nuxt-vitest</pre><pre>XHello nuxt-vitest</pre>
122
+ </div>
123
+ ` . trim ( ) )
196
124
} )
197
125
} )
0 commit comments