2023-09-13 22:07:33 +08:00
|
|
|
import React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { render } from '@testing-library/react';
|
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
import { devUseWarning as useWarning } from '../warning';
|
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
describe('Test warning', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
let spy: jest.SpyInstance;
|
2022-05-10 15:43:29 +08:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
spy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-05-10 15:43:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
spy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.resetModules();
|
2022-05-10 15:43:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
spy.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Test noop', async () => {
|
|
|
|
const { noop } = await import('../warning');
|
|
|
|
const value = noop();
|
|
|
|
|
|
|
|
expect(value).toBe(undefined);
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
2022-08-21 23:25:00 +08:00
|
|
|
expect(noop).not.toThrow();
|
2022-05-10 15:43:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('process.env.NODE_ENV !== "production"', () => {
|
|
|
|
it('If `false`, exec `console.error`', async () => {
|
|
|
|
const warning = (await import('../warning')).default;
|
|
|
|
warning(false, 'error');
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('If `true`, do not exec `console.error`', async () => {
|
|
|
|
const warning = (await import('../warning')).default;
|
|
|
|
warning(true, 'error message');
|
|
|
|
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
|
|
});
|
2023-09-13 22:07:33 +08:00
|
|
|
it('should show warning when using devUseWarning', async () => {
|
|
|
|
const App = () => {
|
|
|
|
// Don't use dynamic import to fixed issue: TypeError: Cannot read properties of null (reading 'useContext')
|
|
|
|
const warning = useWarning('Test');
|
|
|
|
warning(false, 'usage', 'test message');
|
|
|
|
warning.deprecated(false, 'old prop', 'new prop');
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
render(<App />);
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalledWith('Warning: [antd: Test] test message');
|
|
|
|
expect(spy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Test] `old prop` is deprecated. Please use `new prop` instead.',
|
|
|
|
);
|
|
|
|
expect(spy).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
2022-05-10 15:43:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('process.env.NODE_ENV === "production"', () => {
|
2023-09-13 22:07:33 +08:00
|
|
|
let prevEnv: string | undefined;
|
|
|
|
const mockNodeEnv = () => {
|
|
|
|
prevEnv = process.env.NODE_ENV;
|
2022-05-10 15:43:29 +08:00
|
|
|
process.env.NODE_ENV = 'production';
|
2023-09-13 22:07:33 +08:00
|
|
|
};
|
|
|
|
const restoreNodeEnv = () => {
|
|
|
|
process.env.NODE_ENV = prevEnv;
|
|
|
|
};
|
|
|
|
beforeEach(() => {
|
|
|
|
mockNodeEnv();
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
restoreNodeEnv();
|
|
|
|
});
|
|
|
|
it('Whether `true` or `false`, do not exec `console.error`', async () => {
|
2022-05-10 15:43:29 +08:00
|
|
|
const { default: warning, noop } = await import('../warning');
|
|
|
|
|
|
|
|
expect(warning).toEqual(noop);
|
|
|
|
|
|
|
|
warning(false, 'error message');
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
warning(true, 'error message');
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
2023-09-13 22:07:33 +08:00
|
|
|
});
|
2022-05-10 15:43:29 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
it('should not show warning when using devUseWarning', async () => {
|
|
|
|
const { devUseWarning } = await import('../warning');
|
|
|
|
const App = () => {
|
|
|
|
const warning = devUseWarning('Test');
|
|
|
|
warning(false, 'usage', 'test message');
|
|
|
|
warning.deprecated(false, 'old prop', 'new prop');
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
render(<App />);
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalledTimes(0);
|
2022-05-10 15:43:29 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|