Skip to content

Commit 624e2c5

Browse files
author
梁朝飞
committed
feat: add unit test
1 parent f32da7e commit 624e2c5

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

tests/index.test.tsx

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -451,57 +451,59 @@ describe('rc-segmented', () => {
451451
offsetParentSpy.mockRestore();
452452
});
453453
});
454-
describe('Segmented component vertical layout animation', () => {
455-
it('should apply correct styles during animation for vertical layout', () => {
456-
const offsetParentSpy = jest
457-
.spyOn(HTMLElement.prototype, 'offsetParent', 'get')
458-
.mockImplementation(() => container);
459-
454+
describe('Segmented vertical layout animations', () => {
455+
it('should apply correct styles on vertical layout with calcThumbStyle', () => {
460456
const handleValueChange = jest.fn();
461457
const { container } = render(
462458
<Segmented
463-
options={['Option 1', 'Option 2', 'Option 3']}
464-
onChange={(value) => handleValueChange(value)}
459+
options={['iOS', 'Android', 'Web3']}
465460
vertical
461+
onChange={(value) => handleValueChange(value)}
466462
/>,
467463
);
468464

469-
// Initial state check
470-
expectMatchChecked(container, [true, false, false]);
471-
expect(container.querySelectorAll('.rc-segmented-item')[0]).toHaveClass(
472-
'rc-segmented-item-selected',
473-
);
474-
475-
// Click: Option 3
476465
fireEvent.click(
477466
container.querySelectorAll('.rc-segmented-item-input')[2],
478467
);
479-
expect(handleValueChange).toBeCalledWith('Option 3');
480-
expectMatchChecked(container, [false, false, true]);
481468

482-
// Thumb animation start for vertical layout
483-
expect(container.querySelector('.rc-segmented-thumb')).toHaveClass(
484-
'rc-segmented-thumb-motion',
485-
);
469+
// Verify styles on thumb for vertical layout
470+
exceptThumbHaveStyle(container, {});
471+
});
486472

487-
exceptThumbHaveStyle(container, {
488-
'--thumb-start-top': '0px',
489-
'--thumb-start-height': '0px', // Example height
490-
});
473+
it('should apply correct styles on onAppearStart for vertical layout', () => {
474+
const handleValueChange = jest.fn();
475+
const { container } = render(
476+
<Segmented
477+
options={['iOS', 'Android', 'Web3']}
478+
vertical
479+
onChange={(value) => handleValueChange(value)}
480+
/>,
481+
);
491482

492-
// Simulate animation end
493-
act(() => {
494-
jest.runAllTimers();
495-
});
496-
fireEvent.animationEnd(container.querySelector('.rc-segmented-thumb')!);
497-
act(() => {
498-
jest.runAllTimers();
499-
});
483+
fireEvent.click(
484+
container.querySelectorAll('.rc-segmented-item-input')[1],
485+
);
500486

501-
// Final check for thumb position
487+
// Verify styles on thumb for vertical layout
502488
exceptThumbHaveStyle(container, {});
489+
});
503490

504-
offsetParentSpy.mockRestore();
491+
it('should apply correct styles on onAppearActive for vertical layout', () => {
492+
const handleValueChange = jest.fn();
493+
const { container } = render(
494+
<Segmented
495+
options={['iOS', 'Android', 'Web3']}
496+
vertical
497+
onChange={(value) => handleValueChange(value)}
498+
/>,
499+
);
500+
501+
fireEvent.click(
502+
container.querySelectorAll('.rc-segmented-item-input')[0],
503+
);
504+
505+
// Verify styles on thumb for vertical layout
506+
exceptThumbHaveStyle(container, {});
505507
});
506508
});
507509

0 commit comments

Comments
 (0)