File size: 9,848 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
import React from 'react';
import message, { actDestroy, actWrapper } from '..';
import { act } from '../../../tests/utils';
import App from '../../app';
import ConfigProvider, { defaultPrefixCls } from '../../config-provider';
import { awaitPromise, triggerMotionEnd } from './util';
// TODO: Remove this. Mock for React 19
jest.mock('react-dom', () => {
const realReactDOM = jest.requireActual('react-dom');
if (realReactDOM.version.startsWith('19')) {
const realReactDOMClient = jest.requireActual('react-dom/client');
realReactDOM.createRoot = realReactDOMClient.createRoot;
}
return realReactDOM;
});
describe('message.config', () => {
beforeAll(() => {
actWrapper(act);
});
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(async () => {
// Clean up
message.destroy();
await triggerMotionEnd();
jest.useRealTimers();
await awaitPromise();
});
it('should be able to config top', async () => {
message.config({
top: 100,
});
message.info('whatever');
await awaitPromise();
expect(document.querySelector('.ant-message')).toHaveStyle({
top: '100px',
});
});
it('should be able to config top with string value', async () => {
message.config({
top: '10vh',
});
message.info('test message');
await awaitPromise();
expect(document.querySelector('.ant-message')).toHaveStyle({
top: '10vh',
});
});
it('should be able to config rtl', async () => {
message.config({
rtl: true,
});
message.info('whatever');
await awaitPromise();
expect(document.querySelector('.ant-message-rtl')).toBeTruthy();
});
it('should be able to config getContainer', async () => {
const div = document.createElement('div');
div.className = 'custom-container';
document.body.appendChild(div);
message.config({
getContainer: () => div,
});
message.info('whatever');
await awaitPromise();
expect(div.querySelector('.ant-message')).toBeTruthy();
message.config({
getContainer: undefined,
});
document.body.removeChild(div);
});
it('should be able to config maxCount', async () => {
message.config({
maxCount: 5,
});
for (let i = 0; i < 10; i += 1) {
message.info('test');
}
message.info('last');
await awaitPromise();
const noticeWithoutLeaving = Array.from(
document.querySelectorAll('.ant-message-notice-wrapper'),
).filter((ele) => !ele.classList.contains('ant-message-move-up-leave'));
expect(noticeWithoutLeaving).toHaveLength(5);
expect(noticeWithoutLeaving[4].textContent).toEqual('last');
await triggerMotionEnd();
expect(document.querySelectorAll('.ant-message-notice')).toHaveLength(0);
message.config({
maxCount: undefined,
});
});
it('should be able to config duration', async () => {
message.config({
duration: 5,
});
message.info('last');
await awaitPromise();
expect(document.querySelectorAll('.ant-message-notice')).toHaveLength(1);
act(() => {
jest.advanceTimersByTime(4000);
});
expect(document.querySelectorAll('.ant-message-notice')).toHaveLength(1);
act(() => {
jest.advanceTimersByTime(2000);
});
await triggerMotionEnd('.ant-message-notice-wrapper');
expect(document.querySelectorAll('.ant-message-notice')).toHaveLength(0);
message.config({
duration: undefined,
});
});
it('customize prefix should auto get transition prefixCls', async () => {
message.config({
prefixCls: 'light-message',
});
message.info('bamboo');
await awaitPromise();
expect(document.querySelector('.light-message-move-up')).toBeTruthy();
message.config({
prefixCls: undefined,
});
});
it('should be able to global config rootPrefixCls', async () => {
ConfigProvider.config({ prefixCls: 'prefix-test', iconPrefixCls: 'bamboo' });
message.info('last');
await awaitPromise();
expect(document.querySelectorAll('.ant-message-notice')).toHaveLength(0);
expect(document.querySelectorAll('.prefix-test-message-notice')).toHaveLength(1);
expect(document.querySelectorAll('.bamboo-info-circle')).toHaveLength(1);
ConfigProvider.config({ prefixCls: defaultPrefixCls, iconPrefixCls: null! });
});
it('should be able to config prefixCls', async () => {
message.config({
prefixCls: 'prefix-test',
});
message.info('last');
await awaitPromise();
expect(document.querySelectorAll('.ant-message-notice')).toHaveLength(0);
expect(document.querySelectorAll('.prefix-test-notice')).toHaveLength(1);
message.config({
prefixCls: '', // can be set to empty, ant default value is set in ConfigProvider
});
});
it('should be able to config transitionName', async () => {
message.config({
transitionName: '',
});
message.info('last');
await awaitPromise();
expect(document.querySelector('.ant-message-notice')).toBeTruthy();
expect(document.querySelectorAll('.ant-move-up-enter')).toHaveLength(0);
message.config({
transitionName: undefined,
});
});
it('should be able to config getContainer, although messageInstance already exists', async () => {
function createContainer(): [HTMLElement, VoidFunction] {
const container = document.createElement('div');
document.body.appendChild(container);
return [
container,
() => {
document.body.removeChild(container);
},
];
}
const [container1, removeContainer1] = createContainer();
const [container2, removeContainer2] = createContainer();
expect(container1.querySelector('.ant-message-notice')).toBeFalsy();
expect(container2.querySelector('.ant-message-notice')).toBeFalsy();
message.config({
getContainer: () => container1,
});
const messageText1 = 'mounted in container1';
message.info(messageText1);
await awaitPromise();
expect(container1.querySelector('.ant-message-notice')!.textContent).toEqual(messageText1);
// Config will directly change container
message.config({
getContainer: () => container2,
});
const messageText2 = 'mounted in container2';
message.info(messageText2);
expect(container2.querySelectorAll('.ant-message-notice')[1]!.textContent).toEqual(
messageText2,
);
removeContainer1();
removeContainer2();
message.config({ getContainer: undefined });
});
it('should be able to config holderRender', async () => {
actDestroy();
ConfigProvider.config({
holderRender: (children) => (
<ConfigProvider prefixCls="test" iconPrefixCls="icon">
{children}
</ConfigProvider>
),
});
message.info('last');
await awaitPromise();
expect(document.querySelectorAll('.ant-message')).toHaveLength(0);
expect(document.querySelectorAll('.anticon-info-circle')).toHaveLength(0);
expect(document.querySelectorAll('.test-message')).toHaveLength(1);
expect(document.querySelectorAll('.icon-info-circle')).toHaveLength(1);
ConfigProvider.config({ holderRender: undefined });
});
it('should be able to config holderRender config rtl', async () => {
document.body.innerHTML = '';
actDestroy();
ConfigProvider.config({
holderRender: (children) => <ConfigProvider direction="rtl">{children}</ConfigProvider>,
});
message.info('last');
await awaitPromise();
expect(document.querySelector('.ant-message-rtl')).toBeTruthy();
document.body.innerHTML = '';
actDestroy();
message.config({ rtl: true });
message.info('last');
await awaitPromise();
expect(document.querySelector('.ant-message-rtl')).toBeTruthy();
document.body.innerHTML = '';
actDestroy();
message.config({ rtl: false });
message.info('last');
await awaitPromise();
expect(document.querySelector('.ant-message-rtl')).toBeFalsy();
message.config({ rtl: undefined });
ConfigProvider.config({ holderRender: undefined });
});
it('should be able to config holderRender and static config', async () => {
// level 1
document.body.innerHTML = '';
actDestroy();
ConfigProvider.config({ prefixCls: 'prefix-1' });
message.info('last');
await awaitPromise();
expect(document.querySelectorAll('.prefix-1-message')).toHaveLength(1);
// level 2
document.body.innerHTML = '';
actDestroy();
ConfigProvider.config({
prefixCls: 'prefix-1',
holderRender: (children) => <ConfigProvider prefixCls="prefix-2">{children}</ConfigProvider>,
});
message.info('last');
await awaitPromise();
expect(document.querySelectorAll('.prefix-2-message')).toHaveLength(1);
// level 3
document.body.innerHTML = '';
actDestroy();
message.config({ prefixCls: 'prefix-3-message' });
message.info('last');
await awaitPromise();
expect(document.querySelectorAll('.prefix-3-message')).toHaveLength(1);
// clear config
message.config({ prefixCls: '' });
ConfigProvider.config({ prefixCls: '', iconPrefixCls: '', holderRender: undefined });
});
it('should be able to config holderRender use App', async () => {
document.body.innerHTML = '';
actDestroy();
ConfigProvider.config({
holderRender: (children) => <App message={{ maxCount: 1 }}>{children}</App>,
});
message.info('last');
message.info('last');
await awaitPromise();
const noticeWithoutLeaving = Array.from(
document.querySelectorAll('.ant-message-notice-wrapper'),
).filter((ele) => !ele.classList.contains('ant-message-move-up-leave'));
expect(noticeWithoutLeaving).toHaveLength(1);
ConfigProvider.config({ holderRender: undefined });
});
});
|