Skip to content

Commit 86cd646

Browse files
authored
fix: add new poll option only when all of the current options are filled (#1532)
Currently the options are added when the last option has some text. But when the new option added is reordered, there is no way to add a new option. To improve this, the new behaviour is to add new option when all of the option fields are filled.
1 parent 8f6948f commit 86cd646

File tree

2 files changed

+61
-10
lines changed
  • src/messageComposer/middleware/pollComposer
  • test/unit/MessageComposer/middleware/pollComposer

2 files changed

+61
-10
lines changed

src/messageComposer/middleware/pollComposer/state.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,26 @@ export const pollCompositionStateProcessors: Partial<
116116
const { index, text } = value;
117117
const prevOptions = data.options || [];
118118

119-
const shouldAddEmptyOption =
120-
prevOptions.length < MAX_POLL_OPTIONS &&
121-
(!prevOptions || (prevOptions.slice(index + 1).length === 0 && !!text));
122-
123119
const shouldRemoveOption =
124120
prevOptions && prevOptions.slice(index + 1).length > 0 && !text;
125121

126122
const optionListHead = prevOptions.slice(0, index);
127-
const optionListTail = shouldAddEmptyOption
128-
? [{ id: generateUUIDv4(), text: '' }]
129-
: prevOptions.slice(index + 1);
123+
const optionListTail = prevOptions.slice(index + 1);
130124

131125
const newOptions = [
132126
...optionListHead,
133127
...(shouldRemoveOption ? [] : [{ ...prevOptions[index], text }]),
134128
...optionListTail,
135129
];
136130

131+
const shouldAddNewOption =
132+
prevOptions.length < MAX_POLL_OPTIONS &&
133+
!newOptions.some((option) => !option.text.trim());
134+
135+
if (shouldAddNewOption) {
136+
newOptions.push({ id: generateUUIDv4(), text: '' });
137+
}
138+
137139
return { options: newOptions };
138140
},
139141
};

test/unit/MessageComposer/middleware/pollComposer/state.test.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,14 @@ describe('PollComposerStateMiddleware', () => {
182182
expect(result.status).toBeUndefined;
183183
});
184184

185-
it('should add a new empty option when the last option is filled', async () => {
185+
it('should add a new empty option when the all the options are filled', async () => {
186186
const stateMiddleware = setup();
187187
const result = await stateMiddleware.handlers.handleFieldChange(
188188
setupHandlerParams({
189189
nextState: { ...getInitialState() },
190-
previousState: { ...getInitialState() },
190+
previousState: {
191+
...getInitialState(),
192+
},
191193
targetFields: {
192194
options: {
193195
index: 0,
@@ -199,7 +201,54 @@ describe('PollComposerStateMiddleware', () => {
199201

200202
expect(result.state.nextState.data.options.length).toBe(2);
201203
expect(result.state.nextState.data.options[0].text).toBe('Option 1');
202-
expect(result.state.nextState.data.options[1].text).toBe('');
204+
expect(result.state.nextState.data.options[1].text).toEqual('');
205+
206+
expect(result.status).toBeUndefined;
207+
});
208+
209+
it('should reorder options and add a new empty option when all the options are filled', async () => {
210+
const stateMiddleware = setup();
211+
212+
const reOrderedOptions = [
213+
{
214+
id: 'option-2',
215+
text: '',
216+
},
217+
{
218+
id: 'option-1',
219+
text: 'Option 1',
220+
},
221+
];
222+
223+
const result = await stateMiddleware.handlers.handleFieldChange(
224+
setupHandlerParams({
225+
nextState: {
226+
...getInitialState(),
227+
data: {
228+
...getInitialState().data,
229+
options: reOrderedOptions,
230+
},
231+
},
232+
previousState: {
233+
...getInitialState(),
234+
data: {
235+
...getInitialState().data,
236+
options: reOrderedOptions,
237+
},
238+
},
239+
targetFields: {
240+
options: {
241+
index: 0,
242+
text: 'Option 2',
243+
},
244+
},
245+
}),
246+
);
247+
248+
expect(result.state.nextState.data.options.length).toBe(3);
249+
expect(result.state.nextState.data.options[0].text).toBe('Option 2');
250+
expect(result.state.nextState.data.options[1].text).toBe('Option 1');
251+
expect(result.state.nextState.data.options[2].text).toEqual('');
203252
expect(result.status).toBeUndefined;
204253
});
205254

0 commit comments

Comments
 (0)