Skip to content

Commit 6f1e283

Browse files
SavePointSamBrian Vaughn
authored andcommitted
feat: match HOC display names during search (#360)
* feat: match HOC display names during search * chore: update tests for new search logic
1 parent 6b3ae64 commit 6f1e283

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/__tests__/__snapshots__/treeContext-test.js.snap

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,13 @@ exports[`TreeListContext search state should find elements matching search text:
558558
<Foo>
559559
<Bar>
560560
<Baz>
561+
<Qux> [withHOC]
561562
`;
562563
563564
exports[`TreeListContext search state should find elements matching search text: 1: initial state 1`] = `
564565
Object {
565566
"inspectedElementID": null,
566-
"numElements": 3,
567+
"numElements": 4,
567568
"ownerFlatTree": null,
568569
"ownerID": null,
569570
"searchIndex": null,
@@ -577,7 +578,7 @@ Object {
577578
exports[`TreeListContext search state should find elements matching search text: 2: search for "ba" 1`] = `
578579
Object {
579580
"inspectedElementID": 3,
580-
"numElements": 3,
581+
"numElements": 4,
581582
"ownerFlatTree": null,
582583
"ownerID": null,
583584
"searchIndex": 0,
@@ -594,7 +595,7 @@ Object {
594595
exports[`TreeListContext search state should find elements matching search text: 3: search for "f" 1`] = `
595596
Object {
596597
"inspectedElementID": 2,
597-
"numElements": 3,
598+
"numElements": 4,
598599
"ownerFlatTree": null,
599600
"ownerID": null,
600601
"searchIndex": 0,
@@ -607,20 +608,36 @@ Object {
607608
}
608609
`;
609610
610-
exports[`TreeListContext search state should find elements matching search text: 4: search for "q" 1`] = `
611+
exports[`TreeListContext search state should find elements matching search text: 4: search for "y" 1`] = `
611612
Object {
612613
"inspectedElementID": 2,
613-
"numElements": 3,
614+
"numElements": 4,
614615
"ownerFlatTree": null,
615616
"ownerID": null,
616617
"searchIndex": null,
617618
"searchResults": Array [],
618-
"searchText": "q",
619+
"searchText": "y",
619620
"selectedElementID": 2,
620621
"selectedElementIndex": 0,
621622
}
622623
`;
623624
625+
exports[`TreeListContext search state should find elements matching search text: 5: search for "w" 1`] = `
626+
Object {
627+
"inspectedElementID": 5,
628+
"numElements": 4,
629+
"ownerFlatTree": null,
630+
"ownerID": null,
631+
"searchIndex": 0,
632+
"searchResults": Array [
633+
5,
634+
],
635+
"searchText": "w",
636+
"selectedElementID": 5,
637+
"selectedElementIndex": 3,
638+
}
639+
`;
640+
624641
exports[`TreeListContext search state should remove unmounted elements from the search results set: 0: mount 1`] = `
625642
[root]
626643
<Foo>

src/__tests__/treeContext-test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,17 @@ describe('TreeListContext', () => {
270270
const Foo = () => null;
271271
const Bar = () => null;
272272
const Baz = () => null;
273+
const Qux = () => null;
274+
275+
Qux.displayName = `withHOC(${Qux.name})`;
273276

274277
utils.act(() =>
275278
ReactDOM.render(
276279
<React.Fragment>
277280
<Foo />
278281
<Bar />
279282
<Baz />
283+
<Qux />
280284
</React.Fragment>,
281285
document.createElement('div')
282286
)
@@ -288,17 +292,25 @@ describe('TreeListContext', () => {
288292
utils.act(() => (renderer = TestRenderer.create(<Contexts />)));
289293
expect(state).toMatchSnapshot('1: initial state');
290294

295+
// NOTE: multi-match
291296
utils.act(() => dispatch({ type: 'SET_SEARCH_TEXT', payload: 'ba' }));
292297
utils.act(() => renderer.update(<Contexts />));
293298
expect(state).toMatchSnapshot('2: search for "ba"');
294299

300+
// NOTE: single match
295301
utils.act(() => dispatch({ type: 'SET_SEARCH_TEXT', payload: 'f' }));
296302
utils.act(() => renderer.update(<Contexts />));
297303
expect(state).toMatchSnapshot('3: search for "f"');
298304

299-
utils.act(() => dispatch({ type: 'SET_SEARCH_TEXT', payload: 'q' }));
305+
// NOTE: no match
306+
utils.act(() => dispatch({ type: 'SET_SEARCH_TEXT', payload: 'y' }));
307+
utils.act(() => renderer.update(<Contexts />));
308+
expect(state).toMatchSnapshot('4: search for "y"');
309+
310+
// NOTE: HOC match
311+
utils.act(() => dispatch({ type: 'SET_SEARCH_TEXT', payload: 'w' }));
300312
utils.act(() => renderer.update(<Contexts />));
301-
expect(state).toMatchSnapshot('4: search for "q"');
313+
expect(state).toMatchSnapshot('5: search for "w"');
302314
});
303315

304316
it('should select the next and previous items within the search results', () => {

src/devtools/views/Components/TreeContext.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,14 +773,20 @@ function recursivelySearchTree(
773773
regExp: RegExp,
774774
searchResults: Array<number>
775775
): void {
776-
const { children, displayName } = ((store.getElementByID(
776+
const { children, displayName, hocDisplayNames } = ((store.getElementByID(
777777
elementID
778778
): any): Element);
779-
if (displayName !== null) {
780-
if (regExp.test(displayName)) {
781-
searchResults.push(elementID);
782-
}
779+
780+
if (displayName != null && regExp.test(displayName) === true) {
781+
searchResults.push(elementID);
782+
} else if (
783+
hocDisplayNames != null &&
784+
hocDisplayNames.length > 0 &&
785+
hocDisplayNames.some(name => regExp.test(name)) === true
786+
) {
787+
searchResults.push(elementID);
783788
}
789+
784790
children.forEach(childID =>
785791
recursivelySearchTree(store, childID, regExp, searchResults)
786792
);

0 commit comments

Comments
 (0)