-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathwarning.test.js
97 lines (75 loc) · 2.7 KB
/
warning.test.js
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
import { render } from '@testing-library/react';
import React from 'react';
import { warning } from '../src';
import unsafeLifecyclesPolyfill from '../src/unsafeLifecyclesPolyfill';
const { resetWarned, noteOnce } = warning;
describe('warning', () => {
beforeEach(() => {
resetWarned();
});
it('warning', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
warning(false, '[antd Component] test hello world');
expect(warnSpy).toHaveBeenCalledWith(
'Warning: [antd Component] test hello world',
);
warning(false, '[antd Component] test hello world');
expect(warnSpy).toHaveBeenCalledTimes(1);
resetWarned();
warning(false, '[antd Component] test hello world');
expect(warnSpy).toHaveBeenCalledTimes(2);
warning(true, '[antd Component] test1');
expect(warnSpy).not.toHaveBeenCalledWith('[antd Component] test1');
warnSpy.mockRestore();
});
it('note', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
noteOnce(false, '[antd Component] test hello world');
expect(warnSpy).toHaveBeenCalledWith(
'Note: [antd Component] test hello world',
);
noteOnce(false, '[antd Component] test hello world');
expect(warnSpy).toHaveBeenCalledTimes(1);
resetWarned();
noteOnce(false, '[antd Component] test hello world');
expect(warnSpy).toHaveBeenCalledTimes(2);
noteOnce(true, '[antd Component] test1');
expect(warnSpy).not.toHaveBeenCalledWith('[antd Component] test1');
warnSpy.mockRestore();
});
// https://github.com/ant-design/ant-design/issues/9792
it('should not warning React componentWillReceiveProps', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
class App extends React.Component {
state = {};
render() {
return null;
}
}
const FixedWarningApp = unsafeLifecyclesPolyfill(App);
render(<FixedWarningApp />);
expect(warnSpy).not.toHaveBeenCalledWith(
expect.stringContaining('componentWillReceiveProps has been renamed'),
);
warnSpy.mockRestore();
});
describe('preMessage', () => {
it('modify message', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
warning.preMessage((msg, type) => {
if (type === 'warning') {
return `WW-${msg}`;
}
if (type === 'note') {
return `NN-${msg}`;
}
return null;
});
warning(false, 'warn');
expect(warnSpy).toHaveBeenCalledWith('Warning: WW-warn');
warning.noteOnce(false, 'note');
expect(warnSpy).toHaveBeenCalledWith('Note: NN-note');
warnSpy.mockRestore();
});
});
});