Skip to content
代码片段 群组 项目
未验证 提交 4676ed31 编辑于 作者: Miguel Rincon's avatar Miguel Rincon 提交者: GitLab
浏览文件

Merge branch 'hly-fix-autocomplete-overlapping-searches' into 'master'

Fix @ autocomplete when search requests overlap

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/153924



Merged-by: default avatarMiguel Rincon <mrincon@gitlab.com>
Approved-by: default avatarJames Rushford <jrushford@gitlab.com>
Approved-by: default avatarMiguel Rincon <mrincon@gitlab.com>
Approved-by: default avatarCostel Maxim <cmaxim@gitlab.com>
Co-authored-by: default avatarHeinrich Lee Yu <heinrich@gitlab.com>
No related branches found
No related tags found
无相关合并请求
......@@ -155,6 +155,7 @@ class GfmAutoComplete {
this.cachedData = {};
this.isLoadingData = {};
this.previousQuery = undefined;
this.currentBackendFilterRequestController = null;
}
setup(input, enableMap = defaultAutocompleteConfig) {
......@@ -930,7 +931,7 @@ class GfmAutoComplete {
}
fetchData($input, at, search) {
if (this.isLoadingData[at]) return;
if (this.isLoadingData[at] && !GfmAutoComplete.isTypeWithBackendFiltering(at)) return;
this.isLoadingData[at] = true;
const dataSource = this.dataSources[GfmAutoComplete.atTypeMap[at]];
......@@ -939,13 +940,25 @@ class GfmAutoComplete {
if (this.cachedData[at]?.[search]) {
this.loadData($input, at, this.cachedData[at][search], { search });
} else {
if (this.currentBackendFilterRequestController) {
this.currentBackendFilterRequestController.abort();
}
this.currentBackendFilterRequestController = new AbortController();
axios
.get(dataSource, { params: { search } })
.get(dataSource, {
params: { search },
signal: this.currentBackendFilterRequestController.signal,
})
.then(({ data }) => {
this.loadData($input, at, data, { search });
})
.catch(() => {
this.isLoadingData[at] = false;
})
.finally(() => {
this.currentBackendFilterRequestController = null;
});
}
} else if (this.cachedData[at]) {
......
......@@ -188,22 +188,6 @@ describe('GfmAutoComplete', () => {
mock.restore();
});
describe('already loading data', () => {
beforeEach(() => {
const context = {
isLoadingData: { '[vulnerability:': true },
dataSources: {},
cachedData: {},
};
fetchData.call(context, {}, '[vulnerability:', '');
});
it('should not call either axios nor AjaxCache', () => {
expect(axios.get).not.toHaveBeenCalled();
expect(AjaxCache.retrieve).not.toHaveBeenCalled();
});
});
describe('backend filtering', () => {
describe('data is not in cache', () => {
let context;
......@@ -221,6 +205,26 @@ describe('GfmAutoComplete', () => {
expect(axios.get).toHaveBeenCalledWith('vulnerabilities_autocomplete_url', {
params: { search: 'query' },
signal: expect.any(AbortSignal),
});
});
it('should abort previous request and call axios again with another search query', () => {
const abortSpy = jest.spyOn(AbortController.prototype, 'abort');
fetchData.call(context, {}, '[vulnerability:', 'query');
fetchData.call(context, {}, '[vulnerability:', 'query2');
expect(axios.get).toHaveBeenCalledWith('vulnerabilities_autocomplete_url', {
params: { search: 'query' },
signal: expect.any(AbortSignal),
});
expect(abortSpy).toHaveBeenCalled();
expect(axios.get).toHaveBeenCalledWith('vulnerabilities_autocomplete_url', {
params: { search: 'query2' },
signal: expect.any(AbortSignal),
});
});
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册