Skip to content

Commit a4a01ee

Browse files
committed
test: add test for dynamic value update
1 parent 5864687 commit a4a01ee

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/components/__tests__/MultipleValueTextInput.spec.tsx

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,19 @@ describe('behavior', () => {
182182
});
183183
it('should allow user to add multiple unique items from clipboard if submitkeys are present', async () => {
184184
const onItemAdd = jest.fn();
185-
const props = createTestProps({ onItemAdded: onItemAdd, submitKeys: [",",";"], values: ["input3"] });
185+
const props = createTestProps({
186+
onItemAdded: onItemAdd,
187+
submitKeys: [',', ';'],
188+
values: ['input3']
189+
});
186190
const { user, getByRole, queryAllByRole } = renderWithUserInteraction(
187191
<MultipleValueTextInput {...props} />
188192
);
189193
const input = getByRole('textbox');
190194
const initialItems = queryAllByRole('listitem');
191195
expect(initialItems.length).toBe(1);
192196
await user.click(input);
193-
await user.paste("input1,input2;input3,input4,,input5,input1;");
197+
await user.paste('input1,input2;input3,input4,,input5,input1;');
194198
await waitFor(() => {
195199
expect(onItemAdd).toHaveBeenCalledTimes(4);
196200
// const items = queryAllByRole('listitem');
@@ -199,64 +203,87 @@ describe('behavior', () => {
199203
// expect(items[1]).toHaveTextContent('input2');
200204
// expect(items[2]).toHaveTextContent('input4');
201205
// expect(items[3]).toHaveTextContent('input5');
202-
expect(input).toHaveValue("")
206+
expect(input).toHaveValue('');
203207
});
204208
});
205209
it('should allow user to view text from clipboard if submitkeys are not present', async () => {
206210
const onItemAdd = jest.fn();
207-
const props = createTestProps({ onItemAdded: onItemAdd, submitKeys: [",",";"] });
211+
const props = createTestProps({ onItemAdded: onItemAdd, submitKeys: [',', ';'] });
208212
const { user, getByRole, queryAllByRole } = renderWithUserInteraction(
209213
<MultipleValueTextInput {...props} />
210214
);
211215
const input = getByRole('textbox');
212216
const initialItems = queryAllByRole('listitem');
213217
expect(initialItems.length).toBe(0);
214218
await user.click(input);
215-
await user.paste("john doe");
219+
await user.paste('john doe');
216220
await waitFor(() => {
217221
expect(onItemAdd).toHaveBeenCalledTimes(0);
218222
const items = queryAllByRole('listitem');
219223
expect(items).toHaveLength(0);
220-
expect(input).toHaveValue("john doe")
224+
expect(input).toHaveValue('john doe');
221225
});
222226
});
223227
it('should ignore non-character submit keys from being recognized as delimiters in copied text', async () => {
224228
const onItemAdd = jest.fn();
225-
const props = createTestProps({ onItemAdded: onItemAdd, submitKeys: [",","Enter","Tab"] });
229+
const props = createTestProps({
230+
onItemAdded: onItemAdd,
231+
submitKeys: [',', 'Enter', 'Tab']
232+
});
226233
const { user, getByRole, queryAllByRole } = renderWithUserInteraction(
227234
<MultipleValueTextInput {...props} />
228235
);
229236
const input = getByRole('textbox');
230237
const initialItems = queryAllByRole('listitem');
231238
expect(initialItems.length).toBe(0);
232239
await user.click(input);
233-
await user.paste("My Tablet is about to Enter the door");
240+
await user.paste('My Tablet is about to Enter the door');
234241
await waitFor(() => {
235242
expect(onItemAdd).toHaveBeenCalledTimes(0);
236243
const items = queryAllByRole('listitem');
237244
expect(items).toHaveLength(0);
238-
expect(input).toHaveValue("My Tablet is about to Enter the door")
245+
expect(input).toHaveValue('My Tablet is about to Enter the door');
239246
});
240247
});
241248
it('should selectively ignore non-character submit keys from being recognized as delimiters in copied text', async () => {
242249
const onItemAdd = jest.fn();
243-
const props = createTestProps({ onItemAdded: onItemAdd, submitKeys: [",","Enter","Tab"] });
250+
const props = createTestProps({
251+
onItemAdded: onItemAdd,
252+
submitKeys: [',', 'Enter', 'Tab']
253+
});
244254
const { user, getByRole, queryAllByRole } = renderWithUserInteraction(
245255
<MultipleValueTextInput {...props} />
246256
);
247257
const input = getByRole('textbox');
248258
const initialItems = queryAllByRole('listitem');
249259
expect(initialItems.length).toBe(0);
250260
await user.click(input);
251-
await user.paste("My Tablet is about to Enter the door, value1, value2");
261+
await user.paste('My Tablet is about to Enter the door, value1, value2');
252262
await waitFor(() => {
253263
expect(onItemAdd).toHaveBeenCalledTimes(3);
254264
const items = queryAllByRole('listitem');
255265
expect(items).toHaveLength(3);
256266
expect(items[0]).toHaveTextContent('My Tablet is about to Enter the door');
257267
expect(items[1]).toHaveTextContent('value1');
258268
expect(items[2]).toHaveTextContent('value2');
259-
expect(input).toHaveValue("")
269+
expect(input).toHaveValue('');
270+
});
271+
});
272+
it('should dynamically update values from an external prop if provided', async () => {
273+
const props = createTestProps();
274+
const { rerender, queryAllByRole } = renderWithUserInteraction(
275+
<MultipleValueTextInput {...props} />
276+
);
277+
const initialItems = queryAllByRole('listitem');
278+
expect(initialItems.length).toBe(0);
279+
const updatedProps = createTestProps({ values: ['test1', 'test2', 'test3'] });
280+
rerender(<MultipleValueTextInput {...updatedProps} />);
281+
await waitFor(() => {
282+
const items = queryAllByRole('listitem');
283+
expect(items).toHaveLength(3);
284+
expect(items[0]).toHaveTextContent('test1');
285+
expect(items[1]).toHaveTextContent('test2');
286+
expect(items[2]).toHaveTextContent('test3');
260287
});
261288
});
262289
});

0 commit comments

Comments
 (0)