From 3c8e87e31d5d410889e8a18c6eca70b3fd28fea6 Mon Sep 17 00:00:00 2001 From: Georgii Lobko Date: Thu, 25 Jul 2024 14:32:44 +0200 Subject: [PATCH 1/3] chore: Exclude `act` from selectors --- src/converter/convert-to-selectors.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/converter/convert-to-selectors.ts b/src/converter/convert-to-selectors.ts index ce53485..f0d4952 100644 --- a/src/converter/convert-to-selectors.ts +++ b/src/converter/convert-to-selectors.ts @@ -21,11 +21,11 @@ function selectorUtilsGenerator({ types: t }: PluginArguments): PluginObj { source.replaceWith(t.stringLiteral(newImportPath)); } - // Remove @usesDom decorator + // Remove @usesDom decorator and act function if (source.node.value === runtimeSelectorsPath) { path .get('specifiers') - .filter(spec => spec.node.local.name === 'usesDom') + .filter(spec => ['usesDom', 'act'].includes(spec.node.local.name)) .forEach(spec => spec.remove()); } }, From 32053c0f40a88397db2126db0842ac3b29f16974 Mon Sep 17 00:00:00 2001 From: Georgii Lobko Date: Thu, 25 Jul 2024 14:45:40 +0200 Subject: [PATCH 2/3] chore: Update tests snapshots --- src/converter/test/__snapshots__/converter.test.ts.snap | 2 +- src/converter/test/inputs/converter/strip-imports.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/converter/test/__snapshots__/converter.test.ts.snap b/src/converter/test/__snapshots__/converter.test.ts.snap index d9e11da..53db655 100644 --- a/src/converter/test/__snapshots__/converter.test.ts.snap +++ b/src/converter/test/__snapshots__/converter.test.ts.snap @@ -48,7 +48,7 @@ export default class DummyWrapper extends ComponentWrapper { exports[`strip-imports 1`] = ` "// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { ComponentWrapper, usesDom } from '../../../../../lib/core/dom'; +import { ComponentWrapper, usesDom, act } from '../../../../../lib/core/dom'; import { KeyCode } from '../../../../../lib/core/utils'; import ChildWrapper from './simple'; export default class DummyWrapper extends ComponentWrapper { diff --git a/src/converter/test/inputs/converter/strip-imports.ts b/src/converter/test/inputs/converter/strip-imports.ts index c93141d..7947e10 100644 --- a/src/converter/test/inputs/converter/strip-imports.ts +++ b/src/converter/test/inputs/converter/strip-imports.ts @@ -1,6 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { ComponentWrapper, usesDom } from '../../../../../lib/core/dom'; +import { ComponentWrapper, usesDom, act } from '../../../../../lib/core/dom'; import { KeyCode } from '../../../../../lib/core/utils'; import ChildWrapper from './simple'; @@ -14,6 +14,13 @@ export default class DummyWrapper extends ComponentWrapper { return new ChildWrapper(this.find('.awsui-child')!.getElement()); } + @usesDom + click(): void { + act(() => { + this.click(); + }); + } + findSomething(): number { return KeyCode.enter; } From 78084f24ec608adba1b56b599454fc6ea8f36ab0 Mon Sep 17 00:00:00 2001 From: Georgii Lobko Date: Thu, 25 Jul 2024 15:29:05 +0200 Subject: [PATCH 3/3] chore: Import act from utils --- src/converter/convert-to-selectors.ts | 4 ++-- src/converter/test/__snapshots__/converter.test.ts.snap | 2 +- src/converter/test/inputs/converter/strip-imports.ts | 9 +-------- src/core/dom.ts | 6 +----- src/core/utils.ts | 4 ++++ 5 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/converter/convert-to-selectors.ts b/src/converter/convert-to-selectors.ts index f0d4952..ce53485 100644 --- a/src/converter/convert-to-selectors.ts +++ b/src/converter/convert-to-selectors.ts @@ -21,11 +21,11 @@ function selectorUtilsGenerator({ types: t }: PluginArguments): PluginObj { source.replaceWith(t.stringLiteral(newImportPath)); } - // Remove @usesDom decorator and act function + // Remove @usesDom decorator if (source.node.value === runtimeSelectorsPath) { path .get('specifiers') - .filter(spec => ['usesDom', 'act'].includes(spec.node.local.name)) + .filter(spec => spec.node.local.name === 'usesDom') .forEach(spec => spec.remove()); } }, diff --git a/src/converter/test/__snapshots__/converter.test.ts.snap b/src/converter/test/__snapshots__/converter.test.ts.snap index 53db655..d9e11da 100644 --- a/src/converter/test/__snapshots__/converter.test.ts.snap +++ b/src/converter/test/__snapshots__/converter.test.ts.snap @@ -48,7 +48,7 @@ export default class DummyWrapper extends ComponentWrapper { exports[`strip-imports 1`] = ` "// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { ComponentWrapper, usesDom, act } from '../../../../../lib/core/dom'; +import { ComponentWrapper, usesDom } from '../../../../../lib/core/dom'; import { KeyCode } from '../../../../../lib/core/utils'; import ChildWrapper from './simple'; export default class DummyWrapper extends ComponentWrapper { diff --git a/src/converter/test/inputs/converter/strip-imports.ts b/src/converter/test/inputs/converter/strip-imports.ts index 7947e10..c93141d 100644 --- a/src/converter/test/inputs/converter/strip-imports.ts +++ b/src/converter/test/inputs/converter/strip-imports.ts @@ -1,6 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { ComponentWrapper, usesDom, act } from '../../../../../lib/core/dom'; +import { ComponentWrapper, usesDom } from '../../../../../lib/core/dom'; import { KeyCode } from '../../../../../lib/core/utils'; import ChildWrapper from './simple'; @@ -14,13 +14,6 @@ export default class DummyWrapper extends ComponentWrapper { return new ChildWrapper(this.find('.awsui-child')!.getElement()); } - @usesDom - click(): void { - act(() => { - this.click(); - }); - } - findSomething(): number { return KeyCode.enter; } diff --git a/src/core/dom.ts b/src/core/dom.ts index 52c82f8..2c78c68 100644 --- a/src/core/dom.ts +++ b/src/core/dom.ts @@ -1,12 +1,8 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /*eslint-env browser*/ -import * as React from 'react'; -import { act as reactDomAct } from 'react-dom/test-utils'; import { IElementWrapper } from './interfaces'; -import { KeyCode, isScopedSelector, substituteScope } from './utils'; - -export const act = ('act' in React ? React.act : reactDomAct) as typeof reactDomAct; +import { act, KeyCode, isScopedSelector, substituteScope } from './utils'; // Original KeyboardEventInit lacks some properties https://github.com/Microsoft/TypeScript/issues/15228 declare global { diff --git a/src/core/utils.ts b/src/core/utils.ts index 58a961e..6ef72ae 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -1,6 +1,8 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /*eslint-env browser*/ +import * as React from 'react'; +import { act as reactDomAct } from 'react-dom/test-utils'; import * as Tokenizer from 'css-selector-tokenizer'; import 'css.escape'; @@ -86,3 +88,5 @@ export enum KeyCode { alt = 18, meta = 91, } + +export const act = ('act' in React ? React.act : reactDomAct) as typeof reactDomAct;