Skip to content

Commit 8864c7a

Browse files
committed
fix: add new poll option only when all of the current options are filled
1 parent 2703018 commit 8864c7a

File tree

2 files changed

+23
-13
lines changed
  • src/messageComposer/middleware/pollComposer
  • test/unit/MessageComposer/middleware/pollComposer

2 files changed

+23
-13
lines changed

src/messageComposer/middleware/pollComposer/state.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,22 @@ 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+
if (!newOptions.some((option) => !option.text.trim())) {
132+
newOptions.push({ id: generateUUIDv4(), text: '' });
133+
}
134+
137135
return { options: newOptions };
138136
},
139137
};

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,36 @@ 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+
data: {
193+
...getInitialState().data,
194+
options: [
195+
{ id: 'option-1', text: 'Option 1' },
196+
{ id: 'option-2', text: '' },
197+
{ id: 'option-3', text: 'Option 3' },
198+
],
199+
},
200+
},
191201
targetFields: {
192202
options: {
193-
index: 0,
194-
text: 'Option 1',
203+
index: 1,
204+
text: 'Option 2',
195205
},
196206
},
197207
}),
198208
);
199209

200-
expect(result.state.nextState.data.options.length).toBe(2);
210+
expect(result.state.nextState.data.options.length).toBe(4);
201211
expect(result.state.nextState.data.options[0].text).toBe('Option 1');
202-
expect(result.state.nextState.data.options[1].text).toBe('');
212+
expect(result.state.nextState.data.options[1].text).toBe('Option 2');
213+
expect(result.state.nextState.data.options[2].text).toBe('Option 3');
214+
expect(result.state.nextState.data.options[3].text).toEqual('');
203215
expect(result.status).toBeUndefined;
204216
});
205217

0 commit comments

Comments
 (0)