77 * file that was distributed with this source code.
88 */
99
10-
1110import { Application } from '@hotwired/stimulus' ;
1211import { getByTestId , waitFor } from '@testing-library/dom' ;
1312import AutocompleteController , {
@@ -21,7 +20,7 @@ import { vi } from 'vitest';
2120
2221const shortDelay = ( ms : number ) : Promise < void > => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
2322
24- const startAutocompleteTest = async ( html : string ) : Promise < { container : HTMLElement , tomSelect : TomSelect } > => {
23+ const startAutocompleteTest = async ( html : string ) : Promise < { container : HTMLElement ; tomSelect : TomSelect } > => {
2524 const container = document . createElement ( 'div' ) ;
2625 container . innerHTML = html ;
2726
@@ -48,7 +47,7 @@ const startAutocompleteTest = async (html: string): Promise<{ container: HTMLEle
4847 }
4948
5049 return { container, tomSelect } ;
51- }
50+ } ;
5251
5352const fetchMocker = createFetchMock ( vi ) ;
5453describe ( 'AutocompleteController' , ( ) => {
@@ -80,7 +79,7 @@ describe('AutocompleteController', () => {
8079 } ) ;
8180
8281 it ( 'connect with ajax URL on a select element' , async ( ) => {
83- const { container, tomSelect} = await startAutocompleteTest ( `
82+ const { container, tomSelect } = await startAutocompleteTest ( `
8483 <label for="the-select">Items</label>
8584 <select
8685 id="the-select"
@@ -96,25 +95,25 @@ describe('AutocompleteController', () => {
9695 results : [
9796 {
9897 value : 3 ,
99- text : 'salad'
98+ text : 'salad' ,
10099 } ,
101- ]
102- } ) ,
100+ ] ,
101+ } )
103102 ) ;
104103
105104 fetchMock . mockResponseOnce (
106105 JSON . stringify ( {
107106 results : [
108107 {
109108 value : 1 ,
110- text : 'pizza'
109+ text : 'pizza' ,
111110 } ,
112111 {
113112 value : 2 ,
114- text : 'popcorn'
115- }
116- ]
117- } ) ,
113+ text : 'popcorn' ,
114+ } ,
115+ ] ,
116+ } )
118117 ) ;
119118
120119 const controlInput = tomSelect . control_input ;
@@ -140,7 +139,7 @@ describe('AutocompleteController', () => {
140139 } ) ;
141140
142141 it ( 'connect with ajax URL on an input element' , async ( ) => {
143- const { container, tomSelect} = await startAutocompleteTest ( `
142+ const { container, tomSelect } = await startAutocompleteTest ( `
144143 <label for="the-input">Items</label>
145144 <input
146145 id="the-input"
@@ -156,25 +155,25 @@ describe('AutocompleteController', () => {
156155 results : [
157156 {
158157 value : 3 ,
159- text : 'salad'
158+ text : 'salad' ,
160159 } ,
161160 ] ,
162- } ) ,
161+ } )
163162 ) ;
164163
165164 fetchMock . mockResponseOnce (
166165 JSON . stringify ( {
167166 results : [
168167 {
169168 value : 1 ,
170- text : 'pizza'
169+ text : 'pizza' ,
171170 } ,
172171 {
173172 value : 2 ,
174- text : 'popcorn'
173+ text : 'popcorn' ,
175174 } ,
176175 ] ,
177- } ) ,
176+ } )
178177 ) ;
179178
180179 const controlInput = tomSelect . control_input ;
@@ -233,8 +232,8 @@ describe('AutocompleteController', () => {
233232 ></select>
234233 ` ) ;
235234
236- expect ( tomSelect . settings . shouldLoad ( '' ) ) . toBeTruthy ( )
237- } )
235+ expect ( tomSelect . settings . shouldLoad ( '' ) ) . toBeTruthy ( ) ;
236+ } ) ;
238237
239238 it ( 'default min-characters will always load after first load' , async ( ) => {
240239 const { container, tomSelect } = await startAutocompleteTest ( `
@@ -255,10 +254,10 @@ describe('AutocompleteController', () => {
255254 results : [
256255 {
257256 value : 1 ,
258- text : 'pizza'
257+ text : 'pizza' ,
259258 } ,
260259 ] ,
261- } ) ,
260+ } )
262261 ) ;
263262 // wait for the initial Ajax request to finish
264263 userEvent . click ( controlInput ) ;
@@ -280,14 +279,14 @@ describe('AutocompleteController', () => {
280279 results : [
281280 {
282281 value : 1 ,
283- text : 'pizza'
282+ text : 'pizza' ,
284283 } ,
285284 {
286285 value : 2 ,
287- text : 'popcorn'
286+ text : 'popcorn' ,
288287 } ,
289288 ] ,
290- } ) ,
289+ } )
291290 ) ;
292291 controlInput . value = 'foo' ;
293292 controlInput . dispatchEvent ( new Event ( 'input' ) ) ;
@@ -302,18 +301,18 @@ describe('AutocompleteController', () => {
302301 results : [
303302 {
304303 value : 1 ,
305- text : 'pizza'
304+ text : 'pizza' ,
306305 } ,
307306 {
308307 value : 2 ,
309- text : 'popcorn'
308+ text : 'popcorn' ,
310309 } ,
311310 {
312311 value : 3 ,
313- text : 'apples'
312+ text : 'apples' ,
314313 } ,
315314 ] ,
316- } ) ,
315+ } )
317316 ) ;
318317 controlInput . value = 'fo' ;
319318 controlInput . dispatchEvent ( new Event ( 'input' ) ) ;
@@ -350,19 +349,19 @@ describe('AutocompleteController', () => {
350349 fetchMock . mockResponseOnce (
351350 JSON . stringify ( {
352351 results : [
353- { value : 1 , text : 'dog1' } ,
354- { value : 2 , text : 'dog2' } ,
355- { value : 3 , text : 'dog3' } ,
356- { value : 4 , text : 'dog4' } ,
357- { value : 5 , text : 'dog5' } ,
358- { value : 6 , text : 'dog6' } ,
359- { value : 7 , text : 'dog7' } ,
360- { value : 8 , text : 'dog8' } ,
361- { value : 9 , text : 'dog9' } ,
362- { value : 10 , text : 'dog10' } ,
352+ { value : 1 , text : 'dog1' } ,
353+ { value : 2 , text : 'dog2' } ,
354+ { value : 3 , text : 'dog3' } ,
355+ { value : 4 , text : 'dog4' } ,
356+ { value : 5 , text : 'dog5' } ,
357+ { value : 6 , text : 'dog6' } ,
358+ { value : 7 , text : 'dog7' } ,
359+ { value : 8 , text : 'dog8' } ,
360+ { value : 9 , text : 'dog9' } ,
361+ { value : 10 , text : 'dog10' } ,
363362 ] ,
364- next_page : '/path/to/autocomplete?query=&page=2'
365- } ) ,
363+ next_page : '/path/to/autocomplete?query=&page=2' ,
364+ } )
366365 ) ;
367366
368367 const controlInput = tomSelect . control_input ;
@@ -380,11 +379,11 @@ describe('AutocompleteController', () => {
380379 fetchMock . mockResponseOnce (
381380 JSON . stringify ( {
382381 results : [
383- { value : 11 , text : 'dog11' } ,
384- { value : 12 , text : 'dog12' } ,
382+ { value : 11 , text : 'dog11' } ,
383+ { value : 12 , text : 'dog12' } ,
385384 ] ,
386385 next_page : null ,
387- } ) ,
386+ } )
388387 ) ;
389388
390389 // trigger a scroll, this will cause TomSelect to check "shouldLoadMore"
@@ -514,7 +513,7 @@ describe('AutocompleteController', () => {
514513 <option value="8">dog8</option>
515514 ` ;
516515
517- let newTomSelect : TomSelect | null = null ;
516+ let newTomSelect : TomSelect | null = null ;
518517 container . addEventListener ( 'autocomplete:connect' , ( event : any ) => {
519518 newTomSelect = ( event . detail as AutocompleteConnectOptions ) . tomSelect ;
520519 } ) ;
@@ -570,8 +569,10 @@ describe('AutocompleteController', () => {
570569 tomSelect . addItem ( '3' ) ;
571570 tomSelect . addItem ( '2' ) ;
572571 const getSelectedValues = ( ) => {
573- return Array . from ( selectElement . selectedOptions ) . map ( ( option ) => option . value ) . sort ( ) ;
574- }
572+ return Array . from ( selectElement . selectedOptions )
573+ . map ( ( option ) => option . value )
574+ . sort ( ) ;
575+ } ;
575576 const selectElement = getByTestId ( container , 'main-element' ) as HTMLSelectElement ;
576577 expect ( getSelectedValues ( ) ) . toEqual ( [ '2' , '3' ] ) ;
577578
@@ -645,7 +646,7 @@ describe('AutocompleteController', () => {
645646 const selectElement = getByTestId ( container , 'main-element' ) as HTMLSelectElement ;
646647 expect ( tomSelect . control_input . placeholder ) . toBe ( 'Select a dog' ) ;
647648
648- let newTomSelect : TomSelect | null = null ;
649+ let newTomSelect : TomSelect | null = null ;
649650 container . addEventListener ( 'autocomplete:connect' , ( event : any ) => {
650651 newTomSelect = ( event . detail as AutocompleteConnectOptions ) . tomSelect ;
651652 } ) ;
@@ -685,36 +686,36 @@ describe('AutocompleteController', () => {
685686 {
686687 group_by : [ 'Meat' ] ,
687688 value : 1 ,
688- text : 'Beef'
689+ text : 'Beef' ,
689690 } ,
690691 {
691692 group_by : [ 'Meat' ] ,
692693 value : 2 ,
693- text : 'Mutton'
694+ text : 'Mutton' ,
694695 } ,
695696 {
696697 group_by : [ 'starchy' ] ,
697698 value : 3 ,
698- text : 'Potatoes'
699+ text : 'Potatoes' ,
699700 } ,
700701 {
701702 group_by : [ 'starchy' , 'Meat' ] ,
702703 value : 4 ,
703- text : 'chili con carne'
704+ text : 'chili con carne' ,
704705 } ,
705706 ] ,
706707 optgroups : [
707708 {
708709 value : 'Meat' ,
709- label : 'Meat'
710+ label : 'Meat' ,
710711 } ,
711712 {
712713 value : 'starchy' ,
713- label : 'starchy'
714+ label : 'starchy' ,
714715 } ,
715- ]
716+ ] ,
716717 } ,
717- } ) ,
718+ } )
718719 ) ;
719720
720721 fetchMock . mockResponseOnce (
@@ -724,22 +725,22 @@ describe('AutocompleteController', () => {
724725 {
725726 group_by : [ 'Meat' ] ,
726727 value : 1 ,
727- text : 'Beef'
728+ text : 'Beef' ,
728729 } ,
729730 {
730731 group_by : [ 'Meat' ] ,
731732 value : 2 ,
732- text : 'Mutton'
733+ text : 'Mutton' ,
733734 } ,
734735 ] ,
735736 optgroups : [
736737 {
737738 value : 'Meat' ,
738- label : 'Meat'
739+ label : 'Meat' ,
739740 } ,
740- ]
741- }
742- } ) ,
741+ ] ,
742+ } ,
743+ } )
743744 ) ;
744745
745746 const controlInput = tomSelect . control_input ;
@@ -801,8 +802,10 @@ describe('AutocompleteController', () => {
801802 tomSelect . addItem ( '3' ) ;
802803
803804 const getSelectedValues = ( ) => {
804- return Array . from ( selectElement . selectedOptions ) . map ( ( option ) => option . value ) . sort ( ) ;
805- }
805+ return Array . from ( selectElement . selectedOptions )
806+ . map ( ( option ) => option . value )
807+ . sort ( ) ;
808+ } ;
806809
807810 const selectElement = getByTestId ( container , 'main-element' ) as HTMLSelectElement ;
808811 expect ( getSelectedValues ( ) ) . toEqual ( [ '2' , '3' ] ) ;
0 commit comments