Skip to content

Commit 9ab75e2

Browse files
committed
test: Add test for parent attribute in select + multiselect
1 parent 556f3fd commit 9ab75e2

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/multiselect/__tests__/render-option.test.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Multiselect renderOption', () => {
2727
expect(elementWrapper).toHaveTextContent('Custom 0');
2828
});
2929

30-
test('receives correct item properties for child option', () => {
30+
test('receives correct item properties for item option', () => {
3131
const renderOption = jest.fn(() => <div>Custom</div>);
3232
const childOption = { label: 'Test', value: '1' };
3333
const wrapper = renderMultiselect({
@@ -48,7 +48,7 @@ describe('Multiselect renderOption', () => {
4848
);
4949
});
5050

51-
test('receives correct item properties for parent option', () => {
51+
test('receives correct item properties for group option', () => {
5252
const renderOption = jest.fn(() => <div>Custom</div>);
5353
const groupOption = { label: 'Group', value: 'g1', options: [{ label: 'Child', value: 'c1' }] };
5454
const wrapper = renderMultiselect({
@@ -160,6 +160,30 @@ describe('Multiselect renderOption', () => {
160160
);
161161
});
162162

163+
test('receives correct parent attribute for child item in group', () => {
164+
const renderOption = jest.fn(() => <div>Custom</div>);
165+
const groupOption = { label: 'Parent Group', value: 'g1', options: [{ label: 'Child Item', value: 'c1' }] };
166+
const wrapper = renderMultiselect({
167+
options: [groupOption],
168+
renderOption,
169+
});
170+
wrapper.openDropdown();
171+
172+
// Verify that the child item receives the correct parent attribute
173+
expect(renderOption).toHaveBeenCalledWith(
174+
expect.objectContaining({
175+
item: expect.objectContaining({
176+
type: 'item',
177+
option: expect.objectContaining({ label: 'Child Item', value: 'c1' }),
178+
parent: expect.objectContaining({
179+
type: 'group',
180+
option: expect.objectContaining(groupOption),
181+
}),
182+
}),
183+
})
184+
);
185+
});
186+
163187
test('allows selection with custom rendered options', () => {
164188
const onChange = jest.fn();
165189
const renderOption = jest.fn(props => <div>{props.item.option.value}</div>);

src/select/__tests__/render-option.test.tsx

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Select renderOption', () => {
2727
expect(elementWrapper).toHaveTextContent('Custom');
2828
});
2929

30-
test('receives correct item properties for child option', () => {
30+
test('receives correct item properties for item option', () => {
3131
const renderOption = jest.fn(() => <div>Custom</div>);
3232
const childOption = { label: 'Test', value: '1' };
3333
const wrapper = renderSelect({
@@ -47,7 +47,7 @@ describe('Select renderOption', () => {
4747
})
4848
);
4949
});
50-
test('receives correct item properties for parent option', () => {
50+
test('receives correct item properties for group option', () => {
5151
const renderOption = jest.fn(() => <div>Custom</div>);
5252
const groupOption = { label: 'Group', value: 'g1', options: [{ label: 'Child', value: 'c1' }] };
5353
const wrapper = renderSelect({
@@ -65,6 +65,30 @@ describe('Select renderOption', () => {
6565
})
6666
);
6767
});
68+
69+
test('receives correct item properties for trigger option', () => {
70+
const renderOption = jest.fn(() => <div>Custom</div>);
71+
const selectedOption = { label: 'Test', value: '1' };
72+
const wrapper = renderSelect({
73+
options: [selectedOption],
74+
selectedOption,
75+
triggerVariant: 'option',
76+
renderOption,
77+
});
78+
const triggerWrapper = wrapper.findTrigger()!;
79+
expect(triggerWrapper).not.toBeNull();
80+
expect(triggerWrapper.getElement()).toHaveTextContent('Custom');
81+
expect(renderOption).toHaveBeenCalledWith(
82+
expect.objectContaining({
83+
filterText: undefined,
84+
item: expect.objectContaining({
85+
type: 'trigger',
86+
option: expect.objectContaining(selectedOption),
87+
}),
88+
})
89+
);
90+
});
91+
6892
test('reflects highlighted state', () => {
6993
const renderOption = jest.fn(props => <div>{props.item.highlighted ? 'highlighted' : 'normal'}</div>);
7094
const wrapper = renderSelect({ options: [{ label: 'First', value: '1' }], renderOption });
@@ -104,6 +128,30 @@ describe('Select renderOption', () => {
104128
})
105129
);
106130
});
131+
132+
test('receives correct parent attribute for child item in group', () => {
133+
const renderOption = jest.fn(() => <div>Custom</div>);
134+
const groupOption = { label: 'Parent Group', value: 'g1', options: [{ label: 'Child Item', value: 'c1' }] };
135+
const wrapper = renderSelect({
136+
options: [groupOption],
137+
renderOption,
138+
});
139+
wrapper.openDropdown();
140+
141+
// Verify that the child item receives the correct parent attribute
142+
expect(renderOption).toHaveBeenCalledWith(
143+
expect.objectContaining({
144+
item: expect.objectContaining({
145+
type: 'item',
146+
option: expect.objectContaining({ label: 'Child Item', value: 'c1' }),
147+
parent: expect.objectContaining({
148+
type: 'group',
149+
option: expect.objectContaining(groupOption),
150+
}),
151+
}),
152+
})
153+
);
154+
});
107155
test('reflects disabled state', () => {
108156
const renderOption = jest.fn(props => <div>{props.item.disabled ? 'disabled' : 'enabled'}</div>);
109157
const wrapper = renderSelect({

0 commit comments

Comments
 (0)