2020-02-24 16:58:09 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
2020-09-28 00:41:16 +08:00
|
|
|
import message, { getInstance } from '..';
|
2021-02-09 21:49:15 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2020-02-24 16:58:09 +08:00
|
|
|
|
|
|
|
describe('message.config', () => {
|
2020-09-28 00:41:16 +08:00
|
|
|
// Mock for rc-util raf
|
2020-12-09 17:12:32 +08:00
|
|
|
window.requestAnimationFrame = callback => window.setTimeout(callback, 16);
|
2020-09-28 00:41:16 +08:00
|
|
|
window.cancelAnimationFrame = id => {
|
|
|
|
window.clearTimeout(id);
|
|
|
|
};
|
|
|
|
|
2020-04-04 13:35:36 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
|
|
|
|
2020-04-04 13:35:36 +08:00
|
|
|
afterEach(() => {
|
|
|
|
message.destroy();
|
|
|
|
jest.useRealTimers();
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
|
|
|
|
2020-04-04 13:35:36 +08:00
|
|
|
it('should be able to config top', () => {
|
|
|
|
message.config({
|
|
|
|
top: 100,
|
|
|
|
});
|
|
|
|
message.info('whatever');
|
|
|
|
expect(document.querySelectorAll('.ant-message')[0].style.top).toBe('100px');
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
|
|
|
|
2020-04-04 13:35:36 +08:00
|
|
|
it('should be able to config rtl', () => {
|
|
|
|
message.config({
|
|
|
|
rtl: true,
|
|
|
|
});
|
|
|
|
message.info('whatever');
|
|
|
|
expect(document.querySelectorAll('.ant-message-rtl').length).toBe(1);
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
2020-04-04 13:35:36 +08:00
|
|
|
|
|
|
|
it('should be able to config getContainer', () => {
|
|
|
|
message.config({
|
|
|
|
getContainer: () => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.className = 'custom-container';
|
|
|
|
document.body.appendChild(div);
|
|
|
|
return div;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
message.info('whatever');
|
|
|
|
expect(document.querySelectorAll('.custom-container').length).toBe(1);
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
|
|
|
|
2020-04-04 13:35:36 +08:00
|
|
|
it('should be able to config maxCount', () => {
|
|
|
|
message.config({
|
|
|
|
maxCount: 5,
|
|
|
|
});
|
|
|
|
for (let i = 0; i < 10; i += 1) {
|
|
|
|
message.info('test');
|
|
|
|
}
|
2020-09-28 00:41:16 +08:00
|
|
|
|
2020-04-04 13:35:36 +08:00
|
|
|
message.info('last');
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(5);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice')[4].textContent).toBe('last');
|
|
|
|
jest.runAllTimers();
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(0);
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
2020-04-04 13:35:36 +08:00
|
|
|
|
|
|
|
it('should be able to config duration', async () => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
message.config({
|
|
|
|
duration: 0.5,
|
|
|
|
});
|
|
|
|
message.info('last');
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(1);
|
|
|
|
|
2020-09-18 16:53:18 +08:00
|
|
|
await sleep(1000);
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(0);
|
2020-04-04 13:35:36 +08:00
|
|
|
message.config({
|
|
|
|
duration: 3,
|
|
|
|
});
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
2021-03-02 11:53:12 +08:00
|
|
|
|
|
|
|
it('customize prefix should auto get transition prefixCls', () => {
|
|
|
|
message.config({
|
|
|
|
prefixCls: 'light-message',
|
|
|
|
});
|
|
|
|
|
|
|
|
message.info('bamboo');
|
|
|
|
|
|
|
|
expect(getInstance().config).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
transitionName: 'light-move-up',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
message.config({
|
|
|
|
prefixCls: '',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-09 21:49:15 +08:00
|
|
|
it('should be able to global config rootPrefixCls', () => {
|
|
|
|
ConfigProvider.config({ prefixCls: 'prefix-test' });
|
|
|
|
message.info('last');
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
expect(document.querySelectorAll('.prefix-test-message-notice').length).toBe(1);
|
|
|
|
ConfigProvider.config({ prefixCls: 'ant' });
|
|
|
|
});
|
2020-04-04 13:35:36 +08:00
|
|
|
it('should be able to config prefixCls', () => {
|
|
|
|
message.config({
|
|
|
|
prefixCls: 'prefix-test',
|
|
|
|
});
|
|
|
|
message.info('last');
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
expect(document.querySelectorAll('.prefix-test-notice').length).toBe(1);
|
|
|
|
message.config({
|
2021-02-09 21:49:15 +08:00
|
|
|
prefixCls: '', // can be set to empty, ant default value is set in ConfigProvider
|
2020-04-04 13:35:36 +08:00
|
|
|
});
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
2020-04-04 13:35:36 +08:00
|
|
|
|
|
|
|
it('should be able to config transitionName', () => {
|
|
|
|
message.config({
|
|
|
|
transitionName: '',
|
|
|
|
});
|
|
|
|
message.info('last');
|
|
|
|
expect(document.querySelectorAll('.move-up-enter').length).toBe(0);
|
|
|
|
message.config({
|
2021-02-08 17:09:13 +08:00
|
|
|
transitionName: 'ant-move-up',
|
2020-04-04 13:35:36 +08:00
|
|
|
});
|
2020-02-24 16:58:09 +08:00
|
|
|
});
|
|
|
|
});
|