Skip to content

Commit 9f76ff9

Browse files
committed
refactor: add BC on tom-select remote data
1 parent 94cc1b5 commit 9f76ff9

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

src/Autocomplete/assets/dist/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ _instances = new WeakSet(), _getCommonConfig = function _getCommonConfig() {
131131
const url = this.getUrl(query);
132132
fetch(url)
133133
.then(response => response.json())
134-
.then(json => { this.setNextUrl(query, json.next_page); callback(json.results.options, json.results.optgroups || []); })
134+
.then(json => { this.setNextUrl(query, json.next_page); callback(json.results.options || json.results, json.results.optgroups || []); })
135135
.catch(() => callback());
136136
},
137137
shouldLoad: function (query) {

src/Autocomplete/assets/src/controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export default class extends Controller {
141141
fetch(url)
142142
.then(response => response.json())
143143
// important: next_url must be set before invoking callback()
144-
.then(json => { this.setNextUrl(query, json.next_page); callback(json.results.options, json.results.optgroups || []) })
144+
.then(json => { this.setNextUrl(query, json.next_page); callback(json.results.options || json.results, json.results.optgroups || []) })
145145
.catch(() => callback());
146146
},
147147
shouldLoad: function (query: string) {

src/Autocomplete/assets/test/controller.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)