ant-design/components/notification/__tests__/hooks.test.tsx
二货机器人 2341a25d91
feat: refactor useNotification (#35423)
* more refactor

* chore: motion support

* chore: tmp test

* test: Hooks

* chore: static function

* tmp of it

* all of it

* mv prefix

* chore: clean up

* chore: clean up

* more test case

* test: all base test

* test: all test case

* init

* refactor: rm notification.open instance related code

* follow up

* refactor: singlton

* test: notification test case

* refactor to destroy

* refactor: message base

* test: part test case

* test: more

* test: more

* test: all test

* chore: clean up

* docs: reorder

* chore: fix lint

* test: fix test case

* chore: add act

* chore: back

* chore: fix style

* test: notification test

* test: more and more

* test: fix more test

* test: index

* test: more & more

* test: fix placement

* test: fix coverage

* chore: clean up

* chore: bundle size

* fix: 17

* chore: more

* test: message

* test: more test

* fix: lint

* test: rm class in static

* chore: clean up

* test: coverage

* chore: fix lint
2022-05-11 14:26:18 +08:00

159 lines
4.2 KiB
TypeScript

/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { mount } from 'enzyme';
import notification from '..';
import ConfigProvider from '../../config-provider';
import { render, fireEvent } from '../../../tests/utils';
describe('notification.hooks', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('should work', () => {
const Context = React.createContext('light');
const Demo = () => {
const [api, holder] = notification.useNotification();
return (
<ConfigProvider prefixCls="my-test">
<Context.Provider value="bamboo">
<button
type="button"
onClick={() => {
api.open({
message: null,
description: (
<Context.Consumer>
{name => <span className="hook-test-result">{name}</span>}
</Context.Consumer>
),
duration: 0,
});
}}
/>
{holder}
</Context.Provider>
</ConfigProvider>
);
};
const { container } = render(<Demo />);
fireEvent.click(container.querySelector('button')!);
expect(document.querySelectorAll('.my-test-notification-notice')).toHaveLength(1);
expect(document.querySelector('.hook-test-result')!.textContent).toEqual('bamboo');
});
it('should work with success', () => {
const Context = React.createContext('light');
const Demo = () => {
const [api, holder] = notification.useNotification();
return (
<ConfigProvider prefixCls="my-test">
<Context.Provider value="bamboo">
<button
type="button"
onClick={() => {
api.success({
message: null,
description: (
<Context.Consumer>
{name => <span className="hook-test-result">{name}</span>}
</Context.Consumer>
),
duration: 0,
});
}}
/>
{holder}
</Context.Provider>
</ConfigProvider>
);
};
const { container } = render(<Demo />);
fireEvent.click(container.querySelector('button')!);
expect(document.querySelectorAll('.my-test-notification-notice')).toHaveLength(1);
expect(document.querySelectorAll('.anticon-check-circle')).toHaveLength(1);
expect(document.querySelector('.hook-test-result')!.textContent).toEqual('bamboo');
});
it('should be same hook', () => {
let count = 0;
const Demo = () => {
const [, forceUpdate] = React.useState({});
const [api] = notification.useNotification();
React.useEffect(() => {
count += 1;
expect(count).toEqual(1);
forceUpdate({});
}, [api]);
return null;
};
mount(<Demo />);
});
describe('not break in effect', () => {
it('basic', () => {
const Demo = () => {
const [api, holder] = notification.useNotification();
React.useEffect(() => {
api.info({
message: null,
description: <div className="bamboo" />,
});
}, []);
return holder;
};
render(<Demo />);
expect(document.querySelector('.bamboo')).toBeTruthy();
});
it('warning if user call update in render', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const Demo = () => {
const [api, holder] = notification.useNotification();
const calledRef = React.useRef(false);
if (!calledRef.current) {
api.info({
message: null,
description: <div className="bamboo" />,
});
calledRef.current = true;
}
return holder;
};
render(<Demo />);
expect(document.querySelector('.bamboo')).toBeFalsy();
expect(errorSpy).toHaveBeenCalledWith(
'Warning: [antd: Notification] You are calling notice in render which will break in React 18 concurrent mode. Please trigger in effect instead.',
);
errorSpy.mockRestore();
});
});
});