From 3c0bc666dbdec5d66165375f2a5f139e03fa4c03 Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Wed, 30 Mar 2022 14:23:32 -0400 Subject: [PATCH 1/4] Dynamically get react-select parent Get react-select parent using .closest and attribute matchers to match dynamically generated class --- src/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index b365c20..1fefcdd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,8 +12,7 @@ import act from "./act-compat"; // find the react-select container from its input field 🤷 function getReactSelectContainerFromInput(input: HTMLElement): HTMLElement { - return input.parentNode!.parentNode!.parentNode!.parentNode! - .parentNode as HTMLElement; + return input.closest('[class^="css-"], [class$="-container"]') as HTMLElement; } /** From 2de411306657011684573812d463b8f7962f28f7 Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Thu, 31 Mar 2022 10:00:48 -0400 Subject: [PATCH 2/4] Update dynamic selector to be more robust and add fallback to legacy traversal --- src/index.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1fefcdd..ee90c00 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ /** Simulate user events on react-select dropdowns */ import { - Matcher, findAllByText, findByText, fireEvent, + Matcher, waitFor, } from "@testing-library/dom"; @@ -12,7 +12,19 @@ import act from "./act-compat"; // find the react-select container from its input field 🤷 function getReactSelectContainerFromInput(input: HTMLElement): HTMLElement { - return input.closest('[class^="css-"], [class$="-container"]') as HTMLElement; + // i hate this so much but older versions insist on inserting a space before their generated class name + let parent = + input.closest('[class^="css-"][class$="-container"]') || + input.closest('[class^=" css-"][class$="-container"]'); + + // for older versions of react-select fall back to old parent crawler + if (!parent) { + console.log("index:19", "legacy crawler"); + parent = input.parentNode!.parentNode!.parentNode!.parentNode! + .parentNode as HTMLElement; + } + + return parent as HTMLElement; } /** @@ -104,6 +116,7 @@ interface CreateConfig extends Config { createOptionText?: string | RegExp; waitForElement?: boolean; } + /** * Utility for creating and selecting a value in a Creatable `react-select` dropdown. * @async From 5dde19c4d002e98d6487f381900ad3bcd7915a2e Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Thu, 31 Mar 2022 10:10:42 -0400 Subject: [PATCH 3/4] remove log --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index ee90c00..6ad9be0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,6 @@ function getReactSelectContainerFromInput(input: HTMLElement): HTMLElement { // for older versions of react-select fall back to old parent crawler if (!parent) { - console.log("index:19", "legacy crawler"); parent = input.parentNode!.parentNode!.parentNode!.parentNode! .parentNode as HTMLElement; } From 60c3dd00cc7fda0f13a8a433bcfe500c137c6838 Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Thu, 31 Mar 2022 12:30:34 -0400 Subject: [PATCH 4/4] coverage fix --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 6ad9be0..13b576f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,7 @@ function getReactSelectContainerFromInput(input: HTMLElement): HTMLElement { input.closest('[class^=" css-"][class$="-container"]'); // for older versions of react-select fall back to old parent crawler + // istanbul ignore if if (!parent) { parent = input.parentNode!.parentNode!.parentNode!.parentNode! .parentNode as HTMLElement;