@@ -87,6 +87,71 @@ describe('AutocompleteController', () => {
8787 expect ( tomSelect . input ) . toBe ( getByTestId ( container , 'main-element' ) ) ;
8888 } ) ;
8989
90+ it ( 'connect with ajax URL BC data' , async ( ) => {
91+ const container = mountDOM ( `
92+ <label for="the-select">Items</label>
93+ <select
94+ id="the-select"
95+ data-testid="main-element"
96+ data-controller="check autocomplete"
97+ data-autocomplete-url-value="/path/to/autocomplete"
98+ ></select>
99+ ` ) ;
100+
101+ application = startStimulus ( ) ;
102+
103+ await waitFor ( ( ) => {
104+ expect ( getByTestId ( container , 'main-element' ) ) . toHaveClass ( 'connected' ) ;
105+ } ) ;
106+
107+ // initial Ajax request on focus
108+ fetchMock . mock (
109+ '/path/to/autocomplete?query=' ,
110+ JSON . stringify ( {
111+ results : [
112+ {
113+ value : 3 ,
114+ text : 'salad'
115+ } ,
116+ ]
117+ } ) ,
118+ ) ;
119+
120+ fetchMock . mock (
121+ '/path/to/autocomplete?query=foo' ,
122+ JSON . stringify ( {
123+ results : [
124+ {
125+ value : 1 ,
126+ text : 'pizza'
127+ } ,
128+ {
129+ value : 2 ,
130+ text : 'popcorn'
131+ }
132+ ]
133+ } ) ,
134+ ) ;
135+
136+ const tomSelect = getByTestId ( container , 'main-element' ) . tomSelect ;
137+ const controlInput = tomSelect . control_input ;
138+
139+ // wait for the initial Ajax request to finish
140+ userEvent . click ( controlInput ) ;
141+ await waitFor ( ( ) => {
142+ expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 1 ) ;
143+ } ) ;
144+
145+ // typing was not properly triggering, for some reason
146+ //userEvent.type(controlInput, 'foo');
147+ controlInput . value = 'foo' ;
148+ controlInput . dispatchEvent ( new Event ( 'input' ) ) ;
149+
150+ await waitFor ( ( ) => {
151+ expect ( container . querySelectorAll ( '.option[data-selectable]' ) ) . toHaveLength ( 2 ) ;
152+ } ) ;
153+ } ) ;
154+
90155 it ( 'connect with ajax URL' , async ( ) => {
91156 const container = mountDOM ( `
92157 <label for="the-select">Items</label>
0 commit comments