-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
memo.test.js
158 lines (123 loc) · 3.03 KB
/
memo.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
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
import { setupRerender } from 'preact/test-utils';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { Component, render, createElement as h, memo } from '../../src';
/** @jsx h */
describe('memo()', () => {
let scratch, rerender;
beforeEach(() => {
scratch = setupScratch();
rerender = setupRerender();
});
afterEach(() => {
teardown(scratch);
});
it('should have isReactComponent flag', () => {
// eslint-disable-next-line react/display-name
let App = memo(() => <div>foo</div>);
expect(App.prototype.isReactComponent).to.equal(true);
});
it('should work with function components', () => {
let spy = sinon.spy();
function Foo() {
spy();
return <h1>Hello World</h1>;
}
let Memoized = memo(Foo);
let update;
class App extends Component {
constructor() {
super();
update = () => this.setState({});
}
render() {
return <Memoized />;
}
}
render(<App />, scratch);
expect(spy).to.be.calledOnce;
update();
rerender();
expect(spy).to.be.calledOnce;
});
it('should support adding refs', () => {
let spy = sinon.spy();
let ref = null;
function Foo() {
spy();
return <h1>Hello World</h1>;
}
let Memoized = memo(Foo);
let update;
class App extends Component {
constructor() {
super();
update = () => this.setState({});
}
render() {
return <Memoized ref={ref} />;
}
}
render(<App />, scratch);
expect(spy).to.be.calledOnce;
ref = {};
update();
rerender();
expect(ref.current).not.to.be.undefined;
// TODO: not sure whether this is in-line with react...
expect(spy).to.be.calledTwice;
});
it('should support custom comparer functions', () => {
function Foo() {
return <h1>Hello World</h1>;
}
let spy = sinon.spy(() => true);
let Memoized = memo(Foo, spy);
let update;
class App extends Component {
constructor() {
super();
update = () => this.setState({});
}
render() {
return <Memoized />;
}
}
render(<App />, scratch);
update();
rerender();
expect(spy).to.be.calledOnce;
expect(spy).to.be.calledWith({}, {});
});
it('should rerender when custom comparer returns false', () => {
const spy = sinon.spy();
function Foo() {
spy();
return <h1>Hello World</h1>;
}
const App = memo(Foo, () => false);
render(<App />, scratch);
expect(spy).to.be.calledOnce;
render(<App foo="bar" />, scratch);
expect(spy).to.be.calledTwice;
});
it('should pass props and nextProps to comparer fn', () => {
const spy = sinon.spy(() => false);
function Foo() {
return <div>foo</div>;
}
const props = { foo: true };
const nextProps = { foo: false };
const App = memo(Foo, spy);
render(h(App, props), scratch);
render(h(App, nextProps), scratch);
expect(spy).to.be.calledWith(props, nextProps);
});
it('should nest without errors', () => {
const Foo = () => <div>foo</div>;
const App = memo(memo(Foo));
// eslint-disable-next-line prefer-arrow-callback
expect(function() {
render(<App />, scratch);
}).to.not.throw();
});
});