Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

MM-9973: ignore dispatched events against unregistered suggestion boxes #1052

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions stores/suggestion_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions tests/stores/suggestion_store.test.jsx
Original file line number Diff line number Diff line change
@@ -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);
});
});