-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCustomMultiAutocompleteFormik.test.tsx
More file actions
83 lines (71 loc) · 2.86 KB
/
CustomMultiAutocompleteFormik.test.tsx
File metadata and controls
83 lines (71 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Formik } from 'formik';
import { render } from 'test-helpers/test-utils';
import { sortAutocompleteOptions } from 'utils/autocomplete';
import { CustomMultiAutocompleteFormik } from './CustomMultiAutocompleteFormik';
import { ICustomMultiAutocompleteOption } from './CustomMultiAutocomplete';
describe('CustomMultiAutocompleteFormik', () => {
it('renders correctly', () => {
const { getByText } = render(
<Formik initialValues={{ categories: [] }} onSubmit={async () => {}}>
<CustomMultiAutocompleteFormik
id="categories"
name="categories"
label="Categories"
options={[{ value: 'val', label: 'label' }]}
required={true}
filterLimit={1}
/>
</Formik>
);
expect(getByText('Categories')).toBeVisible();
});
describe('sortAutocompleteOptions', () => {
it('retains selected options if no remaining options are given', () => {
const selected: ICustomMultiAutocompleteOption[] = [
{ value: 'value1', label: 'Value_1' },
{ value: 'value2', label: 'Value_2' },
{ value: 'value3', label: 'Value_3' }
];
const sorted = sortAutocompleteOptions(selected, []);
expect(sorted.length).toEqual(3);
});
it('combines selected and given options in sorted order', () => {
const selected: ICustomMultiAutocompleteOption[] = [
{ value: 'value1', label: 'Value_1' },
{ value: 'value2', label: 'Value_2' },
{ value: 'value3', label: 'Value_3' }
];
const optionsLeft: ICustomMultiAutocompleteOption[] = [
{ value: 'value4', label: 'Value_4' },
{ value: 'value5', label: 'Value_5' }
];
const sorted = sortAutocompleteOptions(selected, optionsLeft);
expect(sorted.length).toEqual(5);
expect(sorted[0].value).toEqual('value1');
});
it('removes duplicate options from selected and given options', () => {
const selected: ICustomMultiAutocompleteOption[] = [
{ value: 'value1', label: 'Value_1' },
{ value: 'value2', label: 'Value_2' },
{ value: 'value3', label: 'Value_3' }
];
const optionsLeft: ICustomMultiAutocompleteOption[] = [
{ value: 'value2', label: 'Value_2' },
{ value: 'value3', label: 'Value_3' },
{ value: 'value4', label: 'Value_4' }
];
const sorted = sortAutocompleteOptions(selected, optionsLeft);
expect(sorted.length).toEqual(4);
});
it('returns all given options if none are selected', () => {
const optionsLeft: ICustomMultiAutocompleteOption[] = [
{ value: 'value2', label: 'Value_2' },
{ value: 'value3', label: 'Value_3' },
{ value: 'value4', label: 'Value_4' }
];
const sorted = sortAutocompleteOptions([], optionsLeft);
expect(sorted.length).toEqual(3);
expect(sorted[0].value).toEqual('value2');
});
});
});