mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 23:35:38 +08:00
01cec29a8e
* chore: comment on usePatchElement * refactor: conform message & notifaction code logic * feat: message useMessage, wip * feat: message.useMessage, it works now * fix: promise on regular api * feat: message hooks * chore: fix lint * chore: new line * chore: revert new line * refactor: prefixCls * fix: prefixCls * test: cov * chore * chore * chore * chore * docs * docs: message hooks faq * test: remove useless config provider * chore: remove some test codes * chore * docs: hooks version
196 lines
4.9 KiB
JavaScript
196 lines
4.9 KiB
JavaScript
/* eslint-disable jsx-a11y/control-has-associated-label */
|
|
import React from 'react';
|
|
import { mount } from 'enzyme';
|
|
import message from '..';
|
|
import ConfigProvider from '../../config-provider';
|
|
|
|
describe('message.hooks', () => {
|
|
beforeAll(() => {
|
|
jest.useFakeTimers();
|
|
});
|
|
|
|
afterAll(() => {
|
|
jest.useRealTimers();
|
|
});
|
|
|
|
afterEach(() => {
|
|
message.destroy();
|
|
});
|
|
|
|
it('should work', () => {
|
|
const Context = React.createContext('light');
|
|
|
|
const Demo = () => {
|
|
const [api, holder] = message.useMessage();
|
|
|
|
return (
|
|
<ConfigProvider prefixCls="my-test">
|
|
<Context.Provider value="bamboo">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
api.open({
|
|
content: (
|
|
<Context.Consumer>
|
|
{name => <span className="hook-test-result">{name}</span>}
|
|
</Context.Consumer>
|
|
),
|
|
duration: 0,
|
|
});
|
|
}}
|
|
/>
|
|
{holder}
|
|
</Context.Provider>
|
|
</ConfigProvider>
|
|
);
|
|
};
|
|
|
|
const wrapper = mount(<Demo />);
|
|
wrapper.find('button').simulate('click');
|
|
expect(document.querySelectorAll('.my-test-message-notice').length).toBe(1);
|
|
expect(document.querySelector('.hook-test-result').innerHTML).toEqual('bamboo');
|
|
});
|
|
|
|
it('should work with success', () => {
|
|
const Context = React.createContext('light');
|
|
|
|
const Demo = () => {
|
|
const [api, holder] = message.useMessage();
|
|
|
|
return (
|
|
<ConfigProvider prefixCls="my-test">
|
|
<Context.Provider value="bamboo">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
api.success({
|
|
content: (
|
|
<Context.Consumer>
|
|
{name => <span className="hook-test-result">{name}</span>}
|
|
</Context.Consumer>
|
|
),
|
|
duration: 0,
|
|
});
|
|
}}
|
|
/>
|
|
{holder}
|
|
</Context.Provider>
|
|
</ConfigProvider>
|
|
);
|
|
};
|
|
|
|
const wrapper = mount(<Demo />);
|
|
wrapper.find('button').simulate('click');
|
|
expect(document.querySelectorAll('.my-test-message-notice').length).toBe(1);
|
|
expect(document.querySelectorAll('.anticon-check-circle').length).toBe(1);
|
|
expect(document.querySelector('.hook-test-result').innerHTML).toEqual('bamboo');
|
|
});
|
|
|
|
it('should work with onClose', done => {
|
|
// if not use real timer, done won't be called
|
|
jest.useRealTimers();
|
|
const Demo = () => {
|
|
const [api, holder] = message.useMessage();
|
|
return (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
api.open({
|
|
content: 'amazing',
|
|
duration: 1,
|
|
onClose() {
|
|
done();
|
|
},
|
|
});
|
|
}}
|
|
/>
|
|
{holder}
|
|
</>
|
|
);
|
|
};
|
|
|
|
const wrapper = mount(<Demo />);
|
|
wrapper.find('button').simulate('click');
|
|
jest.useFakeTimers();
|
|
});
|
|
|
|
it('should work with close promise', done => {
|
|
// if not use real timer, done won't be called
|
|
jest.useRealTimers();
|
|
const Demo = () => {
|
|
const [api, holder] = message.useMessage();
|
|
return (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
api
|
|
.open({
|
|
content: 'good',
|
|
duration: 1,
|
|
})
|
|
.then(() => {
|
|
done();
|
|
});
|
|
}}
|
|
/>
|
|
{holder}
|
|
</>
|
|
);
|
|
};
|
|
|
|
const wrapper = mount(<Demo />);
|
|
wrapper.find('button').simulate('click');
|
|
jest.useFakeTimers();
|
|
});
|
|
|
|
it('should work with hide', () => {
|
|
let hide;
|
|
const Demo = () => {
|
|
const [api, holder] = message.useMessage();
|
|
return (
|
|
<ConfigProvider prefixCls="my-test">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
hide = api.open({
|
|
content: 'nice',
|
|
duration: 0,
|
|
});
|
|
}}
|
|
/>
|
|
{holder}
|
|
</ConfigProvider>
|
|
);
|
|
};
|
|
|
|
const wrapper = mount(<Demo />);
|
|
wrapper.find('button').simulate('click');
|
|
jest.runAllTimers();
|
|
expect(document.querySelectorAll('.my-test-message-notice').length).toBe(1);
|
|
hide();
|
|
jest.runAllTimers();
|
|
expect(document.querySelectorAll('.my-test-message-notice').length).toBe(0);
|
|
});
|
|
|
|
it('should be same hook', () => {
|
|
let count = 0;
|
|
|
|
const Demo = () => {
|
|
const [, forceUpdate] = React.useState({});
|
|
const [api] = message.useMessage();
|
|
|
|
React.useEffect(() => {
|
|
count += 1;
|
|
expect(count).toEqual(1);
|
|
forceUpdate();
|
|
}, [api]);
|
|
|
|
return null;
|
|
};
|
|
|
|
mount(<Demo />);
|
|
});
|
|
});
|