import { CloseOutlined } from '@ant-design/icons';
import { render } from '@testing-library/react';
import React, { useEffect } from 'react';
import type { UseClosableParams } from '../hooks/useClosable';
import useClosable from '../hooks/useClosable';
type ParamsOfUseClosable = [
UseClosableParams['closable'],
UseClosableParams['closeIcon'],
UseClosableParams['defaultClosable'],
];
describe('hooks test', () => {
const useClosableParams: { params: ParamsOfUseClosable; res: [boolean, string] }[] = [
// test case like:
{
params: [undefined, undefined, undefined],
res: [false, ''],
},
{
params: [undefined, undefined, true],
res: [true, 'anticon-close'],
},
{
params: [undefined, undefined, false],
res: [false, ''],
},
// test case like:
{
params: [false, undefined, undefined],
res: [false, ''],
},
{
params: [true, undefined, true],
res: [true, 'anticon-close'],
},
{
params: [true, undefined, false],
res: [true, 'anticon-close'],
},
// test case like:
{
params: [false, null, undefined],
res: [false, ''],
},
{
params: [false, false, undefined],
res: [false, ''],
},
{
params: [true, null, true],
res: [true, 'anticon-close'],
},
{
params: [true, false, true],
res: [true, 'anticon-close'],
},
{
params: [true, null, false],
res: [true, 'anticon-close'],
},
{
params: [true, false, false],
res: [true, 'anticon-close'],
},
{
params: [
true,
close
,
false,
],
res: [true, 'custom-close'],
},
{
params: [false, close
, false],
res: [false, ''],
},
// test case like:
{
params: [undefined, null, undefined],
res: [false, ''],
},
{
params: [undefined, false, undefined],
res: [false, ''],
},
{
params: [
undefined,
close
,
undefined,
],
res: [true, 'custom-close'],
},
{
params: [
undefined,
close
,
true,
],
res: [true, 'custom-close'],
},
{
params: [
undefined,
close
,
false,
],
res: [true, 'custom-close'],
},
];
useClosableParams.forEach(({ params, res }) => {
it(`useClosable with closable=${params[0]},closeIcon=${
React.isValidElement(params[1]) ? 'element' : params[1]
},defaultClosable=${params[2]}. the result should be ${res}`, () => {
const App = () => {
const [closable, closeIcon] = useClosable(
params[0],
params[1],
undefined,
undefined,
params[2],
);
useEffect(() => {
expect(closable).toBe(res[0]);
}, [closable]);
return hooks test {closeIcon}
;
};
const { container } = render();
if (res[1] === '') {
expect(container.querySelector('.anticon-close')).toBeFalsy();
} else {
expect(container.querySelector(`.${res[1]}`)).toBeTruthy();
}
});
});
it('useClosable with defaultCloseIcon', () => {
const App = () => {
const [closable, closeIcon] = useClosable(
true,
undefined,
undefined,
,
);
useEffect(() => {
expect(closable).toBe(true);
}, [closable]);
return hooks test {closeIcon}
;
};
const { container } = render();
expect(container.querySelector('.custom-close-icon')).toBeTruthy();
});
it('useClosable with customCloseIconRender', () => {
const App = () => {
const customCloseIconRender = (icon: React.ReactNode) => (
{icon}
);
const [closable, closeIcon] = useClosable(true, undefined, customCloseIconRender);
useEffect(() => {
expect(closable).toBe(true);
}, [closable]);
return hooks test {closeIcon}
;
};
const { container } = render();
expect(container.querySelector('.custom-close-wrapper')).toBeTruthy();
});
});