diff --git a/stores/suggestion_store.jsx b/stores/suggestion_store.jsx index 6646f1b4764d..9c752e68a6d8 100644 --- a/stores/suggestion_store.jsx +++ b/stores/suggestion_store.jsx @@ -117,6 +117,10 @@ class SuggestionStore extends EventEmitter { suggestion.selection = ''; } + suggestionBoxExists(id) { + return this.suggestions.has(id); + } + hasSuggestions(id) { return this.getSuggestions(id).terms.length > 0; } @@ -250,6 +254,10 @@ class SuggestionStore extends EventEmitter { handleEventPayload(payload) { const {type, id, ...other} = payload.action; + if (id && !this.suggestionBoxExists(id)) { + return; + } + switch (type) { case ActionTypes.SUGGESTION_PRETEXT_CHANGED: // Clear the suggestions if the pretext is empty or ends with whitespace diff --git a/tests/stores/suggestion_store.test.jsx b/tests/stores/suggestion_store.test.jsx new file mode 100644 index 000000000000..8d48fd80715b --- /dev/null +++ b/tests/stores/suggestion_store.test.jsx @@ -0,0 +1,52 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import assert from 'assert'; + +import {ActionTypes} from 'utils/constants.jsx'; +import SuggestionStore from 'stores/suggestion_store.jsx'; + +describe('stores/SuggestionStore', () => { + test('should ignore events from unknown suggestion boxes', () => { + const id = 'unknown'; + const pretext = 'pretext'; + SuggestionStore.handleEventPayload({ + action: { + id, + type: ActionTypes.SUGGESTION_PRETEXT_CHANGED, + pretext, + }, + }); + assert.equal(SuggestionStore.suggestions.has(id), false); + }); + + test('should process events from registered suggestion boxes', () => { + const id = 'id1'; + const pretext = 'pretext'; + SuggestionStore.registerSuggestionBox(id); + SuggestionStore.handleEventPayload({ + action: { + id, + type: ActionTypes.SUGGESTION_PRETEXT_CHANGED, + pretext, + }, + }); + assert.equal(SuggestionStore.suggestions.get(id).pretext, pretext); + }); + + test('should ignore events from unregistered suggestion boxes', () => { + const id = 'id1'; + const pretext = 'pretext'; + SuggestionStore.registerSuggestionBox(id); + SuggestionStore.unregisterSuggestionBox(id); + + SuggestionStore.handleEventPayload({ + action: { + id, + type: ActionTypes.SUGGESTION_PRETEXT_CHANGED, + pretext, + }, + }); + assert.equal(SuggestionStore.suggestions.has(id), false); + }); +});