From cfe83509c094390f170f9cec53d1f6576c2162de Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Fri, 20 Jan 2017 11:38:06 -0600 Subject: [PATCH] Fix autocomplete initial undefined state Fix https://gitlab.com/gitlab-org/gitlab-ce/issues/26775 --- .../javascripts/gfm_auto_complete.js.es6 | 11 +++++--- ...6775-fix-auto-complete-initial-loading.yml | 4 +++ .../javascripts/gfm_auto_complete_spec.js.es6 | 26 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/26775-fix-auto-complete-initial-loading.yml diff --git a/app/assets/javascripts/gfm_auto_complete.js.es6 b/app/assets/javascripts/gfm_auto_complete.js.es6 index a1b7b442882..3f23095dad9 100644 --- a/app/assets/javascripts/gfm_auto_complete.js.es6 +++ b/app/assets/javascripts/gfm_auto_complete.js.es6 @@ -367,9 +367,14 @@ return $input.trigger('keyup'); }, isLoading(data) { - if (!data || !data.length) return false; - if (Array.isArray(data)) data = data[0]; - return data === this.defaultLoadingData[0] || data.name === this.defaultLoadingData[0]; + var dataToInspect = data; + if (data && data.length > 0) { + dataToInspect = data[0]; + } + + var loadingState = this.defaultLoadingData[0]; + return dataToInspect && + (dataToInspect === loadingState || dataToInspect.name === loadingState); } }; }).call(this); diff --git a/changelogs/unreleased/26775-fix-auto-complete-initial-loading.yml b/changelogs/unreleased/26775-fix-auto-complete-initial-loading.yml new file mode 100644 index 00000000000..2d4ec482ee0 --- /dev/null +++ b/changelogs/unreleased/26775-fix-auto-complete-initial-loading.yml @@ -0,0 +1,4 @@ +--- +title: Fix autocomplete initial undefined state +merge_request: +author: diff --git a/spec/javascripts/gfm_auto_complete_spec.js.es6 b/spec/javascripts/gfm_auto_complete_spec.js.es6 index 6b48d82cb23..99cebb32a8b 100644 --- a/spec/javascripts/gfm_auto_complete_spec.js.es6 +++ b/spec/javascripts/gfm_auto_complete_spec.js.es6 @@ -62,4 +62,30 @@ describe('GfmAutoComplete', function () { }); }); }); + + describe('isLoading', function () { + it('should be true with loading data object item', function () { + expect(GfmAutoComplete.isLoading({ name: 'loading' })).toBe(true); + }); + + it('should be true with loading data array', function () { + expect(GfmAutoComplete.isLoading(['loading'])).toBe(true); + }); + + it('should be true with loading data object array', function () { + expect(GfmAutoComplete.isLoading([{ name: 'loading' }])).toBe(true); + }); + + it('should be false with actual array data', function () { + expect(GfmAutoComplete.isLoading([ + { title: 'Foo' }, + { title: 'Bar' }, + { title: 'Qux' }, + ])).toBe(false); + }); + + it('should be false with actual data item', function () { + expect(GfmAutoComplete.isLoading({ title: 'Foo' })).toBe(false); + }); + }); });