Added another (disabled) test; refactored getChildren slightly
Brian Vaughn committed
May 26, 2019 at 08:37 UTC
4422337562574cb87012d3dc68ea66cab638dda6
2 files changed
+72
-29
src/__tests__/legacy/storeLegacy-v15-test.js
+46
-7
@@ -176,6 +176,45 @@ describe('Store (legacy)', () => {
176
expect(store).toMatchSnapshot('6: expand Grandparent');
177
});
178
179
+ // TODO Re-enable this test once the renderer supports it.
180
+ xit('should support adding and removing children', () => {
181
+ const Root = ({ children }) => <div>{children}</div>;
182
+ const Component = () => null;
183
+
184
+ const container = document.createElement('div');
185
+
186
+ act(() =>
187
+ ReactDOM.render(
188
+ <Root>
189
+ <Component key="a" />
190
+ </Root>,
191
+ container
192
+ )
193
+ );
194
+ expect(store).toMatchSnapshot('1: mount');
195
+
196
+ act(() =>
197
+ ReactDOM.render(
198
+ <Root>
199
+ <Component key="a" />
200
+ <Component key="b" />
201
+ </Root>,
202
+ container
203
+ )
204
+ );
205
+ expect(store).toMatchSnapshot('2: add child');
206
+
207
+ act(() =>
208
+ ReactDOM.render(
209
+ <Root>
210
+ <Component key="b" />
211
+ </Root>,
212
+ container
213
+ )
214
+ );
215
+ expect(store).toMatchSnapshot('3: remove child');
216
+ });
217
+
218
// TODO Re-enable this test once the renderer supports it.
219
xit('should support reordering of children', () => {
220
const Root = ({ children }) => <div>{children}</div>;
@@ -194,13 +233,13 @@ describe('Store (legacy)', () => {
233
expect(store).toMatchSnapshot('1: mount');
234
235
act(() => ReactDOM.render(<Root>{[bar, foo]}</Root>, container));
197
- expect(store).toMatchSnapshot('3: reorder children');
236
+ expect(store).toMatchSnapshot('2: reorder children');
237
238
act(() => store.toggleIsCollapsed(store.getElementIDAtIndex(0), true));
200
- expect(store).toMatchSnapshot('4: collapse root');
239
+ expect(store).toMatchSnapshot('3: collapse root');
240
241
act(() => store.toggleIsCollapsed(store.getElementIDAtIndex(0), false));
203
- expect(store).toMatchSnapshot('5: expand root');
242
+ expect(store).toMatchSnapshot('4: expand root');
243
});
244
});
245
@@ -422,19 +461,19 @@ describe('Store (legacy)', () => {
461
expect(store).toMatchSnapshot('1: mount');
462
463
act(() => ReactDOM.render(<Root>{[bar, foo]}</Root>, container));
425
- expect(store).toMatchSnapshot('3: reorder children');
464
+ expect(store).toMatchSnapshot('2: reorder children');
465
466
act(() => store.toggleIsCollapsed(store.getElementIDAtIndex(0), false));
428
- expect(store).toMatchSnapshot('4: expand root');
467
+ expect(store).toMatchSnapshot('3: expand root');
468
469
act(() => {
470
store.toggleIsCollapsed(store.getElementIDAtIndex(2), false);
471
store.toggleIsCollapsed(store.getElementIDAtIndex(1), false);
472
});
434
- expect(store).toMatchSnapshot('5: expand leaves');
473
+ expect(store).toMatchSnapshot('4: expand leaves');
474
475
act(() => store.toggleIsCollapsed(store.getElementIDAtIndex(0), true));
437
- expect(store).toMatchSnapshot('6: collapse root');
476
+ expect(store).toMatchSnapshot('5: collapse root');
477
});
478
});
479
});
src/backend/legacy/getChildren.js
+26
-22
@@ -1,22 +1,30 @@
1
// @flow
2
3
+import traverseAllChildrenImpl from './traverseAllChildrenImpl';
4
+
5
+// TODO (legacy) Respect component filters
6
+
7
export default function getChildren(internalInstance: Object): Array<any> {
8
+ let children = [];
9
+
10
// If the parent is a native node without rendered children, but with
11
// multiple string children, then the `element` that gets passed in here is
12
// a plain value -- a string or number.
7
- if (internalInstance._renderedComponent) {
8
- return [internalInstance._renderedComponent];
13
+ if (typeof internalInstance !== 'object') {
14
+ // No children
15
+ } else if (
16
+ internalInstance._currentElement === null ||
17
+ internalInstance._currentElement === false
18
+ ) {
19
+ // No children
20
+ } else if (internalInstance._renderedComponent) {
21
+ children = [internalInstance._renderedComponent];
22
} else if (internalInstance._renderedChildren) {
10
- return childrenToArray(internalInstance._renderedChildren);
23
+ children = renderedChildrenToArray(internalInstance._renderedChildren);
24
} else if (
25
internalInstance._currentElement &&
26
internalInstance._currentElement.props
27
) {
15
- // DevTools doesn't need to display primative child types,
16
- // So we can filter them out early.
17
- /*
18
- const children = [];
19
-
28
// This is a native node without rendered children -- meaning the children
29
// prop is the unfiltered list of children.
30
// This may include 'null' or even other invalid values, so we need to
@@ -34,30 +42,26 @@ export default function getChildren(internalInstance: Object): Array<any> {
42
children.push(child);
43
}
44
}
37
- // traverseContext
45
);
39
-
40
- return children;
41
- */
46
}
47
44
- if (internalInstance._instance) {
45
- var inst = internalInstance._instance;
48
+ const instance = internalInstance._instance;
49
+ if (instance) {
50
// TODO: React ART currently falls in this bucket, but this doesn't
51
// actually make sense and we should clean this up after stabilizing our
52
// API for backends
49
- if (inst._renderedChildren) {
50
- return childrenToArray(inst._renderedChildren);
53
+ if (instance._renderedChildren) {
54
+ children = renderedChildrenToArray(instance._renderedChildren);
55
}
56
}
57
54
- return [];
58
+ return children;
59
}
60
57
-function childrenToArray(children) {
58
- const array = [];
59
- for (var name in children) {
60
- array.push(children[name]);
61
+function renderedChildrenToArray(renderedChildren): Array<any> {
62
+ const childrenArray = [];
63
+ for (let name in renderedChildren) {
64
+ childrenArray.push(renderedChildren[name]);
65
}
62
- return array;
66
+ return childrenArray;
67
}