-
Notifications
You must be signed in to change notification settings - Fork 45.7k
/
storeComponentFilters-test.js
230 lines (190 loc) · 6.1 KB
/
storeComponentFilters-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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
import type Store from 'react-devtools-shared/src/devtools/store';
describe('Store component filters', () => {
let React;
let ReactDOM;
let TestUtils;
let Types;
let bridge: FrontendBridge;
let store: Store;
let utils;
const act = (callback: Function) => {
TestUtils.unstable_concurrentAct(() => {
callback();
});
jest.runAllTimers(); // Flush Bridge operations
};
beforeEach(() => {
bridge = global.bridge;
store = global.store;
store.collapseNodesByDefault = false;
store.componentFilters = [];
store.recordChangeDescriptions = true;
React = require('react');
ReactDOM = require('react-dom');
TestUtils = require('react-dom/test-utils');
Types = require('react-devtools-shared/src/types');
utils = require('./utils');
});
it('should throw if filters are updated while profiling', () => {
act(() => store.profilerStore.startProfiling());
expect(() => (store.componentFilters = [])).toThrow(
'Cannot modify filter preferences while profiling',
);
});
it('should support filtering by element type', () => {
class Root extends React.Component<{|children: React$Node|}> {
render() {
return <div>{this.props.children}</div>;
}
}
const Component = () => <div>Hi</div>;
act(() =>
ReactDOM.render(
<Root>
<Component />
</Root>,
document.createElement('div'),
),
);
expect(store).toMatchSnapshot('1: mount');
act(
() =>
(store.componentFilters = [
utils.createElementTypeFilter(Types.ElementTypeHostComponent),
]),
);
expect(store).toMatchSnapshot('2: hide host components');
act(
() =>
(store.componentFilters = [
utils.createElementTypeFilter(Types.ElementTypeClass),
]),
);
expect(store).toMatchSnapshot('3: hide class components');
act(
() =>
(store.componentFilters = [
utils.createElementTypeFilter(Types.ElementTypeClass),
utils.createElementTypeFilter(Types.ElementTypeFunction),
]),
);
expect(store).toMatchSnapshot('4: hide class and function components');
act(
() =>
(store.componentFilters = [
utils.createElementTypeFilter(Types.ElementTypeClass, false),
utils.createElementTypeFilter(Types.ElementTypeFunction, false),
]),
);
expect(store).toMatchSnapshot('5: disable all filters');
});
it('should ignore invalid ElementTypeRoot filter', () => {
const Root = () => <div>Hi</div>;
act(() => ReactDOM.render(<Root />, document.createElement('div')));
expect(store).toMatchSnapshot('1: mount');
act(
() =>
(store.componentFilters = [
utils.createElementTypeFilter(Types.ElementTypeRoot),
]),
);
expect(store).toMatchSnapshot('2: add invalid filter');
});
it('should filter by display name', () => {
const Text = ({label}) => label;
const Foo = () => <Text label="foo" />;
const Bar = () => <Text label="bar" />;
const Baz = () => <Text label="baz" />;
act(() =>
ReactDOM.render(
<React.Fragment>
<Foo />
<Bar />
<Baz />
</React.Fragment>,
document.createElement('div'),
),
);
expect(store).toMatchSnapshot('1: mount');
act(
() => (store.componentFilters = [utils.createDisplayNameFilter('Foo')]),
);
expect(store).toMatchSnapshot('2: filter "Foo"');
act(() => (store.componentFilters = [utils.createDisplayNameFilter('Ba')]));
expect(store).toMatchSnapshot('3: filter "Ba"');
act(
() => (store.componentFilters = [utils.createDisplayNameFilter('B.z')]),
);
expect(store).toMatchSnapshot('4: filter "B.z"');
});
it('should filter by path', () => {
const Component = () => <div>Hi</div>;
act(() => ReactDOM.render(<Component />, document.createElement('div')));
expect(store).toMatchSnapshot('1: mount');
act(
() =>
(store.componentFilters = [
utils.createLocationFilter(__filename.replace(__dirname, '')),
]),
);
expect(store).toMatchSnapshot(
'2: hide all components declared within this test filed',
);
act(
() =>
(store.componentFilters = [
utils.createLocationFilter('this:is:a:made:up:path'),
]),
);
expect(store).toMatchSnapshot('3: hide components in a made up fake path');
});
it('should filter HOCs', () => {
const Component = () => <div>Hi</div>;
const Foo = () => <Component />;
Foo.displayName = 'Foo(Component)';
const Bar = () => <Foo />;
Bar.displayName = 'Bar(Foo(Component))';
act(() => ReactDOM.render(<Bar />, document.createElement('div')));
expect(store).toMatchSnapshot('1: mount');
act(() => (store.componentFilters = [utils.createHOCFilter(true)]));
expect(store).toMatchSnapshot('2: hide all HOCs');
act(() => (store.componentFilters = [utils.createHOCFilter(false)]));
expect(store).toMatchSnapshot('3: disable HOC filter');
});
it('should not send a bridge update if the set of enabled filters has not changed', () => {
act(() => (store.componentFilters = [utils.createHOCFilter(true)]));
bridge.addListener('updateComponentFilters', componentFilters => {
throw Error('Unexpected component update');
});
act(
() =>
(store.componentFilters = [
utils.createHOCFilter(false),
utils.createHOCFilter(true),
]),
);
act(
() =>
(store.componentFilters = [
utils.createHOCFilter(true),
utils.createLocationFilter('abc', false),
]),
);
act(
() =>
(store.componentFilters = [
utils.createHOCFilter(true),
utils.createElementTypeFilter(Types.ElementTypeHostComponent, false),
]),
);
});
});