Skip to content

Commit dc4a7d4

Browse files
committed
test: add test case
1 parent 236853d commit dc4a7d4

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/utils/warningPropsUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function warningProps(props: SelectProps) {
161161

162162
Object.entries(deprecatedProps).forEach(([deprecatedProp, newProp]) => {
163163
if (deprecatedProp in props) {
164-
warning(false, `'${deprecatedProp}' is deprecated. Please use '${newProp}' instead.`);
164+
warning(false, `\`${deprecatedProp}\` is deprecated. Please use \`${newProp}\` instead.`);
165165
}
166166
});
167167

tests/Select.test.tsx

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,3 +2368,91 @@ describe('Select.Basic', () => {
23682368
expect(element[1]).toHaveClass('rc-select-item-option-disabled');
23692369
});
23702370
});
2371+
2372+
describe('Select.Deprecated APIs', () => {
2373+
beforeAll(() => {
2374+
jest.useFakeTimers();
2375+
});
2376+
2377+
afterAll(() => {
2378+
jest.useRealTimers();
2379+
});
2380+
2381+
beforeEach(() => {
2382+
resetWarned();
2383+
});
2384+
2385+
const deprecatedAPIs = {
2386+
dropdownClassName: 'popupClassName',
2387+
dropdownStyle: 'popupStyle',
2388+
dropdownAlign: 'popupAlign',
2389+
dropdownRender: 'popupRender',
2390+
dropdownMatchSelectWidth: 'popupMatchSelectWidth',
2391+
onDropdownVisibleChange: 'onPopupVisibleChange',
2392+
};
2393+
2394+
Object.entries(deprecatedAPIs).forEach(([deprecatedProp, newProp]) => {
2395+
it(`should warning if use deprecated API '${deprecatedProp}'`, () => {
2396+
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
2397+
2398+
const props = {
2399+
[deprecatedProp]: deprecatedProp === 'dropdownRender' ? (menu) => menu : true,
2400+
};
2401+
2402+
render(
2403+
<Select {...props}>
2404+
<Option value="1">1</Option>
2405+
</Select>,
2406+
);
2407+
2408+
expect(errorSpy).toHaveBeenCalledWith(
2409+
`Warning: \`${deprecatedProp}\` is deprecated. Please use \`${newProp}\` instead.`,
2410+
);
2411+
2412+
errorSpy.mockRestore();
2413+
});
2414+
});
2415+
2416+
it('should not warning if use new APIs', () => {
2417+
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
2418+
2419+
render(
2420+
<Select
2421+
popupClassName="test"
2422+
popupStyle={{ color: 'red' }}
2423+
popupAlign={{ offset: [0, 0] }}
2424+
popupRender={(menu) => menu}
2425+
popupMatchSelectWidth={100}
2426+
onPopupVisibleChange={() => {}}
2427+
>
2428+
<Option value="1">1</Option>
2429+
</Select>,
2430+
);
2431+
2432+
expect(errorSpy).not.toHaveBeenCalled();
2433+
errorSpy.mockRestore();
2434+
});
2435+
2436+
it('should work with both old and new APIs', () => {
2437+
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
2438+
2439+
const { container } = render(
2440+
<Select dropdownMatchSelectWidth={100} popupMatchSelectWidth={200}>
2441+
<Option value="1">1</Option>
2442+
</Select>,
2443+
);
2444+
2445+
// Should use new API value
2446+
toggleOpen(container);
2447+
expect((container.querySelector('.rc-select-dropdown') as HTMLElement).style.width).toBe(
2448+
'200px',
2449+
);
2450+
2451+
// Should still show warning
2452+
expect(errorSpy).toHaveBeenCalledWith(
2453+
`Warning: \`dropdownMatchSelectWidth\` is deprecated. Please use \`popupMatchSelectWidth\` instead.`,
2454+
);
2455+
2456+
errorSpy.mockRestore();
2457+
});
2458+
});

0 commit comments

Comments
 (0)