From 4584df5f783e7e5d2ad0693dc788701aaed0e60a Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Mon, 29 Jan 2024 15:50:34 +0100 Subject: [PATCH] NO JIRA Test selector chain will look all before reducing --- .../src/main/js/helpers/testSelector.ts | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/server/sonar-web/src/main/js/helpers/testSelector.ts b/server/sonar-web/src/main/js/helpers/testSelector.ts index d33d83838c1..b3ceca3e274 100644 --- a/server/sonar-web/src/main/js/helpers/testSelector.ts +++ b/server/sonar-web/src/main/js/helpers/testSelector.ts @@ -148,7 +148,24 @@ class ChainDispatch extends QuerySelector { container?: HTMLElement, waitForOptions?: waitForOptions, ) { - return this.dispatchQuery.find(await this.innerQuery.find(container, waitForOptions)); + let inside: HTMLElement; + try { + inside = await this.innerQuery.find(container, waitForOptions); + } catch (e) { + const elements = this.innerQuery.getAll(container); + const all = ( + await Promise.all( + elements.map((e) => this.dispatchQuery.findAll(e, waitForOptions).catch(() => null)), + ) + ) + .flat() + .filter((e) => e !== null); + if (all.length !== 1) { + throw e; + } + return all[0] as T; + } + return this.dispatchQuery.find(inside, waitForOptions); } async findAll( @@ -159,7 +176,18 @@ class ChainDispatch extends QuerySelector { } get(container?: HTMLElement) { - return this.dispatchQuery.get(this.innerQuery.get(container)); + let inside: HTMLElement; + try { + inside = this.innerQuery.get(container); + } catch (e) { + const elements = this.innerQuery.getAll(container); + const all = elements.map((e) => this.dispatchQuery.query(e)).filter((e) => e !== null); + if (all.length !== 1) { + throw e; + } + return all[0] as T; + } + return this.dispatchQuery.get(inside); } getAll(container?: HTMLElement) { @@ -171,9 +199,19 @@ class ChainDispatch extends QuerySelector { } query(container?: HTMLElement) { - const innerContainer = this.innerQuery.query(container); - if (innerContainer) { - return this.dispatchQuery.query(innerContainer); + let inside: HTMLElement | null; + try { + inside = this.innerQuery.query(container); + } catch (e) { + const elements = this.innerQuery.queryAll(container); + const all = elements?.map((e) => this.dispatchQuery.query(e)).filter((e) => e !== null); + if (all?.length !== 1) { + throw e; + } + return all[0]; + } + if (inside) { + return this.dispatchQuery.query(inside); } return null; } -- 2.39.5