@@ -15,9 +15,10 @@ import AutocompleteController, {
15
15
AutocompleteConnectOptions ,
16
16
AutocompletePreConnectOptions ,
17
17
} from '../src/controller' ;
18
- import fetchMock from 'fetch-mock-jest' ;
19
18
import userEvent from '@testing-library/user-event' ;
20
19
import TomSelect from 'tom-select' ;
20
+ import createFetchMock from 'vitest-fetch-mock' ;
21
+ import { vi } from 'vitest' ;
21
22
22
23
const shortDelay = ( ms : number ) : Promise < void > => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
23
24
@@ -50,19 +51,21 @@ const startAutocompleteTest = async (html: string): Promise<{ container: HTMLEle
50
51
return { container, tomSelect } ;
51
52
}
52
53
54
+ const fetchMocker = createFetchMock ( vi ) ;
53
55
describe ( 'AutocompleteController' , ( ) => {
54
56
beforeAll ( ( ) => {
55
57
const application = Application . start ( ) ;
56
58
application . register ( 'autocomplete' , AutocompleteController ) ;
59
+
60
+ fetchMocker . enableMocks ( ) ;
61
+ } ) ;
62
+
63
+ beforeEach ( ( ) => {
64
+ fetchMocker . resetMocks ( ) ;
57
65
} ) ;
58
66
59
67
afterEach ( ( ) => {
60
68
document . body . innerHTML = '' ;
61
-
62
- if ( ! fetchMock . done ( ) ) {
63
- throw new Error ( 'Mocked requests did not match' ) ;
64
- }
65
- fetchMock . reset ( ) ;
66
69
} ) ;
67
70
68
71
it ( 'connect without options' , async ( ) => {
@@ -74,6 +77,7 @@ describe('AutocompleteController', () => {
74
77
` ) ;
75
78
76
79
expect ( tomSelect . input ) . toBe ( getByTestId ( container , 'main-element' ) ) ;
80
+ expect ( fetchMock . requests ( ) . length ) . toEqual ( 0 ) ;
77
81
} ) ;
78
82
79
83
it ( 'connect with ajax URL on a select element' , async ( ) => {
@@ -88,8 +92,7 @@ describe('AutocompleteController', () => {
88
92
` ) ;
89
93
90
94
// initial Ajax request on focus
91
- fetchMock . mock (
92
- '/path/to/autocomplete?query=' ,
95
+ fetchMock . mockResponseOnce (
93
96
JSON . stringify ( {
94
97
results : [
95
98
{
@@ -100,8 +103,7 @@ describe('AutocompleteController', () => {
100
103
} ) ,
101
104
) ;
102
105
103
- fetchMock . mock (
104
- '/path/to/autocomplete?query=foo' ,
106
+ fetchMock . mockResponseOnce (
105
107
JSON . stringify ( {
106
108
results : [
107
109
{
@@ -132,6 +134,10 @@ describe('AutocompleteController', () => {
132
134
await waitFor ( ( ) => {
133
135
expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 2 ) ;
134
136
} ) ;
137
+
138
+ expect ( fetchMock . requests ( ) . length ) . toEqual ( 2 ) ;
139
+ expect ( fetchMock . requests ( ) [ 0 ] . url ) . toEqual ( '/path/to/autocomplete?query=' ) ;
140
+ expect ( fetchMock . requests ( ) [ 1 ] . url ) . toEqual ( '/path/to/autocomplete?query=foo' ) ;
135
141
} ) ;
136
142
137
143
it ( 'connect with ajax URL on an input element' , async ( ) => {
@@ -146,8 +152,7 @@ describe('AutocompleteController', () => {
146
152
` ) ;
147
153
148
154
// initial Ajax request on focus
149
- fetchMock . mock (
150
- '/path/to/autocomplete?query=' ,
155
+ fetchMock . mockResponseOnce (
151
156
JSON . stringify ( {
152
157
results : [
153
158
{
@@ -158,8 +163,7 @@ describe('AutocompleteController', () => {
158
163
} ) ,
159
164
) ;
160
165
161
- fetchMock . mock (
162
- '/path/to/autocomplete?query=foo' ,
166
+ fetchMock . mockResponseOnce (
163
167
JSON . stringify ( {
164
168
results : [
165
169
{
@@ -190,6 +194,10 @@ describe('AutocompleteController', () => {
190
194
await waitFor ( ( ) => {
191
195
expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 2 ) ;
192
196
} ) ;
197
+
198
+ expect ( fetchMock . requests ( ) . length ) . toEqual ( 2 ) ;
199
+ expect ( fetchMock . requests ( ) [ 0 ] . url ) . toEqual ( '/path/to/autocomplete?query=' ) ;
200
+ expect ( fetchMock . requests ( ) [ 1 ] . url ) . toEqual ( '/path/to/autocomplete?query=foo' ) ;
193
201
} ) ;
194
202
195
203
it ( 'limits updates when min-characters' , async ( ) => {
@@ -212,6 +220,8 @@ describe('AutocompleteController', () => {
212
220
await waitFor ( ( ) => {
213
221
expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 0 ) ;
214
222
} ) ;
223
+
224
+ expect ( fetchMock . requests ( ) . length ) . toEqual ( 0 ) ;
215
225
} ) ;
216
226
217
227
it ( 'min-characters can be a falsy value' , async ( ) => {
@@ -241,8 +251,7 @@ describe('AutocompleteController', () => {
241
251
const controlInput = tomSelect . control_input ;
242
252
243
253
// ajax call from initial focus
244
- fetchMock . mock (
245
- '/path/to/autocomplete?query=' ,
254
+ fetchMock . mockResponseOnce (
246
255
JSON . stringify ( {
247
256
results : [
248
257
{
@@ -267,8 +276,7 @@ describe('AutocompleteController', () => {
267
276
} ) ;
268
277
269
278
// now trigger a load
270
- fetchMock . mock (
271
- '/path/to/autocomplete?query=foo' ,
279
+ fetchMock . mockResponseOnce (
272
280
JSON . stringify ( {
273
281
results : [
274
282
{
@@ -290,8 +298,7 @@ describe('AutocompleteController', () => {
290
298
} ) ;
291
299
292
300
// now go below the min characters, but it should still load
293
- fetchMock . mock (
294
- '/path/to/autocomplete?query=fo' ,
301
+ fetchMock . mockResponseOnce (
295
302
JSON . stringify ( {
296
303
results : [
297
304
{
@@ -315,6 +322,11 @@ describe('AutocompleteController', () => {
315
322
await waitFor ( ( ) => {
316
323
expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 3 ) ;
317
324
} ) ;
325
+
326
+ expect ( fetchMock . requests ( ) . length ) . toEqual ( 3 ) ;
327
+ expect ( fetchMock . requests ( ) [ 0 ] . url ) . toEqual ( '/path/to/autocomplete?query=' ) ;
328
+ expect ( fetchMock . requests ( ) [ 1 ] . url ) . toEqual ( '/path/to/autocomplete?query=foo' ) ;
329
+ expect ( fetchMock . requests ( ) [ 2 ] . url ) . toEqual ( '/path/to/autocomplete?query=fo' ) ;
318
330
} ) ;
319
331
320
332
it ( 'adds work-around for live-component & multiple select' , async ( ) => {
@@ -359,8 +371,7 @@ describe('AutocompleteController', () => {
359
371
` ) ;
360
372
361
373
// initial Ajax request on focus
362
- fetchMock . mock (
363
- '/path/to/autocomplete?query=' ,
374
+ fetchMock . mockResponseOnce (
364
375
JSON . stringify ( {
365
376
results : [
366
377
{ value : 1 , text : 'dog1' } ,
@@ -390,8 +401,7 @@ describe('AutocompleteController', () => {
390
401
throw new Error ( 'cannot find dropdown content element' ) ;
391
402
}
392
403
393
- fetchMock . mock (
394
- '/path/to/autocomplete?query=&page=2' ,
404
+ fetchMock . mockResponseOnce (
395
405
JSON . stringify ( {
396
406
results : [
397
407
{ value : 11 , text : 'dog11' } ,
@@ -407,6 +417,10 @@ describe('AutocompleteController', () => {
407
417
await waitFor ( ( ) => {
408
418
expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 12 ) ;
409
419
} ) ;
420
+
421
+ expect ( fetchMock . requests ( ) . length ) . toEqual ( 2 ) ;
422
+ expect ( fetchMock . requests ( ) [ 0 ] . url ) . toEqual ( '/path/to/autocomplete?query=' ) ;
423
+ expect ( fetchMock . requests ( ) [ 1 ] . url ) . toEqual ( '/path/to/autocomplete?query=&page=2' ) ;
410
424
} ) ;
411
425
412
426
it ( 'continues working even if options html rearranges' , async ( ) => {
@@ -625,8 +639,7 @@ describe('AutocompleteController', () => {
625
639
` ) ;
626
640
627
641
// initial Ajax request on focus with group_by options
628
- fetchMock . mock (
629
- '/path/to/autocomplete?query=' ,
642
+ fetchMock . mockResponseOnce (
630
643
JSON . stringify ( {
631
644
results : {
632
645
options : [
@@ -665,8 +678,7 @@ describe('AutocompleteController', () => {
665
678
} ) ,
666
679
) ;
667
680
668
- fetchMock . mock (
669
- '/path/to/autocomplete?query=foo' ,
681
+ fetchMock . mockResponseOnce (
670
682
JSON . stringify ( {
671
683
results : {
672
684
options : [
@@ -709,5 +721,9 @@ describe('AutocompleteController', () => {
709
721
expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 2 ) ;
710
722
expect ( container . querySelectorAll ( '.optgroup-header' ) ) . toHaveLength ( 1 ) ;
711
723
} ) ;
724
+
725
+ expect ( fetchMock . requests ( ) . length ) . toEqual ( 2 ) ;
726
+ expect ( fetchMock . requests ( ) [ 0 ] . url ) . toEqual ( '/path/to/autocomplete?query=' ) ;
727
+ expect ( fetchMock . requests ( ) [ 1 ] . url ) . toEqual ( '/path/to/autocomplete?query=foo' ) ;
712
728
} ) ;
713
729
} ) ;
0 commit comments